📌  相关文章
📜  检查数组arr []是否可以重新排列,使得每个ith索引的arr [2×i + 1] = 2 * arr [2×i]

📅  最后修改于: 2021-05-13 23:46:30             🧑  作者: Mango

给定一个由2 * N个整数组成的数组arr [] ,任务是检查是否有可能重新排列数组元素,使得每iarr [2 * i + 1] = 2 * arr [2 * i]指数。如果可能的话,请打印“是。否则,打印“否”

例子:

方法:解决给定问题的想法是使用Map,并观察到一个人需要N个不同的对,以使一个元素是另一元素的两倍。请按照以下步骤解决问题:

  • 初始化映射,例如count ,以存储数组元素的数量。
  • 遍历数组arr []并更新Map count中每个元素的计数
  • 遍历映射计数并执行以下操作:
    • 如果X小于0 ,则初始化一个变量例如want与当前元素X组成一对,并分配want = X / 2 。否则,分配want = 2 * X。
    • 检查X是否小于0X为奇数,或者X的计数大于want的计数,然后打印“否” ,因为不可能与数组的任何其他元素形成一对剩余的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.


输出:
Yes

时间复杂度: O(N * log N)
辅助空间: O(N)