Check if the product of every contiguous subsequence is different or not in a number | Java implementation of the approach ; Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Utility reverse funciton ; Driver code
import java . util . * ; class GFG { static boolean productsDistinct ( int N ) { String s = " " ; while ( N > 0 ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } s = reverse ( s ) ; int sz = s . length ( ) ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s . charAt ( j ) - '0' ) ; if ( se . contains ( product ) ) return false ; else se . add ( product ) ; } } return true ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int N = 2345 ; if ( productsDistinct ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }