Check if an array arr[] can be rearranged such that arr[2 × i + 1] = 2* arr[2 × i] for every ith index
Given an array arr[] consisting of 2*N integers, the task is to check if it is possible to rearrange the array elements such that arr[2 * i + 1] = 2* arr[2 * i] for every ith index. If it is possible to do so, then print “Yes. Otherwise, print “No”.
Examples:
Input: arr[] = {4, -2, 2, -4}, N = 2
Output: Yes
Explanation: Rearrange the array as arr[] = {-2, -4, 2, 4}.
Input: arr[] = {3, 1, 3, 6}, N = 2
Output: No
Approach: The idea to solve the given problem is to use a Map and the observation that one needs N distinct pairs such that one element is double that of another element. Follow the steps below to solve the problem:
- Initialize a map < integer, integer >, say, count, to store the count of array elements.
- Traverse the array arr[] and update the count of each element in the Map count.
- Iterate over the map count and perform the following operations:
- Initialize a variable, say want, to form a pair with the current element, say X, and assign want = X/2, if X is less than 0. Otherwise, assign want = 2*X.
- Check if X is less than 0 and X is odd or the count of X is greater than the count of want, then print “No” as it is impossible to form the pair of remaining X with any other element of the array.
- Otherwise, update the count of want in the Map count as count(want) – count(X).
- After completing the above steps, print “Yes” as there exists any combination of elements that satisfy the given property.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string canReorderDoubled(vector< int > A)
{
map< int , int > count;
for ( int a : A)
count[a]++;
for ( auto x : count) {
if (x.second == 0)
continue ;
int xx = x.first;
int want = xx < 0 ? xx / 2 : xx * 2;
if (xx < 0 && xx % 2 != 0)
return "No" ;
if (x.second
> count[want])
return "No" ;
count[want] -= x.second;
}
return "Yes" ;
}
int main()
{
vector< int > arr = { 4, -2, 2, -4 };
int N = 2;
string res = canReorderDoubled(arr);
cout<<(res);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String canReorderDoubled( int [] A)
{
Map<Integer, Integer> count
= new TreeMap<>();
for ( int a : A)
count.put(
a, count.getOrDefault(a, 0 ) + 1 );
for ( int x : count.keySet()) {
if (count.get(x) == 0 )
continue ;
int want = x < 0 ? x / 2 : x * 2 ;
if (x < 0 && x % 2 != 0 )
return "No" ;
if (count.get(x)
> count.getOrDefault(want, 0 ))
return "No" ;
count.put(want,
count.get(want)
- count.get(x));
}
return "Yes" ;
}
public static void main(String[] args)
{
int [] arr = { 4 , - 2 , 2 , - 4 };
int N = 2 ;
String res = canReorderDoubled(arr);
System.out.println(res);
}
}
|
Python3
def canReorderDoubled(A):
count = {}
for a in A:
if a in count:
count[a] + = 1
else :
count[a] = 1
for key,value in count.items():
if (value = = 0 ):
continue
xx = key
if xx < 0 :
want = xx / 2
else :
want = xx * 2
if (xx < 0 and xx % 2 ! = 0 ):
return "No"
if (want in count and value > count[want]):
return "No"
if want in count:
count[want] - = value
return "Yes"
if __name__ = = '__main__' :
arr = [ 4 , - 2 , 2 , - 4 ]
N = 2
res = canReorderDoubled(arr)
print (res)
|
C#
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
public static string CanReorderDoubled( int [] A)
{
Dictionary< int , int > count
= new Dictionary< int , int >();
foreach ( int a in A)
{
if (count.ContainsKey(a))
{
count[a]++;
}
else
{
count[a] = 1;
}
}
foreach ( int x in count.Keys)
{
if (count[x] == 0)
continue ;
int want = x < 0 ? x / 2 : x * 2;
if (x < 0 && x % 2 != 0)
return "No" ;
if (count[x]
> count.GetValueOrDefault(want, 0))
return "Yes" ;
count[want] =
count.GetValueOrDefault(want, 0)
- count[x];
}
return "Yes" ;
}
static void Main( string [] args)
{
int [] arr = { 4, -2, 2, -4 };
string res = CanReorderDoubled(arr);
Console.WriteLine(res);
}
}
}
|
Javascript
<script>
function canReorderDoubled(A)
{
var count= new Map();
A.forEach(a => {
if (count.has(a))
count.set(a, count.get(a)+1)
else
count.set(a, 1)
});
count.forEach((value, key) => {
if (value != 0)
{
var xx = key;
var want = xx < 0 ? xx / 2 : xx * 2;
if (xx < 0 && xx % 2 != 0)
return "No" ;
if (value
> count.get(want))
return "No" ;
if (count.has(want))
count.set(want, count.get(want)-value)
}
});
return "Yes" ;
}
var arr = [4, -2, 2, -4];
var N = 2;
var res = canReorderDoubled(arr);
document.write(res);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Last Updated :
11 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...