Saturday, October 5, 2024
Google search engine
HomeData Modelling & AINumber of ways N can be divided into four parts to construct...

Number of ways N can be divided into four parts to construct a rectangle

Given an integer N, the task is to divide the number into four parts such that the divided parts can be used to construct a rectangle but not a square. Find how many numbers of ways are there so that the number can be divided fulfilling the condition.
Examples: 
 

Input: N = 8 
Output: 1
Input: N = 10 
Output:
 

 

Approach: As the number has to be divided such that rectangle is formed from the divided four parts, so if the number is odd, then the number of ways will be zero, as perimeter of a rectangle is always even
Now, if n is even, then only (n – 2) / 4 number of ways are there to divide the number, for example, 
if 8 has to be divided in four parts then there is only (8 – 2) / 4 = 1 way, i.e., [1, 1, 3, 3], no other way is there. It’s because you can only take sides length < = n/2 – 1 to form a valid rectangle and from those n/2 – 1 rectangles count divide again by 2 to avoid double counting.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the
// required number of ways
int cntWays(int n)
{
    if (n % 2 == 1) {
        return 0;
    }
    else {
        return (n - 2) / 4;
    }
}
 
// Driver code
int main()
{
    int n = 18;
 
    cout << cntWays(n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
     
    // Function to return the
    // required number of ways
    static int cntWays(int n)
    {
        if (n % 2 == 1)
        {
            return 0;
        }
        else
        {
            return (n - 2) / 4;
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 18;
     
        System.out.println(cntWays(n));
 
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python 3 implementation of the approach
 
# Function to return the
# required number of ways
def cntWays(n) :
    if n % 2 == 1 :
        return 0
    else:
        return (n - 2) // 4
         
# Driver code
n = 18
print(cntWays(n))
 
# This code is contributed by
# divyamohan123


C#




// C# implementation of the approach
using System;
     
class GFG
{
     
    // Function to return the
    // required number of ways
    static int cntWays(int n)
    {
        if (n % 2 == 1)
        {
            return 0;
        }
        else
        {
            return (n - 2) / 4;
        }
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 18;
     
        Console.WriteLine(cntWays(n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// javascript implementation of the approach
     
// Function to return the
// required number of ways
function cntWays(n)
{
    if (n % 2 == 1)
    {
        return 0;
    }
    else
    {
        return (n - 2) / 4;
    }
}
 
// Driver code
 
var n = 18;
 
document.write(cntWays(n));
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

4

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments