Computer Science, asked by kakondhar09, 7 months ago

Use coding..
We have a permutation p= {P1, P2, ..., Pn} of {1, 2, ..
n}
Print the number of elements pi (1 <i<n) that satisfy the following condition:
pi is the second smallest number among the three numbers Pi-1, Pi, and Pi+1.
Constraints
• All values in input are integers.
3 <n <20
p is a permutation of {1, 2,
n}
Input
Input is given from Standard Input in the following format:
n
P1 P2 ... Pn
Output
Print the number of elements pi (1<i<n) that satisfy the condition.​

Answers

Answered by pari2008chitra61
2

Answer:

Given two integers n and k. Consider first permutation of natural n numbers, P = “1 2 3 … n”, print a permutation “Result” such that abs(Resulti – Pi) = k where Pi denotes the position of i in permutation P. The value of Pi varies from 1 to n. If there are multiple possible results, then print the lexicographically smallest one.

Input: n = 6 k = 3

Output: 4 5 6 1 2 3

Explanation:

P = 1 2 3 4 5 6

Result = 4 5 6 1 2 3

We can notice that the difference between

individual numbers (at same positions) of

P and result is 3 and "4 5 6 1 2 3" is

lexicographically smallest such permutation.

Other greater permutations could be

Input : n = 6 k = 2

Output : Not possible

Explanation: No permutation is possible

with difference is k

Similar questions