codeparrot/xlcost-text-to-code huggingface dataset:
Length of the smallest subarray with maximum possible sum | Java program for the above approach ; Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver Code
import java . util . * ; class GFG { static int minimumSizeSubarray ( int arr [ ] , int N ) { int i = 0 , j = N - 1 ; while ( i < N && arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 && arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( minimumSizeSubarray ( arr , N ) ) ; } }
Count ways to represent N as XOR of distinct integers not exceeding N | java program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code
import java . io . * ; class GFG { static void countXorPartition ( int N ) { double a = Math . pow ( 2 , ( int ) ( N - Math . log ( N + 1 ) / Math . log ( 2 ) ) ) ; System . out . print ( a ) ; } public static void main ( String [ ] args ) { int N = 5 ; countXorPartition ( N ) ; } }
Count numbers less than N whose modulo with A is equal to B | Java program for the above approach ; Function to count numbers less than N , whose modulo with A gives B ; If the value of B at least A ; If the value of B is 0 or not ; Stores the resultant count of numbers less than N ; Update the value of ans ; Print the value of ans ; Driver Code
public class MyClass { static void countValues ( int A , int B , int C ) { if ( B >= A ) { System . out . println ( 0 ) ; return ; } if ( B == 0 ) { System . out . println ( C / A ) ; return ; } int ans = C / A ; if ( ans * A + B <= C ) { ans ++ ; } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int A = 6 , B = 3 , N = 15 ; countValues ( A , B , N ) ; } }
Reduce a number to 1 by performing given operations | Set 3 | java program for the above approach ; Utility function to check if n is power of 2 ; Utility function to find highest power of 2 less than or equal to given number ; Recursive function to find steps needed to reduce a given integer to 1 ; Base Condition ; If the number is a power of 2 ; Else subtract the greatest power of 2 smaller than N from N ; Driver Code ; Input ; Function call
class GFG { static int highestPowerof2 ( int n ) { int p = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return ( int ) Math . pow ( 2 , p ) ; } static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( int ) ( Math . ceil ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) == ( int ) ( Math . floor ( ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) ) ; } static int reduceToOne ( int N ) { if ( N == 1 ) { return 0 ; } if ( isPowerOfTwo ( N ) == true ) { return 1 + reduceToOne ( N / 2 ) ; } else { return 1 + reduceToOne ( N - highestPowerof2 ( N ) ) ; } } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( reduceToOne ( N ) ) ; } }
Maximum sum of a subsequence whose Bitwise AND is non | Java program for the above approach ; Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver code
public class GFG { static int maximumSum ( int arr [ ] , int N ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & ( 1 << bit ) ) == 1 ) { sum += arr [ i ] ; } } ans = Math . max ( ans , sum ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 1 , 7 , 11 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , N ) ) ; } }
Check if an array can be reduced to at most length K by removal of distinct elements | Java program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver code
import java . util . HashSet ; public class GFG { static void maxCount ( int arr [ ] , int N , int K ) { HashSet < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } if ( N - st . size ( ) <= K ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . length ; maxCount ( arr , N , K ) ; } }
Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Java program for the above approach ; Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ; Calculate cumulative sum ; Calculate maximum sum ; Return the maximum sum after q queries ; Driver code ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
import java . lang . * ; import java . util . * ; class GFG { static int max_sum ( int a [ ] , ArrayList < ArrayList < Integer > > v , int q , int n ) { int x = 0 ; int m = Integer . MIN_VALUE ; for ( int i = 0 ; i < q ; i ++ ) { int p , qq , k ; p = v . get ( i ) . get ( 0 ) ; qq = v . get ( i ) . get ( 1 ) ; k = v . get ( i ) . get ( 2 ) ; a [ p ] += k ; if ( qq + 1 <= n ) a [ qq + 1 ] -= k ; } for ( int i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = Math . max ( m , x ) ; } return m ; } public static void main ( String [ ] args ) { int n = 10 , q = 3 ; int [ ] a = new int [ n + 5 ] ; ArrayList < ArrayList < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < q ; i ++ ) v . add ( new ArrayList < > ( ) ) ; v . get ( 0 ) . add ( 1 ) ; v . get ( 0 ) . add ( 5 ) ; v . get ( 0 ) . add ( 3 ) ; v . get ( 1 ) . add ( 4 ) ; v . get ( 1 ) . add ( 8 ) ; v . get ( 1 ) . add ( 7 ) ; v . get ( 2 ) . add ( 6 ) ; v . get ( 2 ) . add ( 9 ) ; v . get ( 2 ) . add ( 1 ) ; System . out . println ( max_sum ( a , v , q , n ) ) ; } }
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Java program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is found ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int playerA = ( X ^ Y ) ; boolean flag = false ; for ( int i = 1 ; i <= X ; i ++ ) { for ( int j = 1 ; j <= Y ; j ++ ) { int val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Java program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int first = ( X ^ Y ) ; int second = ( X + Y ) ; if ( first == second ) { System . out . println ( " No " ) ; } else { System . out . println ( " Yes " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
Smallest number required to be added to M to make it divisible by N | Java program for the above approach ; Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call
class GFG { public static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static int findSmallest ( int M , int N ) { int x = findNum ( M , N ) ; return x - M ; } public static void main ( String args [ ] ) { int M = 100 , N = 28 ; System . out . println ( findSmallest ( M , N ) ) ; } }
Prefix Factorials of a Prefix Sum Array | Java program for the above approach ; Function to find the factorial of prefix sum at every possible index ; Find the prefix sum array ; Stores the factorial of all the element till the sum of array ; Find the factorial array ; Find the factorials of each array element ; Print the resultant array ; Driver code
class GFG { static void prefixFactorialArray ( int A [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { A [ i ] += A [ i - 1 ] ; } int fact [ ] = new int [ A [ N - 1 ] + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= A [ N - 1 ] ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = fact [ A [ i ] ] ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( A [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; prefixFactorialArray ( arr , N ) ; } }
Maximum frequency of any array element possible by at most K increments | Java program for the above approach ; Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array ; Add the current element to the window ; If it is not possible to make the array elements in the window equal ; Update the value of sum ; Increment the value of start ; Update the maximum possible frequency ; Print the frequency of the most frequent array element after K increments ; Driver code
import java . util . Arrays ; class GFG { static void maxFrequency ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int start = 0 , end = 0 ; int sum = 0 , res = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum += arr [ end ] ; while ( ( end - start + 1 ) * arr [ end ] - sum > K ) { sum -= arr [ start ] ; start ++ ; } res = Math . max ( res , end - start + 1 ) ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 8 , 13 } ; int N = 4 ; int K = 5 ; maxFrequency ( arr , N , K ) ; } }
Count pairs ( i , j ) from an array such that i < j and arr [ j ] | Java program for the above approach ; Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ; Increase count of pairs ; Print the count of such pairs ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int n , int x ) { int count = 0 ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] - x * i , mp . getOrDefault ( arr [ i ] - x * i , 0 ) + 1 ) ; } for ( int v : mp . values ( ) ) { count += ( v * ( v - 1 ) ) / 2 ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int n = 6 , x = 3 ; int arr [ ] = { 5 , 4 , 8 , 11 , 13 , 16 } ; countPairs ( arr , n , x ) ; } }
Queries to find the sum of weights of all nodes with vertical width from given range in a Binary Tree | Java program for the above approach ; Structure of a node ; Function to create new node ; Function to pre - compute the sum of weights at each width position ; Base Case ; Update the current width position weight ; Recursive Call to its left ; Recursive Call to its right ; Function to find the sum of the weights of nodes whose vertical widths lies over the range [ L , R ] for Q queries ; Stores the weight sum of each width position ; Function Call to fill um ; Stores the sum of all previous nodes , while traversing Map ; Traverse the Map um ; Iterate over all queries ; Print the result for the current query [ l , r ] ; Driver Code ; Given Tree
import java . io . * ; import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int d ) { Node n = new Node ( ) ; n . data = d ; n . left = null ; n . right = null ; return n ; } static void findwt ( Node root , int wt [ ] , TreeMap < Integer , Integer > um , int width ) { if ( root == null ) { return ; } if ( um . containsKey ( width ) ) { um . put ( width , um . get ( width ) + wt [ root . data ] ) ; } else { um . put ( width , wt [ root . data ] ) ; } findwt ( root . left , wt , um , width - 1 ) ; findwt ( root . right , wt , um , width + 1 ) ; } static void solveQueries ( int wt [ ] , Node root , int [ ] [ ] queries ) { TreeMap < Integer , Integer > um = new TreeMap < Integer , Integer > ( ) ; findwt ( root , wt , um , 0 ) ; int x = 0 ; for ( Map . Entry < Integer , Integer > it : um . entrySet ( ) ) { x += it . getValue ( ) ; um . put ( it . getKey ( ) , x ) ; } for ( int i = 0 ; i < queries . length ; i ++ ) { int l = queries [ i ] [ 0 ] ; int r = queries [ i ] [ 1 ] ; int ans = 0 ; if ( um . containsKey ( r ) ) { ans = um . get ( r ) ; } if ( um . containsKey ( l - 1 ) ) { ans -= um . get ( l - 1 ) ; } System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int N = 8 ; Node root = newNode ( 1 ) ; root . left = newNode ( 3 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 4 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 0 ) ; int wt [ ] = { 8 , 6 , 4 , 5 , 1 , 2 , 9 , 1 } ; int queries [ ] [ ] = { { - 1 , 1 } , { - 2 , - 1 } , { 0 , 3 } } ; solveQueries ( wt , root , queries ) ; } }
Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | Java implementation of the above approach ; Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores the element from the ending in increasing order ; Iterate until j is greater than or equal to 0 ; If current array element is greater than the prefix min upto j ; Iterate while stack is not empty and top element is less than or equal to preMin [ j ] ; Remove the top element ; If stack is not empty and top element of the stack is less than the current element ; Push the arr [ j ] in stack ; If none of the above case satisfy then return " No " ; Driver code ; Input
import java . io . * ; import java . util . * ; class GFG { static String recreationalSpot ( int arr [ ] , int N ) { if ( N < 3 ) { return " No " ; } int preMin [ ] = new int [ N ] ; preMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMin [ i ] = Math . min ( preMin [ i - 1 ] , arr [ i ] ) ; } Stack < Integer > stack = new Stack < Integer > ( ) ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > preMin [ j ] ) { while ( stack . empty ( ) == false && stack . peek ( ) <= preMin [ j ] ) { stack . pop ( ) ; } if ( stack . empty ( ) == false && stack . peek ( ) < arr [ j ] ) { return " Yes " ; } stack . push ( arr [ j ] ) ; } } return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 11 , 5 , 13 , 2 } ; int size = arr . length ; System . out . println ( recreationalSpot ( arr , size ) ) ; } }
Check if a number N can be expressed as the sum of powers of X or not | Java program for the above approach ; Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static boolean ToCheckPowerofX ( int n , int x ) { while ( n > 0 ) { int rem = n % x ; if ( rem >= 2 ) { return false ; } n = n / x ; } return true ; } public static void main ( String [ ] args ) { int N = 10 , X = 3 ; if ( ToCheckPowerofX ( N , X ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Find the maximum between N and the number formed by reversing 32 | Java program for the above approach ; Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating the decimal value ; Check for set bits ; Function to find the maximum value between N and the obtained number ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int reverseBin ( int N ) { String S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( ( N & ( 1L << i ) ) != 0 ) S += '1' ; else S += '0' ; } S = ( new StringBuilder ( S ) ) . reverse ( ) . toString ( ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S . charAt ( i ) == '1' ) M += ( 1L << i ) ; } return M ; } static int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return Math . max ( N , M ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( maximumOfTwo ( N ) ) ; } }
Sum of Euler Totient Functions obtained for each divisor of N | Java program for the above approach ; Function to find the sum of Euler Totient Function of divisors of N ; Return the value of N ; Driver code
public class GFG { static int sumOfDivisors ( int N ) { return N ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( sumOfDivisors ( N ) ) ; } }
Square root of two Complex Numbers | Java program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ; Stores the third square root ; If X3 is greater than 0 ; Push the square root in the array ans [ ] ; Stores the fourth square root ; Push the square root in the array ans [ ] ; Prints the square roots ; Driver Code
import java . util . * ; class GFG { static class pair { double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static void complexRoot ( int A , int B ) { Vector < pair > ans = new Vector < pair > ( ) ; double X1 = Math . abs ( Math . sqrt ( ( A + Math . sqrt ( A * A + B * B ) ) / 2 ) ) ; double Y1 = B / ( 2 * X1 ) ; ans . add ( new pair ( X1 , Y1 ) ) ; double X2 = - 1 * X1 ; double Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . add ( new pair ( X2 , Y2 ) ) ; } double X3 = ( A - Math . sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = Math . abs ( Math . sqrt ( X3 ) ) ; double Y3 = B / ( 2 * X3 ) ; ans . add ( new pair ( X3 , Y3 ) ) ; double X4 = - 1 * X3 ; double Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . add ( new pair ( X4 , Y4 ) ) ; } } System . out . print ( " The ▁ Square ▁ roots ▁ are : ▁ " + "NEW_LINE"); for ( pair p : ans ) { System . out . printf ( " % .4f " , p . first ) ; if ( p . second > 0 ) System . out . print ( " + " ) ; if ( p . second != 0 ) System . out . printf ( "%.4f*iNEW_LINE", p.second); else System . out . println ( ) ; } } public static void main ( String [ ] args ) { int A = 0 , B = 1 ; complexRoot ( A , B ) ; } }
Minimum time required to schedule K processes | Java program for the above approach ; Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static int minTime ( int [ ] A , int n , int K ) { int max_ability = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { max_ability = Math . max ( max_ability , A [ i ] ) ; } int tmp [ ] = new int [ max_ability + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( int i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ i / 2 ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += ( K / i ) + 1 ; } else { count += ( K / i ) ; } return count ; } } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 7 , 2 , 4 } ; int N = arr . length ; int K = 15 ; System . out . println ( minTime ( arr , N , K ) ) ; } }
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | Java program for the above approach ; Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; Function to find the minimum length of the rod that can be divided into N equals parts and each part can be further divided into arr [ i ] equal parts ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } static void minimumRod ( int A [ ] , int N ) { System . out . println ( N * findlcm ( A , N ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int N = arr . length ; minimumRod ( arr , N ) ; } }
Check if N can be represented as sum of positive integers containing digit D at least once | Java approach for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code
import java . util . * ; class GFG { static boolean findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } static boolean check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } public static void main ( String [ ] args ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }