×

# AHAPH - Editorial

Author: vipulvikram

Tester: D Teja Vardhan Reddy

Editorialist: vipulvikram

MEDIUM

# PREREQUISITES:

DP,Geometric progression

# PROBLEM:

Given the size of an array and two integers with their index where they are already filled.Find the number of way to fill the remaining array with following conditions:

1) The numbers filled should be in range [L,R] (both inclusive).

2) consecutive positions should have different numbers.

# QUICK EXPLANATION:

Let i and j are the indexes and X and Y are the corresponding integers which are already filled.

Now, The problem can be divided into 3 sub-problems. First is from index 1 to i, second is from i to j, and third is from j to n (size of array).

Now, it can be solved using dynamic programming. Let’s say we have to solve for index i to j. Index i contain X and index j contain Y. So, first we find the number of way to fill index i+1 with Y ( such that it is not equal to number in index i) , then we find the total number of ways to fill index i+2 with Y and so on till we reach index j. So the number of ways to fill up to index j with Y is its answer. But, if you observe, this series forms geometric progression which can be solved in O(logn) time.

Similarly, we find for index 1 to i, and for index j to n. Then, multiply all these three to get final answer.

# AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

This question is marked "community wiki".

11
accept rate: 0%

19.8k350498541

 0 can someone please help me out in fun2.It is not clear to me yet. answered 08 Feb, 00:06 5★ram_24 158●6 accept rate: 35%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,683
×2,172
×22
×5

question was seen: 122 times

last updated: 08 Feb, 00:06