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.
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.
Please Login to comment...