codeparrot/xlcost-text-to-code huggingface dataset:
Reduce N to 0 or less by given X and Y operations | Java program to implement the above approach ; Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver Code
class GFG { static boolean NegEqu ( int N , int X , int Y ) { while ( X > 0 && ( N > N / 2 + 10 ) ) { N = N / 2 + 10 ; X -= 1 ; } while ( Y > 0 ) { N = N - 10 ; Y -= 1 ; } if ( N <= 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 100 ; int X = 3 ; int Y = 4 ; if ( NegEqu ( N , X , Y ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | Java program for the above approach ; Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call
import java . util . * ; class GFG { static int minSubarrays ( int arr [ ] , int n ) { int right = n - 1 ; int left = 0 ; int subarrays = 0 ; while ( right >= 0 ) { for ( left = 0 ; left <= right ; left += 1 ) { if ( __gcd ( arr [ left ] , arr [ right ] ) > 1 ) { subarrays += 1 ; right = left - 1 ; break ; } if ( left == right && __gcd ( arr [ left ] , arr [ right ] ) == 1 ) { return 0 ; } } } return subarrays ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 2 , 3 , 4 , 4 , 4 , 3 } ; System . out . print ( minSubarrays ( arr , N ) ) ; } }
Find the largest possible value of K such that K modulo X is Y | Java program for the above approach ; Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
import java . util . * ; class GFG { static int findMaxSoln ( int n , int x , int y ) { int ans = Integer . MIN_VALUE ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = Math . max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : - 1 ) ; } public static void main ( String [ ] args ) { int n = 15 , x = 10 , y = 5 ; System . out . print ( findMaxSoln ( n , x , y ) ) ; } }
Count all numbers up to N having M as the last digit | Java program to implement the above approach ; Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function call
import java . util . * ; class GFG { static int getCount ( int N , int M ) { int total_count = 0 ; total_count += ( N / 10 ) ; int x = ( N / 10 ) * 10 ; if ( ( N - x ) >= M ) { total_count = total_count + 1 ; } return total_count ; } public static void main ( String [ ] args ) { int N = 100 , M = 1 ; System . out . print ( getCount ( N , M ) ) ; } }
Count of Right | Java program for the above approach ; Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function Call
import java . util . * ; class GFG { static int RightAngled ( int a [ ] [ ] , int n ) { HashMap < Integer , Integer > xpoints = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > ypoints = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . containsKey ( a [ i ] [ 0 ] ) ) { xpoints . put ( a [ i ] [ 0 ] , xpoints . get ( a [ i ] [ 0 ] ) + 1 ) ; } else { xpoints . put ( a [ i ] [ 0 ] , 1 ) ; } if ( ypoints . containsKey ( a [ i ] [ 1 ] ) ) { ypoints . put ( a [ i ] [ 1 ] , ypoints . get ( a [ i ] [ 1 ] ) + 1 ) ; } else { ypoints . put ( a [ i ] [ 1 ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . get ( a [ i ] [ 0 ] ) >= 1 && ypoints . get ( a [ i ] [ 1 ] ) >= 1 ) { count += ( xpoints . get ( a [ i ] [ 0 ] ) - 1 ) * ( ypoints . get ( a [ i ] [ 1 ] ) - 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] [ ] = { { 1 , 2 } , { 2 , 1 } , { 2 , 2 } , { 2 , 3 } , { 3 , 2 } } ; System . out . print ( RightAngled ( arr , N ) ) ; } }
Smallest subarray which upon repetition gives the original array | Java program for the above approach ; Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
import java . util . * ; class GFG { static void printArray ( Vector < Integer > brr ) { for ( int it : brr ) { System . out . print ( it + " ▁ " ) ; } } static void RepeatingSubarray ( int arr [ ] , int N ) { if ( N < 2 ) { System . out . print ( " - 1" ) ; } Vector < Integer > brr = new Vector < Integer > ( ) ; brr . add ( arr [ 0 ] ) ; brr . add ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { boolean a = false ; int n = brr . size ( ) ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr . get ( K ) ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . add ( arr [ i ] ) ; } System . out . print ( " - 1" ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = arr . length ; RepeatingSubarray ( arr , N ) ; } }
Find all missing numbers from a given sorted array | Java program for the above approach ; Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static void printMissingElements ( int arr [ ] , int N ) { int diff = arr [ 0 ] - 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { System . out . print ( ( i + diff ) + " ▁ " ) ; diff ++ ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . length ; printMissingElements ( arr , N ) ; } }
Find all missing numbers from a given sorted array | Java program for the above approach ; Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static void printMissingElements ( int arr [ ] , int N ) { int [ ] b = new int [ arr [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . length ; printMissingElements ( arr , N ) ; } }
Least common element in given two Arithmetic sequences | Java program to implement the above approach ; Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield true in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
import java . util . * ; class GFG { static int smallestCommon ( int a , int b , int c , int d ) { if ( a == c ) return a ; if ( a > c ) { swap ( a , c ) ; swap ( b , d ) ; } int first_term_diff = ( c - a ) ; int possible_y ; for ( possible_y = 0 ; possible_y < b ; possible_y ++ ) { if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) { break ; } } if ( possible_y != b ) { return c + possible_y * d ; } return - 1 ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int A = 2 , B = 20 , C = 19 , D = 9 ; System . out . print ( smallestCommon ( A , B , C , D ) ) ; } }
Minimum number of flips with rotation to make binary string alternating | Java program for the above approach ; Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Driver Code ; Given String ; Length of given string ; Function call
import java . util . * ; class GFG { static int MinimumFlips ( String s , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( s . charAt ( i ) == '1' ? 1 : 0 ) ; } int [ ] oddone = new int [ n + 1 ] ; int [ ] evenone = new int [ n + 1 ] ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } int minimum = Math . min ( oddone [ n ] , evenone [ n ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } public static void main ( String [ ] args ) { String S = "000001100" ; int n = S . length ( ) ; System . out . print ( MinimumFlips ( S , n ) ) ; } }
Create a Graph by connecting divisors from N to M and find shortest path | Java program for the above approach ; Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , Math . sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To astatic void duplicate printing of same element ; Print the factors from n ; Driver Code ; Given N and M ; Function call
import java . util . * ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } static void shortestpath ( int m , int n ) { Vector < Integer > mfactor = new Vector < > ( ) ; Vector < Integer > nfactor = new Vector < > ( ) ; HashMap < Integer , Integer > fre = new HashMap < > ( ) ; mfactor . add ( m ) ; fre . put ( m , 1 ) ; while ( m != 1 ) { if ( isprm ( m ) != 0 ) { mfactor . add ( 1 ) ; fre . put ( 1 , 1 ) ; m = 1 ; } else { for ( int i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . add ( m / i ) ; fre . put ( m / i , 1 ) ; m = ( m / i ) ; break ; } } } } nfactor . add ( n ) ; while ( fre . containsKey ( n ) && fre . get ( n ) != 1 ) { if ( isprm ( n ) != 0 ) { nfactor . add ( 1 ) ; n = 1 ; } else { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . add ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( int i = 0 ; i < mfactor . size ( ) ; i ++ ) { if ( mfactor . get ( i ) == n ) break ; System . out . print ( mfactor . get ( i ) + " ▁ < - - > ▁ " ) ; } for ( int i = nfactor . size ( ) - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) System . out . print ( nfactor . get ( i ) ) ; else System . out . print ( nfactor . get ( i ) + " ▁ < - - > ▁ " ) ; } } public static void main ( String [ ] args ) { int m = 18 , n = 19 ; shortestpath ( m , n ) ; } }
Possible number of Trees having N vertex | Java program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the final count of trees ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int NumberOfTrees ( int arr [ ] , int N ) { int maxElement = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int level [ ] = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { for ( int j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } public static void main ( String [ ] args ) { int N = 7 ; int arr [ ] = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; System . out . print ( NumberOfTrees ( arr , N ) ) ; } }
Possible number of Trees having N vertex | Java program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( y & 1 ) != 0 ) p = ( x * p ) % mod ; return p ; } static int NumberOfTrees ( int arr [ ] , int N ) { int maxElement = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int [ ] level = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod ; } return ans ; } public static void main ( String [ ] args ) { int N = 7 ; int arr [ ] = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; System . out . print ( NumberOfTrees ( arr , N ) ) ; } }
A Binary String Game | Java program for the above approach ; Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Driver Code ; Given string S ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static String gameMax ( String S ) { int N = S . length ( ) ; List < Integer > list = new ArrayList < > ( ) ; int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) { one ++ ; } else { if ( one != 0 ) { list . add ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . add ( one ) ; } Collections . sort ( list , Collections . reverseOrder ( ) ) ; int score_1 = 0 , score_2 = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { if ( list . get ( i ) % 2 == 1 ) { score_1 += list . get ( i ) ; } else { score_2 += list . get ( i ) ; } } if ( score_1 == score_2 ) return " - 1" ; return ( score_1 > score_2 ) ? " Player ▁ 1" : " Player ▁ 2" ; } public static void main ( String [ ] args ) { String S = "11111101" ; System . out . println ( gameMax ( S ) ) ; } }
Count of carry operations on adding two Binary numbers | Java program for the above approach ; Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to final count ; Return the final count ; Driver Code ; Given two numbers ; Function call
class GFG { static int carryCount ( int num1 , int num2 ) { int count = 0 ; while ( num2 != 0 ) { int carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += Integer . bitCount ( num2 ) ; } return count ; } public static void main ( String [ ] args ) { int A = 15 , B = 10 ; System . out . print ( carryCount ( A , B ) ) ; } }
Count of all possible ways to reach a target by a Knight | Java Program to implement the above approach ; Function to return X ^ Y % Mod ; Base Case ; Function to return the inverse of factorial of N ; Base case ; Function to return factorial of n % Mod ; Base case ; Function to return the value of n ! / ( ( n - k ) ! * k ! ) ; Function to return the count of ways to reach ( n , m ) from ( 0 , 0 ) ; If ( N + M ) % 3 != 0 ; No possible way exists ; Calculate X and Y from the equations X + 2 Y = N and 2 X + Y == M ; Driver Code
import java . util . * ; class GFG { static int Mod = ( int ) ( 1e9 + 7 ) ; static int power ( int X , int Y , int Mod ) { if ( Y == 0 ) return 1 ; int p = power ( X , Y / 2 , Mod ) % Mod ; p = ( p * p ) % Mod ; if ( ( Y & 1 ) != 0 ) { p = ( X * p ) % Mod ; } return p ; } static int Inversefactorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return power ( fact , Mod - 2 , Mod ) ; } static int factorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return fact ; } static int nck ( int N , int K ) { int factN = factorial ( N ) ; int inv = Inversefactorial ( K ) ; int invFact = Inversefactorial ( N - K ) ; return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod ; } static int TotalWaYs ( int N , int M ) { if ( ( ( N + M ) % 3 ) != 0 ) return 0 ; int X = N - ( N + M ) / 3 ; int Y = M - ( N + M ) / 3 ; if ( X < 0 Y < 0 ) return 0 ; return nck ( X + Y , Y ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . print ( TotalWaYs ( N , M ) ) ; } }
Lead a life problem | Java program for the above approach ; Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Driver Code ; Given days ; Given earnings ; Given cost ; Given energy e ; Function call
import java . util . * ; class GFG { static void calculateProfit ( int n , int [ ] earnings , int [ ] cost , int e ) { int profit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int earning_per_day = 0 ; int daily_spent_food = 0 ; if ( i == ( n - 1 ) ) { earning_per_day = earnings [ i ] * e ; profit = profit + earning_per_day ; break ; } if ( cost [ i ] < earnings [ i ] ) { earning_per_day = earnings [ i ] * e ; daily_spent_food = cost [ i ] * e ; profit = profit + earning_per_day - daily_spent_food ; } } System . out . print ( profit + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 4 ; int earnings [ ] = { 1 , 8 , 6 , 7 } ; int cost [ ] = { 1 , 3 , 4 , 1 } ; int e = 5 ; calculateProfit ( n , earnings , cost , e ) ; } }
Check if N numbers with Even Sum can be selected from a given Array | Java efficient program to check if N numbers with Odd sum can be selected from the given array ; Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
import java . util . * ; class GFG { static boolean checkEvenSum ( int arr [ ] , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq % 2 == 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 } ; int size = arr . length ; int N = 5 ; if ( checkEvenSum ( arr , N , size ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
Minimum count of digits required to obtain given Sum | Java program to implement the above approach required to make the given sum ; Function to print the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code
import java . util . * ; class Main { static void mindigits ( int n ) { if ( n % 9 == 0 ) { System . out . println ( n / 9 ) ; } else { System . out . println ( ( n / 9 ) + 1 ) ; } } public static void main ( String [ ] args ) { int n1 = 24 ; int n2 = 18 ; mindigits ( n1 ) ; mindigits ( n2 ) ; } }
Count of all possible numbers not exceeding M having suffix N | Java program to implement the above approach ; Function to count the no . of digits of N ; Function to count all possible numbers having Suffix as N ; Difference of the A . P ; Count of the number of terms ; Driver code
import java . util . * ; class GFG { static int digitsOf ( int num ) { return Integer . toString ( num ) . length ( ) ; } static int count ( int a , int tn ) { int diff = ( int ) Math . pow ( 10 , digitsOf ( a ) ) ; return ( ( tn - a ) / diff ) + 1 ; } public static void main ( String [ ] args ) { int n = 25 , m = 4500 ; System . out . println ( count ( n , m ) ) ; } }