Given an array of N elements where each element i, the absolute difference between total elements to the right and left of it are given. Find the number of possible ordering of the actual array elements.
Examples:
Input : N = 5, arr[] = {2, 4, 4, 0, 2}
Output : 4
There are four possible orders, as follows:
2, 1, 4, 5, 3
2, 5, 4, 1, 3
3, 1, 4, 5, 2
3, 5, 4, 1, 2Input : N = 7, arr[] = {6, 4, 0, 2, 4, 0, 2}
Output : 0
No any valid order is possible hence answer is 0.
Approach: Divide the problem into two parts. When N is odd and when N is even.
- Case 1 : When N is odd.
Consider N = 7, there are 7 empty spaces and the absolute difference between the elements to the left and right must be like [6 4 2 0 2 4 6]. Observe that the element which is at the middle must have absolute difference 0, while other elements are from 2 to N-1 and each of their counts should be 2. If it doesn’t fulfil it then there is no valid order else for each element i from 2 to N-1 we have 2 ways to fill the spaces, hence total ways will be the product of all the ways. -
Case 2 : When N is even.
Consider N = 6, There are 6 spaces and it will be like [5 3 1 1 3 5], where a[i] gives the absolute difference between the number of elements to the left and right. For each a[i] we have 2 ways, hence answer will be the product of all the ways.
Below is the implementation of the approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the number of permutations // possible of the original array to satisfy // the given absolute differences int totalways(int* arr, int n) { // To store the count of each // a[i] in a map unordered_map<int, int> cnt; for (int i = 0; i < n; ++i) { cnt[arr[i]]++; } // if n is odd if (n % 2 == 1) { int start = 0, endd = n - 1; // check the count of each whether // it satisfy the given criteria or not for (int i = start; i <= endd; i = i + 2) { if (i == 0) { // there is only 1 way // for middle element. if (cnt[i] != 1) { return 0; } } else { // for others there are 2 ways. if (cnt[i] != 2) { return 0; } } } // now find total ways int ways = 1; start = 2, endd = n - 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } // When n is even. else if (n % 2 == 0) { // there will be no middle element so // for each a[i] there will be 2 ways int start = 1, endd = n - 1; for (int i = 1; i <= endd; i = i + 2) { if (cnt[i] != 2) return 0; } int ways = 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } } // Driver Code int main() { int N = 5; int arr[N] = { 2, 4, 4, 0, 2 }; cout<<totalways(arr, N); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { // Function to find the number of permutations // possible of the original array to satisfy // the given absolute differences static int totalways(int[] arr, int n) { // To store the count of each // a[i] in a map HashMap<Integer, Integer>cnt = new HashMap<Integer, Integer>(); for (int i = 0 ; i < n; i++) { if(cnt.containsKey(arr[i])) { cnt.put(arr[i], cnt.get(arr[i])+1); } else { cnt.put(arr[i], 1); } } // if n is odd if (n % 2 == 1) { int start = 0, endd = n - 1; // check the count of each whether // it satisfy the given criteria or not for (int i = start; i <= endd; i = i + 2) { if (i == 0) { // there is only 1 way // for middle element. if (cnt.get(i) != 1) { return 0; } } else { // for others there are 2 ways. if (cnt.get(i) != 2) { return 0; } } } // now find total ways int ways = 1; start = 2; endd = n - 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } // When n is even. else if (n % 2 == 0) { // there will be no middle element so // for each a[i] there will be 2 ways int start = 1, endd = n - 1; for (int i = 1; i <= endd; i = i + 2) { if (cnt.get(i) != 2) return 0; } int ways = 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } return Integer.MIN_VALUE; } // Driver Code public static void main(String[] args) { int N = 5; int []arr = { 2, 4, 4, 0, 2 }; System.out.println(totalways(arr, N)); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the above approach # Function to find the number of permutations # possible of the original array to satisfy # the given absolute differences def totalways(arr, n): # To store the count of each # a[i] in a map cnt = dict() for i in range(n): cnt[arr[i]] = cnt.get(arr[i], 0) + 1 # if n is odd if (n % 2 == 1): start, endd = 0, n - 1 # check the count of each whether # it satisfy the given criteria or not for i in range(start, endd + 1, 2): if (i == 0): # there is only 1 way # for middle element. if (cnt[i] != 1): return 0 else: # for others there are 2 ways. if (cnt[i] != 2): return 0 # now find total ways ways = 1 start = 2 endd = n - 1 for i in range(start, endd + 1, 2): ways = ways * 2 return ways # When n is even. elif (n % 2 == 0): # there will be no middle element so # for each a[i] there will be 2 ways start = 1 endd = n - 1 for i in range(1, endd + 1, 2): if (cnt[i] != 2): return 0 ways = 1 for i in range(start, endd + 1, 2): ways = ways * 2 return ways # Driver Code N = 5 arr = [2, 4, 4, 0, 2 ] print(totalways(arr, N)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to find the number of permutations // possible of the original array to satisfy // the given absolute differences static int totalways(int[] arr, int n) { // To store the count of each // a[i] in a map Dictionary<int, int> cnt = new Dictionary<int, int>(); for (int i = 0 ; i < n; i++) { if(cnt.ContainsKey(arr[i])) { cnt[arr[i]] = cnt[arr[i]] + 1; } else { cnt.Add(arr[i], 1); } } // if n is odd if (n % 2 == 1) { int start = 0, endd = n - 1; // check the count of each whether // it satisfy the given criteria or not for (int i = start; i <= endd; i = i + 2) { if (i == 0) { // there is only 1 way // for middle element. if (cnt[i] != 1) { return 0; } } else { // for others there are 2 ways. if (cnt[i] != 2) { return 0; } } } // now find total ways int ways = 1; start = 2; endd = n - 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } // When n is even. else if (n % 2 == 0) { // there will be no middle element so // for each a[i] there will be 2 ways int start = 1, endd = n - 1; for (int i = 1; i <= endd; i = i + 2) { if (cnt[i] != 2) return 0; } int ways = 1; for (int i = start; i <= endd; i = i + 2) { ways = ways * 2; } return ways; } return int.MinValue; } // Driver Code public static void Main(String[] args) { int N = 5; int []arr = { 2, 4, 4, 0, 2 }; Console.WriteLine(totalways(arr, N)); } } // This code is contributed by 29AjayKumar |
4
Time Complexity : O(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.
Recommended Posts:
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Longest subsequence of a number having same left and right rotation
- Print the two possible permutations from a given sequence
- Count of distinct permutations of every possible length of given string
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Maximum time such that absolute difference between hour and minute lies in given range
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Maximum distance between two elements whose absolute difference is K
- Position after taking N steps to the right and left in an alternate manner
- Generate all possible permutations of a Number divisible by N
- Count number of right triangles possible with a given perimeter
- Possible permutations at a railway track
- Reorder an array such that sum of left half is not equal to sum of right half
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Maximum sum path in a matrix from top-left to bottom-right
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

