Size of longest Divisible Subset in an Array in Java10 Sept 2024 | 7 min read In a given input array, the task is to find the size of the longest divisible subset. A subset is known as divisible only if, for each pair (p, q) in the subset, either p divides q (p % q = 0) or q divides p (q % p = 0). Example 1: Input int arr[] = {1, 16, 7, 19, 28, 8, 4, 64, 256} Output: 6 Explanation: Consider the subset {1, 16, 8, 4, 64, 256}. In this subset, we can make 15 (6C2 = 15) pairs of elements. In any of the pairs (comprising of two elements, p and q), either p % q = 0 or q % p = 0. Also, the considered subset is the longest subset that satisfies the given condition, whose size is 6. Hence, the output is 6. Example 2: Input int arr[] = {2, 2, 2, 2, 2, 2, 2} Output: 7 Explanation: All the elements of the input array divide each other completely as, all the elements are of the same value. Therefore, the longest subset is the input array itself, whose size is 7. Hence, the output is 7. Approach: Using RecursionThe simple approach is to generate all of the possible subsets from the given input array and then check whether the generated subsets are valid or not as per the given condition. If the subset is valid, then compare its size with the previously computed maximum size of the previously computed valid subset and update the answer accordingly. All of the subsets can be generated by excluding or including the current element in the current subset. We will be generating and validating the side of the subset by the side in the same method. Observe the following implementation. FileName: LongestDivisibleSubset.java Output: For the input array: 1 16 7 19 28 8 4 64 256 The maximum size of the longest divisible subset is: 6 For the input array: 2 2 2 2 2 2 2 The maximum size of the longest divisible subset is: 7 Complexity Analysis: The program generates two recursive calls in each recursive call. It leads to exponential time complexity. Therefore, the time complexity of the program is O(2n). The array list that stores all of the subsets (subsetAL in our case) make the space complexity of the program exponential, which is also O(2n), where n is the total number of elements present in the input array. The time, as well as the space complexity of the above program, is very high and is not suitable for larger inputs. Therefore, it is required to reduce both complexities. Efficient Approach: Using SortingWith the help of sorting, one can reduce the time as well the space complexity of the program. Observe the following steps. Step 1: Sort all of the elements of the input array in ascending order. The reason for sorting is to ensure that all of the divisors of an element come before it. Step 2: Create an array divisibleCount[] whose size is the same as the input array. divisibleCount[i] keeps the size of divisible subset ending with arr[i] (In the sorted array). The minimum value of divisibleCount [i] would be 1. Step 3: Traverse all array elements. For every element, find a divisor arr[j] with the largest value of divisibleCount[j] and store the value of divisibleCount[i] as divisibleCount[j] + 1. Observe the implementation of the above steps. FileName: LongestDivisibleSubset1.java Output: For the input array: 1 16 7 19 28 8 4 64 256 The maximum size of the longest divisible subset is: 6 For the input array: 2 2 2 2 2 2 2 The maximum size of the longest divisible subset is: 7 Complexity Analysis: The program is using two nested-for loops, which makes the time complexity of the program O(n2). The program is also using auxiliary arrays for finding the result, which makes the space complexity of the program O(n), where n is the total number of elements present in the input array. |
Backward compatibility denotes a system, product, or technology's capability to function with earlier versions or to incorporate legacy systems or inputs created for prior versions. When modifications to a system interfere with this compatibility, it leads to what is referred to as a Breaking Change. In...
6 min read
The java.text.ChoiceFormat is a class containing a getFormats() as a function. When the ChoiceFormat object is being initialized, the ChoiceFormat class is utilized to retrieve the connected format. It provides an array of the specified type. Syntax: public Object[] getFormats() Parameter: There are no parameters accepted by this...
2 min read
A final variable can be initialized at the time of declaration or in a constructor, but once assigned, it cannot be modified. The final keyword is used to declare constants. Use the final keyword to declare a variable as final. It is treated as constant. Syntax: final...
4 min read
In Java, bitwise operators are used to execute binary number bit-level operations. These operators alter the bits in a number by performing operations like bit shifting, AND, OR, NOT, and XOR. With examples and programs, we will go over the various types of bitwise operators...
5 min read
The book "Design Patterns: Elements of Reusable Object-Oriented Software" has 23 design patterns, which are grouped together as Gangs of Four Design Patterns. One of the most well-liked books to understand design patterns was first published in 1994. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides...
3 min read
In the realm of object-oriented programming (OOP), Java has long been a prominent player, offering developers powerful tools to create robust and flexible software systems. With the release of Java 8, the programming landscape witnessed significant changes in the way developers design and structure their code....
4 min read
In Java, the @SuppressWarnings is defined as an annotation that is utilized for suppressing or ignoring particular warnings which are raised by the compiler because of a specific code. In simple words, the compiler gets indicated by the @SuppressWarnings annotation to pass over or ignore particular...
4 min read
Spiral patterns are a popular concept in computer graphics and can be used to visualize data in a unique and interesting way. In this section, we will explore how to create a spiral pattern of numbers using Java. We will cover the logic behind the...
5 min read
Autoboxing is a feature in Java that allows you to convert primitive types to their corresponding wrapper objects automatically. For example, the statement Integer x = 10; will automatically create an Integer object with the value 10 and assign it to the variable x. Here are some...
3 min read
Shadowing is the concept of OOP paradigm. It provides a new implementation to the base member without overriding it. Both shadowing and hiding are the same concepts but uses in different context. These are compile-time process. In this section, we will discuss the concept of variable...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India