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
- Queries for number of distinct elements from a given index till last index in an array
- 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 value to add to arr[i] so that an array can be split at index i with equal sum
- 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
- Find an index such that difference between product of elements before and after it is minimum
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Queries to answer the number of ones and zero to the left of given index
- 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
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
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.