Number of terms in Geometric Series with given conditions
A geometric progression is a sequence of integers b1, b2, b3, …, where for each i > 1, the respective term satisfies the condition bi = bi-1 * q, where q is called the common ratio of the progression.
Given geometric progression b defined by two integers b1 and q, and m “bad” integers a1, a2, .., am, and an integer l, write all progression terms one by one (including repetitive) while the condition |bi| <= l is satisfied (|x| means the absolute value of x).
Calculate how many numbers there will be in our sequence, or print “inf” if there are infinitely many integers.
Note: If a term equals one of the “bad” integers, skip it and move forward to the next term.
Examples:
Input : b1 = 3, q = 2, l = 30,
m = 4
6 14 25 48
Output : 3
The progression will be 3 12 24.
6 will also be there but because
it is a bad integer we won't include it
Input : b1 = 123, q = 1, l = 2143435
m = 4
123 11 -5453 141245
Output : 0
As value of q is 1, progression will
always be 123 and would become infinity
but because it is a bad integer we
won't include it and hence our value
will become 0
Input : b1 = 123, q = 1, l = 2143435
m = 4
5234 11 -5453 141245
Output : inf
In this case, value will be infinity
because series will always be 123 as
q is 1 and 123 is not a bad integer.
Approach:
We can divide our solution into different cases:
Case 1: If the starting value of a series is greater than the given limit, output is 0.
Case 2: If the starting value of a series q is 0, there are three more cases:
Case 2.a: If 0 is not given as a bad integer, the answer will become inf.
Case 2.b: If b1 != 0 but q is 0 and b1 is also not a bad integer, the answer will be 1.
Case 2.c: If 0 is given as a bad integer and b1 = 0, the answer will be 0.
Case 3: If q = 1, we will check if b1 is given as a bad integer or not. If it is, then the answer will be 0, else the answer will be inf.
Case 4: If q = -1, check if b1 and -b1 are present or not. If they are present, our answer will be 0, else our answer will be inf.
Case 5: If none of the above cases hold, simply run a loop from b1 to l and calculate the number of elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map< int , bool > mapp;
void progression( int b1, int q, int l,
int m, int bad[])
{
for ( int i = 0; i < m; i++)
mapp[bad[i]] = 1;
if ( abs (b1) > l)
cout << "0" ;
else if (q == 0 || b1 == 0)
{
if (mapp[0] != 1)
cout << "inf" ;
else if (mapp[0] == 1 && mapp[b1] != 1)
cout << "1" ;
else
cout << "0" ;
}
else if (q == 1)
{
if (mapp[b1] != 1)
cout << "inf" ;
else
cout << "0" ;
}
else if (q == -1)
{
if (mapp[b1] != 1 || mapp[-1 * b1] != 1)
cout << "inf" ;
else
cout << "0" ;
}
else
{
int co = 0;
while ( abs (b1) <= l) {
if (mapp[b1] != 1)
co++;
b1 *= 1LL * q;
}
cout << co;
}
}
int main()
{
int b1 = 3, q = 2, l = 30, m = 4;
int bad[4] = { 6, 14, 25, 48 };
progression(b1, q, l, m, bad);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static HashMap<Integer, Boolean> map = new HashMap<>();
static void progression( int b1, int q, int l,
int m, int [] bad)
{
for ( int i = 0 ; i < m; i++)
map.put(bad[i], true );
if (Math.abs(b1) > l)
System.out.print( "0" );
else if (q == 0 || b1 == 0 )
{
if (!map.containsKey( 0 ))
System.out.print( "inf" );
else if (map.get( 0 ) == true && !map.containsKey(b1))
System.out.print( "1" );
else
System.out.print( "0" );
}
else if (q == 1 )
{
if (!map.containsKey(b1))
System.out.print( "inf" );
else
System.out.print( "0" );
}
else if (q == - 1 )
{
if (!map.containsKey(b1) || !map.containsKey(- 1 * b1))
System.out.print( "inf" );
else
System.out.print( "0" );
}
else
{
int co = 0 ;
while (Math.abs(b1) <= l)
{
if (!map.containsKey(b1))
co++;
b1 *= q;
}
System.out.print(co);
}
}
public static void main(String[] args)
{
int b1 = 3 , q = 2 , l = 30 , m = 4 ;
int [] bad = { 6 , 14 , 25 , 48 };
progression(b1, q, l, m, bad);
}
}
|
Python3
mpp = dict ()
def progression(b1, q, l, m, bad):
for i in range (m):
mpp[bad[i]] = 1
if ( abs (b1) > l):
print ( "0" ,end = "")
elif (q = = 0 or b1 = = 0 ) :
if ( 0 not in mpp.keys()):
print ( "inf" ,end = "")
elif (mpp[ 0 ] = = 1 and b1 not in mpp.keys()) :
print ( "1" ,end = "")
else :
print ( "0" ,end = "")
elif (q = = 1 ):
if (b1 not in mpp.keys()) :
print ( "inf" ,end = "")
else :
print ( "0" ,end = "")
elif (q = = - 1 ):
if (b1 not in mpp.keys() or - 1 * b1 not in mpp.keys()) :
print ( "inf" ,end = "")
else :
print ( "0" ,end = "")
else :
co = 0
while ( abs (b1) < = l):
if (b1 not in mpp.keys()):
co + = 1
b1 * = q
print (co,end = "")
b1 = 3
q = 2
l = 30
m = 4
bad = [ 6 , 14 , 25 , 48 ]
progression(b1, q, l, m, bad)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Dictionary< int ,
bool > map = new Dictionary< int ,
bool >();
static void progression( int b1, int q, int l,
int m, int [] bad)
{
for ( int i = 0; i < m; i++)
if (!map.ContainsKey(bad[i]))
map.Add(bad[i], true );
if (Math.Abs(b1) > l)
Console.Write( "0" );
else if (q == 0 || b1 == 0)
{
if (!map.ContainsKey(0))
Console.Write( "inf" );
else if (map[0] == true &&
!map.ContainsKey(b1))
Console.Write( "1" );
else
Console.Write( "0" );
}
else if (q == 1)
{
if (!map.ContainsKey(b1))
Console.Write( "inf" );
else
Console.Write( "0" );
}
else if (q == -1)
{
if (!map.ContainsKey(b1) ||
!map.ContainsKey(-1 * b1))
Console.Write( "inf" );
else
Console.Write( "0" );
}
else
{
int co = 0;
while (Math.Abs(b1) <= l)
{
if (!map.ContainsKey(b1))
co++;
b1 *= q;
}
Console.Write(co);
}
}
public static void Main(String[] args)
{
int b1 = 3, q = 2, l = 30, m = 4;
int [] bad = { 6, 14, 25, 48 };
progression(b1, q, l, m, bad);
}
}
|
Javascript
<script>
let map = new Map();
function progression(b1,q,l,m,bad)
{
for (let i = 0; i < m; i++)
map.set(bad[i], true );
if (Math.abs(b1) > l)
document.write( "0" );
else if (q == 0 || b1 == 0)
{
if (!map.has(0))
document.write( "inf" );
else if (map[0] == true && !map.has(b1))
document.write( "1" );
else
document.write( "0" );
}
else if (q == 1)
{
if (!map.has(b1))
document.write( "inf" );
else
document.write( "0" );
}
else if (q == -1)
{
if (!map.has(b1) || !map.has(-1 * b1))
document.write( "inf" );
else
document.write( "0" );
}
else
{
let co = 0;
while (Math.abs(b1) <= l)
{
if (!map.has(b1))
co++;
b1 *= q;
}
document.write(co);
}
}
let b1 = 3, q = 2, l = 30, m = 4;
let bad = [ 6, 14, 25, 48 ];
progression(b1, q, l, m, bad);
</script>
|
Output:
3
Time Complexity: O(m+logq(l))
Auxiliary Space: O(m)
Last Updated :
09 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...