Micro and Array Update

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 nint kint a[])
{
    sort(aa + n);
    if (k - a[0] < 0)
        return 0;
    else
        return k - a[0];
}
int main()
{

    int n = 3k = 4;

    int a[n] = {125};

    cout << microArrayUpdate(nka<< "\n";
    return 0;
}


No comments:

Post a Comment