Find the number of ways to divide number into four parts such that a = c and b = d - GeeksforGeeks

09 Nov, 2021
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}, {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++
Java
Python3
C#
PHP
Javascript
// 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 = dint possibleways(int n){    if (n % 2 == 1)        return 0;    else if (n % 4 == 0)        return n / 4 - 1;    else        return n / 4;} // Driver codeint main(){    int n = 20;    cout << possibleways(n);    return 0;}
// Java implementation for above approachclass GFG{     // Function to find the number of ways to divide// N into four parts such that a = c and b = dstatic int possibleways(int n){    if (n % 2 == 1)        return 0;    else if (n % 4 == 0)        return n / 4 - 1;    else        return n / 4;} // Driver codepublic static void main(String[] args){    int n = 20;    System.out.println(possibleways(n));}} // This code contributed by Rajput-Ji
# Python3 implementation for above approach # Function to find the number of ways# to divide N into four parts# such that a = c and b = ddef possibleways(n):     if (n % 2 == 1):        return 0;    elif (n % 4 == 0):        return n // 4 - 1;    else:        return n // 4; # Driver coden = 20;print(possibleways(n)); # This code is contributed by mits
// C# implementation for above approachclass GFG{     // Function to find the number of ways to divide// N into four parts such that a = c and b = dstatic int possibleways(int n){    if (n % 2 == 1)        return 0;    else if (n % 4 == 0)        return n / 4 - 1;    else        return n / 4;} // Driver codestatic void Main(){    int n = 20;    System.Console.WriteLine(possibleways(n));}} // This code is contributed by mits
<?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 = dfunction 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?>
<script>// java script  implementation for above approach // Function to find the number of ways to divide// N into four parts such that a = c and b = dfunction possibleways(n){    if (n % 2 == 1)        return 0;    else if (n % 4 == 0)        return n / 4 - 1;    else        return n / 4;} // Driver codelet n = 20;document.write( possibleways(n)); // This code is contributed by Gottumukkala Bobby</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Mithun Kumar
Rajput-Ji
jit_t
gottumukkalabobby
ruhelaa48
rohitkumarsinghcna
Competitive Programming
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Shortest path in a directed graph by Dijkstra’s algorithm
5 Best Books for Competitive Programming
5 Best Languages for Competitive Programming
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7