Search
 
SCRIPT & CODE EXAMPLE
 

CPP

def minimulHeaviestSetA(arr,n)

vector<int> minimalHeaviestSetA(vector<int> arr){
    // sort the array arr[] in descending order
    sort(arr.begin(),arr.end(),greater<int>());

    // create array A, sum variable S and Sa
    vector<int> A;
    int S = 0, Sa = 0;

    // store the sum of elements in S
    for(int i=0;i<arr.size();i++) S+=arr[i];

    int i = 0;
    while(i<arr.size() && Sa <= S - Sa){
        Sa += arr[i];
        A.push_back(arr[i]);
        i++;
    }

    // finally return A in increasing order (reverse it)
    reverse(A.begin(),A.end());
    return A;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to list directory in c++ 
Cpp :: c++ copy pointer vector to static 
Cpp :: arrays to function c++ 
Cpp :: c++ calorie calculator using a for loop 
Cpp :: What will be the values of variables p, q and i at the end of following loop? int p = 5; int q = 18; for(int i=1;i&lt;5;i++) p++; --q; 
Cpp :: 2d stl array 
Cpp :: how to use printf with microseconds c++ 
Cpp :: construct string with repeat limit 
Cpp :: vprintf 
Cpp :: Structure of s void function 
Cpp :: total sales in array c++ two dimensional array 
Cpp :: _ZNSolsEi 
Cpp :: sum of 2 arrays c++ 
Cpp :: c++ to c converter online free 
Cpp :: c++ map value int to string 
Cpp :: online c++ compiler 
Cpp :: c++ is nan 
Cpp :: concatenate 2 vectors in c++ 
Cpp :: batch to exe 
Cpp :: c vs c++ vs c# 
Cpp :: c++ function with parameters 
Cpp :: c++ generate random number upper and lower bound 
C :: swapping of two numbers in c without temporary variable 
C :: golang loop through array 
C :: react-textfit 
C :: bootstrap 5 modal not working vue js 3 
C :: fonction recursive successeur nombre chaine de caractere en c 
C :: scan numbers into array c 
C :: check if the c code is a palindrome 
C :: multiplicationin c 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =