📜  检查任何子数组的总和是否是回文

📅  最后修改于: 2021-05-17 03:28:05             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是检查是否存在大小至少为2的子数组,以使其总和为回文。如果存在这样的子数组,则打印YES 。否则,打印NO

例子:

方法:
要解决此问题,请执行以下步骤:

  • 创建给定数组的前缀和数组。
  • 使用嵌套的for循环遍历数组,以表示子数组的开始和结束。索引[x,y]中子数组的总和可以通过pref [y] – pref [x – 1]获得
  • 检查此总和是否是回文。如果是回文,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ program to check if sum of any
// subarray of size atleast 2 is
// palindrome or not
  
#include 
using namespace std;
  
// Function which checks whether
// a given number is palindrome or not
bool checkPalindrome(int n)
{
    // Store the reverse of
    // the number n
    int rev = 0;
    for (int x = n; x != 0; x /= 10) {
        int d = x % 10;
        rev = rev * 10 + d;
    }
    if (rev == n)
        return true;
  
    else
        return false;
}
  
// Function which checks if the
// requires subarray exists or not
void findSubarray(int ar[], int n)
{
    // Making a prefix sum array of ar[]
    int pref[n];
    pref[0] = ar[0];
  
    for (int x = 1; x < n; x++)
        pref[x] = pref[x - 1] + ar[x];
  
    // Boolean variable that will store
    // whether such subarray exists or not
    bool found = false;
    for (int x = 0; x < n; x++) {
        for (int y = x + 1; y < n; y++) {
            // sum stores the sum of subarray
            // from index x to y of array
            int sum = pref[y];
            if (x > 0) {
                sum -= pref[x - 1];
            }
            if (checkPalindrome(sum)) {
                // Required subarray is found
                found = true;
                break;
            }
        }
  
        if (found)
            break;
    }
    if (found)
        cout << "Yes" << endl;
  
    else
        cout << "No" << endl;
}
  
// Driver code
int main()
{
    int ar[] = { 1, 11, 20, 35 };
  
    int n = sizeof(ar) / sizeof(ar[0]);
  
    findSubarray(ar, n);
  
    return 0;
}


Java
// Java program to check if sum of any 
// subarray of size atleast 2 is 
// palindrome or not 
class GFG{
      
// Function which checks whether 
// a given number is palindrome or not 
static boolean checkPalindrome(int n) 
{ 
      
    // Store the reverse of 
    // the number n 
    int rev = 0; 
    for(int x = n; x != 0; x /= 10) 
    { 
       int d = x % 10; 
       rev = rev * 10 + d; 
    } 
    if (rev == n) 
        return true; 
    else
        return false; 
} 
      
// Function which checks if the 
// requires subarray exists or not 
static void findSubarray(int []ar, int n) 
{ 
      
    // Making a prefix sum array of ar[] 
    int []pref = new int[n]; 
    pref[0] = ar[0]; 
      
    for(int x = 1; x < n; x++) 
    pref[x] = pref[x - 1] + ar[x]; 
      
    // Boolean variable that will store 
    // whether such subarray exists or not 
    boolean found = false; 
      
    for(int x = 0; x < n; x++) 
    { 
       for(int y = x + 1; y < n; y++) 
       { 
         
          // sum stores the sum of subarray 
          // from index x to y of array 
          int sum = pref[y]; 
          if (x > 0) 
          { 
              sum -= pref[x - 1]; 
          } 
          if (checkPalindrome(sum)) 
          { 
                
              // Required subarray is found 
              found = true; 
              break; 
          } 
       } 
       if (found) 
           break; 
    } 
    if (found) 
        System.out.println("Yes"); 
    else
        System.out.println("No"); 
} 
      
// Driver code 
public static void main(String args[]) 
{ 
    int []ar = { 1, 11, 20, 35 }; 
    int n = ar.length; 
      
    findSubarray(ar, n); 
} 
} 
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to check if sum of 
# any subarray of size atleast 2 is
# palindrome or not
  
# Function which checks whether a 
# given number is palindrome or not
def checkPalindrome(n):
      
    # Store the reverse 
    # of the number n
    rev = 0
    x = n
      
    while(x != 0):
        d = x % 10
        rev = rev * 10 + d
        x = x // 10
      
    if (rev == n):
        return True
    else:
        return False
  
# Function which checks if the
# requires subarray exists or not
def findSubarray(ar, n):
      
    # Making a prefix sum array of ar[]
    pref = [0 for i in range(n)]
    pref[0] = ar[0]
  
    for x in range(1, n):
        pref[x] = pref[x - 1] + ar[x]
  
    # Boolean variable that will store
    # whether such subarray exists or not
    found = False
      
    for x in range(n):
        for y in range(x + 1, n, 1):
              
            # Sum stores the sum of subarray
            # from index x to y of array
            sum = pref[y]
            if (x > 0):
                sum -= pref[x - 1]
  
            if (checkPalindrome(sum)):
                  
                # Required subarray is found
                found = True
                break
  
        if (found):
            break
    if (found):
        print("Yes")
    else:
        print("No")
  
# Driver code
if __name__ == '__main__':
      
    ar = [ 1, 11, 20, 35 ]
    n = len(ar)
      
    findSubarray(ar, n)
  
# This code is contributed by Surendra_Gangwar


C#
// C# program to check if sum of any
// subarray of size atleast 2 is
// palindrome or not
using System;
  
class GFG{
  
// Function which checks whether
// a given number is palindrome or not
static bool checkPalindrome(int n)
{
    // Store the reverse of
    // the number n
    int rev = 0;
    for(int x = n; x != 0; x /= 10) 
    {
       int d = x % 10;
       rev = rev * 10 + d;
    }
    if (rev == n)
        return true;
    else
        return false;
}
  
// Function which checks if the
// requires subarray exists or not
static void findSubarray(int []ar, int n)
{
    // Making a prefix sum array of ar[]
    int []pref = new int[n];
    pref[0] = ar[0];
  
    for(int x = 1; x < n; x++)
       pref[x] = pref[x - 1] + ar[x];
  
    // Boolean variable that will store
    // whether such subarray exists or not
    bool found = false;
    for(int x = 0; x < n; x++)
    {
       for(int y = x + 1; y < n; y++) 
       {
          // sum stores the sum of subarray
          // from index x to y of array
          int sum = pref[y];
          if (x > 0) 
          {
              sum -= pref[x - 1];
          }
          if (checkPalindrome(sum))
          {
                
              // Required subarray is found
              found = true;
              break;
          }
       }
       if (found)
           break;
    }
    if (found)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
  
// Driver code
public static void Main()
{
    int []ar = { 1, 11, 20, 35 };
    int n = ar.Length;
  
    findSubarray(ar, n);
}
}
  
// This code is contributed by Code_Mech


输出:
Yes

时间复杂度: O(N 2 )
辅助空间: O(1)