Largest possible Subset from an Array such that no element is K times any other element in the Subset
Last Updated :
01 Nov, 2022
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 = 2
Output: 4
Explanation:
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 = 3
output: 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 frequency indices 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++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int findMaxLen(vector<ll>& a, ll k)
{
int n = a.size();
sort(a.begin(), a.end());
vector< bool > vis(n, 0);
map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[a[i]] = i;
}
int c = 0;
for ( int i = 0; i < n; ++i) {
if (vis[i] == false ) {
int check = a[i] * k;
if (mp.find(check) != mp.end()) {
c++;
vis[mp[check]] = true ;
}
}
}
return n - c;
}
int main()
{
int K = 3;
vector<ll> arr = { 1, 4, 3, 2 };
cout << findMaxLen(arr, K);
}
|
Java
import java.util.*;
class GFG{
static int findMaxLen( int [] a, int k)
{
int n = a.length;
Arrays.sort(a);
boolean []vis = new boolean [n];
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
mp.put(a[i], i);
}
int c = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (vis[i] == false )
{
int check = a[i] * k;
if (mp.containsKey(check))
{
c++;
vis[mp.get(check)] = true ;
}
}
}
return n - c;
}
public static void main(String[] args)
{
int K = 3 ;
int []arr = { 1 , 4 , 3 , 2 };
System.out.print(findMaxLen(arr, K));
}
}
|
Python3
def findMaxLen(a, k):
n = len (a)
a.sort()
vis = [ 0 ] * n
mp = {}
for i in range (n):
mp[a[i]] = i
c = 0
for i in range (n):
if (vis[i] = = False ):
check = a[i] * k
if (check in mp.keys()):
c + = 1
vis[mp[check]] = True
return n - c
if __name__ = = '__main__' :
K = 3
arr = [ 1 , 4 , 3 , 2 ]
print (findMaxLen(arr, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findMaxLen( int [] a, int k)
{
int n = a.Length;
Array.Sort(a);
bool []vis = new bool [n];
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
mp.Add(a[i], i);
}
int c = 0;
for ( int i = 0; i < n; ++i)
{
if (vis[i] == false )
{
int check = a[i] * k;
if (mp.ContainsKey(check))
{
c++;
vis[mp[check]] = true ;
}
}
}
return n - c;
}
public static void Main(String[] args)
{
int K = 3;
int []arr = { 1, 4, 3, 2 };
Console.Write(findMaxLen(arr, K));
}
}
|
Javascript
<script>
function findMaxLen(a, k)
{
let n = a.length;
a.sort();
let vis = Array.from({length: n}, (_, i) => 0);
let mp = new Map();
for (let i = 0; i < n; i++)
{
mp.set(a[i], i);
}
let c = 0;
for (let i = 0; i < n; ++i)
{
if (vis[i] == false )
{
let check = a[i] * k;
if (mp.has(check))
{
c++;
vis[mp.get(check)] = true ;
}
}
}
return n - c;
}
let K = 3;
let arr = [ 1, 4, 3, 2 ];
document.write(findMaxLen(arr, K));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...