Largest sub-set possible for an array satisfying the given condition
Last Updated :
05 Jan, 2023
Given an array arr[] and an integer K. The task is to find the size of the maximum sub-set such that every pair from the sub-set (X, Y) is of the form Y != (X * K) where X < Y.
Examples:
Input: arr[] = {2, 3, 6, 5, 4, 10}, K = 2
Output: 3
{2, 3, 5} is the required sub-set
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, K = 2
Output: 6
Approach:
- Sort all the array elements.
- Create an empty set of integers S, which will hold the elements for the sub-set.
- Traverse the sorted array, and for each integer x in the array:
- If x % k = 0 or x / k is not already present in S then insert x into S.
- Else discard x and check the next element.
- Print the size of the set S in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sizeSubSet( int a[], int k, int n)
{
sort(a, a + n);
unordered_set< int > s;
for ( int i = 0; i < n; i++) {
if (a[i] % k != 0 || s.count(a[i] / k) == 0)
s.insert(a[i]);
}
return s.size();
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = sizeof (a) / sizeof (a[0]);
int k = 2;
cout << sizeSubSet(a, k, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int sizeSubSet( int a[], int k, int n)
{
Arrays.sort(a);
HashMap< Integer, Integer> s = new HashMap< Integer, Integer>();
for ( int i = 0 ; i < n; i++)
{
if (a[i] % k != 0 || s.get(a[i] / k) == null )
s.put(a[i], s.get(a[i]) == null ? 1 : s.get(a[i]) + 1 );
}
return s.size();
}
public static void main(String args[])
{
int a[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int n = a.length;
int k = 2 ;
System.out.println( sizeSubSet(a, k, n));
}
}
|
Python3
import math as mt
def sizeSubSet(a, k, n):
a.sort()
s = set ()
for i in range (n):
if (a[i] % k ! = 0 or a[i] / / k not in s):
s.add(a[i])
return len (s)
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
n = len (a)
k = 2
print (sizeSubSet(a, k, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int sizeSubSet( int []a, int k, int n)
{
Array.Sort(a);
Dictionary< int ,
int > s = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (a[i] % k != 0 || !s.ContainsKey(a[i] / k))
{
if (s.ContainsKey(a[i]))
{
var val = s[a[i]];
s.Remove(a[i]);
s.Add(a[i], val + 1);
}
else
{
s.Add(a[i], 1);
}
}
}
return s.Count;
}
public static void Main(String []args)
{
int []a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = a.Length;
int k = 2;
Console.WriteLine(sizeSubSet(a, k, n));
}
}
|
PHP
<?php
function sizeSubSet( $a , $k , $n )
{
sort( $a ) ;
$s = array ();
for ( $i = 0 ; $i < $n ; $i ++)
{
if ( $a [ $i ] % $k != 0 or
!in_array( floor ( $a [ $i ] / $k ), $s ))
array_push ( $s , $a [ $i ]);
}
return sizeof( $s );
}
$a = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
$n = sizeof( $a );
$k = 2;
echo sizeSubSet( $a , $k , $n );
?>
|
Javascript
<script>
function sizeSubSet(a, k, n)
{
a.sort( function (a, b){ return a - b;});
let s = new Map();
for (let i = 0; i < n; i++)
{
if (a[i] % k != 0 ||
s.get(a[i] / k) == null )
s.set(a[i], s.get(a[i]) == null ?
1 : s.get(a[i]) + 1);
}
return s.size;
}
let a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
let n = a.length;
let k = 2;
document.write(sizeSubSet(a, k, n));
</script>
|
Time Complexity: O(n*log(n)), As we are sorting the array
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...