Wipro - C & C++ Coding Ability

Test Number 2


You are given a positive number N of 3 digits. Your task is to check whether the number is an Armstrong number or not.  An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Print "Yes" if it is an Armstrong number else print "No".

INPUT:
First line of input contains T for test cases.

For each test case, the next line contains a positive integer indicating the value of N.

OUTPUT:
Print Yes or NO.



Test Case: 1

INPUT:
1

371

OUTPUT:
Yes

Explanation :

371 is an Armstrong number since 33 + 73 + 13 = 371.
Hence answer is "Yes".                

#include 

using namespace std;



class Solution

{

  public:

    string armstrongNumber(int n)

    {

        int sum = 0;

        int x = n;

        

        while( n != 0)

        {

            int a = n % 10;

            sum += a*a*a;

            n/= 10;

            

        }

        

        return sum == x ? "Yes":"No";

    }

};



int main() {

    int T;

    cin >> T;

    while (T--) {

        int n;

        cin >> n;

        Solution ob;

        cout << ob.armstrongNumber(n) << endl;

    }

    return 0;

}               






Given, N students and N bicycles are placed in a straight line. Each bicycle can accommodate only 1 student. A student can stay at his position, move one step right from x to x + 1, or move one step left from x to x -1. Any of these moves consumes 1 minute. Write a program to assign students to bicycles so that the time when the last student gets his bicycle is minimized.



INPUT:
First line of the input contains T for test cases.

For each test case: 

the first line contains an integer value containing the value of N.

The second line contains N space separate integers denoting the values of positions of the students.

The third line contains N space separate integers denoting the values of positions of the bicycles.



OUTPUT:
Print the maximum distance.



Test Case: 1

INPUT:

1

4

2 3 4 5

-1 9 5 6



OUTPUT:
4



The maximum distance between any two elements is student with position 5 and bicycle with position 9, so 9-5 = 4.                   

#include 

using namespace std;



class Solution {

  public:

    int assignMiceHoles(int N , int M[] , int H[])

    {

        

        sort(M,M+N);

      sort(H,H+N);

      int assign=0;

      for(int i=0;i< N;i++) 

      {

          assign=max(assign,abs(M[i]-H[i]));

      }

      return assign;

    }

};



int main() {

    int T;

    cin >> T;

    while (T--) {

        int N;

        

        cin>>N;

        

        int M[N],H[N];

        

        for(int i=0 ; i< N ; i++)

            cin>>M[i];

        for(int i=0 ; i< N ; i++)

            cin>>H[i];



        Solution ob;

        cout << ob.assignMiceHoles(N,M,H) << endl;

    }

    return 0;

}                   






You are playing a Battleground game with your team. Your team assigns a character code ID for each gun. Your task is to write a program that takes the input of the character and displays the gun name. Suppose there are 5 types of Guns in the game with their id's given below:

                                        ID

                                 Guns Name

                  G or g

                Groza

                 S or s

                Scar-L

                A or a

                AWM

                M or m

                M416

                T or t

                Thompson


INPUT:
The first line contains an integer T, the total number of test cases. 

Then T lines follow, each line contains a character.



OUTPUT:
For each test case, display the Guns Name depending on the ID, in a new line.



Test Case: 1

INPUT:
3

a

T

M



OUTPUT:

AWM

Thompson

M416                          

#include 

using namespace std;



int main() {

    int T;

    cin>>T;

    while(T--){

        

        char a;

        cin>>a;

        

        if(a=='g' || a=='G'){

            cout<<"Groza"<< endl;

        }

        else if(a=='S' || a=='s'){

            cout<<"Scar-L"<< endl;

        }

        else if(a=='M' || a=='m'){

            cout<<"M416"<< endl;

        }

        else if(a=='T' || a=='t'){

            cout<<"Thompson"<< endl;

        }

        else if(a=='A' || a=='a'){

            cout<<"AWM"<< endl;

        }

        

    }

	return 0;

}                          






You are given the height of the triangle. your task is to make a triangle containing alternate 0 and 1, starting with zero.



INPUT:
First Line of the input contains the height of the triangle.



OUTPUT:
Contians the required pattern triangle.



Test Case: 1

INPUT:
5



OUTPUT:
1

0 1

1 0 1

0 1 0 1 

1 0 1 0 1                     

#include

using namespace std;

int main()

{

	int h;

	cin>>h;

	

	for(int i=1; i<=h; i++)

	{

		for(int j=1; j<=i; j++)

		{

			if((i+j)%2==0)

			cout<<" 1";

			else cout<< " 0";

		}

		cout< < endl;

	}

	return 0;

}                             






You are given a positive integer N. Your task is to convert all bits at even positions to 0.

INPUT:
The first line of input contains T for test cases. 

For each test case, the next line contains a positive integer indicating the value of N.

OUTPUT:

For each test case print a single integer as output.



Test Case: 1

INPUT:
1

30

OUTPUT:
10

Explanation:

Binary representation of 11110. Bits at Even positions are  highlighted. After making all of them 0, we get 01010. Hence the answer is 10.
                    

#include 

using namespace std;



class Solution {

  public:

    long long int convertEvenBitToZero(long long int n) {

        

        return (n & 0xAAAAAAAA);

    }

};



int main() {

    int T;

    cin >> T;

    while (T--) {

        long long int n;

        cin >> n;

        Solution ob;

        cout << ob.convertEvenBitToZero(n) << endl;

    }

    return 0;

}