检查重新排列数组元素是否可以形成回文
给定一个大小为N的正整数数组arr ,任务是检查从数组元素的任何排列中形成的数字是否形成回文。
例子:
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”
方法:给定问题可以使用map来存储数组元素的频率来解决
- 存储所有数组元素的频率
- 检查每个元素的频率是否是偶数
- 对于频率为奇数的元素,如果只有一个这样的元素,则打印 Yes。否则打印编号
下面是上述方法的实现:
C++14
// C++ implementation of the above approach
#include
using namespace std;
#define MAX 256
// Function to check whether elements of
// an array can form a palindrome
bool can_form_palindrome(int arr[], int n)
{
// create an empty string
// to append elements of an array
string str = "";
// append each element to the string str to form
// a string so that we can solve it in easy way
for (int i = 0; i < n; i++) {
str += arr[i];
}
// Create a freq array and initialize all
// values as 0
int freq[MAX] = { 0 };
// For each character in formed string,
// increment freq in the corresponding
// freq array
for (int i = 0; str[i]; i++) {
freq[str[i]]++;
}
int count = 0;
// Count odd occurring characters
for (int i = 0; i < MAX; i++) {
if (freq[i] & 1) {
count++;
}
if (count > 1) {
return false;
}
}
// Return true if odd count is 0 or 1,
return true;
}
// Drive code
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
// Java implementation of the above approach
import java.io.*;
import java.util.Arrays;
class GFG{
static int MAX = 256;
// Function to check whether elements of
// an array can form a palindrome
static boolean can_form_palindrome(int []arr, int n)
{
// create an empty string
// to append elements of an array
String str = "";
// append each element to the string str to form
// a string so that we can solve it in easy way
for (int i = 0; i < n; i++) {
str += arr[i];
}
// Create a freq array and initialize all
// values as 0
int freq[] = new int[MAX];
Arrays.fill(freq,0);
// For each character in formed string,
// increment freq in the corresponding
// freq array
for (int i = 0; i 1) {
return false;
}
}
// Return true if odd count is 0 or 1,
return true;
}
// Drive code
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");
}
}
// This code is contributed by shivanisinghss2110
Python3
# python implementation of the above approach
# Function to check whether elements of
# an array can form a palindrome
def can_form_palindrome(arr, n):
MAX = 256
# create an empty string
# to append elements of an array
s = ""
# append each element to the string str to form
# a string so that we can solve it in easy way
for i in range(n) :
s = s + str(arr[i])
# Create a freq array and initialize all
# values as 0
freq = [0]*MAX
# For each character in formed string,
# increment freq in the corresponding
# freq array
for i in range(N) :
freq[arr[i]]=freq[arr[i]]+1
count = 0
# Count odd occurring characters
for i in range(MAX) :
if (freq[i] & 1) :
count=count+1
if (count > 1) :
return False
# Return true if odd count is 0 or 1,
return True
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 3, 1, 2 ]
N = len(arr)
# Function Call
if(can_form_palindrome(arr, N)):
print("YES")
else:
print("NO")
# This code is contributed by anudeep23042002
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 256;
// Function to check whether elements of
// an array can form a palindrome
static bool can_form_palindrome(int []arr, int n)
{
// create an empty string
// to append elements of an array
string str = "";
// append each element to the string str to form
// a string so that we can solve it in easy way
for (int i = 0; i < n; i++) {
str += arr[i];
}
// Create a freq array and initialize all
// values as 0
int []freq = new int[MAX];
Array.Clear(freq,0,MAX);
// For each character in formed string,
// increment freq in the corresponding
// freq array
for (int i = 0; i 1) {
return false;
}
}
// Return true if odd count is 0 or 1,
return true;
}
// Drive code
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");
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
YES
时间复杂度: O(N)
辅助空间: O(N)