Wipro - C & C++ Coding Ability

Test Number 9


You are given an integer N. Your task is to check whether the numbers a Krishnamurthy number or not. A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. Print "YES" if it's a Krishnamurthy Number, else Print "NO".



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

For each test case,  the next line contains an integer value.



OUTPUT:
Contains YES or NO in output.



Test Case: 1

INPUT:

1

145



OUTPUT:
YES

Explanation:
1! + 4! + 5! = 145 so the answer is YES                

#include < bits/stdc++.h>

using namespace std;



class Solution

{

  public:

    string isKrishnamurthy(int N) 

    {

         int fact[10];

        fact[0] = fact[1] = 1;

        

        for(int i = 2; i <= 9; i++)

        {

            fact[i] = fact[i-1] * i;

        }

        

        int num = N;

        int sum = 0;

        while(num > 0)

        {

            sum += fact[num%10];

            num /= 10;

        }

        

        return sum == N ? "YES" : "NO";

    }

};



int main() 

{

    int T;

    cin >> T;

    while (T--) 

    {

        int N;

        

        cin>>N;



        Solution ob;

        cout << ob.isKrishnamurthy(N) << endl;

    }

    return 0;

}               






Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example, gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match.
Your task is simple. Given a string, you need to tell if it is a lapindrome.



INPUT:
The first line of input contains a single integer T, the number of test cases.
Each test is a single line containing a string S composed of only the lowercase English alphabet.



OUTPUT:
For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.



INPUT:

6
gaga
geeks
rotor
xyzxy
abbaab
ababc
OUTPUT:
YES
NO
YES
YES
NO
NO
                   

#include 

using namespace std;



void solve(){

    string s;

    cin>>s;

    int len = s.size();

    

    int ch[26];

    for(int i=0;i< 26;i++){

        ch[i] = 0;

    }

    

    for(int i=0;i<(len);i++){

        if((len%2 != 0) && i == (len/2)) continue;

        ch[s[i] - 'a']++;

    }

    

    int oddcnt = 0;

    for(int i=0;i< 26;i++){

        if((ch[i]%2 != 0) && ch[i] > 0 ){

            oddcnt++;

        }

    }

    

    if(oddcnt){

        cout<<"NO"<< endl;

    }else{

        cout<<"YES"<< endl;

    }

}



int main()

{

	

	int T;

	cin>>T;

	while(T--)

       {

	    solve();

	}

	return 0;

}

                   






Your task is to make a function largest( ) which takes the input of an array[ ] of size n and returns the maximum element of the array. 

For example:

Test	2
Input	
2
7
23
5
7
34
91
32
76
4
3
5
7
9
Result
91
9
                          

#include

#include

int largest(int arr[], int n);

int main()

{

  int arr[1000];

    int i, t, n;

    scanf("%d", &t);

    while (t--) {

        scanf("%d", &n);

        for (i = 0; i < n; i++) scanf("%d", &arr[i]);

        printf("%d", largest(arr, n));

        printf("\n");

    }

    return 0;

}





int largest(int arr[], int n) 

{

    int largestelement=0;

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

    {

        if (largestelement < arr[i])

        {

            largestelement = arr[i];

        }

    }

    return largestelement;

}                          






You are given an integer N denoting the value of a year. Your task is to write a program that checks whether the year is a leap year or not. 

If the given year is a leap year then print "YES", if not then print "NO".



INPUT:
Contains an integer value denoting the year.



OUTPUT:
Print YES or NO as output.



Test Case: 1

INPUT:
2004

OUTPUT:
YES

Test Case: 2

INPUT:
2003

OUTPUT:
NO                     

#include 

int main()

{

   int year;

   scanf("%d", &year);



   if (year % 400 == 0) 

   {

      printf("YES");

   }

   

   else if (year % 100 == 0) 

   {

      printf("NO");

   }

   

   else if (year % 4 == 0) 

   {

      printf("YES");

   }

   

   else 

   {

      printf("NO");

   }



   return 0;

}                             






Given an array arr[ ] of length N consisting cost of N chocolates and an integer K depicting the amount, you have with you. Your task is to find the maximum number of chocolates you can buy with K amount. 

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

For each Test Case:
The first line contains the value of N denoting the total number of chocolates.

The second line contains the value of K denoting the amount you have.

The next line contains N space separated integers denoting the value of different chocolates.



OUTPUT:
Contains a single integer denoting the number of chocolates you can buy.



Test Case:1

INPUT:
1

5

70

12 35 18 6 34



OUTPUT:
4



Explanation: 

Given you have an amount of 70 with you. So in this amount you can buy the chocolates of amount 12, 18, 6, 34 as 12 + 18 + 6 + 34 <=70. 

SO our answer is 4.

                    

#include < bits/stdc++.h>

using namespace std;



class Solution

{

public:

    int toyCount(int N, int K,vector< int> arr)

    {

        int count = 0;

        sort(arr.begin() , arr.end());

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

        {

            K -= arr[i];

            if(K < 0)

            break;

            count++; 

        }

            return count;

    }

};





int main()

{

    int T;

    cin>>T;

    while(T--)

    {

        int N, K;

        cin>>N>>K;

        vector< int> arr(N);

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

            cin>>arr[i];

        

        Solution ob;

        cout<< ob.toyCount(N, K, arr)<< endl;

    }

    return 0;

}