Find minimum and maximum number of terms to divide N as sum of 4 or 6
Last Updated :
02 Aug, 2022
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// and the maximum terms
pair<int, int> checkdivisibilityby4and6(int n)
{
// Edge case
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) {
// This indicates series can't be represented
// with the means of 4
difficulty1 = true;
}
if (n % 6 == 2) {
// One 6 combine with this 2
// lets say series is 6+6+6+2
// it becomes 6+6+8
// further 6+6+4+4
mini++;
}
else if (n % 6 == 4) {
// Say series is 6+6+6+4
// count this 4 also
mini++;
}
else if (n % 6 != 0) {
// This indicates series can't be represented
// with the means of 6
difficulty2 = true;
}
if (difficulty1 and difficulty2) {
return { -1, -1 };
}
return { mini, maxi };
}
// Driver Code
int main()
{
int N = 10;
// Function Call
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
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to find the minimum
// and the maximum terms
public static int[] checkdivisibilityby4and6(int n)
{
// Edge case
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) {
// This indicates series can't be represented
// with the means of 4
difficulty1 = true;
}
if (n % 6 == 2) {
// One 6 combine with this 2
// lets say series is 6+6+6+2
// it becomes 6+6+8
// further 6+6+4+4
mini++;
}
else if (n % 6 == 4) {
// Say series is 6+6+6+4
// count this 4 also
mini++;
}
else if (n % 6 != 0) {
// This indicates series can't be represented
// with the means of 6
difficulty2 = true;
}
if (difficulty1 == true && difficulty2 == true) {
int ans[] = { -1, -1 };
return ans;
}
int ans[] = { mini, maxi };
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
// Function Call
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]);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python3 code to implement the approach
# Function to find the minimum
# and the maximum terms
def checkdivisibilityby4and6(n) :
# Edge case
if (n < 4) :
return (-1, -1 );
maxi = n // 4;
mini = n // 6;
difficulty1 = False;
difficulty2 = False;
if (n % 4 != 2 and n % 4 != 0) :
# This indicates series can't be represented
# with the means of 4
difficulty1 = True;
if (n % 6 == 2) :
# One 6 combine with this 2
# lets say series is 6+6+6+2
# it becomes 6+6+8
# further 6+6+4+4
mini += 1;
elif (n % 6 == 4) :
# Say series is 6+6+6+4
# count this 4 also
mini += 1;
elif (n % 6 != 0) :
# This indicates series can't be represented
# with the means of 6
difficulty2 = True;
if (difficulty1 and difficulty2) :
return ( -1, -1 );
return ( mini, maxi );
# Driver Code
if __name__ == "__main__" :
N = 10;
# Function Call
ans = checkdivisibilityby4and6(N);
if (ans[0] == -1 and ans[1] == -1) :
print("NOT POSSIBLE");
else :
print("Minimum Terms = ",ans[0],"\nMaximum Terms = ",ans[1]);
# This code is contributed by AnkThon
C#
// C# code to implement the approach
using System;
class GFG {
// Function to find the minimum
// and the maximum terms
public static int[] checkdivisibilityby4and6(int n)
{
// Edge case
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) {
// This indicates series can't be represented
// with the means of 4
difficulty1 = true;
}
if (n % 6 == 2) {
// One 6 combine with this 2
// lets say series is 6+6+6+2
// it becomes 6+6+8
// further 6+6+4+4
mini++;
}
else if (n % 6 == 4) {
// Say series is 6+6+6+4
// count this 4 also
mini++;
}
else if (n % 6 != 0) {
// This indicates series can't be represented
// with the means of 6
difficulty2 = true;
}
if (difficulty1 == true && difficulty2 == true) {
int []ans_2 = { -1, -1 };
return ans_2;
}
int []ans_3 = { mini, maxi };
return ans_3;
}
// Driver Code
public static void Main(string[] args)
{
int N = 10;
// Function Call
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]);
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Javascript code to implement the approach.
// Function to find the minimum
// and the maximum terms
function checkdivisibilityby4and6(n)
{
// Edge case
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) {
// This indicates series can't be represented
// with the means of 4
difficulty1 = 1;
}
if (n % 6 == 2) {
// One 6 combine with this 2
// lets say series is 6+6+6+2
// it becomes 6+6+8
// further 6+6+4+4
mini++;
}
else if (n % 6 == 4) {
// Say series is 6+6+6+4
// count this 4 also
mini++;
}
else if (n % 6 != 0) {
// This indicates series can't be represented
// with the means of 6
difficulty2 = 1;
}
if (difficulty1 && difficulty2) {
let ans=[-1,-1];
return ans;
}
let ans = [ mini, maxi ];
return ans;
}
// Driver Code
let N = 10;
// Function Call
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]);
}
// This code is contributed by satwik4409.
</script>
OutputMinimum Terms = 2
Maximum Terms = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find the maximum number of composite summands of a number Given an integer N(1<=N<=10^9). The task is to represent N as a sum of the maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. There can be multiple queries Examples: Input : 12 Output : 3 Explanation : 12 can be written ha
8 min read
Maximum number of times N can be divided by distinct powers of prime integers Given an integer N, the task is to calculate the maximum number of times N can be divided by an integer K, where K is a power of a prime integer and the value of K is always distinct. Example: Input: N = 24Output: 3Explanation: In the first operation, K = 2 (=21). Hence, the value of N becomes N = 2
6 min read
Minimum number of power terms with sum equal to n Given two positive integer n and x. The task is to express n as sum of powers of x (xa1 + xa2 +.....+ xa3) such that the number of powers of x (xa1, xa2, ....., xa3) should be minimum. Print the minimum number of power of x used to make sum equal to n.Examples: Input : n = 5, x = 3 Output : 3 5 = 30
6 min read
Divide a number into two parts such that sum of digits is maximum Given a number N. The task is to find the maximum possible value of SumOfDigits(A) + SumOfDigits(B) such that A + B = n (0<=A, B<=n). Examples: Input: N = 35Output: 1735 = 9 + 26SumOfDigits(26) = 8, SumOfDigits(9) = 9So, 17 is the answer.Input: N = 7Output: 7Naive Approach: The idea is to pick
9 min read
Find the number of ways to divide number into four parts such that a = c and b = d Given a number N. Find the number of ways to divide a number into four parts(a, b, c, d) such that a = c and b = d and a not equal to b. Examples: Input : N = 6 Output : 1 Explanation : four parts are {1, 2, 1, 2}Input : N = 20 Output : 4 Explanation : possible ways are {1, 1, 9, 9}, {2, 2, 8, 8}, {
8 min read
Make n using 1s and 2s with minimum number of terms multiple of k Given two positive integer n and k. n can be represented as the sum of 1s and 2s in many ways, using multiple numbers of terms. The task is to find the minimum number of terms of 1s and 2s use to make the sum n and also number of terms must be multiple of k. Print "-1", if no such number of terms ex
6 min read
Minimum number of squares whose sum equals to a given number N | Set-3 A number can always be represented as a sum of squares of other numbers. Note that 1 is a square, and we can always break a number as (1*1 + 1*1 + 1*1 + â¦). Given a number n, find the minimum number of squares that sum to N.Examples:Input: N = 13 Output: 2 Explanation: 13 can be expressed as, 13 = 3
7 min read
Count number of ways to divide a number in 4 parts Given a positive integer n, find number of ways to divide n in four parts or represent n as sum of four positive integers. Here n varies from 0 to 5000.Examples : Input: n = 5Output: 1There is only one way (1, 1, 1, 2)Input: n = 6Output: 2There are two ways (1, 1, 1, 3) and (1, 1, 2, 2)Input: n = 8O
12 min read
Maximum number of segments of lengths a, b and c Given a positive integer N, find the maximum number of segments of lengths a, b and c that can be formed from N . Examples : Input : N = 7, a = 5, b, = 2, c = 5 Output : 2 N can be divided into 2 segments of lengths 2 and 5. For the second example, Input : N = 17, a = 2, b = 1, c = 3 Output : 17 N c
13 min read
Minimum number of digits required to be removed to make a number divisible by 4 Given a number N, the task is to count the minimum number of digits to be removed from N to make it divisible by 4. Examples: Input: N = 12367Output: 1Explanation: Removing 7 from the number 1236 make the number divisible by 4. Therefore, the minimum count of digit to be removed is 1. Input: N = 243
9 min read