Given an array **arr[]** consisting of **N** distinct integers and an integer **K**, the task is to find the maximum size of a subset possible such that no element in the subset is **K** times any other element of the subset(i.e. no such pair **{n, m}** should be present in the subset such that either **m = n * K** or **n = m * K**).**Examples:**

Input:arr[] = {2, 8, 6, 5, 3}, K = 2Output:4Explanation:

Only possible pair existing in the array with an element being K( = 2) times the other is {6, 3}.

Hence, all possible subsets which does not contain both the elements of the pair {6, 3} together can be considered.

Therefore, the longest possible subset can be of length 4.Input:arr[] = {1, 4, 3, 2}, K = 3output:3

**Approach:**

Follow the steps below to solve the problem:

- Find the number of pairs possible such that one element is K times the other from the given array
- Sort the array in
**increasing order**of elements. - Traverse the array and store the frequenciindices of array elements in Map.
- Initialize an array
**visited**to mark for every index, whether that element is included(**0**) or not(**1**) in the subset. - Traverse the array again and for every index having
**vis[i] = 0**, check if**arr[i] * K**is present in the**Map**or not. If found, then increase the**count of pairs**and set**vis[mp[arr[i] * K]] = 1**. - Finally, print
**N – count of pairs**as the answer.

Below is implementation of above approach:

## C++

`// C++ implementation of ` `// the above aproach ` `#include <bits/stdc++.h> ` `#define ll long long ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// size of the required subset ` `int` `findMaxLen(vector<ll>& a, ll k) ` `{ ` ` ` ` ` `// Size of the array ` ` ` `int` `n = a.size(); ` ` ` ` ` `// Sort the array ` ` ` `sort(a.begin(), a.end()); ` ` ` ` ` `// Stores which index is ` ` ` `// included or excluded ` ` ` `vector<` `bool` `> vis(n, 0); ` ` ` ` ` `// Stores the indices of ` ` ` `// array elements ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `mp[a[i]] = i; ` ` ` `} ` ` ` ` ` `// Count of pairs ` ` ` `int` `c = 0; ` ` ` ` ` `// Iterate through all ` ` ` `// the element ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// If element is included ` ` ` `if` `(vis[i] == ` `false` `) { ` ` ` `int` `check = a[i] * k; ` ` ` ` ` `// Check if a[i] * k is present ` ` ` `// in the array or not ` ` ` `if` `(mp.find(check) != mp.end()) { ` ` ` ` ` `// Increase count of pair ` ` ` `c++; ` ` ` ` ` `// Exclude the pair ` ` ` `vis[mp[check]] = ` `true` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `n - c; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `K = 3; ` ` ` `vector<ll> arr = { 1, 4, 3, 2 }; ` ` ` ` ` `cout << findMaxLen(arr, K); ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# the above approach ` ` ` `# Function to find the maximum ` `# size of the required subset ` `def` `findMaxLen(a, k): ` ` ` ` ` `# Size of the array ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `# Sort the array ` ` ` `a.sort() ` ` ` ` ` `# Stores which index is ` ` ` `# included or excluded ` ` ` `vis ` `=` `[` `0` `] ` `*` `n ` ` ` ` ` `# Stores the indices of ` ` ` `# array elements ` ` ` `mp ` `=` `{} ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `mp[a[i]] ` `=` `i ` ` ` ` ` `# Count of pairs ` ` ` `c ` `=` `0` ` ` ` ` `# Iterate through all ` ` ` `# the element ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# If element is included ` ` ` `if` `(vis[i] ` `=` `=` `False` `): ` ` ` `check ` `=` `a[i] ` `*` `k ` ` ` ` ` `# Check if a[i] * k is present ` ` ` `# in the array or not ` ` ` `if` `(check ` `in` `mp.keys()): ` ` ` ` ` `# Increase count of pair ` ` ` `c ` `+` `=` `1` ` ` ` ` `# Exclude the pair ` ` ` `vis[mp[check]] ` `=` `True` ` ` ` ` `return` `n ` `-` `c ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `K ` `=` `3` ` ` `arr ` `=` `[ ` `1` `, ` `4` `, ` `3` `, ` `2` `] ` ` ` ` ` `print` `(findMaxLen(arr, K)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N) **Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest subset where absolute difference of any two element is a power of 2
- Largest element in the array that is repeated exactly k times
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Largest divisible subset in array
- Size of the largest divisible subset in an Array
- Find the Largest divisor Subset in the Array
- Largest divisible pairs subset
- Largest subset with maximum difference as 1
- Largest subset with M as smallest missing number
- Largest subset having with sum less than equal to sum of respective indices
- Largest subset whose all elements are Fibonacci numbers
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- First element occurring k times in an array
- Reduce the array such that each element appears at most 2 times
- Smallest element in an array that is repeated exactly 'k' times.
- Check whether K times of a element is present in array
- Unique element in an array where all elements occur k times except one
- Find Second largest element in an array | Set 2
- Third largest element in an array of distinct elements
- Kth smallest or largest element in unsorted Array | Set 4

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.