Kth smallest element in an array that contains A[i] exactly B[i] times
Last Updated :
13 Jul, 2021
Given two arrays A[] and B[] consisting of N positive integers and an integer K, the task is to find the Kth smallest element in the array formed by taking the ith element from the array A[] exactly B[i] times. If there exists no such element, then print -1.
Examples:
Input: K = 4, A[] = {1, 2, 3}, B[] = {1, 2, 3}
Output: 3
Explanation:
The array obtained by taking A[0], B[0] (= 1) time, A[1], B[1] (= 2) times, A[2], B[2]( = 3) times is {1, 2, 2, 3, 3, 3}. Therefore, the 4th element of the array is 3.
Input: K = 4, A[] = {3, 4, 5}, B[] = {2, 1, 3}
Output: 3
Explanation:The array formed is {3, 3, 4, 5, 5, 5}. Therefore, the 4th element of the array i.e 5.
Naive Approach: The simplest approach is to iterate over the range [0, N – 1] and push the element at the ith index of the array, B[i] times into the new array. Finally, print the Kth element of the obtained array after sorting the array in ascending order.
Time Complexity: O(N*log(N)), where N is the number of elements in the new array.
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by using a frequency array to keep the count of every element. Follow the steps below to solve the problem:
- Find the maximum element of the array A[] and store it in a variable, say M.
- Initialize an array, say freq[] of size M + 1 with {0}, to store the frequency of every element.
- Iterate in the range [0, N-1] using the variable i:
- Initialize a variable, say sum as 0, to store the prefix sum up to a particular index.
- Iterate over the range [0, N – 1] using a variable, say i:
- Add freq[i] in sum.
- If sum is greater than or equal to K, then return i.
- Finally, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int KthSmallest( int A[], int B[], int N, int K)
{
int M = 0;
for ( int i = 0; i < N; i++) {
M = max(A[i], M);
}
int freq[M + 1] = { 0 };
for ( int i = 0; i < N; i++) {
freq[A[i]] += B[i];
}
int sum = 0;
for ( int i = 0; i <= M; i++) {
sum += freq[i];
if (sum >= K) {
return i;
}
}
return -1;
}
int main()
{
int A[] = { 3, 4, 5 };
int B[] = { 2, 1, 3 };
int N = sizeof (A) / sizeof (A[0]);
int K = 4;
cout << KthSmallest(A, B, N, K);
return 0;
}
|
Java
public class GFG_JAVA {
static int KthSmallest( int A[], int B[], int N, int K)
{
int M = 0 ;
for ( int i = 0 ; i < N; i++) {
M = Math.max(A[i], M);
}
int freq[] = new int [M + 1 ];
for ( int i = 0 ; i < N; i++) {
freq[A[i]] += B[i];
}
int sum = 0 ;
for ( int i = 0 ; i <= M; i++) {
sum += freq[i];
if (sum >= K) {
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int A[] = { 3 , 4 , 5 };
int B[] = { 2 , 1 , 3 };
int N = A.length;
int K = 4 ;
System.out.println(KthSmallest(A, B, N, K));
}
}
|
Python3
def KthSmallest(A, B, N, K):
M = 0
for i in range (N):
M = max (A[i], M)
freq = [ 0 ] * (M + 1 )
for i in range (N):
freq[A[i]] + = B[i]
sum = 0
for i in range (M + 1 ):
sum + = freq[i]
if ( sum > = K):
return i
return - 1
if __name__ = = "__main__" :
A = [ 3 , 4 , 5 ]
B = [ 2 , 1 , 3 ]
N = len (A)
K = 4
print (KthSmallest(A, B, N, K))
|
C#
using System;
class GFG{
static int KthSmallest( int []A, int []B,
int N, int K)
{
int M = 0;
for ( int i = 0; i < N; i++)
{
M = Math.Max(A[i], M);
}
int []freq = new int [M + 1];
for ( int i = 0; i < N; i++)
{
freq[A[i]] += B[i];
}
int sum = 0;
for ( int i = 0; i <= M; i++)
{
sum += freq[i];
if (sum >= K)
{
return i;
}
}
return -1;
}
public static void Main(String[] args)
{
int []A = { 3, 4, 5 };
int []B = { 2, 1, 3 };
int N = A.Length;
int K = 4;
Console.Write(KthSmallest(A, B, N, K));
}
}
|
Javascript
<script>
function KthSmallest(A, B, N, K)
{
let M = 0;
for (let i = 0; i < N; i++) {
M = Math.max(A[i], M);
}
let freq = Array.from({length: M + 1}, (_, i) => 0);
for (let i = 0; i < N; i++) {
freq[A[i]] += B[i];
}
let sum = 0;
for (let i = 0; i <= M; i++) {
sum += freq[i];
if (sum >= K) {
return i;
}
}
return -1;
}
let A = [ 3, 4, 5 ];
let B = [ 2, 1, 3 ];
let N = A.length;
let K = 4;
document.write(KthSmallest(A, B, N, K));
</script>
|
Time Complexity: O(N), where N is the size of arrays A[] and B[].
Auxiliary Space: O(M), where M is the maximum element of the array A[].
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...