codeparrot/xlcost-text-to-code huggingface dataset:
Check if given string contains all the digits | Java implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if str contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the String ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in str ; Driver code
class GFG { static int MAX = 10 ; static boolean isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static boolean allDigits ( String str , int len ) { boolean [ ] present = new boolean [ MAX ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( isDigit ( str . charAt ( i ) ) ) { int digit = str . charAt ( i ) - '0' ; present [ digit ] = true ; } } for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " Geeks12345for69708" ; int len = str . length ( ) ; if ( allDigits ( str , len ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if a symmetric plus is possible from the elements of the given array | Java implementation of the approach ; Function that return true if a symmetric is possible with the elements of the array ; Map to store the frequency of the array elements ; Traverse through array elements and count frequencies ; For every unique element ; Element has already been found ; The frequency of the element something other than 0 and 1 ; Driver code
import java . util . * ; class GFG { static boolean isPlusPossible ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } boolean foundModOne = false ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { int element = x . getKey ( ) ; int frequency = x . getValue ( ) ; if ( frequency % 4 == 0 ) continue ; if ( frequency % 4 == 1 ) { if ( foundModOne ) return false ; foundModOne = true ; } else return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 } ; int n = arr . length ; if ( isPlusPossible ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Queries for elements greater than K in the given index range using Segment Tree | Java implementation of the approach ; combine function to make parent node ; building the tree ; leaf node ; merging the nodes while backtracking ; performing query ; Out of Bounds ; completely overlaps ; partially overlaps ; Driver Code ; 1 - based indexing
public class GFG { int [ ] arr , tree ; int combine ( int a , int b ) { if ( a != 0 && b != 0 ) { return a ; } if ( a >= b ) { return a ; } return b ; } void buildTree ( int ind , int low , int high , int x ) { if ( low == high ) { if ( arr [ low ] > x ) { tree [ ind ] = arr [ low ] ; } else { tree [ ind ] = 0 ; } return ; } int mid = ( high - low ) / 2 + low ; buildTree ( 2 * ind + 1 , low , mid , x ) ; buildTree ( 2 * ind + 2 , mid + 1 , high , x ) ; tree [ ind ] = combine ( tree [ 2 * ind + 1 ] , tree [ 2 * ind + 2 ] ) ; } int query ( int ind , int low , int high , int l , int r ) { int mid = ( high - low ) / 2 + low ; if ( low > r high < l ) { return 0 ; } if ( l <= low && r >= high ) { return tree [ ind ] ; } return combine ( query ( 2 * ind + 1 , low , mid , l , r ) , query ( 2 * ind + 2 , mid + 1 , high , l , r ) ) ; } public static void main ( String args [ ] ) { GFG ob = new GFG ( ) ; ob . arr = new int [ ] { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = ob . arr . length ; int k = 6 ; ob . tree = new int [ 4 * n ] ; int l = 1 , r = 4 ; ob . buildTree ( 0 , 0 , n - 1 , k ) ; System . out . println ( ob . query ( 0 , 0 , n - 1 , l - 1 , r - 1 ) ) ; } }
Calculate the Sum of GCD over all subarrays | Java program to find Sum of GCD over all subarrays . ; Utility function to calculate sum of gcd of all sub - arrays . ; Fixing the starting index of a subarray ; Fixing the ending index of a subarray ; Finding the GCD of this subarray ; Adding this GCD in our sum ; Driver Code
class GFG { static int findGCDSum ( int n , int a [ ] ) { int GCDSum = 0 ; int tempGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( int k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int totalSum = findGCDSum ( n , a ) ; System . out . print ( totalSum + "NEW_LINE"); } }
Check if the array has an element which is equal to XOR of remaining elements | Java implementation of the approach ; Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code
class GFG { static boolean containsElement ( int [ ] arr , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { int x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 2 , 4 , 15 , 1 } ; int n = arr . length ; if ( containsElement ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Square root of a number without using sqrt ( ) function | Java implementation of the approach ; Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
import java . util . * ; class GFG { static double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( Math . abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } static void findSqrt ( double n ) { double i = 1 ; boolean found = false ; while ( ! found ) { if ( i * i == n ) { System . out . println ( i ) ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; System . out . printf ( " % .5f " , res ) ; found = true ; } i ++ ; } } public static void main ( String [ ] args ) { double n = 3 ; findSqrt ( n ) ; } }
Length of longest substring having all characters as K | Java program for the above approach ; Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
import java . util . * ; class GFG { static int length_substring ( String S , char K ) { int curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } public static void main ( String [ ] args ) { String S = " abcd1111aabc " ; char K = '1' ; System . out . print ( length_substring ( S , K ) ) ; } }
Find a partition point in array to maximize its xor sum | Java program to find partition point in array to maximize xor sum ; Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call
class GFG { public static int Xor_Sum ( int [ ] arr , int n ) { int sum = 0 , index = - 1 ; int left_xor = 0 , right_xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . length ; System . out . println ( Xor_Sum ( arr , n ) ) ; } }
Find a partition point in array to maximize its xor sum | Java program to find partition point in array to maximize xor sum ; Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call
import java . util . * ; class GFG { static void ComputePrefixXor ( int arr [ ] , int PrefixXor [ ] , int n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } static int Xor_Sum ( int arr [ ] , int n ) { int [ ] PrefixXor = new int [ n ] ; ComputePrefixXor ( arr , PrefixXor , n ) ; int sum = 0 , index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . length ; System . out . println ( Xor_Sum ( arr , n ) ) ; } }
Strings from an array which are not prefix of any other string | Java implementation of the approach ; Trie node ; isEndOfWord is true if the node represents end of a word ; Returns new trie node ( initialized to NULLs ) ; Function to insert a String into trie ; While inerting a word make each isEndOfWord as false ; Check if this word is prefix of some already inserted word If it is then don 't insert this word ; If present word is not prefix of any other word then insert it ; Function to display words in Trie ; If node is leaf node , it indicates end of String , so a null character is added and String is displayed ; If NON NULL child is found add parent key to str and call the display function recursively for child node ; Driver code ; Conclass trie
import java . util . Arrays ; class GFG { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children ; boolean isEndOfWord ; TrieNode ( ) { this . children = new TrieNode [ ALPHABET_SIZE ] ; } } static TrieNode getNode ( ) { TrieNode pNode = new TrieNode ( ) ; pNode . isEndOfWord = false ; Arrays . fill ( pNode . children , null ) ; return pNode ; } static void insert ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = getNode ( ) ; pCrawl . isEndOfWord = false ; pCrawl = pCrawl . children [ index ] ; } int i ; for ( i = 0 ; i < 26 ; i ++ ) { if ( pCrawl . children [ i ] != null ) { break ; } } if ( i == 26 ) { pCrawl . isEndOfWord = true ; } } static void display ( TrieNode root , char str [ ] , int level ) { if ( root . isEndOfWord ) { str [ level ] = ' \0' ; System . out . println ( str ) ; } int i ; for ( i = 0 ; i < ALPHABET_SIZE ; i ++ ) { if ( root . children [ i ] != null ) { str [ level ] = ( char ) ( i + ' a ' ) ; display ( root . children [ i ] , str , level + 1 ) ; } } } public static void main ( String [ ] args ) { String keys [ ] = { " apple " , " app " , " there " , " the " , " like " } ; int n = keys . length ; TrieNode root = getNode ( ) ; for ( int i = 0 ; i < n ; i ++ ) insert ( root , keys [ i ] ) ; char [ ] str = new char [ 100 ] ; display ( root , str , 0 ) ; } }
Find the color of given node in an infinite binary tree | JAVA program to find color of the node ; Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
import java . util . * ; class GFG { static int findColor ( HashMap < Integer , Integer > mapWithColor , int query ) { int maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . containsKey ( query ) ) { maximum = Math . max ( maximum , mapWithColor . get ( query ) ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } static HashMap < Integer , Integer > buildMapWithColor ( int arr [ ] , int n ) { HashMap < Integer , Integer > mapWithColor = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mapWithColor . put ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 3 } ; int n = arr . length ; int k = 7 ; HashMap < Integer , Integer > mapWithColor = buildMapWithColor ( arr , n ) ; System . out . print ( findColor ( mapWithColor , k ) ) ; } }
Count of strings in the first array which are smaller than every string in the second array | Java implementation of the approach ; Function to count the number of smaller strings in A [ ] for every String in B [ ] ; Count the frequency of all characters ; Iterate for all possible strings in A [ ] ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every String in B [ ] ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A [ ] which has the frequency of the smaller character less than the frequency of the smaller character of the String in B [ ] ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
import java . util . * ; class GFG { static int MAX = 26 ; static Vector < Integer > findCount ( String a [ ] , String b [ ] , int n , int m ) { int [ ] freq = new int [ MAX ] ; Vector < Integer > smallestFreq = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; Arrays . fill ( freq , 0 ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { freq [ s . charAt ( j ) - ' a ' ] ++ ; } for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { smallestFreq . add ( freq [ j ] ) ; break ; } } } Collections . sort ( smallestFreq ) ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { String s = b [ i ] ; Arrays . fill ( freq , 0 ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { freq [ s . charAt ( j ) - ' a ' ] ++ ; } int frequency = 0 ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { frequency = freq [ j ] ; break ; } } int [ ] array = new int [ smallestFreq . size ( ) ] ; int k = 0 ; for ( Integer val : smallestFreq ) { array [ k ] = val ; k ++ ; } int ind = lower_bound ( array , 0 , smallestFreq . size ( ) , frequency ) ; ans . add ( ind ) ; } return ans ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static void printAnswer ( String a [ ] , String b [ ] , int n , int m ) { Vector < Integer > ans = findCount ( a , b , n , m ) ; for ( Integer it : ans ) { System . out . print ( it + " ▁ " ) ; } } public static void main ( String [ ] args ) { String A [ ] = { " aaa " , " aa " , " bdc " } ; String B [ ] = { " cccch " , " cccd " } ; int n = A . length ; int m = B . length ; printAnswer ( A , B , n , m ) ; } }
Find Leftmost and Rightmost node of BST from its given preorder traversal | Java program to find leftmost and rightmost node from given preorder sequence ; Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
class GFG { static void LeftRightNode ( int preorder [ ] , int n ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } System . out . println ( " Leftmost ▁ node ▁ is ▁ " + min ) ; System . out . println ( " Rightmost ▁ node ▁ is ▁ " + max ) ; } public static void main ( String [ ] args ) { int preorder [ ] = { 3 , 2 , 1 , 5 , 4 } ; int n = 5 ; LeftRightNode ( preorder , n ) ; } }
Find the position of the given row in a 2 | Java implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code
class GFG { static int m = 6 , n = 4 ; static int compareRow ( int a1 [ ] , int a2 [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return - 1 ; } return 0 ; } static int binaryCheck ( int ar [ ] [ ] , int arr [ ] ) { int l = 0 , r = m - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int temp = compareRow ( ar [ mid ] , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ ] = { 10 , 9 , 22 , 23 } ; System . out . println ( binaryCheck ( mat , row ) ) ; } }
Number of subarrays having absolute sum greater than K | Set | Java implementation of the approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value
import java . util . * ; class GFG { static int maxLen = 30 ; static int findCnt ( int arr [ ] , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < - 1 * k ) ans ++ ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ; return ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 4 , - 5 , 6 } ; int n = arr . length ; int k = 0 ; System . out . println ( findCnt ( arr , n , k ) ) ; } }
Number of intersections between two ranges | Java implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int upperBound ( ArrayList < Integer > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a . get ( middle ) > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } static int FindIntersection ( ArrayList < ArrayList < Integer > > type1 , int n , ArrayList < ArrayList < Integer > > type2 , int m ) { int ans = n * m ; ArrayList < Integer > start = new ArrayList < Integer > ( ) ; ArrayList < Integer > end = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { start . add ( type1 . get ( i ) . get ( 0 ) ) ; end . add ( type1 . get ( i ) . get ( 1 ) ) ; } Collections . sort ( start ) ; Collections . sort ( end ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 . get ( i ) . get ( 0 ) ; int R = type2 . get ( i ) . get ( 1 ) ; ans -= start . size ( ) - upperBound ( start , 0 , start . size ( ) , R ) ; ans -= upperBound ( end , 0 , end . size ( ) , L - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > type1 = new ArrayList < ArrayList < Integer > > ( ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 5 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 7 ) ) ) ; ArrayList < ArrayList < Integer > > type2 = new ArrayList < ArrayList < Integer > > ( ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 7 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 5 , 7 ) ) ) ; int n = type1 . size ( ) ; int m = type2 . size ( ) ; System . out . println ( FindIntersection ( type1 , n , type2 , m ) ) ; } }
Find the Nth term divisible by a or b or c | Java implementation of the approach ; Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int divTermCount ( int a , int b , int c , int num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / lcm ( a , b ) ) - ( num / lcm ( b , c ) ) - ( num / lcm ( a , c ) ) + ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } static int findNthTerm ( int a , int b , int c , int n ) { int low = 1 , high = Integer . MAX_VALUE , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int a = 2 , b = 3 , c = 5 , n = 10 ; System . out . println ( findNthTerm ( a , b , c , n ) ) ; } }