Charged Up Array

You are given an array A of size An element Ai is said to be charged if its value(

Ai) is greater than or equal to KiKi is the total number of subsets of array A, that consist of element Ai
Total charge value of the array is defined as the summation of all charged elements present in the array mod 109+7.
Your task is to output the total charge value of the given array A.

Example:

Input:  n = 3, a = [3,4,5]
Output: 9

Approach

C++

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007

long long chargeArray(long long nlong long a[])
{
    long long ans = 0;
    for (long long i = 0i < ni++)
    {
        if (a[i] >= pow(2n) / 2)
        {
            ans = (ans + a[i]) % MOD;
        }
    }
    return ans % MOD;
}
int main()
{

    long long n = 3;
    long long a[n] = {345};

    cout << chargeArray(na<< "\n";

    return 0;
}


No comments:

Post a Comment