Count of distinct integers belonging to first N terms of at least one of given GPs
Given two Geometric Progressions (a1, r1) and (a2, r2) where (x, y) represents GP with initial term x and common ratio y and an integer N, the task is to find the count of the distinct integers that belong to the first N terms of at least one of the given geometric progressions.
Examples:
Input: N = 5, a1 = 3, r1 = 2, a2 = 6, r2 = 2
Output: 6
Explanation: The first 5 terms of the given geometric progressions are {3, 6, 12, 24, 48} and {6, 12, 24, 48, 96} respectively. Hence, the total count of distinct integers in the GP is 6.
Input: N = 5, a1 = 3, r1 = 2, a2 = 2, r2 = 3
Output: 9
Explanation: The first 5 terms of the given geometric progressions are {3, 6, 12, 24, 48} and {2, 6, 18, 54, 162} respectively. Hence, the total count of distinct integers in the GP is 9.
Approach: The given problem can be solved by the observation that the total count of distinct integers can be calculated by generating the first N terms of both the Geometric Progressions and removing the duplicates terms. This can be achieved by the use of the set data structure. Firstly, generate all the N terms of the 1st GP and insert the terms into a set S. Similarly, insert the terms of the 2nd GP into the set S. The size of the set S is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int UniqueGeometricTerms( int N, int a1,
int r1, int a2,
int r2)
{
set< int > S;
long long p1 = a1;
for ( int i = 0; i < N; i++) {
S.insert(p1);
p1 = ( long long )(p1 * r1);
}
long long p2 = a2;
for ( int i = 0; i < N; i++) {
S.insert(p2);
p2 = ( long long )(p2 * r2);
}
return S.size();
}
int main()
{
int N = 5;
int a1 = 3, r1 = 2, a2 = 2, r2 = 3;
cout << UniqueGeometricTerms(
N, a1, r1, a2, r2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int UniqueGeometricTerms( int N, int a1,
int r1, int a2,
int r2)
{
HashSet<Integer> S= new HashSet<Integer>();
int p1 = a1;
for ( int i = 0 ; i < N; i++) {
S.add(p1);
p1 = (p1 * r1);
}
int p2 = a2;
for ( int i = 0 ; i < N; i++) {
S.add(p2);
p2 = (p2 * r2);
}
return S.size();
}
public static void main(String[] args)
{
int N = 5 ;
int a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ;
System.out.print(UniqueGeometricTerms(
N, a1, r1, a2, r2));
}
}
|
Python3
def UniqueGeometricTerms(N, a1, r1, a2, r2):
S = set ()
p1 = a1
for i in range (N):
S.add(p1)
p1 = (p1 * r1)
p2 = a2
for i in range (N):
S.add(p2)
p2 = (p2 * r2)
return len (S)
if __name__ = = '__main__' :
N = 5
a1 = 3
r1 = 2
a2 = 2
r2 = 3
print (UniqueGeometricTerms(N, a1, r1, a2, r2))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int UniqueGeometricTerms( int N, int a1,
int r1, int a2,
int r2)
{
HashSet< int > S= new HashSet< int >();
int p1 = a1;
for ( int i = 0; i < N; i++) {
S.Add(p1);
p1 = (p1 * r1);
}
int p2 = a2;
for ( int i = 0; i < N; i++) {
S.Add(p2);
p2 = (p2 * r2);
}
return S.Count;
}
public static void Main( string [] args)
{
int N = 5;
int a1 = 3, r1 = 2, a2 = 2, r2 = 3;
Console.Write(UniqueGeometricTerms(
N, a1, r1, a2, r2));
}
}
|
Javascript
<script>
function UniqueGeometricTerms(N, a1,
r1, a2,
r2)
{
let S = new Set();
let p1 = a1;
for (let i = 0; i < N; i++) {
S.add(p1);
p1 = (p1 * r1);
}
let p2 = a2;
for (let i = 0; i < N; i++) {
S.add(p2);
p2 = (p2 * r2);
}
return S.size;
}
let N = 5;
let a1 = 3, r1 = 2, a2 = 2, r2 = 3;
document.write(UniqueGeometricTerms(
N, a1, r1, a2, r2));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...