Micro and Array Update

 Problem

Micro purchased an array A having N integer values. After playing it for a while, he got bored of it and decided to update value of its element. In one second he can increase 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.

Input:
First line consists of a single integer, T, denoting the number of test cases.
First line of each test case consists of two space separated integers denoting N and K.
Second line of each test case consists of N space separated integers denoting the array A.

Output:
For each test case, print the minimum time in which all array elements will become greater than or equal to K. Print a new line after each test case.

Constraints:
1T5
1N105
1A[i],K106

Sample Input
2
3 4
1 2 5
3 2
2 5 5
Sample Output
3
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first test case,
After 1 second, array will be {2,3,6}
After 2 second, array will be {3,4,7}
After 3 second, array will be {4,5,8}

So it will take 3 second for all array elements to become greater than or equal to 4.




/*
// Sample code to perform I/O:



int main() {
    int num;
    cin >> num;                                     // Reading input from STDIN
    cout << "Input number is " << num << endl;      // Writing output to STDOUT
}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/

// Write your code here
#include <iostream>

using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k;
        
        cin>>n>>k;
        int arr[n];
        int count=0;
        for(int i=0;i<n;i++){
            cin>>arr[i];

        }
        for(int i=0;i<n;i++){
            int temp=0;
            while(arr[i]<k){
                
                arr[i]++;
                temp++;
            }
            count=max(temp,count);
        }
    cout<<count<<endl;  
    }
}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings