📌  相关文章
📜  检查数组的元素是否可以满足给定条件

📅  最后修改于: 2021-04-29 10:17:42             🧑  作者: Mango

给定N个(偶数)个整数元素的数组arr 。任务是检查是否可以对数组的元素进行重新排序,使得:

arr[2*i + 1] = 2 * A[2 * i] 

for i = 0 ... N-1. 

打印,如果可能的话,否则打印

例子:

方法:的想法是,如果k是数组中当前的最小元素,则它必须与2 * k配对,因为不存在任何其他与k / 2配对的元素。
我们按升序检查元素。当我们检查元素k并且不使用它时,它必须与2 * k配对。我们将尝试将k排列为2 * k,但是如果不能,则答案为False 。最后,如果所有操作都成功,则打印True
我们将存储每个元素的计数,以跟踪我们尚未考虑的内容。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return true if the elements 
// can be arranged in the desired order
string canReorder(int A[],int n)
{
    map m;
      
    for(int i=0;i


Java
// Java implementation of the approach 
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
  
class GfG
{
      
    // Function to return true if the elements 
    // can be arranged in the desired order 
    static String canReorder(int A[],int n) 
    { 
        HashMap m = new HashMap<>(); 
          
        for(int i = 0; i < n; i++)
        { 
              
            if (m.containsKey(A[i]))
                m.put(A[i], m.get(A[i]) + 1);
            else
                m.put(A[i], 1);
        }
          
        Arrays.sort(A); 
        int count = 0; 
      
        for(int i = 0; i < n; i++) 
        { 
            if (m.get(A[i]) == 0) 
                continue; 
      
            // If 2 * x is not found to pair 
            if (m.containsKey(2 * A[i]))
            { 
                  
                count += 2; 
                  
                // Remove an occurrence of x 
                // and an occurrence of 2 * x 
                m.put(A[i], m.get(A[i]) - 1); 
                m.put(2 * A[i], m.get(2 * A[i]) - 1); 
            } 
        } 
          
        if(count == n) 
            return "true"; 
        else
            return "false"; 
    } 
  
    // Driver code
    public static void main(String []args)
    {
          
        int A[] = {4, -2, 2, -4}; 
        int n = A.length; 
          
        // Function call to print required answer 
        System.out.println(canReorder(A,n));
    }
}
  
// This code is contributed by Rituraj Jain


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))


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GFG
{
      
// Function to return true if the elements 
// can be arranged in the desired order 
static String canReorder(int []A,int n) 
{ 
    Dictionary m = new Dictionary();
      
    for(int i = 0; i < n; i++)
    { 
        if (m.ContainsKey(A[i]))
            m[A[i]]= m[A[i]] + 1;
        else
            m.Add(A[i], 1);
    }
      
    Array.Sort(A); 
    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.ContainsKey(2 * A[i]))
        { 
            count += 2; 
              
            // Remove an occurrence of x 
            // and an occurrence of 2 * x 
            m[A[i]]= m[A[i]] - 1;
            if (m.ContainsKey(2 * A[i]))
                m[2 * A[i]]= m[2 * A[i]] - 1;
            else
                m.Add(2 * A[i], m[2 * A[i]] - 1); 
        } 
    } 
    if(count == n) 
        return "True"; 
    else
        return "False"; 
} 
  
// Driver code
public static void Main(String []args)
{
    int []A = {4, -2, 2, -4}; 
    int n = A.Length; 
      
    // Function call to print required answer 
    Console.WriteLine(canReorder(A,n));
}
}
  
// This code is contributed by Rajput-Ji


输出:
True