Check if all sub | Java program to check if all sub - numbers have distinct Digit product ; Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; List to maintain digit products ; Finding all possible subarrays ; Driver code
import java . io . * ; import java . util . * ; public class GFG { static int digitProduct ( int [ ] digits , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } static boolean isDistinct ( int N ) { String s = " " + N ; int len = s . length ( ) ; int [ ] digits = new int [ len ] ; ArrayList < Integer > products = new ArrayList < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = Integer . parseInt ( " " + s . charAt ( i ) ) ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . contains ( val ) ) return false ; else products . add ( val ) ; } } return true ; } public static void main ( String args [ ] ) { int N = 324 ; if ( isDistinct ( N ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }