Count number of subsets whose median is also present in the same subset in C++

C++Server Side ProgrammingProgramming

Given an array arr[] containing positive numbers. The goal is to find subsets of elements of arr[] such that the median of values of the subset is also present in that set.

For Example

Input

arr[] = { 1,2,3 }

Output

Count of number of subsets whose median is also present in the same subset are: 4

Explanation

The sets with their medians in that same set are:
[ 1 ], median is 1
[ 2 ], median is 2
[ 3 ], median is 3
[ 1,2,3 ], median is 2

Input

arr[] = { 4,6,5 }

Output

Count of number of subsets whose median is also present in the same subset are: 4

Explanation

The sets with their medians in that same set are:
[ 4 ], [ 6 ], [ 5 ], [ 4,6,5 ],

Approach used in the below program is as follows

In this approach we will check for even and odd sized elements. Then we can find the median based on the fact that for an odd number of items, the median is present in the same set as the middle element. So we will add 2n-1 to the count. For even length subsets we will check if there are two same elements, so count for even length subsets with two middle elements.

  • Take array arr[] of positive numbers.

  • Function median_subset(arr, size) takes arr and returns the count of the number of subsets whose median is also present in the same subset.

  • Function check(int temp) takes an integer and returns a factorial of that number using for loop from i=2 to i<=temp.

  • Calculate count=count*i, and return it at the end of loop as factorial.

  • Function com(int n, int r) takes n and r and returns value of combination nCr. Inside this take temp = check(r) * check(n − r) and temp_2=check(n) / temp. Return tem_2 as calculated value.

  • Function power(int n, int r) takes n and r and returns value of nr.

  • If r is 0 then answer will be 1 so return 1.

  • Take total = power(n, r / 2). ( nr/2)

  • Update total with total2 % mod. Where mod=1000000007.

  • If r is even then return temp as (total * n) % mod, else return total.

  • Inside the function median_subset(), take count as count = power(2, size − 1) which is the number of odd length subsets.

  • Sort array arr[] using sort(arr, arr + size).

  • Using a while loop we will check for each element for the leftmost middle element till they are equal.

  • Take temp_2 = size − 1 − temp as number of elements in the right of righmost middle element.

  • Take temp_3 = i as the number of elements in the left of the leftmost middle element.

  • Add selected even length subsets to count as count = (count + com(temp_3 + temp_2, temp_3)) % mod.

  • At the end of the while loop we will have count.

  • Return count as result.

Example

 Live Demo

#include <algorithm>
#include <iostream>
using namespace std;
#define mod 1000000007;
int check(int temp){
   int count = 1;
   for (int i = 2; i <= temp; i++){
      count = count * i;
   }
   return count;
}
int com(int n, int r){
   int temp = check(r) * check(n − r);
   int temp_2 = check(n) / temp;
   return temp_2;
}
int power(int n, int r){
   if (r == 0){
      return 1;
   }
   int total = power(n, r / 2);
   total = (total * total) % mod;
   if (r % 2){
      int temp = (total * n) % mod;
      return temp;
   } else {
      return total;
   }
}
int median_subset(int* arr, int size){
   int count = power(2, size − 1);
   sort(arr, arr + size);
   for (int i = 0; i < size; ++i){
      int temp = i + 1;
      while (temp < size && arr[temp] == arr[i]){
         int temp_2 = size − 1 − temp;
         int temp_3 = i;
         count = (count + com(temp_3 + temp_2, temp_3)) % mod;
         temp++;
      }
   }
   return count;
}
int main(){
   int arr[] = { 4, 5, 4, 6 };
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<"Count of number of subsets whose median is also present in the same subset are: "<<median_subset(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of number of subsets whose median is also present in the same subset are: 9
raja
Updated on 05-Jan-2021 06:37:47

Advertisements