Check if the product of every contiguous subsequence is different or not in a number
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
Set |
Product |
{2} |
2 |
{2, 3} |
2 * 3 = 6 |
{2, 3, 4} |
2 * 3 * 4 = 24 |
{3} |
3 |
{3, 4} |
3 * 4 = 12 |
{4} |
4 |
All the products 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++
#include <bits/stdc++.h>
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;
}
|
Java
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" );
}
}
|
Python3
def productsDistinct(A):
A = str (A)
n = len (A)
hs = set ()
for i in range (n):
prod = 1
for j in range (i, n):
prod = prod * int (A[j])
if (prod in hs):
return False
else :
hs.add(prod)
return True
if __name__ = = '__main__' :
N = 2345
if (productsDistinct(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
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< int > se = new HashSet< int >();
for ( int i = 0; i < sz; i++)
{
int product = 1;
for ( int j = i; j < sz; j++)
{
product *= ( int )(s[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.Join( "" ,a);
}
public static void Main(String[] args)
{
int N = 2345;
if (productsDistinct(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function productsDistinct(N) {
const s = N.toString();
const sz = s.length;
const products = new Set();
for (let i = 0; i < sz; i++) {
let product = 1;
for (let j = i; j < sz; j++) {
product *= Number(s[j]);
if (products.has(product)) return false ;
else products.add(product);
}
}
return true ;
}
const N = 2345;
if (productsDistinct(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O((log10N)2*log(log10N))
Auxiliary Space: O((log10N)2)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...