Question Name:Drive the car

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n,k;
	    cin>>n>>k;
	    int A[n];
	    int i;
	    int m=0;
	    for(i=0;i<n;i++)
	    {
	        cin>>A[i];
	        if(A[i]>k&&A[i]>m)
	        m=A[i];
	    }
	    if(m==0)
	    cout<<"-1"<<endl;
	    else
	    cout<<m-k<<endl;
	}
	return 0;
}

Problem Description

Suppose you are car driver and you have to drive a car on a track divided into “N” no. of sub-tracks. You are also given the value of “K” i.e. the total kilometers a car can drive on each sub-track.

If the car can’t cover a sub-track, you can add any unit of Petrol in it. With each unit of petrol added, the total kilometers your car can travel will increase by one unit .

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two space separated integers N and K. The second line of each test case contains N space separated integers (A[]) denoting the distance of each N sub-tracks.

Output:

For each test case in a new line you have to print out the minimum unit of Petrol your car require to cover all the sub-tracks. If no extra unit of petrol is required, print -1.

Constraints:
1<=T<=100
1<=N,K<=200
1<=A[]<=1000

  • Test Case 1

    Input (stdin)

    2
    5 7
    2 5 4 5 2
    5 4
    1 6 3 5 2
    

    Expected Output

    -1
    2
  • Test Case 2

    Input (stdin)

    1
    5 7
    2 5 4 5 2
    

    Expected Output

    -1
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock