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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Check if every index i has an index j such that sum of elements in both directions are equal
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Count of permutations such that sum of K numbers from given range is even
- Sum of elements till the smallest index such that there are no even numbers to its right
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Index of the elements which are equal to the sum of all succeeding elements
- Number of Subsequences with Even and Odd Sum | Set 2
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Find elements of an Array which are Odd and Even using STL in C++
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Count ways to express even number ‘n’ as sum of even integers
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Count number of ordered pairs with Even and Odd Sums
- Queries for number of distinct elements from a given index till last index in an array
- Permutations of string such that no two vowels are adjacent
- Find the number of positive integers less than or equal to N that have an odd number of digits
- 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 firstname.lastname@example.org. 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.