codeparrot/xlcost-text-to-code huggingface dataset:
Minimize count of array elements to be removed to maximize difference between any pair up to K | Java Program to implement the above approach ; Function to count the number of elements to be removed from the array based on the given condition ; Sort the array ; / Initialize the variable ; Iterate for all possible pairs ; Check the difference between the numbers ; Update the minimum removals ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int min_remove ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] - arr [ i ] <= k ) { ans = Math . min ( ans , n - j + i - 1 ) ; } } } return ans ; } public static void main ( String [ ] args ) { int k = 3 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . print ( min_remove ( arr , n , k ) ) ; } }
Reduce a given Binary Array to a single element by removal of Triplets | Java program to implement the above approach ; Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code
class GFG { static void solve ( int arr [ ] , int n ) { int countzeroes = 0 ; int countones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . abs ( countzeroes - countones ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }
Nearest smaller character to a character K from a Sorted Array | Java program to implement the above approach ; Function to return the nearest smaller character ; Stores the nearest smaller character ; Iterate till starts cross end ; Find the mid element ; Check if K is found ; Check if current character is less than K ; Increment the start ; Otherwise ; Increment end ; Return the character ; Driver Code
import java . util . * ; class GFG { static char bs ( char ar [ ] , int n , int ele ) { int start = 0 ; int end = n - 1 ; char ch = ' @ ' ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( ar [ mid ] == ele ) end = mid - 1 ; else if ( ar [ mid ] < ele ) { ch = ar [ mid ] ; start = mid + 1 ; } else end = mid - 1 ; } return ch ; } public static void main ( String [ ] args ) { char ar [ ] = { ' e ' , ' g ' , ' t ' , ' y ' } ; int n = ar . length ; char K = ' u ' ; char ch = bs ( ar , n , K ) ; if ( ch == ' @ ' ) System . out . print ( " - 1" ) ; else System . out . print ( ch ) ; } }
Largest possible Subset from an Array such that no element is K times any other element in the Subset | Java implementation of the above approach ; Function to find the maximum size of the required subset ; Size of the array ; Sort the array ; Stores which index is included or excluded ; Stores the indices of array elements ; Count of pairs ; Iterate through all the element ; If element is included ; Check if a [ i ] * k is present in the array or not ; Increase count of pair ; Exclude the pair ; Driver code
import java . util . * ; class GFG { static int findMaxLen ( int [ ] a , int k ) { int n = a . length ; Arrays . sort ( a ) ; boolean [ ] vis = new boolean [ n ] ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , i ) ; } int c = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == false ) { int check = a [ i ] * k ; if ( mp . containsKey ( check ) ) { c ++ ; vis [ mp . get ( check ) ] = true ; } } } return n - c ; } public static void main ( String [ ] args ) { int K = 3 ; int [ ] arr = { 1 , 4 , 3 , 2 } ; System . out . print ( findMaxLen ( arr , K ) ) ; } }
Minimize length of prefix of string S containing all characters of another string T | Java program for the above approach ; Base Case - if T is empty , it matches 0 length prefix ; Convert strings to lower case for uniformity ; Update dictCount to the letter count of T ; If new character is found , initialize its entry , and increase nUnique ; Increase count of ch ; Iterate from 0 to N ; i - th character ; Skip if ch not in targetStr ; Decrease Count ; If the count of ch reaches 0 , we do not need more ch , and can decrease nUnique ; If nUnique reaches 0 , we have found required prefix ; Otherwise ; Driver code
import java . util . * ; public class Main { public static int getPrefixLength ( String srcStr , String targetStr ) { if ( targetStr . length ( ) == 0 ) return 0 ; srcStr = srcStr . toLowerCase ( ) ; targetStr = targetStr . toLowerCase ( ) ; HashMap < Character , Integer > dictCount = new HashMap < > ( ) ; int nUnique = 0 ; for ( char ch : targetStr . toCharArray ( ) ) { if ( dictCount . containsKey ( ch ) != true ) { nUnique += 1 ; dictCount . put ( ch , 0 ) ; } dictCount . replace ( ch , dictCount . get ( ch ) + 1 ) ; } for ( int i = 0 ; i < srcStr . length ( ) ; i ++ ) { char ch = srcStr . charAt ( i ) ; if ( dictCount . containsKey ( ch ) != true ) continue ; dictCount . replace ( ch , dictCount . get ( ch ) - 1 ) ; if ( dictCount . get ( ch ) == 0 ) nUnique -= 1 ; if ( nUnique == 0 ) return ( i + 1 ) ; } return - 1 ; } public static void main ( String [ ] args ) { String S = " MarvoloGaunt " ; String T = " Tom " ; System . out . println ( getPrefixLength ( S , T ) ) ; } }
Minimize length of Substrings containing at least one common Character | Java program to implement the above approach ; Function to check and return if subStrings of length mid has a common character a ; Length of the String ; Initialise the first occurrence of character a ; Check that distance b / w the current and previous occurrence of character a is less than or equal to mid ; If distance exceeds mid ; Function to check for all the alphabets , if subStrings of length mid have a character common ; Check for all 26 alphabets ; Check that char i + a is common in all the subStrings of length mid ; If no characters is common ; Function to calculate and return the minm length of subStrings ; Initialise low and high ; Perform binary search ; Update mid ; Check if one common character is present in the length of the mid ; Returns the minimum length that contain one common character ; Function to check if all characters are distinct ; Driver Code
import java . util . * ; class GFG { static boolean check ( char [ ] str , int mid , char a ) { int n = str . length ; int previous = - 1 , i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] == a ) { if ( i - previous > mid ) { return false ; } previous = i ; } } if ( i - previous > mid ) return false ; else return true ; } static boolean possible ( char [ ] str , int mid ) { for ( int i = 0 ; i < 26 ; ++ i ) { if ( check ( str , mid , ( char ) ( i + ' a ' ) ) ) return true ; } return false ; } static int findMinLength ( char [ ] str ) { int low = 1 , high = str . length ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( possible ( str , mid ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } static boolean ifAllDistinct ( String str ) { HashSet < Character > s = new HashSet < Character > ( ) ; for ( char c : str . toCharArray ( ) ) { s . add ( c ) ; } return s . size ( ) == str . length ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( ifAllDistinct ( str ) ) System . out . print ( - 1 + "NEW_LINE"); else System . out . print ( findMinLength ( str . toCharArray ( ) ) ) ; } }
Maximize product of absolute index difference with K | Java program to implement the above approach ; Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver Code
class GFG { static int solve ( int A [ ] , int N ) { int i = 0 ; int k = 0 ; for ( int j = 1 ; j < N ; j ++ ) { int tempK = Math . min ( A [ i ] , A [ j ] ) / ( j - i ) ; if ( tempK > k ) { k = tempK ; } if ( A [ j ] >= A [ i ] / ( j - i ) ) i = j ; } return k ; } public static void main ( String [ ] args ) { int A [ ] = { 10 , 5 , 12 , 15 , 8 } ; int N = A . length ; System . out . println ( solve ( A , N ) ) ; } }
Split Array into min number of subsets with difference between each pair greater than 1 | Java program for the above approach ; Function to split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Print the count of partitions ; Driver Code ; Given array ; Size of the array ; Function call
import java . util . * ; class GFG { static void split ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; split ( arr , n ) ; } }
Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | Java program to implement the above approach ; Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code
import java . util . Arrays ; class GFG { public static boolean check ( String str1 , String str2 ) { boolean [ ] mark = new boolean [ 26 ] ; Arrays . fill ( mark , false ) ; int n = str1 . length ( ) , m = str2 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ str1 . charAt ( i ) - ' a ' ] = true ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mark [ str2 . charAt ( i ) - ' a ' ] ) return true ; } return false ; } public static void main ( String [ ] args ) { String str1 = " abca " , str2 = " efad " ; if ( check ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Generate K co | Java implementation of the above approach ; Function prints the required pairs ; First co - prime pair ; As a pair ( 1 n ) has already been Printed ; If i is a factor of N ; Since ( i , i ) won 't form  a coprime pair ; Driver Code
import java . util . * ; class GFG { static void FindPairs ( int n , int k ) { System . out . print ( 1 + " ▁ " + n + "NEW_LINE"); k -- ; for ( long i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { System . out . print ( 1 + " ▁ " + i + "NEW_LINE"); k -- ; if ( k == 0 ) break ; if ( i != n / i ) { System . out . print ( 1 + " ▁ " + n / i + "NEW_LINE"); k -- ; } if ( k == 0 ) break ; } } } public static void main ( String [ ] args ) { int N = 100 ; int K = 5 ; FindPairs ( N , K ) ; } }
Minimum edges required to make a Directed Graph Strongly Connected | Java program to implement the above approach ; Perform DFS to count the in - degree and out - degree of the graph ; Mark the source as visited ; Traversing adjacent nodes ; Mark out - degree as 1 ; Mark in - degree as 1 ; If not visited ; DFS Traversal on adjacent vertex ; Function to return minimum number of edges required to make the graph strongly connected ; For Adjacency List ; Create the Adjacency List ; Initialize the in - degree array ; Initialize the out - degree array ; Initialize the visited array ; Perform DFS to count in - degrees and out - degreess ; To store the result ; To store total count of in - degree and out - degree ; Find total in - degree and out - degree ; Calculate the minimum edges required ; Return the minimum edges ; Driver Code ; Function call
import java . util . * ; class GFG { static void dfs ( int u , Vector < Integer > adj [ ] , int [ ] vis , int [ ] inDeg , int [ ] outDeg ) { vis [ u ] = 1 ; for ( int v : adj [ u ] ) { outDeg [ u ] = 1 ; inDeg [ v ] = 1 ; if ( vis [ v ] == 0 ) { dfs ( v , adj , vis , inDeg , outDeg ) ; } } } static int findMinimumEdges ( int source [ ] , int N , int M , int dest [ ] ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] adj = new Vector [ N + 1 ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { adj ] . add ( dest [ i ] ) ; } int inDeg [ ] = new int [ N + 1 ] ; int outDeg [ ] = new int [ N + 1 ] ; int vis [ ] = new int [ N + 1 ] ; dfs ( 1 , adj , vis , inDeg , outDeg ) ; int minEdges = 0 ; int totalIndegree = 0 ; int totalOutdegree = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( inDeg [ i ] == 1 ) totalIndegree ++ ; if ( outDeg [ i ] == 1 ) totalOutdegree ++ ; } minEdges = Math . max ( N - totalIndegree , N - totalOutdegree ) ; return minEdges ; } public static void main ( String [ ] args ) { int N = 5 , M = 5 ; int source [ ] = { 1 , 3 , 1 , 3 , 4 } ; int destination [ ] = { 2 , 2 , 3 , 4 , 5 } ; System . out . print ( findMinimumEdges ( source , N , M , destination ) ) ; } }
Count of subarrays which forms a permutation from given Array elements | Java program to implement the above approach ; Function returns the required count ; Store the indices of the elements present in A [ ] . ; Store the maximum and minimum index of the elements from 1 to i . ; Update maxi and mini , to store minimum and maximum index for permutation of elements from 1 to i + 1 ; If difference between maxi and mini is equal to i ; Increase count ; Return final count ; Driver Code
class GFG { static int PermuteTheArray ( int A [ ] , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ A [ i ] - 1 ] = i ; } int mini = n , maxi = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mini = Math . min ( mini , arr [ i ] ) ; maxi = Math . max ( maxi , arr [ i ] ) ; if ( maxi - mini == i ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 5 , 1 , 3 , 2 , 6 } ; System . out . print ( PermuteTheArray ( A , 6 ) ) ; } }
Find Second largest element in an array | Set 2 | Java program to implement the above approach ; Function to find the largest element in the array arr [ ] ; Base Condition ; Initialize an empty list ; Divide the array into two equal length subarrays and recursively find the largest among the two ; Store length of compared1 [ ] in the first index ; Store the maximum element ; Return compared1 which contains the maximum element ; Store length of compared2 [ ] in the first index ; Store the maximum element ; Return compared2 [ ] which contains the maximum element ; Function to print the second largest element in the array arr [ ] ; Find the largest element in arr [ ] ; Find the second largest element in arr [ ] ; Print the second largest element ; Driver code
import java . util . * ; class GFG { static int [ ] findLargest ( int beg , int end , int [ ] arr , int n ) { if ( beg == end ) { int [ ] compared = new int [ n ] ; compared [ 0 ] = 1 ; compared [ 1 ] = arr [ beg ] ; return compared ; } int [ ] compared1 = findLargest ( beg , ( beg + end ) / 2 , arr , n ) ; int [ ] compared2 = findLargest ( ( beg + end ) / 2 + 1 , end , arr , n ) ; if ( compared1 [ 1 ] > compared2 [ 1 ] ) { int k = compared1 [ 0 ] + 1 ; compared1 [ 0 ] = k ; compared1 [ k ] = compared2 [ 1 ] ; return compared1 ; } else { int k = compared2 [ 0 ] + 1 ; compared2 [ 0 ] = k ; compared2 [ k ] = compared1 [ 1 ] ; return compared2 ; } } static void findSecondLargest ( int end , int [ ] arr ) { int [ ] compared1 = findLargest ( 0 , end - 1 , arr , end ) ; int [ ] compared2 = findLargest ( 2 , compared1 [ 0 ] + 2 , compared1 , compared1 [ 0 ] ) ; System . out . print ( compared2 [ 1 ] ) ; } public static void main ( String [ ] args ) { int N = 10 ; int [ ] arr = { 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 } ; findSecondLargest ( N , arr ) ; } }
Count of Reverse Bitonic Substrings in a given String | Java program to implement the above approach ; Function to calculate the number of reverse bitonic substrings ; Stores the count ; All possible lengths of substrings ; Starting point of a substring ; Ending point of a substring ; Condition for reverse bitonic substrings of length 1 ; Check for decreasing sequence ; If end of substring is reached ; For increasing sequence ; If end of substring is reached ; Return the number of bitonic substrings ; Driver code
class GFG { public static int CountsubString ( char [ ] str , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > str [ k ] && k <= j ) { temp = str [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < str [ k ] && f != 2 ) { temp = str [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } return c ; } public static void main ( String [ ] args ) { char str [ ] = { ' b ' , ' a ' , ' d ' , ' e ' } ; System . out . println ( CountsubString ( str , str . length ) ) ; } }
Maximum subsequence sum from a given array which is a perfect square | Java program to implement the above approach ; If sum is 0 , then answer is true ; If sum is not 0 and arr [ ] is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to find the sum ; Find sum of all values ; return the value ; ; Driver code
import java . util . * ; class GFG { static boolean isSubsetSum ( int arr [ ] , int n , int sum ) { boolean [ ] [ ] subset = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - arr [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } static int findSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int val = ( int ) Math . sqrt ( sum ) ; for ( int i = val ; i >= 0 ; i -- ) { if ( isSubsetSum ( arr , n , i * i ) ) { return i * i ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
Smallest subarray whose product leaves remainder K when divided by size of the array | Java program to implement the above approach ; Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array
import java . util . * ; class GFG { static int findsubArray ( int arr [ ] , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 } ; int N = arr . length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) System . out . println ( answer ) ; else System . out . println ( " - 1" ) ; } }
Count of array elements whose order of deletion precedes order of insertion | Java program to implement the above approach ; Function returns maximum number of required elements ; Insert the elements of array B in the queue and set ; Stores the answer ; If A [ i ] is already processed ; Until we find A [ i ] in the queue ; Remove elements from the queue ; Increment the count ; Remove the current element A [ i ] from the queue and set . ; Return total count ; Driver Code
import java . util . * ; class GFG { static void maximumCount ( int A [ ] , int B [ ] , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( B [ i ] ) ; q . add ( B [ i ] ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . contains ( A [ i ] ) ) continue ; while ( ! q . isEmpty ( ) && q . peek ( ) != A [ i ] ) { s . remove ( q . peek ( ) ) ; q . remove ( ) ; count ++ ; } if ( A [ i ] == q . peek ( ) ) { q . remove ( ) ; s . remove ( A [ i ] ) ; } if ( q . isEmpty ( ) ) break ; } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 1 , 2 , 3 , 4 } ; int B [ ] = { 1 , 2 , 4 , 3 } ; maximumCount ( A , B , N ) ; } }
Check if a Matrix is Bitonic or not | Java program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
class GFG { final static int N = 3 , M = 3 ; static boolean checkBitonic ( int arr [ ] , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int arr [ ] [ ] ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkBitonic ( arr [ i ] , M ) ) { System . out . println ( " NO " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int temp [ ] = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j ] [ i ] ; } if ( ! checkBitonic ( temp , N ) ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 2 , 3 } , { 3 , 4 , 5 } , { 2 , 6 , 4 } } ; check ( m ) ; } }
Maximum possible GCD for a pair of integers with product N | Java program to implement the above approach ; Function to return the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getMaxGcd ( int N ) { int maxGcd = Integer . MIN_VALUE , A , B ; for ( int i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i ; B = N / i ; maxGcd = Math . max ( maxGcd , gcd ( A , B ) ) ; } } return maxGcd ; } public static void main ( String s [ ] ) { int N = 18 ; System . out . println ( getMaxGcd ( N ) ) ; } }