Count of distinct GCDs among all the non-empty subsequences of given array
Last Updated :
05 Jul, 2021
Given an integer array arr[] of size N, the task is to calculate the total number of distinct Greatest Common Divisors(GCDs) among all the non-empty subsequences of arr[].
Examples:
Input: arr[]={3,4,8} N=3
Output:
4
Explanation:
The different non-empty subsequences possible are {3}, {4}, {8}, {3,4}, {4,8}, {3,8}, {3,4,8} and their corresponding GCDs are 3,4,8,1,4,1,1.
Thus, the total number of distinct GCDs is 4 (1,3,4,8).
Input: arr[]={3,11,14,6,12}, N=5
Output:
7
Naive Approach: The naive approach is to find all subsequences, calculate the GCDs for each of them, and finally, calculating the total number of distinct GCDs.
Time Complexity: O(2N.Log(M)), where M is the largest element in the array.
Approach: The given problem can be solved based on the following observations:
- The largest possible GCD cannot exceed the largest element of the array(say M). Therefore, all possible GCDs lie between 1 to M.
- By iterating over all possible values of GCD i.e from 1 to M, and checking whether there are any multiples of i which are present in the array and have a GCD equal to i, the problem can be solved.
Follow the steps below to solve the problem:
- Initialize a variable ans to 0.
- Calculate and store the maximum element in arr in a variable, say M.
- Store all the elements of arr in a map Mp for constant-time access.
- Iterate through all possible values of GCD i.e from 1 to M, using the variable i, and do the following:
- Iterate through the multiples of M present in arr, using the variable j, and do the following:
- If the GCD becomes i at any point, increment ans and break.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int distinctGCDs( int arr[], int N)
{
int M = -1, ans = 0;
map< int , int > Mp;
for ( int i = 0; i < N; i++) {
M = max(M, arr[i]);
Mp[arr[i]] = 1;
}
for ( int i = 1; i <= M; i++) {
int currGcd = 0;
for ( int j = i; j <= M; j += i) {
if (Mp[j]) {
currGcd = __gcd(currGcd, j);
if (currGcd == i) {
ans++;
break ;
}
}
}
}
return ans;
}
int main()
{
int arr[] = { 3, 11, 14, 6, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << distinctGCDs(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
static int distinctGCDs( int []arr, int N)
{
int M = - 1 , ans = 0 ;
HashMap<Integer,
Integer> Mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
M = Math.max(M, arr[i]);
if (Mp.containsKey(arr[i]))
Mp.put(arr[i], 1 );
else
Mp.put(arr[i], 0 );
}
for ( int i = 1 ; i <= M; i++)
{
int currGcd = 0 ;
for ( int j = i; j <= M; j += i)
{
if (Mp.containsKey(j))
{
currGcd = gcd(currGcd, j);
if (currGcd == i)
{
ans++;
break ;
}
}
}
}
return ans;
}
public static void main(String [] args)
{
int []arr = { 3 , 11 , 14 , 6 , 12 };
int N = arr.length;
System.out.println(distinctGCDs(arr, N));
}
}
|
Python3
from math import gcd
def distinctGCDs(arr, N):
M = - 1
ans = 0
Mp = {}
for i in range (N):
M = max (M, arr[i])
Mp[arr[i]] = 1
for i in range ( 1 , M + 1 , 1 ):
currGcd = 0
for j in range (i, M + 1 , i):
if (j in Mp):
currGcd = gcd(currGcd, j)
if (currGcd = = i):
ans + = 1
break
return ans
if __name__ = = '__main__' :
arr = [ 3 , 11 , 14 , 6 , 12 ]
N = len (arr)
print (distinctGCDs(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
static int distinctGCDs( int []arr, int N)
{
int M = -1, ans = 0;
Dictionary< int ,
int > Mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
M = Math.Max(M, arr[i]);
if (Mp.ContainsKey(arr[i]))
Mp[arr[i]] = 1;
else
Mp.Add(arr[i],1);
}
for ( int i = 1; i <= M; i++)
{
int currGcd = 0;
for ( int j = i; j <= M; j += i)
{
if (Mp.ContainsKey(j))
{
currGcd = gcd(currGcd, j);
if (currGcd == i)
{
ans++;
break ;
}
}
}
}
return ans;
}
public static void Main()
{
int []arr = { 3, 11, 14, 6, 12 };
int N = arr.Length;
Console.Write(distinctGCDs(arr, N));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function distinctGCDs(arr, N)
{
let M = -1, ans = 0;
var Mp = new Map();
for (let i = 0; i < N; i++) {
M = Math.max(M, arr[i]);
Mp.set(arr[i], 1);
}
for (let i = 1; i <= M; i++) {
let currGcd = 0;
for (let j = i; j <= M; j += i) {
if (Mp.has(j)) {
currGcd = gcd(currGcd, j);
if (currGcd == i) {
ans++;
break ;
}
}
}
}
return ans;
}
let arr = [3, 11, 14, 6, 12];
let N = arr.length;
document.write(distinctGCDs(arr, N) + '<br>' );
</script>
|
Time Complexity: O(M*log(M)), where M is the largest element in the array
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...