Check if rearranging Array elements can form a Palindrome or not
Last Updated :
25 Aug, 2021
Given a positive integer array arr of size N, the task is to check if number formed, from any arrangement of array elements, form a palindrome or not.
Examples:
Input: arr = [1, 2, 3, 1, 2]
Output: Yes
Explanation: The elements of a given array can be rearranged as 1, 2, 3, 2, 1.
Since 12321 is a palindrome, so output will be “Yes”
Input: arr = [1, 2, 3, 4, 1]
Output: No
Explanation: The elements of a given array cannot be rearranged to form a palindrome within all the possible permutations. So the output will be “No”
Approach: Given problem can be solved using map to store the frequency of array elements
- Store the frequency of all array elements
- Check if frequency of each element is even
- For element whose frequency is odd, if there is only one such element, then print Yes. Else print No.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define MAX 256
bool can_form_palindrome( int arr[], int n)
{
string str = "" ;
for ( int i = 0; i < n; i++) {
str += arr[i];
}
int freq[MAX] = { 0 };
for ( int i = 0; str[i]; i++) {
freq[str[i]]++;
}
int count = 0;
for ( int i = 0; i < MAX; i++) {
if (freq[i] & 1) {
count++;
}
if (count > 1) {
return false ;
}
}
return true ;
}
int main()
{
int arr[] = { 1, 2, 3, 1, 2 };
int n = sizeof (arr) / sizeof ( int );
can_form_palindrome(arr, n)
? cout << "YES"
: cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static int MAX = 256 ;
static boolean can_form_palindrome( int []arr, int n)
{
String str = "" ;
for ( int i = 0 ; i < n; i++) {
str += arr[i];
}
int freq[] = new int [MAX];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i<str.length(); i++) {
freq[str.charAt(i)]++;
}
int count = 0 ;
for ( int i = 0 ; i < MAX; i++) {
if ((freq[i] & 1 )!= 0 ) {
count++;
}
if (count > 1 ) {
return false ;
}
}
return true ;
}
public static void main (String[] args)
{
int []arr = { 1 , 2 , 3 , 1 , 2 };
int n = arr.length;
if (can_form_palindrome(arr, n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def can_form_palindrome(arr, n):
MAX = 256
s = ""
for i in range (n) :
s = s + str (arr[i])
freq = [ 0 ] * MAX
for i in range (N) :
freq[arr[i]] = freq[arr[i]] + 1
count = 0
for i in range ( MAX ) :
if (freq[i] & 1 ) :
count = count + 1
if (count > 1 ) :
return False
return True
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 1 , 2 ]
N = len (arr)
if (can_form_palindrome(arr, N)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 256;
static bool can_form_palindrome( int []arr, int n)
{
string str = "" ;
for ( int i = 0; i < n; i++) {
str += arr[i];
}
int []freq = new int [MAX];
Array.Clear(freq,0,MAX);
for ( int i = 0; i<str.Length; i++) {
freq[str[i]]++;
}
int count = 0;
for ( int i = 0; i < MAX; i++) {
if ((freq[i] & 1)!=0) {
count++;
}
if (count > 1) {
return false ;
}
}
return true ;
}
public static void Main()
{
int []arr = { 1, 2, 3, 1, 2 };
int n = arr.Length;
if (can_form_palindrome(arr, n))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
let MAX = 256
function can_form_palindrome(arr, n)
{
let str = "" ;
for (let i = 0; i < n; i++) {
str += toString(arr[i]);
}
let freq = new Array(n).fill(0);
for (let i = 0; i < str.length; i++) {
freq[str.charCodeAt(i)]++;
}
let count = 0;
for (let i = 0; i < MAX; i++) {
if (freq[i] & 1) {
count++;
}
if (count > 1) {
return false ;
}
}
return true ;
}
let arr = [1, 2, 3, 1, 2];
let n = arr.length;
can_form_palindrome(arr, n)
? document.write( "YES" )
: document.write( "NO" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...