Check if a symmetric plus is possible from the elements of the given array
Given an array arr[] of N elements, the task is to check whether asymmetric plus is possible with the elements of the given array.
A square symmetric plus is of the form:
Z
Y
Z Y X Y Z
Y
Z
Note that all the elements of the array must be used in forming the square.
Examples:
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
Approach: In order to form an asymmetric plus, the frequency of all the elements of the array must be a multiple of 4 and there has to be an element whose frequency gives 1 when modulo with 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPlusPossible( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
bool foundModOne = false ;
for ( auto x : mp) {
int element = x.first;
int frequency = x.second;
if (frequency % 4 == 0)
continue ;
if (frequency % 4 == 1) {
if (foundModOne)
return false ;
foundModOne = true ;
}
else
return false ;
}
}
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
import java.util.*;
class GFG
{
static boolean isPlusPossible( int arr[], int n)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
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 (Map.Entry<Integer,Integer> x : mp.entrySet())
{
int element = x.getKey();
int frequency = x.getValue();
if (frequency % 4 == 0 )
continue ;
if (frequency % 4 == 1 )
{
if (foundModOne)
return false ;
foundModOne = true ;
}
else
return false ;
}
return true ;
}
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" );
}
}
|
Python3
def isPlusPossible(arr, n):
mp = dict ()
for i in range (n):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
foundModOne = False
for x in mp:
element = x
frequency = mp[x]
if (frequency % 4 = = 0 ):
continue
if (frequency % 4 = = 1 ):
if (foundModOne = = True ):
return False
foundModOne = True
else :
return False
return True
arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 ]
n = len (arr)
if (isPlusPossible(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isPlusPossible( int []arr,
int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
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 ;
foreach (KeyValuePair< int , int > x in mp)
{
int element = x.Key;
int frequency = x.Value;
if (frequency % 4 == 0)
continue ;
if (frequency % 4 == 1)
{
if (foundModOne)
return false ;
foundModOne = true ;
}
else
return false ;
}
return true ;
}
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" );
}
}
|
Javascript
<script>
function isPlusPossible(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1)
}
var foundModOne = false ;
var ans = true ;
mp.forEach((value, key) => {
var element = key;
var frequency = value;
if (frequency % 4 != 0)
{
if (frequency % 4 == 1) {
if (foundModOne)
ans = false
foundModOne = true ;
}
else
ans = false
}
});
return ans;
}
var arr = [1, 1, 1, 1, 2, 2, 2, 3, 2];
var n = arr.length;
if (isPlusPossible(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...