Find the largest possible k-multiple set
Last Updated :
31 May, 2022
Given an array containing distinct positive integers and an integer k. The task is to find the largest possible k-multiple set from the array of given elements.
A set is called a k-multiple set if no two elements of the set i.e., x, y exits such that y = x*k.
There can be multiple answers. You can Print any of them.
Examples:
Input : a[] = {2, 3, 4, 5, 6, 10}, k = 2
Output : {2, 3, 5}
{2, 3, 5}, {2, 3, 10}, {2, 5, 6}, {2, 6, 10}, {3, 4, 5}, {3, 4, 10},
{4, 5, 6}, {4, 6, 10} are possible 2-multiple sets.
Input : a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, k = 2
Output : {1, 3, 4, 5, 7, 9}
Approach: An efficient approach is to sort the given array of elements and traverse through the whole array and push an element x in the set if the set does not contain an element equals to x/k where x is divisible by k.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void K_multiple( int a[], int n, int k)
{
sort(a, a + n);
set< int > s;
for ( int i = 0; i < n; i++) {
if ((a[i] % k == 0 && s.find(a[i] / k) == s.end())
|| a[i] % k != 0)
s.insert(a[i]);
}
for ( auto i = s.begin(); i != s.end(); i++){
cout << *i << " " ;}
}
int main()
{
int a[] = { 2, 3, 4, 5, 6, 10 } ;
int k = 2;
int n = sizeof (a) / sizeof (a[0]);
K_multiple(a, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void K_multiple( int a[], int n, int k)
{
Arrays.sort(a);
HashSet<Integer> s = new HashSet<>();
for ( int i = 0 ; i < n; i++)
{
if ((a[i] % k == 0 && !s.contains(a[i] / k))
|| a[i] % k != 0 )
s.add(a[i]);
}
for (Integer i:s)
System.out.print(i+ " " );
}
public static void main(String args[])
{
int a[] = { 2 , 3 , 4 , 5 , 6 , 10 } ;
int k = 2 ;
int n = a.length;
K_multiple(a, n, k);
}
}
|
Python3
def K_multiple(a, n, k) :
a.sort();
s = set ();
for i in range (n) :
if ((a[i] % k = = 0 and
a[i] / / k not in s ) or a[i] % k ! = 0 ) :
s.add(a[i]);
for i in s :
print (i, end = " " )
if __name__ = = "__main__" :
a = [ 2 , 3 , 4 , 5 , 6 , 10 ];
k = 2 ;
n = len (a);
K_multiple(a, n, k);
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void K_multiple( int []a, int n, int k)
{
Array.Sort(a);
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
if ((a[i] % k == 0 && !s.Contains(a[i] / k))
|| a[i] % k != 0)
s.Add(a[i]);
}
foreach ( int i in s)
Console.Write(i+ " " );
}
public static void Main(String []args)
{
int []a = { 2, 3, 4, 5, 6, 10 } ;
int k = 2;
int n = a.Length;
K_multiple(a, n, k);
}
}
|
Javascript
<script>
function K_multiple(a, n, k) {
a.sort((a, b) => a - b);
let s = new Set();
for (let i = 0; i < n; i++) {
if ((a[i] % k == 0 && !s.has(a[i] / k))
|| a[i] % k != 0)
s.add(a[i]);
}
for (let i of s) {
document.write(i + " " );
}
}
let a = [2, 3, 4, 5, 6, 10];
let k = 2;
let n = a.length;
K_multiple(a, n, k);
</script>
|
Time Complexity : O (N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...