Array ( [0] => [1] => questions [2] => Basic [3] => Mountain-Peak-Sequence ) Codevita Previous Questions | Mountain Peak Sequence | THE INQUISITIVE





Mountain Peak Sequence

LEVEL:Beginner

Description

Given an integer value 3, consider the first thre natural numbers 1,2,3. These can be arranged in the following ways: 2,3,1 and 1,3,2. In both of these arrangements, the numbers increase to a certain point and then decrease. A sequence with this property is called a 'mountain peak sequence'
Given an integer N, write a program to find the remainder of mountain peak arrangements that can be obtained by rearranging the numbers 1,2,............N. When divided by Mod

Input Format

One line containing the integers N
Constraints:
Mod = 10^9 + 7
N <= 10^9

Output Format

An integer m, giving the remainder of the number of moountain peak arrangements that could be obtained from 1,2...........N, is divided by Mod


Example 1:

Input
3
Output
2
Example 2:

Input
4
Output
6
Example 3:

Input
50
Output
949480667

oops

Login to see Discussion




Approach

here let's understand the question , in question it is said that it should be a mountain peak. So the numbers shouldn't be in either increasing or decreasing order

if n=1 answer is 0 answer is 0 as there is no mountain peak (1)
if n=2 answer is 0 as here too there is no mountain peak (1,2)

if n=3 Here there are two chances one we can place 3 in middle of previous combinations which is (1,3,2) and reverse is also possible which is (2,3,1)

if n=4 here we can place place it either before or after 3 or previous combinatin which is (1,4,3,2) or (1,3,4,2) similarly we get (2,4,3,1) and (2,3,4,1) and two more combinations are (1,2,4,3) and (1,4,2,3)

so here we can observe pattern that
from 3 on wards we have no have patterns equal to 2 times of previous one plus 2

it becuase for every previous pattern we can place present number before or after a certain number and 2 more ways to the ascending order of n-1

so our logic will be

for i 1 to n-2 (as there are no combinations for 1 or 2)
answer = (answer *2 ) + 2here let's understand the question , in question it is said that it should be a mountain peak. So the numbers shouldn't be in either increasing or decreasing order

if n=1 answer is 0 answer is 0 as there is no mountain peak (1)
if n=2 answer is 0 as here too there is no mountain peak (1,2)

if n=3 Here there are two chances one we can place 3 in middle of previous combinations which is (1,3,2) and reverse is also possible which is (2,3,1)

if n=4 here we can place place it either before or after 3 or previous combinatin which is (1,4,3,2) or (1,3,4,2) similarly we get (2,4,3,1) and (2,3,4,1) and two more combinations are (1,2,4,3) and (1,4,2,3)

so here we can observe pattern that
from 3 on wards we have a patterns which is present answer equal to 2 times of previous one plus 2

it becuase for every previous pattern we can place present number before or after a certain number and 2 more ways to the ascending order of n-1

so our logic will be

for i 1 to n-2 (as there are no combinations for 1 or 2)
answer = (answer *2 ) + 2


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution