HackWithInfy Previous Year Coding Questions 2020



Question 1 :

Ramu has N dishes of various sorts organized in a very row: A1,A2,…,AN wherever Ai denotes the kind of the ith dish. He needs to settle on as several dishes as potential from the given list however whereas satisfying 2 conditions:

He will select only 1 style of dish.

No 2 chosen dishes ought to be adjacent to every different.

Ramu needs to understand which sort of dish he ought to choose between, in order that he will decide the utmost variety of dishes.


Example :

Given N= nine and A= [1,2,2,1,2,1,1,1,1]

For type 1, Ramu will select at the most four dishes. one in all the ways in which to settle on four dishes of kind one is A1,A4, A7 and A9.


For type 2, Ramu will select at the most 2 dishes. a way is to settle on A3 and A5.

So during this case, Ramu ought to choose kind one, during which he will decide additional dishes.


INPUT FORMAT:

The first line contains T, the quantity of take a look at cases. Then the take a look at cases follow.

For each action, the primary line contains one whole number N.

The second line contains N integers A1,A2,…,AN.

OUTPUT FORMAT

For each action, print one line containing one whole number whole number the kind of the dish that Ramu ought to choose between. If there square measure multiple answers, print the tiniest one.


CONSTRAINTS :

1 <= T <= one0^3

1 <= N <= one0^3

1 <= Ai <= one0^3

Sample Input :

3

5

1 2 2 1 2

6

1 one one one one one

8

1 two two two three four two one

Sample Output :

1

1

2

Solution in C++

 #include <iostream>  
 using namespace std;  
 int main()   
 {  
     int t,n,i,max,x;  
     cin>>t;  
     while(t--)  
     {  
        cin>>n;  
        int arr[n];  
        max=0;  
       for(i=0;i<n;i++)  
        {  
            cin>>arr[i];  
        }  
        int b[1001]={0};  
        for(i=0;i<n;i++)  
        {  
            b[arr[i]]++;  
            if(arr[i]==arr[i+1])  
                i++;  
     }  
     for(i=1;i<=1000;i++)  
      {  
          if(b[i]>max)  
          {  
            max=b[i];  
            x=i;  
          }  
     }  
     cout<<x<<endl;  
   }  
   return 0;  
 }  

Question 2 :

There square measure 3 piles of stones. the primary pile contains a stones, the second pile contains b stones and also the third pile contains c stones. you need to select one among the piles and split the stones from it to the opposite 2 piles; specifically, if the chosen pile at the start contained s stones, you ought to select associate whole number k (0≤k≤s), move k stones from the chosen pile onto one among the remaining 2 piles and s−k stones onto the opposite remaining pile. confirm if it's potential for the 2 remaining piles (in any order) to contain x stones and y stones severally when acting this action.

INPUT FORMAT :

The first line of the input contains one whole number T denoting the quantity of take a look at cases. the outline of T take a look at cases follows.The first and solely line of every action at law contains 5 space-separated integers  a, b, c, x and y.

OUTPUT FORMAT :

For each action at law, print one line containing the string “YES” if it's potential to get piles of the given sizes or “NO” if it's not possible.

CONSTRAINTS :

1 <= T <= one00

1 <= a, b, c, x, y <= 10^9

SAMPLE INPUT :

4

1 2 3 2 4

3 2 5 6 5

2 4 2 6 2

6 5 2 12 1

SAMPLE OUTPUT :

YES

NO

YES

NO

Solution in C++

Test case 1: You can take the two stones on the second pile, put one of them on the first pile and the other one on the third pile.

Test case  2: You do not have enough stones.

Test case 3: You can choose the first pile and put all stones from it on the second pile.

 #include <bits/stdc++.h>  
 using namespace std;  
 using ll = long long;  
 int main()   
 {  
   int t;  
 cin >> t;  
   while(t--)   
 {  
     int a, b, c, x, y;  
     cin >> a >> b >> c >> x >> y;  
     if((a + b + c ) != (x + y) )  
 {  
       cout << "NO" << endl;  
     }   
 else   
 {  
       if(y < min(a, min(b, c)) || x < min(a, min(b, c)))   
 {  
         cout << "NO" << endl;  
       }  
 else  
  {  
         cout << "YES" << endl;  
       }  
     }  
   }  
 }  


Post a Comment

0 Comments