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}.
Naive Approach
The idea is to run four nested loops from 1 to N and pick four numbers such that their sum is equal to N and the first number=third number and the second number=fourth number and the first number!=second number. Every time you pick any numbers then count that. Now those numbers will be repeated means the value in the first, third number and second,fourth number will be interchanged and it will be counted.
For example if N=6 then 1,2,1,2 and 2,1,2,1 both will be counted. So final answer will be the counted answer by 2.
Steps to implement-
- Initialize a variable count with 0. This will contain the final answer
- After that run four nested loops from 1 to n
- When all the values give the sum to n and the value from the first loop is equal to the value from the third loop and the value from the second loop is equal to the value from the fourth loop and the value from the first loop is not equal to the value from the second loop then increment the count
- Then print or return count/2
Code-
C++
#include <bits/stdc++.h>
using namespace std;
int possibleways( int n)
{
int count=0;
for ( int i=1;i<=n;i++){
for ( int j=1;j<=n;j++){
for ( int p=1;p<=n;p++){
for ( int q=1;q<=n;q++){
if ((i+j+p+q==n) && (i==p) && (j==q) && (i!=j)){
count++;}
}
}
}
}
return count/2;
}
int main()
{
int n = 20;
cout << possibleways(n);
return 0;
}
|
Java
import java.util.*;
class Main {
public static int possibleWays( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= n; j++) {
for ( int p = 1 ; p <= n; p++) {
for ( int q = 1 ; q <= n; q++) {
if ((i + j + p + q == n) && (i == p)
&& (j == q) && (i != j)) {
count++;
}
}
}
}
}
return count / 2 ;
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println(possibleWays(n));
}
}
|
Python3
def possibleways(n):
count = 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 , n + 1 ):
for p in range ( 1 , n + 1 ):
for q in range ( 1 , n + 1 ):
if (i + j + p + q = = n) and (i = = p) and (j = = q) and (i ! = j):
count + = 1
return count / / 2
n = 20
print (possibleways(n))
|
C#
using System;
class MainClass {
static int PossibleWays( int n) {
int count = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n; j++) {
for ( int p = 1; p <= n; p++) {
for ( int q = 1; q <= n; q++) {
if ((i+j+p+q == n) && (i == p) && (j == q) && (i != j)) {
count++;
}
}
}
}
}
return count/2;
}
static void Main() {
int n = 20;
Console.WriteLine(PossibleWays(n));
}
}
|
Javascript
function possibleways(n) {
let count = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
for (let p = 1; p <= n; p++) {
for (let q = 1; q <= n; q++) {
if ((i + j + p + q == n) && (i == p) && (j == q) && (i != j))
{
count++;
}
}
}
}
}
return count / 2;
}
let n = 20;
console.log(possibleways(n));
|
Time Complexity: O(N4), because of 4 nested loops from 1 to N
Auxiliary Space: O(1), because no extra space has been used
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++
#include <bits/stdc++.h>
using namespace std;
int possibleways( int n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
int main()
{
int n = 20;
cout << possibleways(n);
return 0;
}
|
Java
class GFG
{
static int possibleways( int n)
{
if (n % 2 == 1 )
return 0 ;
else if (n % 4 == 0 )
return n / 4 - 1 ;
else
return n / 4 ;
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println(possibleways(n));
}
}
|
Python3
def possibleways(n):
if (n % 2 = = 1 ):
return 0 ;
elif (n % 4 = = 0 ):
return n / / 4 - 1 ;
else :
return n / / 4 ;
n = 20 ;
print (possibleways(n));
|
C#
class GFG
{
static int possibleways( int n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
static void Main()
{
int n = 20;
System.Console.WriteLine(possibleways(n));
}
}
|
Javascript
<script>
function possibleways(n)
{
if (n % 2 == 1)
return 0;
else if (n % 4 == 0)
return n / 4 - 1;
else
return n / 4;
}
let n = 20;
document.write( possibleways(n));
</script>
|
PHP
<?php
function possibleways( $n )
{
if ( $n % 2 == 1)
return 0;
else if ( $n % 4 == 0)
return $n / 4 - 1;
else
return $n / 4;
}
$n = 20;
echo possibleways( $n );
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)