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++
// 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> |
Minimum Terms = 2 Maximum Terms = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...