Find the largest possible k-multiple set

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

2 3 5

Time Complexity : O (N*log(N))



My Personal Notes arrow_drop_up

pawanasipugmailcom

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.