Skip to content
Related Articles

Related Articles

Improve Article
Find the largest possible k-multiple set
  • Difficulty Level : Medium
  • Last Updated : 08 Jun, 2021

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++




// C++ program to find the largest
// possible k-multiple set
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the largest
// possible k-multiple set
void K_multiple(int a[], int n, int k)
{
    // Sort the given array
    sort(a, a + n);
  
    // To store k-multiple set
    set<int> s;
  
    // Traverse through the whole array
    for (int i = 0; i < n; i++) {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && s.find(a[i] / k) == s.end())
             || a[i] % k != 0)
            s.insert(a[i]);
    }
  
    // Print the k-multiple set
    for (auto i = s.begin(); i != s.end(); i++){
        cout << *i << " ";}
}
  
// Driver code
int main()
{
    int a[] = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
  
    int n = sizeof(a) / sizeof(a[0]);
  
    // Function call
    K_multiple(a, n, k);
  
    return 0;
}

Java




// Java program to find the largest
// possible k-multiple set
import java.util.*;
 
class GFG
{
 
// Function to find the largest
// possible k-multiple set
static void K_multiple(int a[], int n, int k)
{
    // Sort the given array
    Arrays.sort(a);
 
    // To store k-multiple set
    HashSet<Integer> s = new HashSet<>();
 
    // Traverse through the whole array
    for (int i = 0; i < n; i++)
    {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && !s.contains(a[i] / k))
            || a[i] % k != 0)
            s.add(a[i]);
    }
 
    // Print the k-multiple set
    for (Integer i:s)
        System.out.print(i+" ");
}
 
// Driver code
public static void main(String args[])
{
    int a[] = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
 
    int n = a.length;
 
    // Function call
    K_multiple(a, n, k);
}
}
 
// This code contributed by Rajput-Ji

Python3




# Python3 program to find the largest
# possible k-multiple set
 
# Function to find the largest
# possible k-multiple set
def K_multiple(a, n, k) :
 
    # Sort the given array
    a.sort();
 
    # To store k-multiple set
    s = set();
 
    # Traverse through the whole array
    for i in range(n) :
         
        # Check if x/k is already present or not
        if ((a[i] % k == 0 and
             a[i] // k not in s ) or a[i] % k != 0) :
            s.add(a[i]);
             
    # Print the k-multiple set
    for i in s :
        print(i, end = " ")
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 2, 3, 4, 5, 6, 10 ];
    k = 2;
 
    n = len(a);
 
    # Function call
    K_multiple(a, n, k);
 
# This code is contributed by AnkitRai01

C#




// C# program to find the largest
// possible k-multiple set
using System;
using System.Collections.Generic;
public class GFG
{
 
// Function to find the largest
// possible k-multiple set
static void K_multiple(int []a, int n, int k)
{
    // Sort the given array
    Array.Sort(a);
 
    // To store k-multiple set
    HashSet<int> s = new HashSet<int>();
 
    // Traverse through the whole array
    for (int i = 0; i < n; i++)
    {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && !s.Contains(a[i] / k))
            || a[i] % k != 0)
            s.Add(a[i]);
    }
 
    // Print the k-multiple set
    foreach (int i in s)
        Console.Write(i+" ");
}
 
// Driver code
public static void Main(String []args)
{
    int []a = { 2, 3, 4, 5, 6, 10 } ;
    int k = 2;
 
    int n = a.Length;
 
    // Function call
    K_multiple(a, n, k);
}
}
 
// This code has been contributed by 29AjayKumar

Javascript




<script>
 
// JavaScript program to find the largest
// possible k-multiple set
 
// Function to find the largest
// possible k-multiple set
function K_multiple(a, n, k) {
    // Sort the given array
    a.sort((a, b) => a - b);
 
    // To store k-multiple set
    let s = new Set();
 
    // Traverse through the whole array
    for (let i = 0; i < n; i++) {
        // Check if x/k is already present or not
        if ((a[i] % k == 0 && !s.has(a[i] / k))
            || a[i] % k != 0)
            s.add(a[i]);
    }
 
    // Print the k-multiple set
    for (let i of s) {
        document.write(i + " ");
    }
}
 
// Driver code
 
let a = [2, 3, 4, 5, 6, 10];
let k = 2;
 
let n = a.length;
 
// Function call
K_multiple(a, n, k);
 
 
// This code is contributed by gfgking
 
</script>
Output
2 3 5 

Time Complexity : O (N*log(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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :