Number of permutations such that sum of elements at odd index and even index are equal
Last Updated :
19 Sep, 2022
Given N numbers, find the number of permutations in which the sum of elements at odd index and sum of elements at even index are equal.
Examples:
Input: 1 2 3
Output: 2
The permutations are:
1 3 2 sum at odd index = 1+2 = 3, sum at even index = 3
2 3 1 sum at odd index = 2+1 = 3, sum at even index = 3
Input: 1 2 1 2
Output: 3
The permutations are:
1 2 2 1
2 1 1 2
2 2 1 1
The approach to the problem will be to use next_permutation() in C++ STL which helps to generate all the possible permutation of N numbers. If the sum of the odd index elements is equal to the sum of even index elements of the generated permutation, then increase the count. When all permutations are checked, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfPermutations( int a[], int n)
{
int sumEven, sumOdd, c = 0;
do {
sumEven = sumOdd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2)
sumOdd += a[i];
else
sumEven += a[i];
}
if (sumOdd == sumEven)
c++;
} while (next_permutation(a, a + n));
return c;
}
int main()
{
int a[] = { 1, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << numberOfPermutations(a, n);
return 0;
}
|
Java
class GFG {
static int numberOfPermutations( int a[], int n) {
int sumEven, sumOdd, c = 0 ;
do {
sumEven = sumOdd = 0 ;
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 ) {
sumOdd += a[i];
} else {
sumEven += a[i];
}
}
if (sumOdd == sumEven) {
c++;
}
} while (next_permutation(a));
return c;
}
static boolean next_permutation( int [] p) {
for ( int a = p.length - 2 ; a >= 0 ; --a) {
if (p[a] < p[a + 1 ]) {
for ( int b = p.length - 1 ;; --b) {
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
public static void main(String args[]) {
int a[] = { 1 , 2 , 3 };
int n = a.length;
System.out.println(numberOfPermutations(a, n));
}
}
|
Python3
def next_permutation(arr):
arrCount = len (arr);
i = arrCount - 1 ;
while (i > 0 and arr[i] < = arr[i - 1 ]):
i - = 1 ;
if (i < = 0 ):
return [ False ,arr];
pivotIndex = i - 1 ;
j = arrCount - 1 ;
while (arr[j] < = arr[pivotIndex]):
j - = 1 ;
temp = arr[pivotIndex];
arr[pivotIndex] = arr[j];
arr[j] = temp;
j = arrCount - 1 ;
while (i < j):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i + = 1 ;
j - = 1 ;
return [ True ,arr];
def numberOfPermutations(a, n):
sumEven = 0 ;
sumOdd = 0 ;
c = 0 ;
while ( True ):
sumEven = 0 ;
sumOdd = 0 ;
for i in range (n):
if (i % 2 ):
sumOdd + = a[i];
else :
sumEven + = a[i];
if (sumOdd = = sumEven):
c + = 1 ;
xx = next_permutation(a);
if (xx[ 0 ] = = False ):
break ;
a = xx[ 1 ];
return c;
a = [ 1 , 2 , 3 ];
n = len (a);
print (numberOfPermutations(a, n));
|
C#
using System;
public class GFG {
static int numberOfPermutations( int []a, int n) {
int sumEven, sumOdd, c = 0;
do {
sumEven = sumOdd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
sumOdd += a[i];
} else {
sumEven += a[i];
}
}
if (sumOdd == sumEven) {
c++;
}
} while (next_permutation(a));
return c;
}
static bool next_permutation( int [] p) {
for ( int a = p.Length - 2; a >= 0; --a) {
if (p[a] < p[a + 1]) {
for ( int b = p.Length - 1;; --b) {
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
public static void Main() {
int []a = {1, 2, 3};
int n = a.Length;
Console.WriteLine(numberOfPermutations(a, n));
}
}
|
PHP
<?php
function next_permutation(& $input )
{
$inputCount = count ( $input );
$i = $inputCount - 1;
while ( $i > 0 && $input [ $i ] <= $input [ $i - 1])
{
$i --;
}
if ( $i <= 0)
{
return false;
}
$pivotIndex = $i - 1;
$j = $inputCount - 1;
while ( $input [ $j ] <= $input [ $pivotIndex ])
{
$j --;
}
$temp = $input [ $pivotIndex ];
$input [ $pivotIndex ] = $input [ $j ];
$input [ $j ] = $temp ;
$j = $inputCount - 1;
while ( $i < $j )
{
$temp = $input [ $i ];
$input [ $i ] = $input [ $j ];
$input [ $j ] = $temp ;
$i ++;
$j --;
}
return true;
}
function numberOfPermutations( $a , $n )
{
$sumEven ;
$sumOdd ;
$c = 0;
do {
$sumEven = $sumOdd = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i % 2)
$sumOdd += $a [ $i ];
else
$sumEven += $a [ $i ];
}
if ( $sumOdd == $sumEven )
$c ++;
} while (next_permutation( $a ));
return $c ;
}
$a = array (1, 2, 3);
$n = count ( $a );
echo numberOfPermutations( $a , $n );
?>
|
Javascript
<script>
function numberOfPermutations( a , n) {
var sumEven, sumOdd, c = 0;
do {
sumEven = sumOdd = 0;
for ( var i = 0; i < n; i++) {
if (i % 2 == 0) {
sumOdd += a[i];
} else {
sumEven += a[i];
}
}
if (sumOdd == sumEven) {
c++;
}
} while (next_permutation(a));
return c;
}
function next_permutation(p) {
for ( var a = p.length - 2; a >= 0; --a) {
if (p[a] < p[a + 1]) {
for ( var b = p.length - 1;; --b) {
if (p[b] > p[a]) {
var t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
}
}
}
return false ;
}
var a = [ 1, 2, 3 ];
var n = a.length;
document.write(numberOfPermutations(a, n));
</script>
|
Time Complexity: O(N! * N)
Auxiliary Space: O(1) because it is using constant space for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...