Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 votes
1.5k views

image

image

by (610 points)
edited by | 1.5k views

2 Answers

0 votes

def inventory_process(robots):

    survival = []

    for i in range(len(robots)):

        robots_copy = robots[:]

        repeat = True

        while repeat:

            for j in range(len(robots)):

               

                if i != j:

                    if robots_copy[i] >= robots_copy[j] and robots_copy[j] > 0:

                        robots_copy[i] += robots_copy[j]

                        robots_copy[j] = 0

            robots_at_i_backup = robots_copy[i]

            robots_copy[i] = 0

            if all(x == 0 for x in robots_copy):

                survival.append(i + 1)

                break

            else:

                robots_copy[i] = robots_at_i_backup

            for k in range(len(robots_copy)):

                if k != i and robots_copy[i] >= robots_copy[k] and robots_copy[k] > 0:

                    repeat = True

                    break

                else:

                    repeat = False

    return survival

print(inventory_process([1, 2, 3, 4, 5]))

print(inventory_process([1, 6, 2, 7, 2]))

by
0 votes
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

vector<int> solve(vector<int>& arr, int n){

    vector<pair<int,int>> sorted;

    

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

        sorted.push_back({arr[i], i+1});

    }

    

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

    

    vector<int> prefix;

    

    int c = 0;

    

    for(auto it : sorted){

        c += it.first;

        prefix.push_back(c);

    }

    

    int i = n-2;

    

    while(i >= 0 && prefix[i] >= sorted[i+1].first) i--;

    

    i++;

    vector<int> ans;

    

    while(i < n){

        ans.push_back(sorted[i].second);

        i++;

    }

    

    return ans;    

}

int main()

{

    int n;

    cin >> n;

    vector<int> arr;

    

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

        int t;

        cin >> t;

        arr.push_back(t);

    }

    

    vector<int> ans = solve(arr, n);

    

    for(auto it : ans){

        cout << it << endl;

    }

    

    return 0;

}
by
Welcome to Itjobxs ; we help companies ease their recruitment process ; we shortlist talented candidates via our portal and send those profiles for job opportunities to IT companies. We also provide IT consulting and services.
23 questions
39 answers
22 users