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++
// 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 |
2 3 5
Time Complexity : O (N*log(N))
Recommended Posts:
- Find largest d in array such that a + b + c = d
- Find the largest multiple of 2, 3 and 5
- Find the largest twins in given range
- Find Second largest element in an array
- Find the length of largest subarray with 0 sum
- Find the largest interval that contains exactly one of the given N integers.
- Find the largest three elements in an array
- Find the largest after deleting the given elements
- Find the largest number that can be formed with the given digits
- Program to find largest element in an array
- Find largest prime factor of a number
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Find the k largest numbers after deleting the given elements
- Find largest number smaller than N with same set of digits
- Find the area of largest circle inscribed in ellipse
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.



