Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
4,783 views
in Online Assessments by Expert (111,530 points)
retagged by | 4,783 views

2 Answers

0 like 0 dislike

Question 2 : of PhonePe coding test.. 

 

by Expert (111,530 points)
0 0
is the result of this question the sum of all the elements of the array after performing the range query updates and taking appropriate prefix-sum?
0 0
you are right..
0 like 0 dislike

Question 1 : of PhonePe coding test.. 

by Expert (111,530 points)
0 0
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int t;
     cin>>t;
     while(t--)
     {
         int n;
         cin>>n;
         int arr[n];vector<int>neg;
         for(int i=0;i<n;i++)
         {
         cin>>arr[i];
         if(arr[i]<0)
         neg.push_back(arr[i]);
         }
         sort(neg.begin(),neg.end());
         vector<int>sums;
         int i=0;
         while(i<n)
         {
             if(arr[i]<0)
             i++;
             else
             {
                 int sum=0;
                 int j=i;
                 while(j<n && arr[j]>=0)
                 sum+=arr[j++];
                 i=j;
                 sums.push_back(sum);
             }
         }
         sort(sums.begin(),sums.end());
         if(sums.size()==2)
         cout<<sums[sums.size()-1]+sums[sums.size()-2]<<endl;
         else if(sums.size()==1)
         cout<<sums[0]+neg[neg.size()-1]<<endl;
         else
         cout<<neg[neg.size()-1]+neg[neg.size()-2]<<endl;
         
        //  cout<<cnt+n<<endl;
     }
     return 0;
}


does it work anyone can check please????