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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
#include <bits/stdc++.h>
using namespace std;
  
// Returns factorial of n
long long fact(int n)
{
    long long res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Returns nCr for the
// given values of r and n
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 to store the frequencies
    // of each elements
    map<int, int> m;
  
    // Loop to store the
    // frequencies of elements
    // in the map
    for (int i = 0; i < n; i++) {
        m[arr[i]]++;
    }
  
    for (auto j : m) {
  
        // Using nCR formula to
        // calculate the number
        // of subsequences of a
        // given length
        s = s + 1LL * nCr(j.second, k);
    }
  
    return s;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 1, 1, 1, 2, 2, 2 };
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function calling
    cout << number_of_subsequences(arr, k, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation for above approach
import java.util.*;
      
class GFG
{
  
// Returns factorial of n
static long fact(int n)
{
    long res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Returns nCr for the
// given values of r and n
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;
  
    // Map to store the frequencies
    // of each elements
    HashMap<Integer, 
            Integer> mp = new HashMap<Integer, 
                                      Integer>();
  
    // Loop to store the
    // frequencies of elements
    // in the map
    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())
    {
  
        // Using nCR formula to
        // calculate the number
        // of subsequences of a
        // given length
        s = s + 1 * nCr(j.getValue(), k);
    }
  
    return s;
}
  
// Driver Code
static public void main ( String []arg)
{
    int arr[] = { 1, 1, 1, 1, 2, 2, 2 };
    int k = 2;
    int n = arr.length;
  
    // Function calling
    System.out.println(number_of_subsequences(arr, k, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# Returns factorial of n
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
    return res
  
# Returns nCr for the
# given values of r and n
def nCr(n, r):
    return fact(n) // (fact(r) * fact(n - r))
  
def number_of_subsequences(arr, k, n):
  
    s = 0
  
    # Map to store the frequencies
    # of each elements
    m = dict()
   
    # Loop to store the
    # frequencies of elements
    # in the map
    for i in arr:
        m[i] = m.get(i, 0) + 1
  
    for j in m:
  
        # Using nCR formula to
        # calculate the number
        # of subsequences of a
        # given length
        s = s + nCr(m[j], k)
  
    return s
  
# Driver Code
arr = [1, 1, 1, 1, 2, 2, 2]
k = 2
n = len(arr)
  
# Function calling
print(number_of_subsequences(arr, k, n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation for above approach
using System;
using System.Collections.Generic;
      
public class GFG
{
   
// Returns factorial of n
static long fact(int n)
{
    long res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
   
// Returns nCr for the
// given values of r and n
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;
   
    // Map to store the frequencies
    // of each elements
    Dictionary<int,int> mp = new Dictionary<int,int>();
    // Loop to store the
    // frequencies of elements
    // in the map
    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)
    {
   
        // Using nCR formula to
        // calculate the number
        // of subsequences of a
        // given length
        s = s + 1 * nCr(j.Value, k);
    }
   
    return s;
}
   
// Driver Code
static public void Main ( String []arg)
{
    int []arr = { 1, 1, 1, 1, 2, 2, 2 };
    int k = 2;
    int n = arr.Length;
   
    // Function calling
    Console.Write(number_of_subsequences(arr, k, n));
}
}
// This code is contributed by 29AjayKumar

chevron_right


Output:

9


My Personal Notes arrow_drop_up

Recommended Posts: