Count the number of subsequences of length k having equal LCM and HCF
Given an array Arr and an integer K. The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.
Examples:
Input: Arr = {1, 2, 2, 3, 3}, K = 2
Output: 2
Subsequences are – {2, 2} and {3, 3}
Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3
Output: 4
Approach:
LCM and HCF ( GCD ) are equal for a group of numbers when all the numbers are the same.
It can be proved in the following manner. Let’s take a case for two numbers.
Let the two numbers be x and y
HCF(x, y) = LCM(x, y) = k (say)
Since HCF(x, y) = k,
x = kn and, y = km, for some natural numbers m, n
We know, HCF × LCM = Product of the two numbers
Therefore, k2 = km × kn
Thus, mn = 1
Therefore, m = n = 1, since m, n are natural numbers
As a result,
x = kn = k, and, y = km = k
Implies, x = y = k, i.e. the numbers must be equal.
This concept can be extended to a group of numbers. Thus, it is proved that the numbers which are same have equal GCD and LCM.
After that, we need to find the frequencies of each of the elements in the array with the help of a map. Then, the formula of combinatorial theory will be used to find the count of subsequences of a particular length K for a particular element with a given frequency. This concept will be applied to all the elements with given frequencies and summation of all would be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long fact( int n)
{
long long res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
long long nCr( int n, int r)
{
return fact(n) / (1LL * fact(r)
* fact(n - r));
}
long long number_of_subsequences( int arr[],
int k,
int n)
{
long long s = 0;
map< int , int > m;
for ( int i = 0; i < n; i++) {
m[arr[i]]++;
}
for ( auto j : m) {
s = s + 1LL * nCr(j.second, k);
}
return s;
}
int main()
{
int arr[] = { 1, 1, 1, 1, 2, 2, 2 };
int k = 2;
int n = sizeof (arr) / sizeof (arr[0]);
cout << number_of_subsequences(arr, k, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long fact( int n)
{
long res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
static long nCr( int n, int r)
{
return fact(n) / ( 1 * fact(r) *
fact(n - r));
}
static long number_of_subsequences( int arr[],
int k, int n)
{
long s = 0 ;
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
for (Map.Entry<Integer,
Integer> j : mp.entrySet())
{
s = s + 1 * nCr(j.getValue(), k);
}
return s;
}
static public void main ( String []arg)
{
int arr[] = { 1 , 1 , 1 , 1 , 2 , 2 , 2 };
int k = 2 ;
int n = arr.length;
System.out.println(number_of_subsequences(arr, k, n));
}
}
|
Python3
def fact(n):
res = 1
for i in range ( 2 , n + 1 ):
res = res * i
return res
def nCr(n, r):
return fact(n) / / (fact(r) * fact(n - r))
def number_of_subsequences(arr, k, n):
s = 0
m = dict ()
for i in arr:
m[i] = m.get(i, 0 ) + 1
for j in m:
s = s + nCr(m[j], k)
return s
arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 ]
k = 2
n = len (arr)
print (number_of_subsequences(arr, k, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static long fact( int n)
{
long res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
static long nCr( int n, int r)
{
return fact(n) / (1 * fact(r) *
fact(n - r));
}
static long number_of_subsequences( int []arr,
int k, int n)
{
long s = 0;
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
var val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else
{
mp.Add(arr[i], 1);
}
}
foreach (KeyValuePair< int , int > j in mp)
{
s = s + 1 * nCr(j.Value, k);
}
return s;
}
static public void Main ( String []arg)
{
int []arr = { 1, 1, 1, 1, 2, 2, 2 };
int k = 2;
int n = arr.Length;
Console.Write(number_of_subsequences(arr, k, n));
}
}
|
Javascript
<script>
function fact(n)
{
let res = 1;
for (let i = 2; i <= n; i++)
res = res * i;
return res;
}
function nCr(n,r)
{
return fact(n) / (1 * fact(r) *
fact(n - r));
}
function number_of_subsequences(arr,k,n)
{
let s = 0;
let mp = new Map();
for (let i = 0; i < n; i++)
{
if (mp.has(arr[i]))
{
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.set(arr[i], 1);
}
}
for (let [key, value] of mp.entries())
{
s = s + 1 * nCr(value, k);
}
return s;
}
let arr = [1, 1, 1, 1, 2, 2, 2];
let k = 2;
let n = arr.length;
document.write(number_of_subsequences(arr, k, n));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Last Updated :
13 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...