Given an integer K and an array arr[] of N integers, each of which is the first term and common difference of an Arithmetic Progression, the task is to find the Kth element of the set S formed by merging the N arithmetic progressions.
Examples:
Input: arr[] = {2, 3}, K = 10
Output: 15
Explanation:
There are 2 arithmetic progressions.
First-term and the common difference of the first A.P is 2. Therefore AP1 = {2, 4, 6, …}
First term and the common difference of the second A.P is 3. Therefore AP2 = {3, 6, 9, …}
Set S contains AP1 and AP2, i.e. { 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20…… }.
Hence 10th term is: 15
Input: arr[] = {2, 3, 5, 7, 11}, K = 8
Output: 9
Explanation:
There are 5 arithmetic progressions.
First-term and the common difference of the first A.P is 2. Therefore AP1 = {2, 4, 6, …}
First term and the common difference of the second A.P is 3. Therefore AP2 = {3, 6, 9, …}
First-term and the common difference of the third A.P is 5. Therefore AP3 = {5, 10, 15, …}
First term and the common difference of the fourth A.P is 7. Therefore AP4 = {7, 14, 21, …}
First-term and the common difference of the fifth A.P is 11. Therefore AP5 = {11, 22, 33, …}
Thus set S contains { 2, 3, 4, 5, 6, 7, 8, 9, 10, …. }.
Hence 8th term is: 9
Approach:
Follow the steps below to solve the problem:
- Consider a range of [1, maxm] and compute mid of that range.
- Check if K elements can be obtained by merging the N series’. If the number of terms exceeds K, reduce R to mid. Otherwise, update L to mid. Now, proceed further until we find a mid for which exactly K terms in the series can be obtained.
- To find how many elements will occur before any particular value, we need to apply Inclusion and Exclusion principle to obtain union of all A.P.s.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define maxm 1000000000
int count(vector< int > v, int n)
{
int i, odd = 0, even = 0;
int j, d, count;
int t = ( int )1 << v.size();
int size = v.size();
for (i = 1; i < t; i++) {
d = 1, count = 0;
for (j = 0; j < size; j++) {
if (i & (1 << j)) {
d *= v[j];
count++;
}
}
if (count & 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
int BinarySearch( int l, int r,
vector< int > v,
int key)
{
int mid;
while (r - l > 1) {
mid = (l + r) / 2;
if (key <= count(v, mid)) {
r = mid;
}
else {
l = mid;
}
}
if (key == count(v, l))
return l;
else
return r;
}
int main()
{
int N = 2, K = 10;
vector< int > v = { 2, 3 };
cout << BinarySearch(1, maxm, v, K)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int maxm = 1000000000 ;
static int count( int []v, int n)
{
int i, odd = 0 , even = 0 ;
int j, d, count;
int t = ( int ) 1 << v.length;
int size = v.length;
for (i = 1 ; i < t; i++)
{
d = 1 ;
count = 0 ;
for (j = 0 ; j < size; j++)
{
if ((i & ( 1 << j)) > 0 )
{
d *= v[j];
count++;
}
}
if (count % 2 == 1 )
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
static int BinarySearch( int l, int r,
int []v,
int key)
{
int mid;
while (r - l > 1 )
{
mid = (l + r) / 2 ;
if (key <= count(v, mid))
{
r = mid;
}
else
{
l = mid;
}
}
if (key == count(v, l))
return l;
else
return r;
}
public static void main(String[] args)
{
int N = 2 , K = 10 ;
int []v = { 2 , 3 };
System.out.print(BinarySearch( 1 , maxm, v, K) + "\n" );
}
}
|
Python3
maxm = 1000000000
def count(v, n):
odd, even = 0 , 0
t = 1 << len (v)
size = len (v)
for i in range ( 1 , t):
d, count = 1 , 0
for j in range ( 0 , size):
if (i & ( 1 << j)):
d * = v[j]
count + = 1
if (count & 1 ):
odd + = n / / d
else :
even + = n / / d
return (odd - even)
def BinarySearch(l, r, v, key):
while (r - l > 1 ):
mid = (l + r) / / 2
if (key < = count(v, mid)):
r = mid
else :
l = mid
if (key = = count(v, l)):
return l
else :
return r
N, K = 2 , 10
v = [ 2 , 3 ]
print (BinarySearch( 1 , maxm, v, K))
|
C#
using System;
class GFG{
static readonly int maxm = 1000000000;
static int count( int []v, int n)
{
int i, odd = 0, even = 0;
int j, d, count;
int t = ( int )1 << v.Length;
int size = v.Length;
for (i = 1; i < t; i++)
{
d = 1;
count = 0;
for (j = 0; j < size; j++)
{
if ((i & (1 << j)) > 0)
{
d *= v[j];
count++;
}
}
if (count % 2 == 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
static int BinarySearch( int l, int r,
int []v, int key)
{
int mid;
while (r - l > 1)
{
mid = (l + r) / 2;
if (key <= count(v, mid))
{
r = mid;
}
else
{
l = mid;
}
}
if (key == count(v, l))
return l;
else
return r;
}
public static void Main(String[] args)
{
int K = 10;
int []v = { 2, 3 };
Console.Write(BinarySearch(
1, maxm, v, K) + "\n" );
}
}
|
Javascript
<script>
let maxm = 1000000000;
function count(v, n)
{
let i, odd = 0, even = 0;
let j, d, count;
let t = 1 << v.length;
let size = v.length;
for (i = 1; i < t; i++)
{
d = 1;
count = 0;
for (j = 0; j < size; j++)
{
if ((i & (1 << j)) > 0)
{
d *= v[j];
count++;
}
}
if (count % 2 == 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
function BinarySearch(l, r,
v, key)
{
let mid;
while (r - l > 1)
{
mid = Math.floor((l + r) / 2);
if (key <= count(v, mid))
{
r = mid;
}
else
{
l = mid;
}
}
if (key == count(v, l))
return l;
else
return r;
}
let N = 2, K = 10;
let v = [ 2, 3 ];
document.write(BinarySearch(1, maxm, v, K) + "\n" );
</script>
|
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 May, 2021
Like Article
Save Article