The Wayback Machine - https://web.archive.org/web/20240624085734/https://www.geeksforgeeks.org/split-n-maximum-composite-numbers/
Open In App

Split n into maximum composite numbers

Last Updated : 18 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

  1. If the number is less than 4, it won’t have any combinations.
  2. If the number is 5, 7, 11, it wont have any splitting.
  3. Since smallest composite number is 4, it makes sense to use maximum number of 4s.
  4. 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++




// CPP program to split a number into maximum
// number of composite numbers.
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate the maximum number of
// composite numbers adding upto n
int count(int n)
{
    // 4 is the smallest composite number
    if (n < 4)
        return -1;
  
    // stores the remainder when n is divided
    // by 4
    int rem = n % 4;
  
    // if remainder is 0, then it is perfectly 
    // divisible by 4.
    if (rem == 0)
        return n / 4;
  
    // if the remainder is 1
    if (rem == 1) {
  
        // If the number is less than 9, that
        // is 5, then it cannot be expressed as 
        // 4 is the only composite number less 
        // than 5
        if (n < 9)
            return -1;
  
        // If the number is greater than 8, and 
        // has a remainder of 1, then express n
        // as n-9 a and it is perfectly divisible 
        // by 4 and for 9, count 1.
        return (n - 9) / 4 + 1;
    }
  
      
    // When remainder is 2, just subtract 6 from n, 
    // so that n is perfectly divisible by 4 and
    // count 1 for 6 which is subtracted.
    if (rem == 2)
        return (n - 6) / 4 + 1;
  
  
    // if the number is 7, 11 which cannot be 
    // expressed as sum of any composite numbers
    if (rem == 3) {
        if (n < 15)
            return -1;
  
        // when the remainder is 3, then subtract
        // 15 from it and n becomes perfectly 
        // divisible by 4 and we add 2 for 9 and 6,
        // which is getting subtracted to make n 
        // perfectly divisible by 4.
        return (n - 15) / 4 + 2;
    }
}
  
// driver program to test the above function
int main()
{
    int n = 90;
    cout << count(n) << endl;
  
    n = 143;
    cout << count(n) << endl;
    return 0; 
}


Java




// Java program to split a number into maximum
// number of composite numbers.
import java.io.*;
  
class GFG 
{
    // function to calculate the maximum number of
    // composite numbers adding upto n
    static int count(int n)
    {
        // 4 is the smallest composite number
        if (n < 4)
            return -1;
      
        // stores the remainder when n is divided
        // by 4
        int rem = n % 4;
      
        // if remainder is 0, then it is perfectly 
        // divisible by 4.
        if (rem == 0)
            return n / 4;
      
        // if the remainder is 1
        if (rem == 1) {
      
            // If the number is less than 9, that
            // is 5, then it cannot be expressed as 
            // 4 is the only composite number less 
            // than 5
            if (n < 9)
                return -1;
      
            // If the number is greater than 8, and 
            // has a remainder of 1, then express n
            // as n-9 a and it is perfectly divisible 
            // by 4 and for 9, count 1.
            return (n - 9) / 4 + 1;
        }
      
          
        // When remainder is 2, just subtract 6 from n, 
        // so that n is perfectly divisible by 4 and
        // count 1 for 6 which is subtracted.
        if (rem == 2)
            return (n - 6) / 4 + 1;
      
      
        // if the number is 7, 11 which cannot be 
        // expressed as sum of any composite numbers
        if (rem == 3) 
        {
            if (n < 15)
                return -1;
      
            // when the remainder is 3, then subtract
            // 15 from it and n becomes perfectly 
            // divisible by 4 and we add 2 for 9 and 6,
            // which is getting subtracted to make n 
            // perfectly divisible by 4.
            return (n - 15) / 4 + 2;
        }
        return 0;
    }
      
    // Driver program 
    public static void main (String[] args) 
    {
        int n = 90;
        System.out.println(count(n));
  
        n = 143;
        System.out.println(count(n));
    }
} 
  
// This code is contributed by vt_m.


Python3




# Python3 program to split a number into 
# maximum number of composite numbers.
  
# Function to calculate the maximum number 
# of composite numbers adding upto n
def count(n):
  
    # 4 is the smallest composite number
    if (n < 4):
        return -1
  
    # stores the remainder when n  
    # is divided n is divided by 4
    rem = n % 4
  
    # if remainder is 0, then it is  
    # perfectly divisible by 4.
    if (rem == 0):
        return n // 4
  
    # if the remainder is 1
    if (rem == 1):
  
        # If the number is less than 9, that
        # is 5, then it cannot be expressed as 
        # 4 is the only composite number less 
        # than 5
        if (n < 9):
            return -1
  
        # If the number is greater than 8, and 
        # has a remainder of 1, then express n
        # as n-9 a and it is perfectly divisible 
        # by 4 and for 9, count 1.
        return (n - 9) // 4 + 1
      
  
      
    # When remainder is 2, just subtract 6 from n, 
    # so that n is perfectly divisible by 4 and
    # count 1 for 6 which is subtracted.
    if (rem == 2):
        return (n - 6) // 4 + 1
  
  
    # if the number is 7, 11 which cannot be 
    # expressed as sum of any composite numbers
    if (rem == 3): 
        if (n < 15):
            return -1
  
        # when the remainder is 3, then subtract
        # 15 from it and n becomes perfectly 
        # divisible by 4 and we add 2 for 9 and 6,
        # which is getting subtracted to make n 
        # perfectly divisible by 4.
        return (n - 15) // 4 + 2
  
# Driver Code
n = 90
print(count(n))
  
n = 143
print(count(n))
  
# This code is contributed by Anant Agarwal.


C#




// C# program to split a number into maximum
// number of composite numbers.
using System;
  
class GFG {
      
    // function to calculate the maximum number
    // of composite numbers adding upto n
    static int count(int n)
    {
          
        // 4 is the smallest composite number
        if (n < 4)
            return -1;
       
        // stores the remainder when n is divided
        // by 4
        int rem = n % 4;
       
        // if remainder is 0, then it is perfectly 
        // divisible by 4.
        if (rem == 0)
            return n / 4;
       
        // if the remainder is 1
        if (rem == 1) {
       
            // If the number is less than 9, that
            // is 5, then it cannot be expressed as 
            // 4 is the only composite number less 
            // than 5
            if (n < 9)
                return -1;
       
            // If the number is greater than 8, and 
            // has a remainder of 1, then express n
            // as n-9 a and it is perfectly divisible 
            // by 4 and for 9, count 1.
            return (n - 9) / 4 + 1;
        }
       
           
        // When remainder is 2, just subtract 6 from n, 
        // so that n is perfectly divisible by 4 and
        // count 1 for 6 which is subtracted.
        if (rem == 2)
            return (n - 6) / 4 + 1;
       
       
        // if the number is 7, 11 which cannot be 
        // expressed as sum of any composite numbers
        if (rem == 3) 
        {
            if (n < 15)
                return -1;
       
            // when the remainder is 3, then subtract
            // 15 from it and n becomes perfectly 
            // divisible by 4 and we add 2 for 9 and 6,
            // which is getting subtracted to make n 
            // perfectly divisible by 4.
            return (n - 15) / 4 + 2;
        }
          
        return 0;
    }
       
    // Driver program 
    public static void Main() 
    {
        int n = 90;
        Console.WriteLine(count(n));
   
        n = 143;
        Console.WriteLine(count(n));
    }
} 
   
// This code is contributed by Anant Agarwal.


PHP




<?php
// PHP program to split a number
// into maximum number of 
// composite numbers.
  
// function to calculate the
// maximum number of composite 
// numbers adding upto n
function c_ount($n)
{
      
    // 4 is the smallest
    // composite number
    if ($n < 4)
        return -1;
  
    // stores the remainder when
    // n is divided by 4
    $rem = $n % 4;
  
    // if remainder is 0, then it 
    // is perfectly divisible by 4.
    if ($rem == 0)
        return $n / 4;
  
    // if the remainder is 1
    if ($rem == 1) {
  
        // If the number is less 
        // then 9, that is 5, then
        // it cannot be expressed
        // as  4 is the only 
        //composite number less 
        // than 5
        if ($n < 9)
            return -1;
  
        // If the number is greater 
        // then 8, and has a 
        // remainder of 1, then 
        // express n as n-9 a and
        // it is perfectly divisible 
        // by 4 and for 9, count 1.
        return ($n - 9) / 4 + 1;
    }
  
      
    // When remainder is 2, just 
    // subtract 6 from n, so that n
    // is perfectly divisible by 4
    // and count 1 for 6 which is
    // subtracted.
    if ($rem == 2)
        return ($n - 6) / 4 + 1;
  
  
    // if the number is 7, 11 which
    // cannot be expressed as sum of
    // any composite numbers
    if ($rem == 3) {
        if ($n < 15)
            return -1;
  
        // when the remainder is 3, 
        // then subtract 15 from it 
        // and n becomes perfectly
        // divisible by 4 and we add
        // 2 for 9 and 6, which is 
        // getting subtracted to make
        // n perfectly divisible by 4.
        return ($n - 15) / 4 + 2;
    }
}
  
// driver program to test the above
// function
  
    $n = 90;
    echo c_ount($n),"\n";
  
    $n = 143;
    echo c_ount($n);
  
// This code is contributed by anuj_67.
?>


Javascript




<script>
    // Javascript program to split a number
// into maximum number of 
// composite numbers.
    
// function to calculate the
// maximum number of composite 
// numbers adding upto n
function c_ount(n)
{
        
    // 4 is the smallest
    // composite number
    if (n < 4)
        return -1;
    
    // stores the remainder when
    // n is divided by 4
    let rem = n % 4;
    
    // if remainder is 0, then it 
    // is perfectly divisible by 4.
    if (rem == 0)
        return n / 4;
    
    // if the remainder is 1
    if (rem == 1) {
    
        // If the number is less 
        // then 9, that is 5, then
        // it cannot be expressed
        // as  4 is the only 
        //composite number less 
        // than 5
        if (n < 9)
            return -1;
    
        // If the number is greater 
        // then 8, and has a 
        // remainder of 1, then 
        // express n as n-9 a and
        // it is perfectly divisible 
        // by 4 and for 9, count 1.
        return (n - 9) / 4 + 1;
    }
    
        
    // When remainder is 2, just 
    // subtract 6 from n, so that n
    // is perfectly divisible by 4
    // and count 1 for 6 which is
    // subtracted.
    if (rem == 2)
        return (n - 6) / 4 + 1;
    
    
    // if the number is 7, 11 which
    // cannot be expressed as sum of
    // any composite numbers
    if (rem == 3) {
        if (n < 15)
            return -1;
    
        // when the remainder is 3, 
        // then subtract 15 from it 
        // and n becomes perfectly
        // divisible by 4 and we add
        // 2 for 9 and 6, which is 
        // getting subtracted to make
        // n perfectly divisible by 4.
        return (n - 15) / 4 + 2;
    }
}
    
// driver program to test the above
// function
    
    let n = 90;
    document.write(c_ount(n) + "<br>");
    
    n = 143;
    document.write(c_ount(n));
    
// This code is contributed by _saurabh_jaiswal.
</script>


Output: 
 

22 
34 

Time complexity: O(1) 
Auxiliary Space: O(1) 

 



Previous Article
Next Article

Similar Reads

Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts. Examples: Input: arr[] = {1, 2}Output: 4Explanation:Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having
7 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K
Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explanation:The array can be split into 3 possible subse
6 min read
Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
Given an array of positive numbers, the task is to find the bitwise AND of the sum of non-prime numbers and the sum of prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 9 Sum of non-primes = 10 + 15 = 25 Sum of primes = 3 + 5 + 7 = 15 25 &amp; 15 = 9Input: arr[] = {3, 4, 6, 7} Output: 10
9 min read
Split a String into two Substring such that the sum of unique characters is maximum
Given a string str, the task is to partition the string into two substrings such that the sum of unique characters of both substrings is the maximum possible. Examples: Input: str = "abcabcd” Output: 7Explanation: Partition the given string into "abc" and "abcd", the sum of unique characters is 3 + 4 = 7 which is maximum possible. Input: str = "aaa
14 min read
Split array into two subarrays such that difference of their maximum is minimum
Given an array arr[] consisting of integers, the task is to split the given array into two sub-arrays such that the difference between their maximum elements is minimum. Example: Input: arr[] = {7, 9, 5, 10} Output: 1 Explanation: The subarrays are {5, 10} and {7, 9} with the difference between their maximums = 10 - 9 = 1.Input: arr[] = {6, 6, 6} O
7 min read
Split a given array into K subarrays minimizing the difference between their maximum and minimum
Given a sorted array arr[] of N integers and an integer K, the task is to split the array into K subarrays such that the sum of the difference of maximum and minimum element of each subarray is minimized. Examples: Input: arr[] = {1, 3, 3, 7}, K = 4 Output: 0 Explanation: The given array can be split into 4 subarrays as {1}, {3}, {3}, and {7}. The
6 min read
Split array into maximum subarrays such that every distinct element lies in a single subarray
Given an array, arr[] of size N, the task is to split the array into the maximum number of subarrays such that the first and the last occurrence of all distinct array element lies in a single subarray. Examples: Input: arr[] = {1, 1, 2, 2}Output: 2Explanation:Split the array into subarrays {1, 1} and {2, 2}.Therefore, the required output is 2. Inpu
6 min read
Split array into minimum number of subsets having maximum pair sum at most K
Given an array, arr[] of size N and an integer K, the task is to partition the array into the minimum number of subsets such that the maximum pair sum in each subset is less than or equal to K. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 5Output: 3Explanation:Subset having maximum pair sum less than or equal to K(= 5) are: {{2, 3}, {1, 4}, {5}}.T
7 min read
Split array into equal length subsets with maximum sum of Kth largest element of each subset
Given an array arr[] of size N, two positive integers M and K, the task is to partition the array into M equal length subsets such that the sum of the Kth largest element of all these subsets is maximum. If it is not possible to partition the array into M equal length subsets, then print -1. Examples: Input: arr[] = { 1, 2, 3, 4, 5, 6 }, M = 2, K =
7 min read
Split a string into maximum number of unique substrings
Given string str, the task is to split the string into the maximum number of unique substrings possible and print their count. Examples: Input: str = "ababccc"Output: 5Explanation:Split the given string into the substrings "a", "b", "ab", "c" and "cc".Therefore, the maximum count of unique substrings is 5. Input: str = "aba"Output: 2 Approach: The
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg