codeparrot/xlcost-text-to-code huggingface dataset:
Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2 | Java program for the above approach ; Function to check if cell with value 1 doesn 't reaches the bottom right cell or not ; Number of rows and columns ; Initialise the deque ; Traverse the matrix ; Push 1 to front of queue ; Push 2 to back of queue ; Store all the possible direction of the current cell ; Run multi - source BFS ; Get the front element ; Pop the front element ; If 1 reached corner first ; Insert new point in queue ; If 1 can 't reach the bottom  right then return false ; Driver Code ; Given matrix ; Function call
import java . util . * ; import java . lang . * ; class GFG { static boolean reachesBottom ( int [ ] [ ] a ) { int n = a . length ; int m = a [ 0 ] . length ; Deque < int [ ] > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == 1 ) { q . addFirst ( new int [ ] { i , j , 1 } ) ; } else if ( a [ i ] [ j ] == 2 ) { q . addLast ( new int [ ] { i , j , 2 } ) ; } a [ i ] [ j ] = 0 ; } } int dx [ ] = { - 1 , 0 , 1 , 0 } ; int dy [ ] = { 0 , 1 , 0 , - 1 } ; while ( ! q . isEmpty ( ) ) { int [ ] front = q . peekFirst ( ) ; q . removeFirst ( ) ; int i = front [ 0 ] , j = front [ 1 ] ; int t = front [ 2 ] ; if ( a [ i ] [ j ] == 1 ) continue ; a [ i ] [ j ] = 1 ; if ( t == 1 && ( i == n - 1 && j == m - 1 ) ) { return true ; } for ( int d = 0 ; d < 4 ; d ++ ) { int ni = i + dx [ d ] ; int nj = j + dy [ d ] ; if ( ni >= 0 && ni < n && nj >= 0 && nj < m ) { q . addLast ( new int [ ] { ni , nj , t } ) ; } } } return false ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , 2 , 0 } , { 0 , 1 , 0 } , { 0 , 2 , 0 } } ; if ( reachesBottom ( matrix ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }
Smallest element from all square submatrices of size K from a given Matrix | Java Program for the above approach ; Function to returns a smallest elements of all KxK submatrices of a given NxM matrix ; Stores the dimensions of the given matrix ; Stores the required smallest elements ; Update the smallest elements row - wise ; Update the minimum column - wise ; Store the final submatrix with required minimum values ; Return the resultant matrix ; Function Call ; Print resultant matrix with the minimum values of KxK sub - matrix ; Driver Code ; Given matrix ; Given K
import java . util . * ; import java . lang . * ; class GFG { public static int [ ] [ ] matrixMinimum ( int [ ] [ ] nums , int K ) { int N = nums . length ; int M = nums [ 0 ] . length ; int [ ] [ ] res = new int [ N - K + 1 ] [ M - K + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M - K + 1 ; j ++ ) { int min = Integer . MAX_VALUE ; for ( int k = j ; k < j + K ; k ++ ) { min = Math . min ( min , nums [ i ] [ k ] ) ; } nums [ i ] [ j ] = min ; } } for ( int j = 0 ; j < M ; j ++ ) { for ( int i = 0 ; i < N - K + 1 ; i ++ ) { int min = Integer . MAX_VALUE ; for ( int k = i ; k < i + K ; k ++ ) { min = Math . min ( min , nums [ k ] [ j ] ) ; } nums [ i ] [ j ] = min ; } } for ( int i = 0 ; i < N - K + 1 ; i ++ ) for ( int j = 0 ; j < M - K + 1 ; j ++ ) res [ i ] [ j ] = nums [ i ] [ j ] ; return res ; } public static void smallestinKsubmatrices ( int arr [ ] [ ] , int K ) { int [ ] [ ] res = matrixMinimum ( arr , K ) ; for ( int i = 0 ; i < res . length ; i ++ ) { for ( int j = 0 ; j < res [ 0 ] . length ; j ++ ) { System . out . print ( res [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { - 1 , 5 , 4 , 1 , - 3 } , { 4 , 3 , 1 , 1 , 6 } , { 2 , - 2 , 5 , 3 , 1 } , { 8 , 5 , 1 , 9 , - 4 } , { 12 , 3 , 5 , 8 , 1 } } ; int K = 3 ; smallestinKsubmatrices ( arr , K ) ; } }
Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Java Program to implement the above approach include " bits / stdJava . h " ; Function to find the kth greatest character from the strijng ; Sorting the String in non - increasing Order ; Function to print the K - th character from the subString S [ l ] . . S [ r ] ; 0 - based indexing ; SubString of str from the indices l to r . ; Extract kth Largest character ; Function to replace character at pos of str by the character s ; Index of S to be updated . ; Character to be replaced at index in S ; Driver Code ; Given String ; Count of queries ; Queries
import java . util . * ; class GFG { static char find_kth_largest ( char [ ] str , int k ) { Arrays . sort ( str ) ; reverse ( str ) ; return str [ k - 1 ] ; } static char [ ] reverse ( char a [ ] ) { int i , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static char printCharacter ( String str , int l , int r , int k ) { l = l - 1 ; r = r - 1 ; String temp = str . substring ( l , r - l + 1 ) ; char ans = find_kth_largest ( temp . toCharArray ( ) , k ) ; return ans ; } static void updateString ( char [ ] str , int pos , char s ) { int index = pos - 1 ; char c = s ; str [ index ] = c ; } public static void main ( String [ ] args ) { String str = " abcddef " ; int Q = 3 ; System . out . print ( printCharacter ( str , 1 , 2 , 2 ) + "NEW_LINE"); updateString ( str . toCharArray ( ) , 4 , ' g ' ) ; System . out . print ( printCharacter ( str , 1 , 5 , 4 ) + "NEW_LINE"); } }
Split array into two subarrays such that difference of their sum is minimum | Java Program for above approach ; Function to return minimum difference between sum of two subarrays ; To store total sum of array ; Calculate the total sum of the array ; Stores the prefix sum ; Stores the minimum difference ; Traverse the given array ; To store minimum difference ; Update minDiff ; Return minDiff ; Driver Code ; Given array ; Length of the array
import java . util . * ; import java . lang . * ; class GFG { static int minDiffSubArray ( int arr [ ] , int n ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) total_sum += arr [ i ] ; int prefix_sum = 0 ; int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix_sum += arr [ i ] ; int diff = Math . abs ( ( total_sum - prefix_sum ) - prefix_sum ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int n = arr . length ; System . out . println ( minDiffSubArray ( arr , n ) ) ; } }
Maximize count of non | Java Program to implement the above approach ; Function to count the maximum number of subarrays with sum K ; Stores all the distinct prefixSums obtained ; Stores the prefix sum of the current subarray ; Stores the count of subarrays with sum K ; If a subarray with sum K is already found ; Increase count ; Reset prefix sum ; Clear the set ; Insert the prefix sum ; Driver Code
import java . util . * ; class GFG { static int CtSubarr ( int [ ] arr , int N , int K ) { Set < Integer > st = new HashSet < Integer > ( ) ; int prefixSum = 0 ; st . add ( prefixSum ) ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefixSum += arr [ i ] ; if ( st . contains ( prefixSum - K ) ) { res += 1 ; prefixSum = 0 ; st . clear ( ) ; st . add ( 0 ) ; } st . add ( prefixSum ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 } ; int N = arr . length ; int K = 10 ; System . out . println ( CtSubarr ( arr , N , K ) ) ; } }
Count of subarrays consisting of only prime numbers | Java Program to implement the above approach ; Function to check if a number is prime or not . ; If n has any factor other than 1 , then n is non - prime . ; Function to return the count of subarrays made up of prime numbers only ; Stores the answer ; Stores the count of continuous prime numbers in an array ; If the current array element is prime ; Increase the count ; Update count of subarrays ; If the array ended with a continuous prime sequence ; Driver Code
import java . util . * ; class GFG { static boolean is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static int count_prime_subarrays ( int ar [ ] , int n ) { int ans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( is_prime ( ar [ i ] ) ) count ++ ; else { if ( count != 0 ) { ans += count * ( count + 1 ) / 2 ; count = 0 ; } } } if ( count != 0 ) ans += count * ( count + 1 ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int N = 10 ; int [ ] ar = { 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 } ; System . out . print ( count_prime_subarrays ( ar , N ) ) ; } }
Minimum Subarray flips required to convert all elements of a Binary Array to K | Java program to implement the above approach ; Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver code
import java . util . * ; class GFG { static int minSteps ( int arr [ ] , int n , int k ) { int i , cnt = 0 ; int flag ; if ( k == 1 ) flag = 0 ; else flag = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == flag ) { cnt ++ ; flag = ( flag + 1 ) % 2 ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; int k = 1 ; System . out . print ( minSteps ( arr , n , k ) ) ; } }
Minimize Sum of an Array by at most K reductions | Java Program to implement the above approach ; Function to obtain the minimum possible sum from the array by K reductions ; Implements the MaxHeap ; Insert elements into the MaxHeap ; Remove the maximum ; Insert maximum / 2 ; Stores the sum of remaining elements ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int minSum ( int a [ ] , int n , int k ) { PriorityQueue < Integer > maxheap = new PriorityQueue < > ( ( one , two ) -> two - one ) ; for ( int i = 0 ; i < n ; i ++ ) maxheap . add ( a [ i ] ) ; while ( maxheap . size ( ) > 0 && k > 0 ) { int max_ele = maxheap . poll ( ) ; maxheap . add ( max_ele / 2 ) ; k -= 1 ; } int sum = 0 ; while ( maxheap . size ( ) > 0 ) sum += maxheap . poll ( ) ; return sum ; } public static void main ( String [ ] args ) { int n = 4 ; int k = 3 ; int a [ ] = { 20 , 7 , 5 , 4 } ; System . out . println ( minSum ( a , n , k ) ) ; } }
Sum of indices of Characters removed to obtain an Empty String based on given conditions | Java program to implement the above approach ; Function to add index of the deleted character ; If index is beyond the range ; Insert the index of the deleted characeter ; Search over the subtrees to find the desired index ; Function to return count of deleted indices which are to the left of the current index ; Function to generate the sum of indices ; Stores the original index of the characters in sorted order of key ; Traverse the map ; Extract smallest index of smallest character ; Delete the character from the map if it has no remaining occurrence ; Stores the original index ; Count of elements removed to the left of current character ; Current index of the current character ; For 1 - based indexing ; Insert the deleted index in the segment tree ; Final answer ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void add_seg ( int seg [ ] , int start , int end , int current , int index ) { if ( index > end index < start ) return ; if ( start == end ) { seg [ current ] = 1 ; return ; } int mid = ( start + end ) / 2 ; add_seg ( seg , start , mid , 2 * current + 1 , index ) ; add_seg ( seg , mid + 1 , end , 2 * current + 2 , index ) ; seg [ current ] = seg [ 2 * current + 1 ] + seg [ 2 * current + 2 ] ; } static int deleted ( int seg [ ] , int l , int r , int start , int end , int current ) { if ( end < l start > r ) return 0 ; if ( start >= l && end <= r ) return seg [ current ] ; int mid = ( start + end ) / 2 ; return deleted ( seg , l , r , start , mid , 2 * current + 1 ) + deleted ( seg , l , r , mid + 1 , end , 2 * current + 2 ) ; } static void sumOfIndices ( String s ) { int N = s . length ( ) ; int x = ( int ) ( Math . ceil ( Math . log ( N ) / Math . log ( 2 ) ) ) ; int seg_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int segment [ ] = new int [ seg_size ] ; int count = 0 ; TreeMap < Integer , ArrayDeque < Integer > > fre = new TreeMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int key = ( int ) ( s . charAt ( i ) ) ; ArrayDeque < Integer > que = fre . getOrDefault ( key , new ArrayDeque < > ( ) ) ; que . addLast ( i ) ; fre . put ( key , que ) ; } while ( ! fre . isEmpty ( ) ) { int it = fre . firstKey ( ) ; if ( fre . get ( it ) . size ( ) == 0 ) fre . remove ( it ) ; else { ArrayDeque < Integer > que = fre . get ( it ) ; int original_index = que . getFirst ( ) ; int curr_index = deleted ( segment , 0 , original_index - 1 , 0 , N - 1 , 0 ) ; int new_index = original_index - curr_index ; count += new_index + 1 ; add_seg ( segment , 0 , N - 1 , 0 , original_index ) ; que . removeFirst ( ) ; fre . put ( it , que ) ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; sumOfIndices ( s ) ; } }
Maximum Length of Sequence of Sums of prime factors generated by the given operations | Java Program to implement the above approach ; Smallest prime factor array ; Stores if a number is prime or not ; Function to compute all primes using Sieve of Eratosthenes ; Function for finding smallest prime factors for every integer ; Function to find the sum of prime factors of number ; Add smallest prime factor to the sum ; Reduce N ; Return the answer ; Function to return the length of sequence of for the given number ; If the number is prime ; If a previously computed subproblem occurred ; Calculate the sum of prime factors ; Function to return the maximum length of sequence for the given range ; Pre - calculate primes ; Precalculate smallest prime factors ; Iterate over the range ; Update maximum length ; Driver Code
import java . util . * ; class GFG { static int spf [ ] = new int [ 100005 ] ; static boolean prime [ ] = new boolean [ 100005 ] ; static int dp [ ] = new int [ 100005 ] ; static void sieve ( ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i < 100005 ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i < 100005 ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < 100005 ; j += i ) { prime [ j ] = false ; } } } } static void smallestPrimeFactors ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( int i = 2 ; i * i < 100005 ; i ++ ) { for ( int j = i ; j < 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } static int sumOfPrimeFactors ( int n ) { int ans = 0 ; while ( n > 1 ) { ans += spf [ n ] ; n /= spf [ n ] ; } return ans ; } static int findLength ( int n ) { if ( prime [ n ] ) { return 1 ; } if ( dp [ n ] != 0 ) { return dp [ n ] ; } int sum = sumOfPrimeFactors ( n ) ; return dp [ n ] = 1 + findLength ( sum ) ; } static int maxLength ( int n , int m ) { sieve ( ) ; smallestPrimeFactors ( ) ; int ans = Integer . MIN_VALUE ; for ( int i = n ; i <= m ; i ++ ) { if ( i == 4 ) { continue ; } ans = Math . max ( ans , findLength ( i ) ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 2 , m = 14 ; System . out . print ( maxLength ( n , m ) ) ; } }
Longest Subarray consisting of unique elements from an Array | Java program to implement the above approach ; Function to find largest subarray with no duplicates ; Stores index of array elements ; Update j based on previous occurrence of a [ i ] ; Update ans to store maximum length of subarray ; Store the index of current occurrence of a [ i ] ; Return final ans ; Driver Code
import java . util . * ; class GFG { static int largest_subarray ( int a [ ] , int n ) { HashMap < Integer , Integer > index = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 , j = 0 ; i < n ; i ++ ) { j = Math . max ( index . containsKey ( a [ i ] ) ? index . get ( a [ i ] ) : 0 , j ) ; ans = Math . max ( ans , i - j + 1 ) ; index . put ( a [ i ] , i + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( largest_subarray ( arr , n ) ) ; } }
Count of Ways to obtain given Sum from the given Array elements | Java Program to implement the above approach ; Function to call dfs ( ) to calculate the number of ways ; Function to count the number of ways ; Base Case : Reached the end of the array ; Sum is equal to the required sum ; Recursively check if required sum can be obtained by adding current element or by subtracting the current index element ; Driver Code
import java . io . * ; class GFG { static int findWays ( int [ ] nums , int S ) { return dfs ( nums , S , 0 , 0 ) ; } static int dfs ( int [ ] nums , int S , int curr_sum , int index ) { if ( index == nums . length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } return dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ; } public static void main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = findWays ( arr , S ) ; System . out . println ( answer ) ; } }
Check if all the Nodes in a Binary Tree having common values are at least D distance apart | Java program to implement the above approach ; Structure of a Tree node ; Function to create a new node ; Function to count the frequency of node value present in the tree ; Function that returns the max distance between the nodes that have the same key ; If right and left subtree did not have node whose key is value ; Check if the current node is equal to value ; If the left subtree has no node whose key is equal to value ; If the right subtree has no node whose key is equal to value ; Function that finds if the distance between any same nodes is at most K ; Create the map to look for same value of nodes ; Counting the frequency of nodes ; If the returned value of distance is exceeds dist ; Print the result ; Driver Code
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static void frequencyCounts ( HashMap < Integer , Integer > map , Node root ) { if ( root == null ) return ; if ( map . containsKey ( root . key ) ) map . put ( root . key , map . get ( root . key ) + 1 ) ; else map . put ( root . key , 1 ) ; frequencyCounts ( map , root . left ) ; frequencyCounts ( map , root . right ) ; } static int computeDistance ( Node root , int value ) { if ( root == null ) { return - 1 ; } int left = computeDistance ( root . left , value ) ; int right = computeDistance ( root . right , value ) ; if ( left == - 1 && right == - 1 ) { if ( root . key == value ) { return 1 ; } else return - 1 ; } if ( left == - 1 ) { return right + 1 ; } if ( right == - 1 ) { return left + 1 ; } else { return 1 + Math . max ( left , right ) ; } } static void solve ( Node root , int dist ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; frequencyCounts ( map , root ) ; int flag = 0 ; for ( Map . Entry < Integer , Integer > it : map . entrySet ( ) ) { if ( it . getValue ( ) > 1 ) { int result = computeDistance ( root , it . getKey ( ) ) ; if ( result > dist result == - 1 ) { flag = 1 ; break ; } } } if ( flag == 0 ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 4 ) ; root . right . left = newNode ( 4 ) ; int dist = 7 ; solve ( root , dist ) ; } }
Minimum Sum of a pair at least K distance apart from an Array | Java Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Print the answer ; Driver Code
import java . util . * ; class GFG { public static void findMinSum ( int A [ ] , int K ) { int n = A . length ; int minimum_sum = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int min = Integer . MAX_VALUE ; for ( int j = i + K ; j < n ; j ++ ) min = Math . min ( min , A [ j ] ) ; if ( min == Integer . MAX_VALUE ) continue ; minimum_sum = Math . min ( minimum_sum , A [ i ] + min ) ; } System . out . println ( minimum_sum ) ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 2 , 5 , 4 , 3 , 2 , 5 } ; int K = 3 ; findMinSum ( A , K ) ; } }
Absolute distinct count in a Linked List | Java program to calculate the count of distinct absolute values in a linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate node ; Insert the data ; Point the current Node to the current head ; Make the current node as the new head ; Function to return the count of distinct absolute values present in the Linked list ; Driver Code ; Create the head ; Insert nodes to the Linked List
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int distinctCount ( Node head_ref1 ) { Set < Integer > s = new HashSet < Integer > ( ) ; Node ptr1 = head_ref1 ; while ( ptr1 != null ) { s . add ( Math . abs ( ptr1 . data ) ) ; ptr1 = ptr1 . next ; } return s . size ( ) ; } public static void main ( String args [ ] ) { Node head1 = null ; head1 = push ( head1 , - 1 ) ; head1 = push ( head1 , - 2 ) ; head1 = push ( head1 , 0 ) ; head1 = push ( head1 , 4 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 8 ) ; int ans = distinctCount ( head1 ) ; System . out . println ( ans ) ; } }
Count of all possible pairs having sum of LCM and GCD equal to N | Java program to implement the above approach ; Recursive function to return gcd of a and b ; Function to calculate and return LCM of two numbers ; Function to count pairs whose sum of GCD and LCM is equal to N ; Driver Code
class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } static int countPair ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( __gcd ( i , j ) + lcm ( i , j ) == N ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int N = 14 ; System . out . print ( countPair ( N ) ) ; } }