Check if the product of every contiguous subsequence is different or not in a number | C ++ 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 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool productsDistinct ( int N ) { string s = " " ; while ( N ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } reverse ( s . begin ( ) , s . end ( ) ) ; int sz = s . size ( ) ; set < int > se ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s [ j ] - '0' ) ; if ( se . find ( product ) != se . end ( ) ) return false ; else se . insert ( product ) ; } } return true ; } int main ( ) { int N = 2345 ; if ( productsDistinct ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }