Math, asked by Arjita8806, 8 months ago

You are given n integers and you want to select exactly k integers such the total sum of all the selected integers is greater than s.

Answers

Answered by sunny3743
0

Step-by-step explanation:

GEEKSFORGEEKS

Number of ways to choose elements from the array such that their average is K

Given an array arr[] of N integers and an integer K. The task is to find the number of ways to select one or more elements from the array such that the average of the selected integers is equal to given number K.

Examples:

Input: arr[] = {7, 9, 8, 9}, K = 8

Output: 5

{8}, {7, 9}, {7, 9}, {7, 8, 9} and {7, 8, 9}

Input: arr[] = {3, 6, 2, 8, 7, 6, 5, 9}, K = 5

Output: 19

Input: arr[] = {6, 6, 9}, K = 8

Output: 0

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach: A simple solution would be to try for all possibilities since N can be large. Time complexity can be 2N.

Efficient Approach: The above approach can be optimized by using dynamic programming to solve this problem. Suppose we are at i_th index, and let val be the current value of that index. We have two possibilities either to choose that element in the answer or discard the element. Hence we are done now. We will also keep track of the number of elements in our current set of chosen elements.

Following is the recursive formula.

ways(index, sum, count)

= ways(index - 1, sum, count)

+ ways(index - 1, sum + arr[index], count + 1)

Below is the implementation of the above approach:

#include <bits/stdc++.h>

using namespace std;

#define MAX_INDEX 51

#define MAX_SUM 2505

// This dp array is used to store our values

// so that we don't have to calculate same

// values again and again

int dp[MAX_INDEX][MAX_SUM][MAX_INDEX];

int waysutil(int index, int sum, int count,

vector<int>& arr, int K)

{

// Base cases

// Index can't be less than 0

if (index < 0)

return 0;

if (index == 0) {

// No element is picked hence

// average cannot be calculated

if (count == 0)

return 0;

int remainder = sum % count;

// If remainder is non zero, we cannot

// divide the sum by count i.e. the average

// will not be an integer

if (remainder != 0)

return 0;

int average = sum / count;

// If we find an average return 1

if (average == K)

return 1;

}

// If we have already calculated this function

// simply return it instead of calculating it again

if (dp[index][sum][count] != -1)

return dp[index][sum][count];

// If we don't pick the current element

// simple recur for index -1

int dontpick = waysutil(index - 1,

sum, count, arr, K);

// If we pick the current element add it to

// our current sum and increment count by 1

int pick = waysutil(index - 1,

sum + arr[index],

count + 1, arr, K);

int total = pick + dontpick;

// Store the value for the current function

dp[index][sum][count] = total;

return total;

}

// Function to return the number of ways

int ways(int N, int K, int* arr)

{

vector<int> Arr;

// Push -1 at the beginning to

// make it 1-based indexing

Arr.push_back(-1);

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

Arr.push_back(arr[i]);

}

// Initialize dp array by -1

memset(dp, -1, sizeof dp);

// Call recursive function

// waysutil to calculate total ways

int answer = waysutil(N, 0, 0, Arr, K);

return answer;

}

// Driver code

int main()

{

int arr[] = { 3, 6, 2, 8, 7, 6, 5, 9 };

int N = sizeof(arr) / sizeof(arr[0]);

int K = 5;

cout << ways(N, K, arr);

return 0;

}

Output:

19

Recommended Posts:

Number of ways to choose an integer such that there are exactly K elements greater than it in the given array

Count occurrences of the average of array elements with a given number

Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array

Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1

Number of ways to form an array with distinct adjacent elements

Choose two elements from the given array such that their sum is not present in any of the arrays

Average of remaining elements after removing K largest and K smallest elements from array

Choose k array elements such that difference of maximum and minimum is minimized

Find the deleted value from the array when average of original elements is given

Find the ratio of number of elements in two Arrays from their individual and combined average

Similar questions