Count of distinct integers belonging to first N terms of at least one of given GPs | C # program for the above approach ; Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
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 ) ) ; } }