codeparrot/xlcost-text-to-code huggingface dataset:
Count characters of a string which when removed individually makes the string equal to another string | Java program for the above approach ; Function to count characters from string A whose removal makes the strings A and B equal ; Stores the index of the longest prefix ; Stores the index of the longest suffix ; Traverse the string B ; Traverse the string B ; If N - M is equal to 1 and Y is less than or equal to X ; Print the count of characters ; Print the positions of the characters ; Otherwise ; Driver Code
class GFG { static void RemoveOneChar ( String A , String B , int N , int M ) { int X = 0 ; int Y = N - 1 ; for ( int i = 0 ; i < M ; i ++ ) { if ( A . charAt ( X ) != B . charAt ( i ) ) break ; X ++ ; } for ( int i = M - 1 ; i >= 0 ; i -- ) { if ( A . charAt ( Y ) != B . charAt ( i ) ) break ; Y -- ; } if ( N - M == 1 && Y < X ) { System . out . println ( X - Y + 1 ) ; for ( int i = Y ; i <= X ; i ++ ) System . out . print ( i + 1 + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( - 1 ) ; } static public void main ( String [ ] args ) { String A = " abaac " ; String B = " abac " ; int N = A . length ( ) ; int M = B . length ( ) ; RemoveOneChar ( A , B , N , M ) ; } }
Minimum flips or swapping of adjacent characters required to make a string equal to another | Java program for the above approach ; Function to find minimum operations required to convert string A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call
public class GFG { static void minimumOperation ( String a , String b ) { int n = a . length ( ) ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( i ) ) { i = i + 1 ; continue ; } else if ( a . charAt ( i ) == b . charAt ( i + 1 ) && a . charAt ( i + 1 ) == b . charAt ( i ) && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a . charAt ( i ) != b . charAt ( i ) ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } System . out . println ( minoperation ) ; } public static void main ( String [ ] args ) { String a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; } }
Minimum replacements required to make sum of all K | Java program for the above approach ; Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Driver code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static void findMinOperations ( int arr [ ] , int N , int K ) { int operations = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int j = i ; j < N ; j += K ) freq . put ( arr [ j ] , freq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; int max1 = 0 , num = - 1 ; for ( Map . Entry < Integer , Integer > x : freq . entrySet ( ) ) { if ( x . getValue ( ) > max1 ) { max1 = x . getValue ( ) ; num = x . getKey ( ) ; } } for ( Map . Entry < Integer , Integer > x : freq . entrySet ( ) ) { if ( x . getKey ( ) != num ) operations += x . getValue ( ) ; } } System . out . print ( operations ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 3 , 5 , 6 } ; int K = 2 ; int N = arr . length ; findMinOperations ( arr , N , K ) ; } }
Count of pairs of integers up to X and Y that generates equal Quotient and Remainder | Java Program for the above approach ; Function to calculate the number of pairs satisfying ( m / n = m % n ) ; Iterate from 1 to sqrt ( x ) ; Combining the conditions - 1 ) n > k 2 ) n <= y 3 ) n <= ( x / k - 1 ) ; Driver code
import java . io . * ; class GFG { static void countOfPairs ( int x , int y ) { int count = 0 ; for ( int k = 1 ; k * k <= x ; ++ k ) { count += Math . max ( 0 , Math . min ( y , x / k - 1 ) - k ) ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int x = 4 ; int y = 5 ; countOfPairs ( x , y ) ; } }
Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square | Java program for the above approach ; Function to count total number of permutation of the first N natural number having the sum of adjacent elements as perfect square ; Create an adjacency matrix ; Count elements whose indegree is 1 ; Generate adjacency matrix ; Find the sum of i and j ; If sum is perfect square . then move from i to j ; Add it in adjacency list of i ; If any list is of size 1 , then the indegree is 1 ; If there is no element whose indegree is 1 , then N such permutations are possible ; If there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; If there are more than 2 elements whose indegree is 1 , then return 0 ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int countPermutations ( int N ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( 105 ) ; for ( int i = 0 ; i < 105 ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; int indeg = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i == j ) continue ; int sum = i + j ; if ( Math . ceil ( Math . sqrt ( sum ) ) == Math . floor ( Math . sqrt ( sum ) ) ) { adj . get ( i ) . add ( j ) ; } } if ( adj . get ( i ) . size ( ) == 1 ) indeg ++ ; } if ( indeg == 0 ) return N ; else if ( indeg <= 2 ) return 2 ; else return 0 ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . println ( countPermutations ( N ) ) ; } }
Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | Java program for the above approach ; Function to construct the list with exactly K unique adjacent element differences ; Stores the resultant array ; Stores the left and the right most element of the range ; Traverse the array ; If k is even , the add left to array and increment the left ; If k is odd , the add right to array and decrement the right ; Repeat the steps for k - 1 times ; Print the resultant list ; Driver Code
class GFG { public static void makeList ( int N , int K ) { int [ ] list = new int [ N ] ; int left = 1 ; int right = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % 2 == 0 ) { list [ i ] = left ; left = left + 1 ; } else { list [ i ] = right ; right = right - 1 ; } if ( K > 1 ) K -- ; } for ( int i = 0 ; i < list . length ; i ++ ) { System . out . print ( list [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; int K = 3 ; makeList ( N , K ) ; } }
Find the date after next half year from a given date | Java program for the above approach ; Function to find the date after the next half - year ; Stores the number of days in the months of a leap year ; List of months ; Days in half of a year ; Index of current month ; Starting day ; Decrement the value of cnt by 1 ; Increment cur_date ; If cnt is equal to 0 , then break out of the loop ; Update cur_month ; Update cur_date ; Print the resultant date ; Driver Code ; Function Call
class GFG { public static void getDate ( int d , String m ) { int [ ] days = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; String [ ] month = { " January " , " February " , " March " , " April " , " May " , " June " , " July " , " August " , " September " , " October " , " November " , " December " } ; int cnt = 183 ; int cur_month = 0 ; for ( int i = 0 ; i < 12 ; i ++ ) if ( m == month [ i ] ) cur_month = i ; int cur_date = d ; while ( true ) { while ( cnt > 0 && cur_date <= days [ cur_month ] ) { cnt -= 1 ; cur_date += 1 ; } if ( cnt == 0 ) break ; cur_month = ( cur_month + 1 ) % 12 ; cur_date = 1 ; } System . out . println ( cur_date + " ▁ " + month [ cur_month ] ) ; } public static void main ( String args [ ] ) { int D = 15 ; String M = " January " ; getDate ( D , M ) ; } }
Maximum number made up of distinct digits whose sum is equal to N | Java program for the above approach ; Function to find the largest positive number made up of distinct digits having the sum of its digits as N ; If given number is greater than 45 , print - 1 ; Store the required number and the digit to be considered ; Loop until N > 0 and digit > 0 ; If the current digit is at most N then , add it to number num ; Update the value of num ; Decrement N by digit ; Consider the next lower digit ; Add 0 at the end and return the number num ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static long largestNumber ( int N ) { if ( N > 45 ) return - 1 ; int num = 0 , digit = 9 ; while ( N > 0 && digit > 0 ) { if ( digit <= N ) { num *= 10 ; num += digit ; N -= digit ; } digit -= 1 ; } return num * 10 ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . print ( largestNumber ( N ) ) ; } }
Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | Java program for the above approach ; Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the String S ; Stores the count of multiplication operator in the String ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
import java . util . * ; class GFG { static int minimumSum ( int A [ ] , int N , String S ) { int mul = 0 ; for ( int i = 0 ; i < ( int ) S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == ' * ' ) mul += 1 ; } int ans = 1000000 ; for ( int i = 0 ; i < ( 1 << ( N - 1 ) ) ; i ++ ) { int cnt = 0 ; Vector < Character > v = new Vector < Character > ( ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( ( ( 1 << j ) & ( i ) ) > 0 ) { cnt += 1 ; v . add ( ' * ' ) ; } else { v . add ( ' + ' ) ; } } if ( cnt == mul ) { LinkedList < Integer > d = new LinkedList < Integer > ( ) ; d . add ( A [ 0 ] ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( v . get ( j ) == ' * ' ) { int x = d . getLast ( ) ; d . removeLast ( ) ; x = x * A [ j + 1 ] ; d . add ( x ) ; } else { d . add ( A [ j + 1 ] ) ; } } int sum = 0 ; while ( d . size ( ) > 0 ) { int x = d . peek ( ) ; sum += x ; d . removeFirst ( ) ; } ans = Math . min ( ans , sum ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 2 , 2 } ; String S = " * * + " ; int N = A . length ; System . out . print ( minimumSum ( A , N , S ) ) ; } }
Minimum sum of medians of all possible K length subsequences of a sorted array | Java program for the above approach ; Function to find the minimum sum of all the medians of the K sized sorted arrays formed from the given array ; Stores the distance between the medians ; Stores the number of subsequences required ; Stores the resultant sum ; Iterate from start and add all the medians ; Add the value of arr [ i ] to the variable minsum ; Increment i by select the median to get the next median index ; Decrement the value of totalArrays by 1 ; Print the resultant minimum sum ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void sumOfMedians ( int arr [ ] , int N , int K ) { int selectMedian = ( K + 1 ) / 2 ; int totalArrays = N / K ; int minSum = 0 ; int i = selectMedian - 1 ; while ( i < N && totalArrays != 0 ) { minSum = minSum + arr [ i ] ; i = i + selectMedian ; totalArrays -- ; } System . out . println ( minSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int K = 2 ; sumOfMedians ( arr , N , K ) ; } }
Find K positive integers not exceeding N and having sum S | Java implementation for the above approach ; Function to represent S as the sum of K positive integers less than or equal to N ; If S can cannot be represented as sum of K integers ; If sum of first i natural numbers exceeds S ; Insert i into nums [ ] ; Insert first K - 1 positive numbers into answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Printing the K numbers ; Driver code
import java . util . Vector ; class GFG { static void solve ( int S , int K , int N ) { if ( K > N ) { System . out . println ( " - 1" ) ; return ; } int max_sum = 0 , min_sum = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { min_sum += i ; max_sum += N - i + 1 ; } if ( S < min_sum S > max_sum ) { System . out . println ( " - 1" ) ; return ; } int s1 = 0 ; Vector < Integer > nums = new Vector < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( s1 > S ) break ; s1 += i ; nums . add ( i ) ; } Vector < Integer > answer = new Vector < > ( ) ; int s2 = 0 ; for ( int i = 0 ; i < K - 1 ; i ++ ) { answer . add ( nums . get ( i ) ) ; s2 += nums . get ( i ) ; } answer . add ( S - s2 ) ; int Max = N ; for ( int i = answer . size ( ) - 1 ; i >= 0 ; i -- ) { if ( answer . get ( i ) > Max ) { int extra = answer . get ( i ) - Max ; if ( i - 1 >= 0 ) answer . set ( i - 1 , answer . get ( i - 1 ) + extra ) ; answer . set ( i , Max ) ; Max -- ; } else break ; } for ( int x : answer ) System . out . print ( x + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int S = 15 , K = 4 , N = 8 ; solve ( S , K , N ) ; } }
Generate an N | Java program for the above approach ; Function to minimize the maximum element present in an N - length array having sum of elements divisible by K ; Return the ceil value of ( K / N ) ; Driver code
class GFG { static int minimumValue ( int N , int K ) { return ( int ) Math . ceil ( ( double ) K / ( double ) N ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 50 ; System . out . print ( minimumValue ( N , K ) ) ; } }
Minimum removal of elements from end of an array required to obtain sum K | Java program for the above approach ; Function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver code ; Given Array ; Given target sum
import java . util . * ; class GFG { static void minSizeArr ( int A [ ] , int N , int K ) { int leftTaken = N , rightTaken = N ; int leftSum = 0 , rightSum = 0 ; for ( int left = - 1 ; left < N ; left ++ ) { if ( left != - 1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( int right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( int i = 0 ; i < leftTaken ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < rightTaken ; i ++ ) System . out . print ( A [ N - i - 1 ] + " ▁ " ) ; } else System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 7 ; int A [ ] = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }
Minimum removal of elements from end of an array required to obtain sum K | Java program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver Code ; Given Array ; Given target sum
import java . io . * ; import java . util . * ; class GFG { static void minSizeArr ( int A [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += A [ i ] ; if ( K > sum ) { System . out . print ( - 1 ) ; return ; } if ( K == sum ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( A [ i ] + " ▁ " ) ; } return ; } int tar = sum - K ; HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; um . put ( 0 , - 1 ) ; int left = 0 , right = 0 ; int cur = 0 , maxi = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { cur += A [ i ] ; if ( um . containsKey ( cur - tar ) && i - um . get ( cur - tar ) > maxi ) { maxi = i - um . get ( cur - tar ) ; right = i ; left = um . get ( cur - tar ) ; } if ( ! um . containsKey ( cur ) ) um . put ( cur , i ) ; } if ( maxi == - 1 ) System . out . println ( - 1 ) ; else { for ( int i = 0 ; i <= left ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < right ; i ++ ) System . out . print ( A [ N - i - 1 ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 7 ; int A [ ] = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }
Minimum product modulo N possible for any pair from a given range | java program for the above approach ; Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Print the minimum value of remainder ; If R - L >= N ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void minModulo ( int L , int R , int N ) { if ( R - L < N ) { int ans = Integer . MAX_VALUE ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( i != j ) ans = Math . min ( ans , ( i * j ) % N ) ; System . out . println ( ans ) ; } else { System . out . println ( 0 ) ; } } public static void main ( String [ ] args ) { int L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ; } }
Count numbers having GCD with N equal to the number itself | java program for the above approach ; Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( N / i != i ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( countNumbers ( N ) ) ; } }
Maximum length of all possible K equal length ropes generated by cutting N ropes | Java program for the above approach ; Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array arr [ ] ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
import java . util . * ; class GFG { static int maximumSize ( Integer [ ] a , int k ) { int low = 1 ; int high = Collections . max ( Arrays . asList ( a ) ) ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < a . length ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; System . out . println ( maximumSize ( arr , K ) ) ; } }
Quadratic equation whose roots are K times the roots of given equation | Java program for the above approach ; Function to find the quadratic equation whose roots are K times the roots of the given equation ; Print quadratic equation ; Driver Code
import java . util . * ; class GFG { static void findEquation ( int A , int B , int C , int K ) { System . out . print ( A + " ▁ " + K * B + " ▁ " + K * K * C ) ; } public static void main ( String [ ] args ) { int A = 1 , B = 2 , C = 1 , K = 2 ; findEquation ( A , B , C , K ) ; } }
Maximum the value of a given expression for any pair of coordinates on a 2D plane | Java program for the above approach ; Function to find the maximum value of the given expression possible for any pair of co - ordinates ; Stores the differences between pairs ; Stores the maximum value ; Traverse the array arr [ ] [ ] ; While pq is not empty and difference between point [ 0 ] and pq . top ( ) [ 1 ] > K ; Removes the top element ; If pq is not empty ; Update the value res ; Push pair { point [ 1 ] - point [ 0 ] , point [ 0 ] } in pq ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void findMaxValueOfEquation ( int arr [ ] [ ] , int K ) { PriorityQueue < int [ ] > pq = new PriorityQueue < > ( ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return b [ 0 ] - a [ 0 ] ; return b [ 1 ] - a [ 1 ] ; } ) ; int res = Integer . MIN_VALUE ; for ( int point [ ] : arr ) { while ( ! pq . isEmpty ( ) && point [ 0 ] - pq . peek ( ) [ 1 ] > K ) { pq . poll ( ) ; } if ( ! pq . isEmpty ( ) ) { res = Math . max ( res , pq . peek ( ) [ 0 ] + point [ 0 ] + point [ 1 ] ) ; } pq . add ( new int [ ] { point [ 1 ] - point [ 0 ] , point [ 0 ] } ) ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 3 } , { 2 , 0 } , { 5 , 10 } , { 6 , - 10 } } ; int K = 1 ; findMaxValueOfEquation ( arr , K ) ; } }
Minimum increments required to make absolute difference of all pairwise adjacent array elements even | Java program for the above approach ; Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int minOperations ( int arr [ ] , int n ) { int oddcount = 0 , evencount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return Math . min ( oddcount , evencount ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 1 , 8 } ; int N = arr . length ; System . out . println ( minOperations ( arr , N ) ) ; } }
Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | Java program for the above approach ; Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver Code
class GFG { static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countDistinctNumbers ( long A , long B , long C ) { long g = gcd ( A , B ) ; long count = C / g ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { long A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ; } }