Skip to content
Related Articles

Related Articles

Find the number of ways to divide number into four parts such that a = c and b = d
  • Last Updated : 10 Apr, 2019

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
Expalantion : four parts are {1, 2, 1, 2}

Input : N = 20
Output : 4
Expalantion : possible ways are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}.

Approach :
If the given N is odd the answer is 0, because the sum of four parts will not be even number.



If n is divisible by 4 then answer will be n/4 -1(here a equals to b can be possible so subtract that possibility) otherwise n/4.

Below is the implementation of the above approach :

C++




// C++ implementation for above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
int possibleways(int n)
{
    if (n % 2 == 1)
        return 0;
    else if (n % 4 == 0)
        return n / 4 - 1;
    else
        return n / 4;
}
  
// Driver code
int main()
{
    int n = 20;
    cout << possibleways(n);
    return 0;
}

Java




// Java implementation for above approach
class GFG
{
      
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
static int possibleways(int n)
{
    if (n % 2 == 1)
        return 0;
    else if (n % 4 == 0)
        return n / 4 - 1;
    else
        return n / 4;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 20;
    System.out.println(possibleways(n));
}
}
  
// This code contributed by Rajput-Ji

Python3




# Python3 implementation for above approach
  
# Function to find the number of ways 
# to divide N into four parts 
# such that a = c and b = d
def possibleways(n):
  
    if (n % 2 == 1):
        return 0;
    elif (n % 4 == 0):
        return n // 4 - 1;
    else:
        return n // 4;
  
# Driver code
n = 20;
print(possibleways(n));
  
# This code is contributed by mits

C#




// C# implementation for above approach
class GFG
{
      
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
static int possibleways(int n)
{
    if (n % 2 == 1)
        return 0;
    else if (n % 4 == 0)
        return n / 4 - 1;
    else
        return n / 4;
}
  
// Driver code
static void Main()
{
    int n = 20;
    System.Console.WriteLine(possibleways(n));
}
}
  
// This code is contributed by mits

PHP




<?php
// PHP implementation for above approach
  
// Function to find the number of ways to divide
// N into four parts such that a = c and b = d
function possibleways($n)
{
    if ($n % 2 == 1)
        return 0;
    else if ($n % 4 == 0)
        return $n / 4 - 1;
    else
        return $n / 4;
}
  
// Driver code
$n = 20;
echo possibleways($n);
  
// This code is contributed by ajit
?>
Output:
4

competitive-programming-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :