codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find number of connected grid components of given sizes in a Matrix | Java implementation for the above approach ; stores information about which cell are already visited in a particular BFS ; Stores the final result grid ; Stores the count of cells in the largest connected component ; Function checks if a cell is valid , i . e . it is inside the grid and equal to 1 ; Map to count the frequency of each connected component ; function to calculate the largest connected component ; Iterate over every cell ; Stores the indices of the matrix cells ; Mark the starting cell as visited and push it into the queue ; Iterate while the queue is not empty ; Go to the adjacent cells ; Drivers Code ; Given input array of 1 s and 0 s ; queries array ; sizeof queries array ; Count the frequency of each connected component ; Iterate over the given queries array and And answer the queries
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int n = 6 ; static int m = 6 ; static int dx [ ] = { 0 , 1 , - 1 , 0 } ; static int dy [ ] = { 1 , 0 , 0 , - 1 } ; static int [ ] [ ] visited = new int [ n ] [ m ] ; static int [ ] [ ] result = new int [ n ] [ m ] ; static int COUNT ; static boolean is_valid ( int x , int y , int matrix [ ] [ ] ) { if ( x < n && y < m && x >= 0 && y >= 0 ) { if ( visited [ x ] [ y ] == 0 && matrix [ x ] [ y ] == 1 ) return true ; else return false ; } else return false ; } static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static void findComponentSize ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( visited [ i ] [ j ] == 0 && matrix [ i ] [ j ] == 1 ) { COUNT = 0 ; Queue < pair > q = new LinkedList < > ( ) ; q . add ( new pair ( i , j ) ) ; visited [ i ] [ j ] = 1 ; while ( ! q . isEmpty ( ) ) { pair p = q . peek ( ) ; q . remove ( ) ; int x = p . first , y = p . second ; COUNT ++ ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = x + dx [ k ] ; int newY = y + dy [ k ] ; if ( is_valid ( newX , newY , matrix ) ) { q . add ( new pair ( newX , newY ) ) ; visited [ newX ] [ newY ] = 1 ; } } } if ( mp . containsKey ( COUNT ) ) { mp . put ( COUNT , mp . get ( COUNT ) + 1 ) ; } else { mp . put ( COUNT , 1 ) ; } } } } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 , 0 } } ; int queries [ ] = { 6 , 1 , 8 , 2 } ; int N = queries . length ; findComponentSize ( matrix ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ( mp . get ( queries [ i ] ) != null ? mp . get ( queries [ i ] ) : 0 ) + " ▁ " ) ; } }
Minimum value of X such that sum of arr [ i ] | Java program for the above approach ; Function to check if it is possible to get desired result ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver code
import java . io . * ; class GFG { static boolean check ( int a [ ] , int b [ ] , int k , int x ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { sum = sum + ( int ) Math . pow ( Math . max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } static int findMin ( int a [ ] , int b [ ] , int n , int k ) { int l = 0 , u = ( int ) 1e9 ; while ( l < u ) { int m = ( l + u ) / 2 ; if ( check ( a , b , k , m ) ) u = m ; else l = m + 1 ; } return l ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 12 ; int a [ ] = { 2 , 1 , 4 , 3 , 5 } ; int b [ ] = { 4 , 3 , 2 , 3 , 1 } ; System . out . println ( findMin ( a , b , n , k ) ) ; } }
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Java program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int getPairsCount ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && ( arr [ i ] * arr [ j ] ) == Math . abs ( i - j ) ) { count ++ ; } } } return count ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 4 } ; int N = arr . length ; System . out . print ( getPairsCount ( arr , N ) ) ; } }
Count of subarrays with X as the most frequent element , for each value of X from 1 to N | Java program for the above approach ; Function to calculate the number of subarrays where X ( 1 <= X <= N ) is the most frequent element ; array to store the final answers ; Array to store current frequencies ; Variable to store the current most frequent element ; Update frequency array ; Update answer ; Print answer ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { public static void mostFrequent ( int [ ] arr , int N ) { int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] count = new int [ N ] ; int best = 1 ; for ( int j = i ; j < N ; j ++ ) { count [ arr [ j ] - 1 ] ++ ; if ( best > 0 ) { if ( ( count [ arr [ j ] - 1 ] > count [ best - 1 ] ) || ( count [ arr [ j ] - 1 ] == count [ best - 1 ] && arr [ j ] < best ) ) { best = arr [ j ] ; } ans [ best - 1 ] ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; int N = arr . length ; mostFrequent ( arr , N ) ; } }
Count array elements whose highest power of 2 less than or equal to that number is present in the given array | Java program for the above approach ; Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
import java . util . HashMap ; import java . io . * ; class GFG { static int log2 ( int N ) { int result = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; return result ; } static int countElement ( int arr [ ] , int N ) { int count = 0 ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < N ; i ++ ) { int lg = log2 ( arr [ i ] ) ; int p = ( int ) Math . pow ( 2 , lg ) ; if ( m . containsKey ( p ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 6 , 9 } ; int N = arr . length ; System . out . println ( countElement ( arr , N ) ) ; } }
Maximum number of intersections possible for any of the N given segments | java program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending points ; Sort arrays points in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not intersecting with the current segment ; Store the count of segments that intersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int lower_bound ( int [ ] a , int low , int high , long element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int maximumIntersections ( int [ ] [ ] arr , int N ) { int count = 0 ; int [ ] L = new int [ N ] ; int [ ] R = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { L [ i ] = arr [ i ] [ 0 ] ; R [ i ] = arr [ i ] [ 1 ] ; } Arrays . sort ( L ) ; Arrays . sort ( R ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] [ 0 ] ; int r = arr [ i ] [ 1 ] ; int x = lower_bound ( L , 0 , N , l ) ; int y = N - lower_bound ( R , 0 , N , r + 1 ) ; int cnt = x + y ; cnt = N - cnt - 1 ; count = Math . max ( count , cnt ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 6 } , { 5 , 5 } , { 2 , 3 } } ; int N = arr . length ; System . out . println ( maximumIntersections ( arr , N ) ) ; } }
Program to find the Nth Composite Number | Java program for the above approach ; Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ; Initialize the array to true ; Iterate over the range [ 2 , MAX_SIZE ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , MAX_SIZE ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , MAX_SIZE ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
import java . util . * ; public class GFG { public static int NthComposite ( int N ) { int MAX_SIZE = 1000005 ; boolean [ ] IsPrime = new boolean [ MAX_SIZE ] ; Arrays . fill ( IsPrime , true ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } Vector < Integer > Composites = new Vector < Integer > ( ) ; for ( int p = 4 ; p < MAX_SIZE ; p ++ ) if ( ! IsPrime [ p ] ) Composites . add ( p ) ; return Composites . get ( N - 1 ) ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( NthComposite ( N ) ) ; } }
Intersection point of two Linked Lists | Set 3 | Java program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
import java . io . * ; class GFG { static Node head1 = null ; static Node head2 = null ; static class Node { int data ; Node next ; Node ( int x ) { data = x ; next = null ; } } static Node intersectingNode ( Node headA , Node headB ) { Node a = headA ; while ( a != null ) { a . data *= - 1 ; a = a . next ; } Node b = headB ; while ( b != null ) { if ( b . data < 0 ) break ; b = b . next ; } return b ; } static void formLinkList ( ) { head1 = new Node ( 3 ) ; head1 . next = new Node ( 6 ) ; head1 . next . next = new Node ( 9 ) ; head1 . next . next . next = new Node ( 15 ) ; head1 . next . next . next . next = new Node ( 30 ) ; head2 = new Node ( 10 ) ; head2 . next = head1 . next . next . next ; return ; } public static void main ( String [ ] args ) { formLinkList ( ) ; System . out . println ( Math . abs ( intersectingNode ( head1 , head2 ) . data ) ) ; } }
Reverse Level Order Traversal | A recursive java program to print reverse level order traversal using stack and queue ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Do something like normal level order traversal order . Following are the differences with normal level order traversal 1 ) Instead of printing a node , we push the node to stack 2 ) Right subtree is visited before left subtree ; Dequeue node and make it root ; Enqueue right child ; NOTE : RIGHT CHILD IS ENQUEUED BEFORE LEFT ; Enqueue left child ; Now pop all items from stack one by one and print them ; Driver program to test above functions ; Let us create trees shown in above diagram
import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root ; void reverseLevelOrder ( Node node ) { Stack < Node > S = new Stack ( ) ; Queue < Node > Q = new LinkedList ( ) ; Q . add ( node ) ; while ( Q . isEmpty ( ) == false ) { node = Q . peek ( ) ; Q . remove ( ) ; S . push ( node ) ; if ( node . right != null ) Q . add ( node . right ) ; if ( node . left != null ) Q . add ( node . left ) ; } while ( S . empty ( ) == false ) { node = S . peek ( ) ; System . out . print ( node . data + " ▁ " ) ; S . pop ( ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; System . out . println ( " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : " ) ; tree . reverseLevelOrder ( tree . root ) ; } }
Modify array of strings by replacing characters repeating in the same or remaining strings | Java program for the above approach ; Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Driver Code ; Given array of strings ; Function Call to modify the given array of strings
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void removeDuplicateCharacters ( String arr [ ] ) { HashSet < Character > cset = new HashSet < > ( ) ; int n = arr . length ; ArrayList < String > out = new ArrayList < > ( ) ; for ( String str : arr ) { String out_curr = " " ; for ( char ch : str . toCharArray ( ) ) { if ( cset . contains ( ch ) ) continue ; out_curr += ch ; cset . add ( ch ) ; } if ( out_curr . length ( ) != 0 ) out . add ( out_curr ) ; } for ( int i = 0 ; i < out . size ( ) ; i ++ ) { System . out . print ( out . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String arr [ ] = { " Geeks " , " For " , " Geeks " , " Post " } ; removeDuplicateCharacters ( arr ) ; } }
Sum of array elements which are prime factors of a given number | Java program for the above approach ; Function to check if a number is prime or not ; Corner cases ; Check if n is a multiple of 2 or 3 ; Above condition allows to check only for every 6 th number , starting from 5 ; If n is a multiple of i and i + 2 ; Function to find the sum of array elements which are prime factors of K ; Stores the required sum ; Traverse the given array ; If current element is a prime factor of k , add it to the sum ; Print the result ; Driver code ; Given arr [ ] ; Store the size of the array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void primeFactorSum ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 && isPrime ( arr [ i ] ) ) { sum = sum + arr [ i ] ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 , 7 , 15 } ; int N = arr . length ; int K = 35 ; primeFactorSum ( arr , N , K ) ; } }
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | Java program to implement the above approach ; Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two pointers to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver Code
import java . util . * ; class GFG { static int minOperations ( int nums [ ] , int x ) { int sum = 0 ; for ( int i = 0 ; i < x ; i ++ ) sum += nums [ i ] ; if ( sum < x ) return - 1 ; int ans = Integer . MAX_VALUE ; int l = nums . length - 1 , r = nums . length ; x -= sum ; while ( l >= 0 ) { if ( x <= 0 ) { x += nums [ l ] ; l -= 1 ; } if ( x > 0 ) { r -= 1 ; x -= nums [ r ] ; } if ( x == 0 ) { ans = Math . min ( ans , ( l + 1 ) + ( nums . length - r ) ) ; } } if ( ans < Integer . MAX_VALUE ) return ans ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] nums = { 1 , 1 , 4 , 2 , 3 } ; int x = 5 ; System . out . println ( minOperations ( nums , x ) ) ; } }
Count of subarrays having product as a perfect cube | Java program for the above approach ; Function to store the prime factorization of a number ; If N is divisible by i ; Increment v [ i ] by 1 and calculate it modulo by 3 ; Divide the number by i ; If the number is not equal to 1 ; Increment v [ n ] by 1 ; Calculate it modulo 3 ; Function to count the number of subarrays whose product is a perfect cube ; Store the required result ; Stores the prime factors modulo 3 ; Stores the occurrences of the prime factors ; Traverse the array , arr [ ] ; Store the prime factors and update the vector v ; Update the answer ; Increment current state of the prime factors by 1 ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int MAX = ( int ) ( 1e5 ) ; static class Key { int arr [ ] ; Key ( int arr [ ] ) { this . arr = arr ; } @ Override public int hashCode ( ) { return 31 + Arrays . hashCode ( arr ) ; } @ Override public boolean equals ( Object obj ) { if ( this == obj ) return true ; if ( obj == null || ( getClass ( ) != obj . getClass ( ) ) ) return false ; Key other = ( Key ) obj ; if ( ! Arrays . equals ( arr , other . arr ) ) return false ; return true ; } } static void primeFactors ( int v [ ] , int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { while ( n % i == 0 ) { v [ i ] ++ ; v [ i ] %= 3 ; n /= i ; } } if ( n != 1 ) { v [ n ] ++ ; v [ n ] %= 3 ; } } static void countSubarrays ( int arr [ ] , int n ) { int ans = 0 ; int v [ ] = new int [ MAX ] ; HashMap < Key , Integer > mp = new HashMap < > ( ) ; mp . put ( new Key ( v ) , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { primeFactors ( v , arr [ i ] ) ; ans += mp . getOrDefault ( new Key ( v ) , 0 ) ; Key vv = new Key ( v ) ; mp . put ( vv , mp . getOrDefault ( vv , 0 ) + 1 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 4 , 2 } ; int N = arr . length ; countSubarrays ( arr , N ) ; } }
Number of subarrays having even product | Java implementation of the above approach ; Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver Code ; Input ; Length of an array ; Function call to count even product subarrays
import java . io . * ; class GFG { static void evenproduct ( int arr [ ] , int length ) { int total_subarray = length * ( length + 1 ) / 2 ; int total_odd = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < arr . length ; ++ i ) { if ( arr [ i ] % 2 == 0 ) { count_odd = 0 ; } else { ++ count_odd ; total_odd += count_odd ; } } System . out . println ( total_subarray - total_odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 5 , 4 , 9 } ; int length = arr . length ; evenproduct ( arr , length ) ; } }
Check if all rows of a Binary Matrix have all ones placed adjacently or not | Java program for the above approach ; Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Driver Code ; Given matrix ; Function Call
import java . util . * ; public class Main { static Boolean checkGroup ( Vector < Integer > arr ) { if ( arr . size ( ) <= 2 ) return true ; int corner = arr . get ( 0 ) + arr . get ( arr . size ( ) - 1 ) ; int xorSum = 0 ; for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) xorSum += ( arr . get ( i ) ^ arr . get ( i + 1 ) ) ; if ( corner == 0 ) if ( xorSum > 2 ) return false ; else if ( corner == 1 ) if ( xorSum > 1 ) return false ; else if ( xorSum > 0 ) return false ; return true ; } static Boolean isInGroupUtil ( int [ ] [ ] mat ) { for ( int i = 0 ; i < mat . length ; i ++ ) { Vector < Integer > arr = new Vector < Integer > ( ) ; for ( int j = 0 ; j < mat [ i ] . length ; j ++ ) { arr . add ( mat [ i ] [ j ] ) ; } if ( ! checkGroup ( arr ) ) return false ; } return true ; } static void isInGroup ( int [ ] [ ] mat ) { Boolean ans = isInGroupUtil ( mat ) ; if ( ans ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 0 , 0 } , { 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 } } ; isInGroup ( mat ) ; } }
Print all numbers up to N having product of digits equal to K | Java Program to implement the above approach ; Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K
import java . io . * ; import java . util . * ; class GFG { static int productOfDigits ( int N ) { int product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = N / 10 ; } return product ; } static void productOfDigitsK ( int N , int K ) { int flag = 0 ; for ( int i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { System . out . print ( i + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 500 , K = 10 ; productOfDigitsK ( N , K ) ; } }