Number of ways to select K small even number from left of each element in given Array
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:
- For arr[0](=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.
- For arr[1](=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.
- For arr[2](=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.
- For arr[3](=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 <bits/stdc++.h> using namespace std; // Policy based data structure #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> 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[0]); // Function Call numberofSmallerElementsInLeft(arr, N, K); return 0; } |
Java
import java.util.*; import java.io.*; import java.math.*; // Class to represent policy-based data structure import java.util.TreeSet; import java.util.function.*; import java.util.stream.*; class Main { // NCR formula to find number of ways // to select K elements out of N static 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 static void numberofSmallerElementsInLeft( int arr[], int N, int K) { // Set to store even elements TreeSet<Integer> S = new TreeSet<Integer>(); // Stores answer for each element int [] ans = new int [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.headSet(arr[i]).size(); // 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.add(arr[i]); } // Print the result for ( int i = 0 ; i < N; i++) { System.out.print(ans[i] + " " ); } } // Driver code public static void main(String[] args) { // Input int arr[] = { 4 , 2 , 12 , 33 }; int K = 2 ; int N = arr.length; // Function call numberofSmallerElementsInLeft(arr, N, K); } } |
Python3
from sortedcontainers import SortedSet # NCR formula to find number of ways # to select K elements out of N def ncr(n, r): if r = = 0 or 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 def numberofSmallerElementsInLeft(arr, N, K): # Sorted set to store even elements S = SortedSet() # Stores answer for each element ans = [ 0 ] * N for i in range (N): # Set is empty if len (S) = = 0 : ans[i] = 0 else : # Finds the count of elements less than arr[i] count = len (S[:S.bisect_left(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.add(arr[i]) # Print the result for i in range (N): print (ans[i], end = ' ' ) # Driver code arr = [ 4 , 2 , 12 , 33 ] K = 2 N = len (arr) # Function call numberofSmallerElementsInLeft(arr, N, K) |
Javascript
// Function to find the number of ways // of selecting K smaller even element // on the left of each element function numberofSmallerElementsInLeft(arr, N, K) { // Set to store even elements let S = new Set(); // Stores answer for each element let ans = []; for (let i = 0; i < N; i++) { // Set is empty if (S.size == 0) { ans.push(0); } else { // Finds the count of elements // less than arr[i] let count = 0; for (let num of S) { if (num < arr[i]) { count++; } else { break ; } } // If count is 0 if (count == 0) { ans.push(0); } // Else else { // Number of ways to choose k // elements from pos ans.push(ncr(count, K)); } } // If the element is even // insert it into S if (arr[i] % 2 == 0) { S.add(arr[i]); } } // Print the result console.log(ans.join( " " )); } // NCR formula to find number // of ways function ncr(n, r) { if (r == 0 || n == r) { return 1; } return ncr(n - 1, r) + ncr(n - 1, r - 1); } // Driver Code let arr = [4, 2, 12, 33]; let K = 2; let N = arr.length; // Function Call numberofSmallerElementsInLeft(arr, N, K); |
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApp1 { class Program { static int ncr( int n, int r) { if (r == 0 || n == r) { return 1; } return ncr(n - 1, r) + ncr(n - 1, r - 1); } static void numberofSmallerElementsInLeft( int [] arr, int N, int K) { SortedSet< int > S = new SortedSet< int >(); int [] ans = new int [N]; for ( int i = 0; i < N; i++) { if (S.Count == 0) ans[i] = 0; else { int count = S.TakeWhile(x => x < arr[i]).Count(); if (count == 0) ans[i] = 0; else { ans[i] = ncr(count, K); } } if (arr[i] % 2 == 0) S.Add(arr[i]); } for ( int i = 0; i < N; i++) { Console.Write(ans[i] + " " ); } } static void Main( string [] args) { int [] arr = { 4, 2, 12, 33 }; int K = 2; int N = arr.Length; numberofSmallerElementsInLeft(arr, N, K); } } } |
0 0 1 3
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)