给定一个由2*N 个整数组成的数组arr[] ,任务是检查是否可以重新排列数组元素,使得arr[2 * i + 1] = 2* arr[2 * i]对于每第i个指数。如果可能,则打印“Yes .否则,打印“否” 。
例子:
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
方法:解决给定问题的想法是使用 Map 并观察到需要N 个不同的对,这样一个元素是另一个元素的两倍。请按照以下步骤解决问题:
- 初始化一个 map < integer, integer > ,比如count ,来存储数组元素的数量。
- 遍历数组arr[]并更新 Map count中每个元素的计数。
- 迭代地图计数并执行以下操作:
- 初始化一个变量,比如want ,与当前元素形成一对,比如X ,如果X小于0 ,则赋值want = X/2 。否则,分配想要 = 2*X。
- 检查,如果X小于0且X为奇数或X的计数大于想要的计数时,则输出“否”,因为它是不能形成一对与所述阵列的任何其它元件其余的X的。
- 否则,想更新地图中的计数数作为计数(想) -数(X)。
- 完成上述步骤后,打印“是” ,因为存在满足给定属性的任何元素组合。
下面是上述方法的实现:
C++
// cpp program of the above approach
#include
using namespace std;
// Function to check if it is possible
// to rearrange the array elements
// that satisfies the given conditions
string canReorderDoubled(vector A)
{
// Stores the count of elements
map count;
// Update the hash table
for (int a : A)
count[a]++;
// Traverse the hash table
for (auto x : count) {
// If the count of current
// element is zero
if (x.second == 0)
continue;
// Stores the element needed
// to form a pair with the
// current element
int xx = x.first;
int want = xx < 0 ? xx / 2 : xx * 2;
// If x is less than zero and odd
if (xx < 0 && xx % 2 != 0)
return "No";
// If count of x is greater
// than count of want
if (x.second
> count[want])
return "No";
// Update the count of want
// in the hash table
count[want] -= x.second;
}
// Return true if none of the
// above cases satisfies
return "Yes";
}
// Driver Code
int main()
{
vector arr = { 4, -2, 2, -4 };
int N = 2;
string res = canReorderDoubled(arr);
// Print the result obtained
cout<<(res);
}
// This code is contributed by mohit kumar 29.
Java
// Java program of the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to check if it is possible
// to rearrange the array elements
// that satisfies the given conditions
public static String canReorderDoubled(int[] A)
{
// Stores the count of elements
Map count
= new TreeMap<>();
// Update the hash table
for (int a : A)
count.put(
a, count.getOrDefault(a, 0) + 1);
// Traverse the hash table
for (int x : count.keySet()) {
// If the count of current
// element is zero
if (count.get(x) == 0)
continue;
// Stores the element needed
// to form a pair with the
// current element
int want = x < 0 ? x / 2 : x * 2;
// If x is less than zero and odd
if (x < 0 && x % 2 != 0)
return "No";
// If count of x is greater
// than count of want
if (count.get(x)
> count.getOrDefault(want, 0))
return "No";
// Update the count of want
// in the hash table
count.put(want,
count.get(want)
- count.get(x));
}
// Return true if none of the
// above cases satisfies
return "Yes";
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 4, -2, 2, -4 };
int N = 2;
String res = canReorderDoubled(arr);
// Print the result obtained
System.out.println(res);
}
}
Python3
# Python 3 program of the above approach
# Function to check if it is possible
# to rearrange the array elements
# that satisfies the given conditions
def canReorderDoubled(A):
# Stores the count of elements
count = {}
# Update the hash table
for a in A:
if a in count:
count[a] += 1
else:
count[a] = 1
# Traverse the hash table
for key,value in count.items():
# If the count of current
# element is zero
if (value == 0):
continue
# Stores the element needed
# to form a pair with the
# current element
xx = key
if xx < 0:
want = xx / 2
else:
want = xx * 2
# If x is less than zero and odd
if (xx < 0 and xx % 2 != 0):
return "No"
# If count of x is greater
# than count of want
if (want in count and value > count[want]):
return "No"
# Update the count of want
# in the hash table
if want in count:
count[want] -= value
# Return true if none of the
# above cases satisfies
return "Yes"
# Driver Code
if __name__ == '__main__':
arr = [4, -2, 2, -4]
N = 2
res = canReorderDoubled(arr)
# Print the result obtained
print(res)
# This code is contributed by bgangwar59.
Javascript
输出:
Yes
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。