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, ………
Examples:
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 than 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 than that, as the composite numbers above 9 can be made up of 4, 6, and 9.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int count( int n)
{
if (n < 4)
return -1;
int rem = n % 4;
if (rem == 0)
return n / 4;
if (rem == 1) {
if (n < 9)
return -1;
return (n - 9) / 4 + 1;
}
if (rem == 2)
return (n - 6) / 4 + 1;
if (rem == 3) {
if (n < 15)
return -1;
return (n - 15) / 4 + 2;
}
}
int main()
{
int n = 90;
cout << count(n) << endl;
n = 143;
cout << count(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int count( int n)
{
if (n < 4 )
return - 1 ;
int rem = n % 4 ;
if (rem == 0 )
return n / 4 ;
if (rem == 1 ) {
if (n < 9 )
return - 1 ;
return (n - 9 ) / 4 + 1 ;
}
if (rem == 2 )
return (n - 6 ) / 4 + 1 ;
if (rem == 3 )
{
if (n < 15 )
return - 1 ;
return (n - 15 ) / 4 + 2 ;
}
return 0 ;
}
public static void main (String[] args)
{
int n = 90 ;
System.out.println(count(n));
n = 143 ;
System.out.println(count(n));
}
}
|
Python3
def count(n):
if (n < 4 ):
return - 1
rem = n % 4
if (rem = = 0 ):
return n / / 4
if (rem = = 1 ):
if (n < 9 ):
return - 1
return (n - 9 ) / / 4 + 1
if (rem = = 2 ):
return (n - 6 ) / / 4 + 1
if (rem = = 3 ):
if (n < 15 ):
return - 1
return (n - 15 ) / / 4 + 2
n = 90
print (count(n))
n = 143
print (count(n))
|
C#
using System;
class GFG {
static int count( int n)
{
if (n < 4)
return -1;
int rem = n % 4;
if (rem == 0)
return n / 4;
if (rem == 1) {
if (n < 9)
return -1;
return (n - 9) / 4 + 1;
}
if (rem == 2)
return (n - 6) / 4 + 1;
if (rem == 3)
{
if (n < 15)
return -1;
return (n - 15) / 4 + 2;
}
return 0;
}
public static void Main()
{
int n = 90;
Console.WriteLine(count(n));
n = 143;
Console.WriteLine(count(n));
}
}
|
PHP
<?php
function c_ount( $n )
{
if ( $n < 4)
return -1;
$rem = $n % 4;
if ( $rem == 0)
return $n / 4;
if ( $rem == 1) {
if ( $n < 9)
return -1;
return ( $n - 9) / 4 + 1;
}
if ( $rem == 2)
return ( $n - 6) / 4 + 1;
if ( $rem == 3) {
if ( $n < 15)
return -1;
return ( $n - 15) / 4 + 2;
}
}
$n = 90;
echo c_ount( $n ), "\n" ;
$n = 143;
echo c_ount( $n );
?>
|
Javascript
<script>
function c_ount(n)
{
if (n < 4)
return -1;
let rem = n % 4;
if (rem == 0)
return n / 4;
if (rem == 1) {
if (n < 9)
return -1;
return (n - 9) / 4 + 1;
}
if (rem == 2)
return (n - 6) / 4 + 1;
if (rem == 3) {
if (n < 15)
return -1;
return (n - 15) / 4 + 2;
}
}
let n = 90;
document.write(c_ount(n) + "<br>" );
n = 143;
document.write(c_ount(n));
</script>
|
Output:
22
34
Time complexity: O(1)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Sep, 2023
Like Article
Save Article