📌  相关文章
📜  检查是否可以从给定数组的元素中获得对称加号

📅  最后修改于: 2021-04-22 03:57:09             🧑  作者: Mango

给定N个元素的数组arr [] ,任务是检查给定数组的元素是否可以使用对称加号。

方形对称加号的形式为:

Z
    Y
Z Y X Y Z
    Y
    Z

请注意,数组的所有元素都必须用于形成正方形。

例子:

Input: arr[] = {1, 2, 1, 1, 1}
Output: Yes
   1
1  2  1
   1

Input: arr[] = {1, 1, 1, 1, 2, 2, 2, 3, 2}
Output: Yes
    1
    2
1 2 3 2 1
    2
    1

Input: arr[] = {1, 1, 1, 4, 2, 2, 2, 3, 2}
Output: No

方法:为了形成对称的加号,数组所有元素的频率必须是4的倍数,并且必须有一个元素,当与4取模时其频率为1。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that return true if
// a symmetric is possible with
// the elements of the array
bool isPlusPossible(int arr[], int n)
{
  
    // Map to store the frequency
    // of the array elements
    unordered_map mp;
  
    // Traverse through array elements and
    // count frequencies
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
  
    bool foundModOne = false;
  
    // For every unique element
    for (auto x : mp) {
        int element = x.first;
        int frequency = x.second;
  
        if (frequency % 4 == 0)
            continue;
        if (frequency % 4 == 1) {
  
            // Element has already been found
            if (foundModOne)
                return false;
            foundModOne = true;
        }
  
        // The frequency of the element
        // something other than 0 and 1
        else
            return false;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 1, 1, 2, 2, 2, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    if (isPlusPossible(arr, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function that return true if
// a symmetric is possible with
// the elements of the array
static boolean isPlusPossible(int arr[], int n)
{
  
    // Map to store the frequency
    // of the array elements
    HashMap mp = new HashMap();
  
    // Traverse through array elements and
    // count frequencies
    for (int i = 0; i < n; i++)
    {
        if(mp.containsKey(arr[i]))
        {
            mp.put(arr[i], mp.get(arr[i]) + 1);
        }
        else
        {
            mp.put(arr[i], 1);
        }
    }
  
    boolean foundModOne = false;
  
    // For every unique element
    for (Map.Entry x : mp.entrySet()) 
    {
        int element = x.getKey();
        int frequency = x.getValue();
  
        if (frequency % 4 == 0)
            continue;
        if (frequency % 4 == 1) 
        {
  
            // Element has already been found
            if (foundModOne)
                return false;
            foundModOne = true;
        }
  
        // The frequency of the element
        // something other than 0 and 1
        else
            return false;
    }
    return true;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 1, 1, 2, 2, 2, 3, 2 };
    int n = arr.length;
  
    if (isPlusPossible(arr, n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function that return true if
# a symmetric is possible with
# the elements of the array
def isPlusPossible(arr, n):
  
    # Map to store the frequency
    # of the array elements
    mp = dict()
  
    # Traverse through array elements and
    # count frequencies
    for i in range(n):
        mp[arr[i]] = mp.get(arr[i], 0) + 1
  
    foundModOne = False
  
    # For every unique element
    for x in mp:
        element = x
        frequency = mp[x]
  
        if (frequency % 4 == 0):
            continue
        if (frequency % 4 == 1):
              
            # Element has already been found
            if (foundModOne == True):
                return False
            foundModOne = True
              
        # The frequency of the element
        # something other than 0 and 1
        else:
            return False
    return True    
  
# Driver code
arr = [1, 1, 1, 1, 2, 2, 2, 3, 2]
n = len(arr)
  
if (isPlusPossible(arr, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function that return true if
// a symmetric is possible with
// the elements of the array
static bool isPlusPossible(int []arr,
                           int n)
{
  
    // Map to store the frequency
    // of the array elements
    Dictionary mp = new Dictionary();
  
    // Traverse through array elements and
    // count frequencies
    for (int i = 0; i < n; i++)
    {
        if(mp.ContainsKey(arr[i]))
        {
            mp[arr[i]] = mp[arr[i]] + 1;
        }
        else
        {
            mp.Add(arr[i], 1);
        }
    }
  
    bool foundModOne = false;
  
    // For every unique element
    foreach(KeyValuePair x in mp)
    {
        int element = x.Key;
        int frequency = x.Value;
  
        if (frequency % 4 == 0)
            continue;
        if (frequency % 4 == 1) 
        {
  
            // Element has already been found
            if (foundModOne)
                return false;
            foundModOne = true;
        }
  
        // The frequency of the element
        // something other than 0 and 1
        else
            return false;
    }
    return true;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 1, 1, 2, 2, 2, 3, 2 };
    int n = arr.Length;
  
    if (isPlusPossible(arr, n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes