Given n, print the maximum number of composite numbers that sum up to n. First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ………
Input: 90 Output: 22 Explanation: If we add 21 4's, then we get 84 and then add 6 to it, we get 90. Input: 10 Output: 2 Explanation: 4 + 6 = 10
Below are some important observations.
- If the number is less then 4, it won’t have any combinations.
- If the number is 5, 7, 11, it wont have any splitting.
- Since smallest composite number is 4, it makes sense to use maximum number of 4s.
- For numbers that don’t leave a composite remainder when divided by 4, we do following. If remainder is 1, we subtract 9 from it to get the number which is perfectly divisible by 4. If the remainder is 2, then subtract 6 from it to make n a number which is perfectly divisible by 4. If the remainder is 3, then subtract 15 from it to make n perfectly divisible by 4, and 15 can be made up by 9 + 6.
So the main observation is to make n such that is composes of maximum no of 4’s and the remaining can be made up by 6 and 9. We won’t need composite numbers more then that, as the composite numbers above 9 can be made up of 4, 6, and 9.
Below is the implementation of the above approach
Time complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Composite numbers with digit sum 1
- Highly Composite Numbers
- Find two Composite Numbers such that there difference is N
- Product of all the Composite Numbers in an array
- Represent the given number as the sum of two composite numbers
- Sum and Product of all Composite numbers which are divisible by k in an array
- Sum of all composite numbers lying in the range [L, R] for Q queries
- Find a range of composite numbers of given length
- Find a sequence of N prime numbers whose sum is a composite number
- Sum and product of k smallest and k largest composite numbers in the array
- Queries for the difference between the count of composite and prime numbers in a given range
- Generate a list of n consecutive composite numbers (An interesting method)
- Split N^2 numbers into N groups of equal sum
- Split a number as sum of K numbers which are not divisible by K
- Find the maximum number of composite summands of a number
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Maximum sum of distinct numbers with LCM as N
Improved By : vt_m