Check if all sub | C ++ 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 ; set to maintain digit products ; Finding all possible subarrays ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitProduct ( int digits [ ] , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } bool isDistinct ( int N ) { string s = to_string ( N ) ; int len = s . length ( ) ; int digits [ len ] ; unordered_set < int > products ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = s [ i ] - '0' ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . find ( val ) != products . end ( ) ) return false ; else products . insert ( val ) ; } } return true ; } int main ( ) { int N = 324 ; if ( isDistinct ( N ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }