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

📅  最后修改于: 2021-10-27 06:19:44             🧑  作者: Mango

给定一个由N (偶数)个整数元素组成的数组arr 。任务是检查是否可以重新排序数组的元素,以便:

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

for i = 0 ... N-1. 

如果可能,打印True ,否则打印False
例子:

方法:这个想法是,如果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


Javascript


输出:
True

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程