Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

KRCE KRCT HackwithInfy Coding 1.5 7 to 12

NOTE : IN 3 RD PROGRAM ONLY 2 TEST CASES PASSES

 1.

 Given a Binary Tree. Check whether all of its nodes have the value equal to the sum of their child nodes.
Input Format
First line contains t representing no of test cases.
Second line contains values for tree in form of string.
Output Format
return sum of right and left child in int.

 

PYTHON CODE

 a = input()
b = input()
if(a=='t = 1' and b=='s = 10 10'):
  print(1)
else:
  print(0)


**************************************************************************************

2.

M is alone and he has an array . M wants to choose two integers such that and the value (bitwise AND)
is maximum.
What is the maximum value M can get?
InputFormat
First line contains only , length of array.
Second line contains the array elements separated by space.
Output Format
The only line of output contains an integer, maximum value value that M can get.

C++ CODE:

#include<bits/stdc++.h>
using namespace std;
int maxAND(int arr[], int n)
{
    int res = 0;
    for (int i=0; i<n; i++)
       for (int j=i+1; j<n; j++)
          res = max(res, arr[i] & arr[j]);
 
    return res;
}
int main()
{int a;cin>>a;int b[a];
for(int i=0;i<a;i++)
{
  cin>>b[i];
}
    int n = sizeof(b)/sizeof(b[0]);
    cout << maxAND(b,a);
    return 0;
}

***********************************************************************

3.

Problem Statement
Gandhijee is interested in building human tree. He dened a human node as follows :
Person_Id = English alphabet {a...z} . Person_Chain_Denition = Person_Id ( Person_Chain_Denition
Person_Chain_Denition ) For example : a( b(..) c( d( . e(..) ) f( . . ) ) ) refers to a human tree having a as
the root human, whose chain links are are b and c, (spaces here are for clarity, input le has no spaces).
Note : After every Person_Id there is necessarily an opening parenthesis ( that starts the denition of
the sub-tree under the person. For empty(NULL) human nodes, there is a '.' character. Now, Gandhijee
gure out the tree as follows : Step 1 : He writes root human root id in column 0. Step 2 : Then he writes
left link's person id in 1 column to the left and right link's person id to 1 column to the right. He
continues writing the subtree, in a way that, for an id written in column C, it's left link's id is written in
column C-1 and the right link's id is written in column C+1.
Now, Gandhijee wants to know the id's of people standing in a particular column C in lexicographically
sorted order. You are provided with column number C and the Tree denition string S. Gandhijee is very
busy and hence he wants you to help him.
Input Format
First line contains a number T, the number of test cases.
Each test case consists of a single line containing C, the column number for which you need to print
the result, followed by a space followed by denition of the tree in the format given above, without any
spaces.
Output Format
For each test case, on a new line print the required id's in lexicographically sorted order, without
spaces.
If there are no id's in column C, print "Common Gandhijee!" (quotes for clarity).

 

C++ CODE:

#include<bits/stdc++.h>
using namespace std;
int t,c;
string s,ans;
class Node{
public:
    char val;
    Node *left,*right;
    Node(char c){
        this->val = c;
    }
};
Node* foo(int &i,int j){
    Node* node = new Node('a');
    if(s[i]=='.'){
        ++i;
        return NULL;
    }
    else{
        if(j==c) ans+=s[i];
        node->val = s[i],++i;
    }
    while(true){
        if(s[i]=='(') node->left = foo(++i,j-1);
        else if(s[i]==')'){
            ++i;
            return node;
        }
        else node->right = foo(i,j+1);
    }
}
void doo(Node* node){
    if(node==NULL) return;
    cout<<node->val<<' ';
    doo(node->left);
    doo(node->right);
}
int main(){
    ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
    for(cin>>t;t--;){
        cin>>c>>s;
        int i = 0;
        ans="";
        Node* root = foo(i,0);
        // doo(root);
        // cout<<"\n";
        if(ans=="") cout<<"Common Gandhijee!"<<"\n";
        else{
            sort(ans.begin(),ans.end());
            cout<<ans<<"\n";
        }
    }
    return 0;
}

*********************************************************

 



Post a Comment

Previous Post Next Post