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
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.




Solution-



import java.util.*;
class Micro{
   public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      int t;
      long ans=0;

      t=sc.nextInt();
      for(int i=0;i<t;i++)
      {
int n=sc.nextInt();
long k=sc.nextLong(); //4
int arr[]=new int[n];
for(int j=0;j<n;j++)
{
arr[j]=sc.nextInt();
}
int min=arr[0];
for(int j=0;j<n;j++)
{
   if(arr[j]<=min)
      min=arr[j];
}

if(min>=k)
{
System.out.println("0\n");
}
else
{
   ans=k-min;
   System.out.println(ans);
}
}
   
   }
}

2 Comments



  1. #include
    int main()
    {
    int T,c;
    scanf("%d\n",&T);
    int N,K;
    scanf("%d %d\n",&N,&K);

    for(int i=0;i=K)
    {
    printf("0\n");
    }
    else
    {
    int c=0;
    while(min!=K){
    min++;
    c++;
    }
    printf("%d\n",c);
    }
    }

    }
    please could u just help me what's wrong with this code as it's showing 2 instead of 0 in the second testcase

    ReplyDelete

Post a Comment

Previous Post Next Post