Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
}
Source by www.chegg.com #
 
PREVIOUS NEXT
Tagged: #def
ADD COMMENT
Topic
Name
1+1 =