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++
#include <bits/stdc++.h>
using namespace std;
#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>
int ncr( int n, int r)
{
if (r == 0 || n == r) {
return 1;
}
return ncr(n - 1, r) + ncr(n - 1, r - 1);
}
void numberofSmallerElementsInLeft( int arr[], int N, int K)
{
ordered_set S;
int ans[N];
for ( int i = 0; i < N; i++) {
if (S.size() == 0)
ans[i] = 0;
else {
int count = S.order_of_key(arr[i]);
if (count == 0)
ans[i] = 0;
else {
ans[i] = ncr(count, K);
}
}
if (arr[i] % 2 == 0)
S.insert(arr[i]);
}
for ( int i = 0; i < N; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
int arr[] = { 4, 2, 12, 33 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
numberofSmallerElementsInLeft(arr, N, K);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.TreeSet;
import java.util.function.*;
import java.util.stream.*;
class Main {
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) {
TreeSet<Integer> S = new TreeSet<Integer>();
int [] ans = new int [N];
for ( int i = 0 ; i < N; i++) {
if (S.size() == 0 )
ans[i] = 0 ;
else {
int count = S.headSet(arr[i]).size();
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++) {
System.out.print(ans[i] + " " );
}
}
public static void main(String[] args) {
int arr[] = { 4 , 2 , 12 , 33 };
int K = 2 ;
int N = arr.length;
numberofSmallerElementsInLeft(arr, N, K);
}
}
|
Python3
from sortedcontainers import SortedSet
def ncr(n, r):
if r = = 0 or n = = r:
return 1
return ncr(n - 1 , r) + ncr(n - 1 , r - 1 )
def numberofSmallerElementsInLeft(arr, N, K):
S = SortedSet()
ans = [ 0 ] * N
for i in range (N):
if len (S) = = 0 :
ans[i] = 0
else :
count = len (S[:S.bisect_left(arr[i])])
if count = = 0 :
ans[i] = 0
else :
ans[i] = ncr(count, K)
if arr[i] % 2 = = 0 :
S.add(arr[i])
for i in range (N):
print (ans[i], end = ' ' )
arr = [ 4 , 2 , 12 , 33 ]
K = 2
N = len (arr)
numberofSmallerElementsInLeft(arr, N, K)
|
Javascript
function numberofSmallerElementsInLeft(arr, N, K) {
let S = new Set();
let ans = [];
for (let i = 0; i < N; i++) {
if (S.size == 0) {
ans.push(0);
}
else {
let count = 0;
for (let num of S) {
if (num < arr[i]) {
count++;
}
else {
break ;
}
}
if (count == 0) {
ans.push(0);
}
else {
ans.push(ncr(count, K));
}
}
if (arr[i] % 2 == 0) {
S.add(arr[i]);
}
}
console.log(ans.join( " " ));
}
function ncr(n, r) {
if (r == 0 || n == r) {
return 1;
}
return ncr(n - 1, r) + ncr(n - 1, r - 1);
}
let arr = [4, 2, 12, 33];
let K = 2;
let N = arr.length;
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)