📌  相关文章
📜  其元素可以重新排列以形成回文的子数组的计数

📅  最后修改于: 2022-05-13 01:57:51.532000             🧑  作者: Mango

其元素可以重新排列以形成回文的子数组的计数

给定一个大小为n的数组arr[] 。任务是计算可能的子数组的数量,以便可以重新排列它们的元素以形成回文。
例子:

方法:有几点意见:

  • 要创建一个偶数长度的回文数,所有不同的数字都需要偶数出现。
  • 要创建一个奇数长度的回文,必须只有一个奇数出现。

现在,棘手的部分是确定是否可以将数组的特定部分制成 O(1) 复杂度的回文。我们可以使用 XOR 来实现:

  • 对于每个数字 m,我们可以在 xor 计算中将其用作 2^n,以便它包含单个设置位。
  • 如果一个部分的所有元素的异或为0 ,则意味着该部分的所有不同数字的出现都是偶数。
  • 如果一个部分的所有元素的异或大于0则意味着:
    • 要么有多个不同的数字出现奇数,在这种情况下,该部分不能重新排列以形成回文
    • 或者恰好是一个奇数出现的数字(该数字的二进制表示将只有 1 个设置位)。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
typedef signed long long ll;
 
// Function that returns true if n is a
// power of 2 i.e. n has only 1 set bit
bool is_power_of_two(ll n)
{
    return !(n & (n - 1LL));
}
 
// Function to return the count
// of all valid sub-arrays
int countSubArrays(int arr[], int n)
{
 
    // To store the count of valid sub-arrays
    int cnt = 0;
 
    for (int j = 0; j < n; j++) {
        ll xorval = 0LL;
        for (int k = j; k < n; k++) {
 
            // num = 2 ^ arr[k]
            ll num = 1LL << arr[k];
            xorval ^= num;
 
            // If frequency of all the elements of the
            // sub-array is even or there is only a
            // single element with odd frequency
            if (xorval == 0LL || is_power_of_two(xorval))
                cnt++;
        }
    }
 
    // Return the required count
    return cnt;
}
 
// Driver code
int main()
{
 
    int arr[] = { 1, 2, 3, 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countSubArrays(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GfG
{
     
static long ll;
 
// Function that returns true if n is a
// power of 2 i.e. n has only 1 set bit
static boolean is_power_of_two(long n)
{
    return n!=0 &&  (n & (n - 1))==0;
}
 
// Function to return the count
// of all valid sub-arrays
static int countSubArrays(int arr[], int n)
{
 
    // To store the count of valid sub-arrays
    int cnt = 0;
 
    for (int j = 0; j < n; j++)
    {
        long xorval = 0;
        for (int k = j; k < n; k++)
        {
 
            // num = 2 ^ arr[k]
            long num = 1 << arr[k];
            xorval ^= num;
 
            // If frequency of all the elements of the
            // sub-array is even or there is only a
            // single element with odd frequency
            if (xorval == 0 || is_power_of_two(xorval))
                cnt++;
        }
    }
 
    // Return the required count
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
 
    int arr[] = { 1, 2, 3, 1, 2, 3, 4 };
    int n = arr.length;
    System.out.println(countSubArrays(arr, n));
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 implementation of the approach
 
# Function that returns true if n is a
# power of 2 i.e. n has only 1 set bit
def is_power_of_two(n):
 
    return 0 if(n & (n - 1)) else 1;
 
# Function to return the count
# of all valid sub-arrays
def countSubArrays(arr, n):
 
    # To store the count of valid sub-arrays
    cnt = 0;
 
    for j in range(n):
        xorval = 0;
        for k in range(j, n):
 
            # num = 2 ^ arr[k]
            num = 1 << arr[k];
            xorval ^= num;
 
            # If frequency of all the elements of the
            # sub-array is even or there is only a
            # single element with odd frequency
            if (xorval == 0 or is_power_of_two(xorval)):
                cnt += 1;
 
    # Return the required count
    return cnt;
 
# Driver code
arr = [ 1, 2, 3, 1, 2, 3, 4 ];
n = len(arr);
print(countSubArrays(arr, n));
 
# This code is contributed by mits


C#
// C# implementation of the approach
using System;
     
class GfG
{
     
static long ll;
 
// Function that returns true if n is a
// power of 2 i.e. n has only 1 set bit
static bool is_power_of_two(long n)
{
    //return !(n & (n - 1));
    return false;
}
 
// Function to return the count
// of all valid sub-arrays
static int countSubArrays(int []arr, int n)
{
 
    // To store the count of valid sub-arrays
    int cnt = 0;
 
    for (int j = 0; j < n; j++)
    {
        long xorval = 0;
        for (int k = j; k < n; k++)
        {
 
            // num = 2 ^ arr[k]
            long num = 1 << arr[k];
            xorval ^= num;
 
            // If frequency of all the elements of the
            // sub-array is even or there is only a
            // single element with odd frequency
            if (xorval == 0 || is_power_of_two(xorval))
                cnt++;
        }
    }
 
    // Return the required count
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
 
    int []arr = { 1, 2, 3, 1, 2, 3, 4 };
    int n = arr.Length;
    Console.WriteLine(countSubArrays(arr, n) + "1");
}
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
11