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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
9
Recommended Posts:
- Count unique subsequences of length K
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Number of palindromic subsequences of length k where k <= 3
- Number of K length subsequences with minimum sum
- Number of subsequences of maximum length K containing no repeated elements
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Number of ways of choosing K equal substrings of any length for every query
- Append a digit in the end to make the number equal to the length of the remaining string
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number with duplicates allowed
- Find all combinations of two equal sum subsequences
- Count number of distinct substrings of a given length
- Count number of binary strings of length N having only 0's and 1's
- Count number of subsets of a set with GCD equal to a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.