A simple problem on the first approach that I have to look at.

Let’s see this as two pointers.

One starting from the index 0 and the other index will start from len-1.

int begin = 0 ; int end = len - 1;

If there is no solution we return -1

return -1;

This will track the number of edits

int count = 0;

The first step is to check if arr[begin] is less than arr[end],

if the arr[begin] is higher than arr[end] but less than x then:

begin++; x -= arr[begin]; count++;

else if arr[end] is higher than arr[begin] but less than x then: end–; x -= arr[end]; count++;

else if begin == end and x is less than arr[begin]: return -1;

else if begin == end and x is greater than arr[begin]: return -1;

else if begin==end and x is equal to arr[begin]; x-=arr[begin];count++; return count;

This should all be inside a loop that should last until…

  1. Begin is equal to end or
  2. x == 0