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.
Input: 1 2 3
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
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:
Time Complexity: O(N! * N)
- Check if every index i has an index j such that sum of elements in both directions are equal
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Count of index pairs with equal elements in an array
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Find a Fixed Point (Value equal to index) in a given array
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Find element at given index after a number of rotations
- Construct an array from XOR of all elements of array except element at same index
- Even numbers at even index and odd numbers at odd index
- Why array index starts from zero ?
- Equilibrium index of an array
- Find the index of first 1 in a sorted array of 0's and 1's
- Find sum of even index binomial coefficients
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : 29AjayKumar