codeparrot/xlcost-text-to-code huggingface dataset:
Minimize length of an array by removing similar subarrays from both ends | Java program for the above approach ; Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Driver Code ; Input ; Function call to find the minimized length of the array
import java . util . * ; class GFG { static void findMinLength ( int arr [ ] , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } System . out . println ( back - front + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = arr . length ; findMinLength ( arr , N ) ; } }
Digits whose alphabetic representations are jumbled in a given string | Java program for the above approach ; Function to convert the jumbled string into digits ; Strings of digits 0 - 9 ; Initialize vector ; Initialize answer ; Size of the string ; Traverse the string ; Update the elements of the vector ; Print the digits into their original format ; Return answer ; Driver Code
import java . io . * ; class GFG { static String finddigits ( String s ) { String [ ] num = { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; int [ ] arr = new int [ 10 ] ; String ans = " " ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' z ' ) arr [ 0 ] ++ ; if ( s . charAt ( i ) == ' w ' ) arr [ 2 ] ++ ; if ( s . charAt ( i ) == ' g ' ) arr [ 8 ] ++ ; if ( s . charAt ( i ) == ' x ' ) arr [ 6 ] ++ ; if ( s . charAt ( i ) == ' v ' ) arr [ 5 ] ++ ; if ( s . charAt ( i ) == ' o ' ) arr [ 1 ] ++ ; if ( s . charAt ( i ) == ' s ' ) arr [ 7 ] ++ ; if ( s . charAt ( i ) == ' f ' ) arr [ 4 ] ++ ; if ( s . charAt ( i ) == ' h ' ) arr [ 3 ] ++ ; if ( s . charAt ( i ) == ' i ' ) arr [ 9 ] ++ ; } arr [ 7 ] -= arr [ 6 ] ; arr [ 5 ] -= arr [ 7 ] ; arr [ 4 ] -= arr [ 5 ] ; arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] ) ; arr [ 3 ] -= arr [ 8 ] ; arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] ) ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) { ans += ( char ) ( i + '0' ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = " owoftnuoer " ; System . out . println ( finddigits ( s ) ) ; } }
Check if two binary strings can be made equal by swapping pairs of unequal characters | Java program to implement the above approach ; Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0' ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver Code
import java . util . * ; class GFG { static void check ( String s1 , String s2 ) { int s1_0 = 0 , s2_0 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == '0' ) { s1_0 ++ ; } if ( s2 . charAt ( i ) == '0' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { System . out . println ( " NO " ) ; return ; } else { int Count1 = 0 , Count2 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == '0' ) { Count1 ++ ; } if ( s2 . charAt ( i ) == '0' ) { Count2 ++ ; } if ( Count1 < Count2 ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } } public static void main ( String [ ] args ) { String s1 = "100111" ; String s2 = "111010" ; check ( s1 , s2 ) ; s1 = "110100" ; s2 = "010101" ; check ( s1 , s2 ) ; } }
Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | Java program for the above approach ; Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then print not possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps  possible ; Swaps 1 with 0 in the string str1 ; Print not possible ; Driver Code ; Given Strings ; Function Call
import java . io . * ; class GFG { static void isBinaryStringsEqual ( String str1 , String str2 ) { int str1Zeros = 0 , str1Ones = 0 ; int str2Zeros = 0 , str2Ones = 0 ; int flag = 0 ; int curStr1Ones = 0 ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 . charAt ( i ) == '1' ) { str1Ones ++ ; } else if ( str1 . charAt ( i ) == '0' ) { str1Zeros ++ ; } if ( str2 . charAt ( i ) == '1' ) { str2Ones ++ ; } else if ( str2 . charAt ( i ) == '0' ) { str2Zeros ++ ; } } if ( str1Zeros != str2Zeros && str1Ones != str2Ones ) { System . out . println ( " Not ▁ Possible " ) ; } else { for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { if ( str1 . charAt ( i ) == '0' && curStr1Ones > 0 ) { str1 = str1 . substring ( 0 , i ) + '1' + str1 . substring ( i + 1 ) ; curStr1Ones -- ; } if ( str1 . charAt ( i ) == '0' && curStr1Ones == 0 ) { flag ++ ; break ; } if ( str1 . charAt ( i ) == '1' && str2 . charAt ( i ) == '0' ) { str1 = str1 . substring ( 0 , i ) + '0' + str1 . substring ( i + 1 ) ; curStr1Ones ++ ; } } } if ( flag == 0 ) { System . out . println ( " Possible " ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } } public static void main ( String [ ] args ) { String str1 = "0110" ; String str2 = "0011" ; isBinaryStringsEqual ( str1 , str2 ) ; } }
Count array elements whose all distinct digits appear in K | Java program to implement the above approach ; Function to the count of array elements whose distinct digits are a subset of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into set ; Update K ; Stores the count of array elements whose distinct digits are a subset of the digits of K ; Traverse the array , arr [ ] ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the set ; Update flag ; Update no ; If all the digits of arr [ i ] present in set ; Update count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int noOfValidKbers ( int K , int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; while ( K != 0 ) { set . add ( K % 10 ) ; K = K / 10 ; } int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { int no = arr [ i ] ; boolean flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! set . contains ( digit ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int K = 12 ; int arr [ ] = { 1 , 12 , 1222 , 13 , 2 } ; System . out . println ( noOfValidKbers ( K , arr ) ) ; } }
Queries to find the minimum index in a range [ L , R ] having at least value X with updates | Java program to implement the above approach ; Stores nodes value of the Tree ; Function to build segment tree ; Base Case ; Find the value of mid ; Update for left subtree ; Update for right subtree ; Update the value at the current index ; Function for finding the index of the first element at least x ; If current range does not lie in query range ; If current range is inside of query range ; Maximum value in this range is less than x ; Finding index of first value in this range ; Update the value of the minimum index ; Find mid of the current range ; Left subtree ; If it does not lie in left subtree ; Function for updating segment tree ; Update the value , we reached leaf node ; Find the mid ; If pos lies in the left subtree ; pos lies in the right subtree ; Update the maximum value in the range ; Function to print the answer for the given queries ; Build segment tree ; Find index of first value atleast 2 in range [ 0 , n - 1 ] ; Update value at index 2 to 5 ; Find index of first value atleast 4 in range [ 0 , n - 1 ] ; Find index of first value atleast 0 in range [ 0 , n - 1 ] ; Driver code ; Function Call
import java . util . * ; class GFG { static int maxN = 100 ; static int Tree [ ] = new int [ 4 * maxN ] ; static void build ( int arr [ ] , int index , int s , int e ) { if ( s == e ) Tree [ index ] = arr [ s ] ; else { int m = ( s + e ) / 2 ; build ( arr , 2 * index , s , m ) ; build ( arr , 2 * index + 1 , m + 1 , e ) ; Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static int atleast_x ( int index , int s , int e , int ql , int qr , int x ) { if ( ql > e qr < s ) return - 1 ; if ( s <= ql && e <= qr ) { if ( Tree [ index ] < x ) return - 1 ; while ( s != e ) { int m = ( s + e ) / 2 ; if ( Tree [ 2 * index ] >= x ) { e = m ; index = 2 * index ; } else { s = m + 1 ; index = 2 * index + 1 ; } } return s ; } int m = ( s + e ) / 2 ; int val = atleast_x ( 2 * index , s , m , ql , qr , x ) ; if ( val != - 1 ) return val ; return atleast_x ( 2 * index + 1 , m + 1 , e , ql , qr , x ) ; } static void update ( int index , int s , int e , int new_val , int pos ) { if ( s == e ) Tree [ index ] = new_val ; else { int m = ( s + e ) / 2 ; if ( pos <= m ) { update ( 2 * index , s , m , new_val , pos ) ; } else { update ( 2 * index + 1 , m + 1 , e , new_val , pos ) ; } Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static void printAnswer ( int [ ] arr , int n ) { build ( arr , 1 , 0 , n - 1 ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) ) ; arr [ 2 ] = 5 ; update ( 1 , 0 , n - 1 , 5 , 2 ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 , 6 } ; int N = arr . length ; printAnswer ( arr , N ) ; } }
Smallest Subtree with all the Deepest Nodes | Java program to implement the above approach ; Structure of a Node ; Function to return depth of the Tree from root ; If current node is a leaf node ; Function to find the root of the smallest subtree consisting of all deepest nodes ; Stores height of left subtree ; Stores height of right subtree ; If height of left subtree exceeds that of the right subtree ; Traverse left subtree ; If height of right subtree exceeds that of the left subtree ; Otherwise ; Return current node ; Driver Code
import java . util . * ; class GFG { static class TreeNode { int val ; TreeNode left ; TreeNode right ; TreeNode ( int data ) { this . val = data ; left = null ; right = null ; } } ; static int find_ht ( TreeNode root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; return Math . max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1 ; } static TreeNode find_node ( TreeNode root , TreeNode req_node ) { if ( root == null ) return req_node ; int left_ht = find_ht ( root . left ) ; int right_ht = find_ht ( root . right ) ; if ( left_ht > right_ht ) { req_node = find_node ( root . left , req_node ) ; } else if ( right_ht > left_ht ) { req_node = find_node ( root . right , req_node ) ; } else { req_node = root ; return req_node ; } return req_node ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 1 ) ; root . left = new TreeNode ( 2 ) ; root . right = new TreeNode ( 3 ) ; TreeNode req_node = null ; req_node = find_node ( root , req_node ) ; System . out . print ( req_node . val ) ; } }
Maximum even numbers present in any subarray of size K | Java program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the maximum count of even numbers from all the subarrays of size K ; Generate all subarrays of size K ; Store count of even numbers in current subarray of size K ; Traverse the current subarray ; If current element is an even number ; Update the answer ; Return answer ; Driver Code ; Size of the input array
import java . util . * ; class GFG { static int maxEvenIntegers ( int arr [ ] , int N , int M ) { int ans = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr [ i + j ] % 2 == 0 ) cnt ++ ; } ans = Math . max ( ans , cnt ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 , 6 } ; int K = 3 ; int N = arr . length ; System . out . print ( maxEvenIntegers ( arr , N , K ) + "NEW_LINE"); } }
Count maximum concatenation of pairs from given array that are divisible by 3 | Java program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver code ; To display the result
public class GFG { static int countDiv ( int [ ] arr ) { int rem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; for ( int i : arr ) { int digitSum = 0 ; digitSum += i ; if ( digitSum % 3 == 0 ) { rem0 += 1 ; } else if ( digitSum % 3 == 1 ) { rem1 += 1 ; } else { rem2 += 1 ; } } return ( rem0 / 2 + Math . min ( rem1 , rem2 ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 2 , 8 , 7 } ; System . out . println ( countDiv ( arr ) ) ; } }
Maximum Sum Subsequence | Java program to implement the above approach ; Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver code
import java . util . * ; class GFG { static int MaxNonEmpSubSeq ( int a [ ] , int n ) { int sum = 0 ; int max = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 11 , - 4 , 2 , - 3 , - 10 } ; int N = arr . length ; System . out . println ( MaxNonEmpSubSeq ( arr , N ) ) ; } }
Count numbers up to N having Kth bit set | Java program for above approach ; Function to return the count of number of 1 's at ith bit  in a range [1, n - 1] ; Store count till nearest power of 2 less than N ; If K - th bit is set in N ; Add to result the nearest power of 2 less than N ; Return result ; Driver code ; Function Call
class GFG { static long getcount ( long n , int k ) { long res = ( n >> ( k + 1 ) ) << k ; if ( ( ( n >> k ) & 1 ) != 0 ) res += n & ( ( 1 << k ) - 1 ) ; return res ; } public static void main ( String [ ] args ) { long N = 14 ; int K = 2 ; System . out . println ( getcount ( N + 1 , K ) ) ; } }
Check if a string can be split into two substrings such that one substring is a substring of the other | Java program to implement the above approach ; Function to check if a String can be divided into two subStrings such that one subString is subString of the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Driver Code ; Given String , S ; Store the size of S ; Function Call
import java . util . * ; class GFG { static void splitString ( String S , int N ) { char c = S . charAt ( N - 1 ) ; int f = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S . charAt ( i ) == c ) { f = 1 ; break ; } } if ( f > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String S = " abcdab " ; int N = S . length ( ) ; splitString ( S , N ) ; } }
Find the light bulb with maximum glowing time | Java program for the above approach ; Function to find the bulb having maximum glow ; Initialize variables ; Traverse the array consisting of glowing time of the bulbs ; For 1 st bulb ; Calculate the glowing time ; Update the maximum glow ; Find lexicographically largest bulb ; Bulb with maximum time ; Return the resultant bulb ; Driver Code ; Function call
import java . util . * ; class GFG { static char longestLastingBulb ( int [ ] onTime , char [ ] s ) { char ans ; int n = onTime . length ; int maxDur = Integer . MIN_VALUE ; int maxPos = Integer . MIN_VALUE ; int currentDiff = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { currentDiff = onTime [ i ] ; maxDur = currentDiff ; maxPos = i ; } else { currentDiff = onTime [ i ] - onTime [ i - 1 ] ; if ( maxDur < currentDiff ) { maxDur = currentDiff ; maxPos = i ; } else { if ( maxDur == currentDiff ) { char one = s [ i ] ; char two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } public static void main ( String [ ] args ) { String S = " spuda " ; int [ ] arr = { 12 , 23 , 36 , 46 , 62 } ; System . out . print ( longestLastingBulb ( arr , S . toCharArray ( ) ) ) ; } }
Find a pair of intersecting ranges from a given array | Java program for the above approach ; Store ranges and their corresponding array indices ; Function to find a pair of intersecting ranges ; Stores ending point of every range ; Stores the maximum ending point obtained ; Iterate from 0 to N - 1 ; Starting point of the current range ; End point of the current range ; Push pairs into tup ; Sort the tup vector sort ( tup . begin ( ) , tup . end ( ) ) ; ; Iterate over the ranges ; If starting points are equal ; Print the indices of the intersecting ranges ; If no such pair of segments exist ; Driver Code ; Given N ; Given 2d ranges [ ] [ ] array ; Function call
import java . util . ArrayList ; import java . util . Collections ; import java . util . Comparator ; class GFG { static ArrayList < int [ ] > tup = new ArrayList < > ( ) ; static void findIntersectingRange ( int N , int ranges [ ] [ ] ) { int curr ; int currPos ; for ( int i = 0 ; i < N ; i ++ ) { int x , y ; x = ranges [ i ] [ 0 ] ; y = ranges [ i ] [ 1 ] ; int [ ] arr = { x , y , i + 1 } ; tup . add ( arr ) ; } Collections . sort ( tup , new Comparator < int [ ] > ( ) { public int compare ( int [ ] a , int [ ] b ) { if ( a [ 0 ] == b [ 0 ] ) { if ( a [ 1 ] == b [ 1 ] ) { return a [ 2 ] - b [ 2 ] ; } else { return a [ 1 ] - b [ 1 ] ; } } return a [ 0 ] - b [ 0 ] ; } } ) ; curr = tup . get ( 0 ) [ 1 ] ; currPos = tup . get ( 0 ) [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { int Q = tup . get ( i - 1 ) [ 0 ] ; int R = tup . get ( i ) [ 0 ] ; if ( Q == R ) { if ( tup . get ( i - 1 ) [ 1 ] < tup . get ( i ) [ 1 ] ) System . out . print ( tup . get ( i - 1 ) [ 2 ] + " ▁ " + tup . get ( i ) [ 2 ] ) ; else System . out . print ( tup . get ( i ) [ 2 ] + " ▁ " + tup . get ( i - 1 ) [ 2 ] ) ; return ; } int T = tup . get ( i ) [ 1 ] ; if ( T <= curr ) { System . out . print ( tup . get ( i ) [ 2 ] + " ▁ " + currPos ) ; return ; } else { curr = T ; currPos = tup . get ( i ) [ 2 ] ; } } System . out . print ( " - 1 ▁ - 1" ) ; } public static void main ( String [ ] args ) { int N = 5 ; int ranges [ ] [ ] = { { 1 , 5 } , { 2 , 10 } , { 3 , 10 } , { 2 , 2 } , { 2 , 15 } } ; findIntersectingRange ( N , ranges ) ; } }
Find the next greater element in a Circular Array | Set 2 | Java program for the above approach ; Function to find the NGE for the given circular array arr [ ] ; Initialize stack and nge [ ] array ; Initialize nge [ ] array to - 1 ; Traverse the array ; If stack is not empty and current element is greater than top element of the stack ; Assign next greater element for the top element of the stack ; Pop the top element of the stack ; Print the nge [ ] array ; Driver Code ; Function Call
import java . util . * ; class GFG { static void printNGE ( int arr [ ] , int n ) { Stack < Integer > s = new Stack < > ( ) ; int nge [ ] = new int [ n ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { nge [ i ] = - 1 ; } i = 0 ; while ( i < 2 * n ) { while ( ! s . isEmpty ( ) && arr [ i % n ] > arr [ s . peek ( ) ] ) { nge [ s . peek ( ) ] = arr [ i % n ] ; s . pop ( ) ; } s . push ( i % n ) ; i ++ ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( nge [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , - 2 , 5 , 8 } ; int N = arr . length ; printNGE ( arr , N ) ; } }
Count array elements whose product of digits is a Composite Number | Java program for the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to calculate the product of digits of the given number ; Stores the product of digits ; Extract digits and add to the sum ; Return the product of its digits ; Function to print number of distinct values with digit product as composite ; Initialize set ; Initialize boolean array ; Pre - compute primes ; Traverse array ; Stores the product of digits of the current array element ; If Product of digits is less than or equal to 1 ; If Product of digits is not a prime ; Print the answer ; Driver Code ; Given array ; Given size ; Function call
import java . io . * ; import java . util . * ; class GFG { static int N = 100005 ; static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int digitProduct ( int number ) { int res = 1 ; while ( number > 0 ) { res *= ( number % 10 ) ; number /= 10 ; } return res ; } static void DistinctCompositeDigitProduct ( int arr [ ] , int n ) { TreeSet < Integer > output = new TreeSet < Integer > ( ) ; boolean prime [ ] = new boolean [ N + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , N ) ; for ( int i = 0 ; i < n ; i ++ ) { int ans = digitProduct ( arr [ i ] ) ; if ( ans <= 1 ) { continue ; } if ( ! prime [ ans ] ) { output . add ( ans ) ; } } System . out . print ( output . size ( ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 } ; int n = arr . length ; DistinctCompositeDigitProduct ( arr , n ) ; } }