- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
Advertisements