Question: Paypal, Online Assessment (IIT Madras) | Discount Tags | SQL : Billing Software Report |13 th October 2023
1
Entering edit mode

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

Yes it is possible 

 

 

 

#include<bits/stdc++.h>
using namespace std;
long long solve(vector<int>&vec,int idx,int dp[]){
    if(idx>=vec.size())return 0;
    if(dp[idx]!=-1)return dp[idx];
    long long a=-1e12,b=-1e12;
    if(vec[idx]%2==0)a=vec[idx]+solve(vec,idx+1,dp);
    b=solve(vec,idx+1,dp);
    return dp[idx]=max(a,b);

}
int main(){
    int n;
    cin>>n;
    int arr[n+1];
    for(int i=0;i<n;++i)cin>>arr[i];
    vector<int>vec;
    priority_queue<int>pq;
    for(int i=0;i<n;++i){
        if(arr[i]%2)pq.push(arr[i]);
        else vec.push_back(arr[i]);
    }
    while(!pq.empty()){
        int x=pq.top();
        pq.pop();
        if(!pq.empty()){
            int y=pq.top();
            pq.pop();
            vec.push_back(x+y);
        }
    }
    int ans=INT_MIN;
    int dp[vec.size()+1];
    memset(dp,-1,sizeof dp);
    cout<<solve(vec,0,dp);
//    cout<<ans<<endl;
}

 

 

 

 

 

 

 

ADD COMMENTlink 14 months ago Yololo • 20
0
Entering edit mode

q2 soln:

SELECT c.iban, b.amount from customers c

LEFT JOIN balances b on c.id=b.customer_id

WHERE c.amount>0

ORDER BY amount DESC;

ADD COMMENTlink 14 months ago DJBawari • 0
0
Entering edit mode

Is it possible to do the first question in O(n) TC??

ADD COMMENTlink 13 months ago Sukhjit Singh • 0
Entering edit mode
0

Yes it is possible 

 

 

 

#include<bits/stdc++.h>
using namespace std;
long long solve(vector<int>&vec,int idx,int dp[]){
    if(idx>=vec.size())return 0;
    if(dp[idx]!=-1)return dp[idx];
    long long a=-1e12,b=-1e12;
    if(vec[idx]%2==0)a=vec[idx]+solve(vec,idx+1,dp);
    b=solve(vec,idx+1,dp);
    return dp[idx]=max(a,b);

}
int main(){
    int n;
    cin>>n;
    int arr[n+1];
    for(int i=0;i<n;++i)cin>>arr[i];
    vector<int>vec;
    priority_queue<int>pq;
    for(int i=0;i<n;++i){
        if(arr[i]%2)pq.push(arr[i]);
        else vec.push_back(arr[i]);
    }
    while(!pq.empty()){
        int x=pq.top();
        pq.pop();
        if(!pq.empty()){
            int y=pq.top();
            pq.pop();
            vec.push_back(x+y);
        }
    }
    int ans=INT_MIN;
    int dp[vec.size()+1];
    memset(dp,-1,sizeof dp);
    cout<<solve(vec,0,dp);
//    cout<<ans<<endl;
}

 

 

 

 

 

 

 

ADD REPLYlink 14 months ago
Yololo
• 20

Login before adding your answer.

Similar Posts
Loading Similar Posts