Given an array of integers, determine the minimum cost to make it either non-increasing or non-decreasing along its length. The cost to change an element is the absolute difference between its initial value and its new value. For example, if the element is initially 10, it can be changed to 7 for a cost of 3.
Example:
array = {0, 1, 2, 5, 6, 5, 7}
Output: 1
Explanation: Only array[5] violates the condition that the array is non-decreasing. If the value if increased to 6, the array is non-decreasing: array = {0, 1, 2, 5, 6, 6, 7}. The cost is | array[5] - array’[5] | = |5-6| = 1.
My code was failing for the input (95 elements) -
9847,3752,5621,7012,1986,3090,1383,6257,9501,7004,6181,9387,9137,9305,7795,9310,3904,8328,6656,8123,1796,2754,4372,5200,3893,8568,4436,3973,8498,1879,2731, 4651,4388,453,2465,2669,6384,819,8565,1952,7219,4362,3012,9380,2645,4800,2945,5778,1993,1170,1356,8557,1497,8921,670,5155,9115,1095,9400,9451,9344,4393, 4201,8167,8129,2004,8839,1457,7682,1881,9266,6366,9889,242,5318,5248,3670,7381,6567,2317,2162,6670,5876,1179,2482,9270,4326,4166,6122,2016,3008,5349,1723, 5816,4030
Expected output: 215404 My output: 159252