Alice has recently found an array containing N integers. As we all know Alice loves sorted arrays so, he wants to sort the array. To sort an array Alice can add 1 to any integer in the array in 1 move.
Alice wants to find a minimum number of moves needed to sort this array. Remember that after sorting the array, all elements in it should be distinct.
Example:
Input: n = 3, A = {1, 6, 5}
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;long sortedArray(long n, vector<long> &A){long sum = 0, x = 0;for (int i = 1; i < n; i++){if (A[i] <= A[i - 1]){x = (A[i - 1] - A[i]) + 1;sum = sum + x;A[i] = A[i] + x;}}return sum;}int main(){long n = 3;vector<long> A = {1, 6, 5};cout << sortedArray(n, A) << "\n";return 0;}
No comments:
Post a Comment