# Number of ways to select K small even number from left of each element in given Array

• Last Updated : 23 Jun, 2021

Given an array, arr[] consisting of N distinct integers and a positive integer K, the task is to find the number of ways to select K elements from the left side of the every ith position such that the elements are even and less than arr[i].

Examples:

Input: arr[] = {4, 2, 12, 33}, K = 2
Output: 0 0 1 3
Explanation:

1. For arr(=4), there are 0, even elements less than arr[i]. Therefore, ways of selecting 2 elements from 0 element is equal to C(0, 2) = 0.
2. For arr(=2), there are 0, even elements less than arr[i]. Therefore, ways of selecting 2 elements from 0 element is equal to C(0, 2) = 0.
3. For arr(=12), there are 2, even elements less than arr[i]. Therefore, ways of selecting 2 elements from 2 elements is equal to C(2, 2) is 1.
4. For arr(=33), there are 3, even elements less than arr[i]. Therefore, ways of selecting 2 elements from 3 elements is equal to C(3, 2) is 3.

Input: arr[] = {1, 2, 3}, K = 2
Output: 0 0 1

Naive Approach: The simplest approach is to traverse the array and for each element find all the numbers that are smaller than the given element and, even on the left side, and check if the count is smaller than K, then print  0, Otherwise, use combinatorics to find the number of ways.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by using an ordered set data structure. Follow the steps below to solve the problem:

• Initialize an ordered set, say S to store the values that are even.
• Also, initialize an array, say ans[], to store the results.
• Traverse the array, arr[] using the variable i and perform the following steps:
• If the size of the set S is 0, then assign 0 to ans[i] and continue.
• Find the count of elements less than arr[i] using the order_of_key() function in the set S and store it in a variable, say, count.
• Now assign the count of ways of selecting K elements from count i, e C(count, K) to the ans[i].
• Finally, after completing the above steps, print the array ans[i].

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;`` ` `// Policy based data structure``#include ``#include ``using` `namespace` `__gnu_pbds;`` ` `#define ordered_set                              \``    ``tree<``int``, null_type, less<``int``>, rb_tree_tag, \``         ``tree_order_statistics_node_update>`` ` `// NCR formula to find number``// of ways``int` `ncr(``int` `n, ``int` `r)``{``    ``if` `(r == 0 || n == r) {``        ``return` `1;``    ``}``    ``return` `ncr(n - 1, r) + ncr(n - 1, r - 1);``}`` ` `// Function to find the number of ways``// of selecting K smaller even element``// on the left of each element``void` `numberofSmallerElementsInLeft(``int` `arr[], ``int` `N, ``int` `K)``{``    ``// Set to store even elements``    ``ordered_set S;`` ` `    ``// Stores answer for each element``    ``int` `ans[N];`` ` `    ``for` `(``int` `i = 0; i < N; i++) {``        ``// Set is empty``        ``if` `(S.size() == 0)``            ``ans[i] = 0;``        ``else` `{`` ` `            ``// Finds the count of elements``            ``// less than arr[i]``            ``int` `count = S.order_of_key(arr[i]);`` ` `            ``// If count is 0``            ``if` `(count == 0)``                ``ans[i] = 0;``            ``// Else``            ``else` `{`` ` `                ``// Number of ways to choose k``                ``// elements from pos``                ``ans[i] = ncr(count, K);``            ``}``        ``}`` ` `        ``// If the element is even``        ``// insert it into S``        ``if` `(arr[i] % 2 == 0)``            ``S.insert(arr[i]);``    ``}``    ``// Print the result``    ``for` `(``int` `i = 0; i < N; i++) {``        ``cout << ans[i] << ``" "``;``    ``}``}`` ` `// Driver Code``int` `main()``{`` ` `    ``// Input``    ``int` `arr[] = { 4, 2, 12, 33 };``    ``int` `K = 2;``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);`` ` `    ``// Function Call``    ``numberofSmallerElementsInLeft(arr, N, K);``    ``return` `0;``}`

Output

`0 0 1 3 `

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up