Question: Airtel Payments Bank , Online Assessment Questions (16th August 2023 - SET- 2 ) | Creating Polygon | Equal Nodes In the Binary Tree
0
Entering edit mode

ADD COMMENTlink 13 months ago PoGo 2.4k
1
Entering edit mode

Question 1

solution

 

 void solve(){

int n;

cin>>n;

vector<int> a(n);

for(int i=0;i<n;i++)cin>>a[i];

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

int sum=0;

int val=0;

if(a.size()<3){

  cout<<0<<endl;

  return;

}

sum+=a[0];

sum+=a[1];

for(int i=2;i<n;i++){

  if(sum>a[i]){

    val=sum+a[i];

  }

  sum+=a[i];

}

cout<<val<<endl;  

 }

ADD COMMENTlink 13 months ago Bipin Kumar • 10
0
Entering edit mode

//Answer Q1:-

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    
    int arr[n];
    
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    
    sort(arr,arr+n);
    
    int ans = -1;
    int prefix_sum = arr[0]+arr[1];
    
    for(int i=2;i<n;i++)
    {
        if(arr[i]<prefix_sum)
        {
            ans = max(ans,prefix_sum+arr[i]);
        }
        
        prefix_sum += arr[i];
    }
    
    cout<<ans<<endl;
}

ADD COMMENTlink 13 months ago Sugam Sharma • 0
0
Entering edit mode

// equal nodes in the binary tree

#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
 
int ans=0;
class node {
    public:
    node *left;
    node *right;
    int val;
    node(int val) {
        this->val=val;
        left=NULL;
        right=NULL;
    }
};
 
ll func_left(node *root) {
    if(!root || root->val==INT_MAX) return 0;
    return root->val+func_left(root->left);
}
 
ll func_right(node *root) {
    if(!root || root->val==INT_MAX) return 0;
    return root->val+func_right(root->right);
}
 
void dfs(node *root) {
    if(!root) return;
    if(!root->left || root->left->val==INT_MAX || !root->right || root->right->val==INT_MAX) return;
 
    ll a=func_left(root->left);
    ll b=func_right(root->right);
 
    if(a==-b) ans++;
 
    dfs(root->left);
    dfs(root->right);
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   
 
    queue<node*> que;
    string s;
    node *root=NULL;
    while(cin >> s) {
        if(root==NULL) {
            root=new node(stoi(s));
            que.push(root);
        }
        else {
            if(que.front()->left) {
                if(s=="null") {
                    que.front()->right=new node(INT_MAX);
                    que.pop();
                    continue;
                }
                node *temp=new node(stoi(s));
                que.front()->right=temp;
                que.pop();
                que.push(temp);
            }
            else {
                if(s=="null") {
                    que.front()->left=new node(INT_MAX);
                    continue;
                }
                node *temp=new node(stoi(s));
                que.front()->left=temp;
                que.push(temp);
            }
        }
    }
   
    dfs(root);
 
    cout << ans << "\n";
 
    return 0;
}
ADD COMMENTlink 12 months ago sumeet kumar sahoo • 290

Login before adding your answer.

Similar Posts
Loading Similar Posts