allcoding1
27.7K subscribers
2.2K photos
2 videos
74 files
851 links
Download Telegram
int solve(vector<int> tc) {
    int n = tc.size();
    vector<int> p(n), s(n);

    p[0] = tc[0];
    for (int i = 1; i < n; ++i) {
        p[i] = p[i - 1] + tc[i];
    }

    s[n - 1] = tc[n - 1];
    for (int i = n - 2; i >= 0; --i) {
        s[i] = s[i + 1] + tc[i];
    }

    int m = max(p[0], s[0]);
    for (int i = 1; i < n; ++i) {
        m = max(m, max(p[i], s[i]));
    }

    return m;
}

Amazon
👍3
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

int getMinTransactions(int n, vector<vector<int>>& debt) {
    unordered_map<int, int> balance
    for (const auto& d : debt) {
        balance[d[0]] -= d[2];
        balance[d[1]] += d[2];
    }
   
    vector<int> transactions;
    for (const auto& entry : balance) {
        if (entry.second != 0) {
            transactions.push_back(entry.second);
        }
    }

    int count = 0;
    int i = 0, j = transactions.size() - 1;
    while (i < j) {
        if (transactions[i] + transactions[j] == 0) {
            count++;
            i++;
            j--;
        } else if (transactions[i] + transactions[j] > 0) {
            transactions[i] += transactions[j];
            j--;
            count++;
        } else {
            transactions[j] += transactions[i];
            i++;
            count++;
        }
    }
    return count;
}

Transaction Simplification
Amazon
#include<bits/stdc++.h>
using namespace std;
string str, bad_string;
struct node{
    bool end_mark;
    node *next[10];
    node()
    {
        end_mark = false;
        for(int i = 0; i<10; i++)
            next[i] = NULL;
    }
}*root;
bool add(string s)
{
    node *current = root;
    for(int i = 0; i<s.size(); i++)
    {
        int nw = s[i] - 'a';
        if(i == (s.size()-1) && current->next[nw] != NULL)
            return false;
        if(current->next[nw] == NULL)
            current->next[nw] = new node();
        current = current->next[nw];
        if(current->end_mark)
            return false;
    }
    current->end_mark = true;
    return true;
}

int main()
{
    int i, N;
    bool ok = true;
    cin >> N;
    root = new node();
    for(i = 1; i<=N; i++)
    {
        cin >> str;
        if(!ok)
            continue;
        ok = add(str);
        if(!ok)
            bad_string = str;
    }
    if(ok)
        printf("GOOD SET\n");
    else
    {
        printf("BAD SET\n");
        cout << bad_string << endl;
    }
}

Good Bad String
Uber
👍4
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

long long solution(int n, vector<long long>& a, vector<long long>& b) {
    long long ans = 0;

    int minP = min_element(a.begin(), a.end()) - a.begin();
    int minQ = min_element(b.begin(), b.end()) - b.begin();

    for (int i = 0; i < n; i++) {
        if (i == minP || i == minQ)
            continue;

        ans += min(a[minP] * b[i], b[minQ] * a[i]);
        ans %= MOD;
    }

    if (minP != minQ) {
        ans += a[minP] * b[minQ];
        ans %= MOD;
    }

    return ans;
}.

professor code
Uber
👍2
#include<bits/stdc++.h>
using namespace std;

int main() {
    int n,c,d ;
    cin>>n>>c>>d ;
    int b[n],p[n],t[n] ;
    for(int i=0;i<n;i++)
        cin>>b[i]>>p[i]>>t[i] ;
    vector<pair<int,int>>type_0,type_1 ;
    for(int i=0;i<n;i++)
    {
        if(t[i]==0)
        {
            type_0.push_back({p[i],b[i]}) ;
        }
        else
        {
            type_1.push_back({p[i],b[i]}) ;
        }
    }
    sort(type_0.begin(),type_0.end()) ;
    sort(type_1.begin(),type_1.end()) ;
    // One using coins and one using diamonds
    int max_0=0,max_1=0 ;
    int x=type_0.size() ;
    int y=type_1.size() ;
    for(int i=0;i<x;i++)
    {
        if(type_0[i].first<=c)
            max_0=max(max_0,type_0[i].second) ;
    }
    for(int i=0;i<y;i++)
    {
        if(type_1[i].first<=d)
            max_1=max(max_1,type_1[i].second) ;
    }
    int ans=0 ;
    if(max_0&&max_1)
        ans=max(ans,max_0+max_1) ;  
    // Both using coins
    multiset<int>m;                        
    for(int i=0;i<x;i++)
    {
       m.insert(type_0[i].second) ;
    }
    int j=type_0.size()-1 ;
    for(int i=0;i<x;i++)
    {
        if(j<=i)
            break ;
        auto it=m.find(type_0[i].second) ;
        m.erase(it) ;
        int flag=0 ;   
        while(j>i)
        {
            if(type_0[j].first+type_0[i].first<=c)
            {
                flag=1 ;
                break ;
            }
            auto it=m.find(type_0[j].second) ;
            m.erase(it) ;
            j-- ;
        }
        if(flag==0)
            break ;
        if(m.size())
        ans=max(ans,type_0[i].second+*m.rbegin()) ;  
    }
    // Both using diamonds
    m.clear() ;
    for(int i=0;i<y;i++)
    {
       m.insert(type_1[i].second) ;
    }
     j=type_1.size()-1 ;
    for(int i=0;i<y;i++)
    {
        if(j<=i)
            break ;
        auto it=m.find(type_1[i].second) ;
        m.erase(it) ;
        int flag=0 ;
        while(j>i)
        {
            if(type_1[j].first+type_1[i].first<=d)
            {
                flag=1 ;
                break ;
            }
            auto it=m.find(type_1[j].second) ;
            m.erase(it) ;
            j-- ;
        }
        if(flag==0)
            break ;
        if(m.size())
        ans=max(ans,type_1[i].second+*m.rbegin()) ;
    }
    cout<<ans<<"\n" ;
    return 0;
}

Uber
👍2
List<Character> duplicates = new ArrayList<>();
        for (int i = 0; i < inputchar.size(); i++) {
            for (int j = i + 1; j < inputchar.size(); j++) {
                if (inputchar.get(i).equals(inputchar.get(j)) && !duplicates.contains(inputchar.get(i))) {
                    duplicates.add(inputchar.get(i));
                    break;
                }
            }
        }
        return duplicates;
    }

Program to find duplicate in element Array
IBM
👍8🔥1