Number of ways to select K small even number from left of each element in given Array
Last Updated :
25 Apr, 2023
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);
}
}
}
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Similar Reads
Count ways to select K array elements lying in a given range Given three positive integers, L, R, K and an array arr[] consisting of N positive integers, the task is to count the number of ways to select at least K array elements from the given array having values in the range [L, R]. Examples: Input: arr[] = {12, 4, 6, 13, 5, 10}, K = 3, L = 4, R = 10 Output
9 min read
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 the given number K. Examples: Input: arr[] = {7, 9, 8, 9}, K = 8 Output: 5 {8}, {7, 9}, {7, 9}, {7, 8,
11 min read
Count of permutations such that sum of K numbers from given range is even Given a range [low, high], both inclusive, and an integer K, the task is to select K numbers from the range(a number can be chosen multiple times) such that the sum of those K numbers is even. Print the number of all such permutations. Examples: Input: low = 4, high = 5, k = 3 Output: 4 Explanation:
7 min read
Count set bits in the Kth number after segregating even and odd from N natural numbers Given two integers N and K, the task is to find the count of set bits in the Kth number in the Odd-Even sequence made of the number from the range [1, N]. The Odd-Even sequence first contains all the odd numbers from 1 to N and then all the even numbers from 1 to N.Examples: Input: N = 8, K = 4 Outp
6 min read
Count of ways to make Array sum even by removing only one element Given an array arr[] positive integers, the task is to find the number of ways to convert the array sum even if we are allowed to remove only one element.Examples: Input: arr[] = { 1, 3, 3, 2 } Output: 3 Explanation: 1. Remove 1, then sum is 3 + 3 + 2 = 8. 2. Remove 3, then sum is 1 + 3 + 2 = 6. 3.
5 min read
Sum of elements till the smallest index such that there are no even numbers to its right Given an array arr[] of N integers. The task is to find the sum of elements to the smallest index such that there are no even elements to the right of the index. Note that the array will have at least one even element.Examples: Input: arr[] = {2, 3, 5, 6, 3, 3} Output: 16 2 + 3 + 5 + 6 = 16Input: ar
4 min read