Saw this question in a hackerrank contest, what's the most optimal solution?
Anonymous User
121

An array a of integers of length n is given, such that each integer is between 0 and m-1 (Both inclusive).The integers are numbered as a[1],a[2],....,a[n]. In one operation you can choose any subbaray a[l,r] (1<=l<=r<=n)and then update all the elements of the subarray a[i] to (a[i]+1)%m. The integer m is fixed for all operations and indices. You want to make all the elements of this array equal to X (X lies in the range of 0 to m-1). Find the minimum number of operations.

Example -

N,M,X = 5 10 1

Array = 5 5 5 8 2

Output = 12

Comments (2)