Check if the product of every contiguous subsequence is different or not in a number - GeeksforGeeks

09 Jun, 2021
Given an integer N, the task is to check if the product of every consecutive set of digits is distinct or not.
Examples: 
Input: N = 234 Output: Yes  
All the productas are distinct.
Input: N = 1234 Output: No Set {1, 2} and {2} both the same product i.e. 2.  
Approach: Store the product of digits of every contiguous subsequence in a set. If the product to be inserted is already present in the set at any point then the answer is “No” else all the product are distinct in the end.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if the product// of every digit of a contiguous subsequence// is distinctbool productsDistinct(int N){    // To store the given number as a string    string s = "";     // Append all the digits    // starting from the end    while (N) {        s += (char)(N % 10 + '0');        N /= 10;    }     // Reverse the string to get    // the  original number    reverse(s.begin(), s.end());     // Store size of the string    int sz = s.size();     // Set to store product of    // each contiguous subsequence    set<int> se;     // Find product of every    // contiguous subsequence    for (int i = 0; i < sz; i++) {        int product = 1;        for (int j = i; j < sz; j++) {            product *= (int)(s[j] - '0');             // If current product already            // exists in the set            if (se.find(product) != se.end())                return false;            else                se.insert(product);        }    }     return true;} // Driver codeint main(){    int N = 2345;     if (productsDistinct(N))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function that returns true if// the product of every digit of a// contiguous subsequence is distinctstatic boolean productsDistinct(int N){         // To store the given number    // as a string    String s = "";     // Append all the digits    // starting from the end    while (N > 0)    {        s += (char)(N % 10 + '0');        N /= 10;    }     // Reverse the string to get    // the original number    s = reverse(s);     // Store size of the string    int sz = s.length();     // Set to store product of    // each contiguous subsequence    HashSet<Integer> se = new HashSet<Integer>();     // Find product of every    // contiguous subsequence    for (int i = 0; i < sz; i++)    {        int product = 1;        for (int j = i; j < sz; j++)        {            product *= (int)(s.charAt(j) - '0');             // If current product already            // exists in the set            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--)    {        // Swap values of l and r        char temp = a[l];        a[l] = a[r];        a[r] = temp;    }    return String.valueOf(a);} // Driver codepublic static void main(String[] args){    int N = 2345;     if (productsDistinct(N))        System.out.println("Yes");    else        System.out.println("No");    }} // This code is contributed// by PrinciRaj1992
# Python 3 implementation of the approach # Function that returns true if the product# of every digit of a contiguous subsequence# is distinctdef productsDistinct(N):          # To store the given number as a string    s = ""     # Append all the digits    # starting from the end    while (N):        s += chr(N % 10 + ord('0'))        N //= 10     # Reverse the string to get    # the original number    s = s[::-1]     # Store size of the string    sz = len(s)     # Set to store product of    # each contiguous subsequence    se = []     # Find product of every    # contiguous subsequence    for i in range(sz):        product = 1        for j in range(i, sz, 1):            product *= ord(s[j]) - ord('0')             # If current product already            # exists in the set            for p in range(len(se)):                if se[p] == product:                    return False                else:                    se.append(product)     return True # Driver codeif __name__ == '__main__':    N = 2345     if (productsDistinct(N)):        print("Yes")    else:        print("No")         # This code is contributed by# Surendra_Gangwar
// C# implementation of the approachusing System;using System.Collections.Generic; class GFG{ // Function that returns true if// the product of every digit of a// contiguous subsequence is distinctstatic Boolean productsDistinct(int N){         // To store the given number    // as a string    String s = "";     // Append all the digits    // starting from the end    while (N > 0)    {        s += (char)(N % 10 + '0');        N /= 10;    }     // Reverse the string to get    // the original number    s = reverse(s);     // Store size of the string    int sz = s.Length;     // Set to store product of    // each contiguous subsequence    HashSet<int> se = new HashSet<int>();     // Find product of every    // contiguous subsequence    for (int i = 0; i < sz; i++)    {        int product = 1;        for (int j = i; j < sz; j++)        {            product *= (int)(s[j] - '0');             // If current product already            // exists in the set            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--)    {        // Swap values of l and r        char temp = a[l];        a[l] = a[r];        a[r] = temp;    }    return String.Join("",a);} // Driver codepublic static void Main(String[] args){    int N = 2345;     if (productsDistinct(N))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}} // This code is contributed by 29AjayKumar
<script> // Javascript implementation of the approach // Function that returns true if the product// of every digit of a contiguous subsequence// is distinctfunction productsDistinct(N){         // To store the given number as a string    var s = "";     // Append all the digits    // starting from the end    while (N)    {        s += String.fromCharCode(            N % 10 + '0'.charCodeAt(0));        N = parseInt(N / 10);    }     // Reverse the string to get    // the  original number    s = s.split('').reverse().join('');     // Store size of the string    var sz = s.length;     // Set to store product of    // each contiguous subsequence    var se = new Set();     // Find product of every    // contiguous subsequence    for(var i = 0; i < sz; i++)    {        var product = 1;        for(var j = i; j < sz; j++)        {            product *= (s[j].charCodeAt(0) -                         '0'.charCodeAt(0));             // If current product already            // exists in the set            if (se.has(product))                return false;            else                se.add(product);        }    }    return true;} // Driver codevar N = 2345;if (productsDistinct(N))    document.write("Yes");else    document.write("No"); // This code is contributed by rrrtnx </script>
Yes
 
princiraj1992
29AjayKumar
SURENDRA_GANGWAR
rrrtnx
subsequence
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Count ways to reach the n'th stair
Find first and last digits of a number
Find Union and Intersection of two unsorted arrays
Complexity Analysis of Binary Search