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
- Index of the elements which are equal to the sum of all succeeding elements
- 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
- Sum of elements till the smallest index such that there are no even numbers to its right
- Find the index of the element in an array which divides most elements before it
- Find an index such that difference between product of elements before and after it is minimum
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Queries for elements greater than K in the given index range using Segment Tree
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.