Find minimum and maximum number of terms to divide N as sum of 4 or 6
Given an integer N, the task is to find the minimum and the maximum number of terms required to divide N as the sum of 4 or 6.
Examples:
Input: N = 3
Output: NOT POSSIBLE
Explanation: As the number is less than 4, it is not possible.
Input: N = 10
Output : Minimum Terms = 2, Maximum Terms = 2
Explanation: There is only one possible division (4 + 6).
Approach: The problem can be solved based on the following mathematical observation:
If N can be expressed in terms of 4 and 6 then the answer must lie near N/6 for minimum terms and near N/4 for maximum terms.
If N%6 leaves a remainder 2 it can be replaced by two 4’s and If N%6 leaves a remainder 4, one 4 can be added.
If N%4 leaves a remainder 2 then it can be replaced by one 6.
Follow the steps mentioned below to implement the idea:
- Initialize two variables (say maxi and mini) with N/4 and N/6 respectively to store the maximum and the minimum number of terms.
- N%4 could be 0, 1, 2 or 3.
- If it is either 1 or 3 we can’t represent this series with the means of 4.
- If N%4 is 2 then the last term of the series i.e., 4 will combine with this 2 to make 6. Hence we can represent the series with the use of either 4 or 6.
- Similarly, N%6 can be 0, 1, 2, 3, 4, 5.
- If it is 1, 3, or 5 we can’t represent this series with the means of 6.
- If N%6 is 2 then the last term of the series i.e., 6 will combine with this 2 to make 8, which further can break into two pieces of 4.
- If N%6 is 4 then just increase the mini by one as 4 can be used to represent the series.
- Return the minimum and maximum calculated in this way.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > checkdivisibilityby4and6( int n)
{
if (n < 4)
return { -1, -1 };
int maxi, mini;
maxi = n / 4;
mini = n / 6;
bool difficulty1 = false , difficulty2 = false ;
if (n % 4 != 2 && n % 4 != 0) {
difficulty1 = true ;
}
if (n % 6 == 2) {
mini++;
}
else if (n % 6 == 4) {
mini++;
}
else if (n % 6 != 0) {
difficulty2 = true ;
}
if (difficulty1 and difficulty2) {
return { -1, -1 };
}
return { mini, maxi };
}
int main()
{
int N = 10;
pair< int , int > ans
= checkdivisibilityby4and6(N);
if (ans.first == -1 and ans.second == -1)
cout << "NOT POSSIBLE" ;
else
cout << "Minimum Terms = " << ans.first
<< "\nMaximum Terms = " << ans.second;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int [] checkdivisibilityby4and6( int n)
{
if (n < 4 ) {
int ans[] = { - 1 , - 1 };
return ans;
}
int maxi = n / 4 ;
int mini = n / 6 ;
boolean difficulty1 = false , difficulty2 = false ;
if (n % 4 != 2 && n % 4 != 0 ) {
difficulty1 = true ;
}
if (n % 6 == 2 ) {
mini++;
}
else if (n % 6 == 4 ) {
mini++;
}
else if (n % 6 != 0 ) {
difficulty2 = true ;
}
if (difficulty1 == true && difficulty2 == true ) {
int ans[] = { - 1 , - 1 };
return ans;
}
int ans[] = { mini, maxi };
return ans;
}
public static void main(String[] args)
{
int N = 10 ;
int ans[] = checkdivisibilityby4and6(N);
if (ans[ 0 ] == - 1 && ans[ 1 ] == - 1 )
System.out.print( "NOT POSSIBLE" );
else
System.out.print( "Minimum Terms = " + ans[ 0 ]
+ "\nMaximum Terms = "
+ ans[ 1 ]);
}
}
|
Python3
def checkdivisibilityby4and6(n) :
if (n < 4 ) :
return ( - 1 , - 1 );
maxi = n / / 4 ;
mini = n / / 6 ;
difficulty1 = False ;
difficulty2 = False ;
if (n % 4 ! = 2 and n % 4 ! = 0 ) :
difficulty1 = True ;
if (n % 6 = = 2 ) :
mini + = 1 ;
elif (n % 6 = = 4 ) :
mini + = 1 ;
elif (n % 6 ! = 0 ) :
difficulty2 = True ;
if (difficulty1 and difficulty2) :
return ( - 1 , - 1 );
return ( mini, maxi );
if __name__ = = "__main__" :
N = 10 ;
ans = checkdivisibilityby4and6(N);
if (ans[ 0 ] = = - 1 and ans[ 1 ] = = - 1 ) :
print ( "NOT POSSIBLE" );
else :
print ( "Minimum Terms = " ,ans[ 0 ], "\nMaximum Terms = " ,ans[ 1 ]);
|
C#
using System;
class GFG {
public static int [] checkdivisibilityby4and6( int n)
{
if (n < 4) {
int []ans_1 = { -1, -1 };
return ans_1;
}
int maxi = n / 4;
int mini = n / 6;
bool difficulty1 = false , difficulty2 = false ;
if (n % 4 != 2 && n % 4 != 0) {
difficulty1 = true ;
}
if (n % 6 == 2) {
mini++;
}
else if (n % 6 == 4) {
mini++;
}
else if (n % 6 != 0) {
difficulty2 = true ;
}
if (difficulty1 == true && difficulty2 == true ) {
int []ans_2 = { -1, -1 };
return ans_2;
}
int []ans_3 = { mini, maxi };
return ans_3;
}
public static void Main( string [] args)
{
int N = 10;
int []ans = checkdivisibilityby4and6(N);
if (ans[0] == -1 && ans[1] == -1)
Console.WriteLine( "NOT POSSIBLE" );
else
Console.WriteLine( "Minimum Terms = " + ans[0]
+ "\nMaximum Terms = "
+ ans[1]);
}
}
|
Javascript
<script>
function checkdivisibilityby4and6(n)
{
if (n < 4)
{
let ans = [-1,-1];
return ans;
}
let maxi, mini;
maxi =(int) n / 4;
mini =(int) n / 6;
let difficulty1 = 0, difficulty2 = 0;
if (n % 4 != 2 && n % 4 != 0) {
difficulty1 = 1;
}
if (n % 6 == 2) {
mini++;
}
else if (n % 6 == 4) {
mini++;
}
else if (n % 6 != 0) {
difficulty2 = 1;
}
if (difficulty1 && difficulty2) {
let ans=[-1,-1];
return ans;
}
let ans = [ mini, maxi ];
return ans;
}
let N = 10;
let ans= checkdivisibilityby4and6(N);
if (ans[0] == -1 and ans[1] == -1)
document.write( "NOT POSSIBLE" );
else
{
document.write( "Minimum Terms = " ans[0]);
document.write( "\n" );
document.write( "Maximum Terms = " ans[1]);
}
</script>
|
Output
Minimum Terms = 2
Maximum Terms = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...