Micro purchased an array A having N integer values. After playing it for a while, he got bored of it and decided to update the value of its element. In one second he can increase the value of each array element by 1. He wants each array element's value to become greater than or equal to K. Please help Micro to find out the minimum amount of time it will take, for him to do so.
Example:
Input: n = 3, k = 4, a = [1,2,5]
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;int microArrayUpdate(int n, int k, int a[]){sort(a, a + n);if (k - a[0] < 0)return 0;elsereturn k - a[0];}int main(){int n = 3, k = 4;int a[n] = {1, 2, 5};cout << microArrayUpdate(n, k, a) << "\n";return 0;}
No comments:
Post a Comment