Given an array **arr** of **N** (even) integer elements. The task is to check if it is possible to reorder the elements of the array such that:

arr[2*i + 1] = 2 * A[2 * i]fori = 0 ... N-1.

Print **True** if it is possible, otherwise print **False**.

**Examples:**

Input:arr[] = {4, -2, 2, -4}

Output:True

{-2, -4, 2, 4} is a valid arrangement, -2 * 2 = -4 and 2 * 2 = 4

Input:arr[] = {1, 2, 4, 16, 8, 4}

Output:False

**Approach:** The idea is that, if **k** is current minimum element in the array then it must pair with **2 * k** as there does not exist any other element **k / 2** to pair it with.

We check elements in ascending order. When we check an element **k** and it isn’t used, it must pair with **2 * k**. We will attempt to arrange **k** followed by **2 * k** however if we can’t, then the answer is **False**. In the end, if all the operations are successful, then print **True**.

We will store a count of each element to keep track of what we have not yet considered.

Below is the implementation of above approach:

## C++

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return true if the elements ` `// can be arranged in the desired order ` `string canReorder(` `int` `A[],` `int` `n) ` `{ ` ` ` `map<` `int` `,` `int` `> m; ` ` ` ` ` `for` `(` `int` `i=0;i<n;i++) ` ` ` `m[A[i]]++; ` ` ` ` ` `sort(A,A+n); ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i=0;i<n;i++) ` ` ` `{ ` ` ` `if` `(m[A[i]] == 0) ` ` ` `continue` `; ` ` ` ` ` `// If 2 * x is not found to pair ` ` ` `if` `(m[2 * A[i]]){ ` ` ` ` ` `count+=2; ` ` ` ` ` `// Remove an occurrence of x ` ` ` `// and an occurrence of 2 * x ` ` ` `m[A[i]] -= 1; ` ` ` `m[2 * A[i]] -= 1; ` ` ` `} ` ` ` `} ` ` ` `if` `(count ==n) ` ` ` `return` `"true"` `; ` ` ` `else` ` ` `return` `"false"` `; ` `} ` ` ` ` ` `// Driver Code ` `int` `main() ` `{ ` `int` `A[] = {4, -2, 2, -4}; ` `int` `n= ` `sizeof` `(A)/` `sizeof` `(` `int` `); ` ` ` `// Function call to print required answer ` `cout<<(canReorder(A,n)); ` ` ` `return` `0; ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation of the approach ` `import` `collections ` ` ` `# Function to return true if the elements ` `# can be arranged in the desired order ` `def` `canReorder(A): ` ` ` ` ` `count ` `=` `collections.Counter(A) ` ` ` ` ` `for` `x ` `in` `sorted` `(A, key ` `=` `abs` `): ` ` ` `if` `count[x] ` `=` `=` `0` `: ` ` ` `continue` ` ` ` ` `# If 2 * x is not found to pair ` ` ` `if` `count[` `2` `*` `x] ` `=` `=` `0` `: ` ` ` `return` `False` ` ` ` ` `# Remove an occurrence of x ` ` ` `# and an occurrence of 2 * x ` ` ` `count[x] ` `-` `=` `1` ` ` `count[` `2` `*` `x] ` `-` `=` `1` ` ` ` ` `return` `True` ` ` ` ` `# Driver Code ` `A ` `=` `[` `4` `, ` `-` `2` `, ` `2` `, ` `-` `4` `] ` ` ` `# Function call to print required answer ` `print` `(canReorder(A)) ` |

*chevron_right*

*filter_none*

**Output:**

True

## Recommended Posts:

- Largest sub-set possible for an array satisfying the given condition
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1
- Check if an array can be Arranged in Left or Right Positioned Array
- Python | Remove first K elements matching some condition
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Count valid pairs in the array satisfying given conditions
- Check if all array elements are distinct
- Check if an array contains all elements of a given range
- Check if all elements of the array are palindrome or not
- Check if a given array contains duplicate elements within k distance from each other
- Pairs from an array that satisfy the given condition
- Check if LCM of array elements is divisible by a prime number or not
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if array elements are consecutive | Added Method 3
- Check if the array has an element which is equal to product of remaining 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.