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.
- Even numbers at even index and odd numbers at odd index
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Check if every index i has an index j such that sum of elements in both directions are equal
- Count of permutations such that sum of K numbers from given range is even
- Count subarrays having sum of elements at even and odd positions equal
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Length of longest Subarray with equal number of odd and even elements
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Sum of elements till the smallest index such that there are no even numbers to its right
- Rearrange array such that all even-indexed elements in the Array is even
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Find Kth element in an array containing odd elements first and then even elements
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- 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 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.