Mountain array

 You are given an array of size N. You need to change this array into a mountain. By mountain we mean,

the either ends of the array should have equal elements. Then as we move towards the middle from

both ends, the next element is just one more than the previous one. So, it would have a peak in the

middle and decreases if you go towards either end, just like a mountain.

Examples of mountains are [1, 2, 3, 2, 1] or [6, 7, 8, 8, 7, 6]. But the array [1, 2, 4, 2, 1] is not a mountain

because from 2 to 4 the difference is 2. The array [1, 2, 3, 1] is also not a mountain because the

elements 2 and 3 are not equal from both ends.

You need to find the minimum number of elements that should be changed to make the array a

mountain. You can make the elements negative or zero as well.



SOLUTION:





Post a Comment

0 Comments