Find the number of ways to divide number into four parts such that a = c and b = d in C++


Suppose we have a number n. We have to find number of ways to divide a number into parts (a, b, c and d) such that a = c, and b = d. So if the number is 20, then output will be 4. As [1, 1, 9, 9], [2, 2, 8, 8], [3, 3, 7, 7] and [4, 4, 6, 6]

So if N is odd, then answer will be 0. If the number is divisible by 4, then answer will be n/4 – 1 otherwise n/4.

Example

 Live Demo

#include <iostream>
using namespace std;
int countPossiblity(int num) {
   if (num % 2 == 1)
      return 0;
   else if (num % 4 == 0)
      return num / 4 - 1;
   else
      return num / 4;
}
int main() {
   int n = 20;
   cout << "Number of possibilities: " << countPossiblity(n);
}

Output

Number of possibilities: 4

Updated on: 19-Dec-2019

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements