Check if sum of any subarray is Palindrome or not
Last Updated :
05 Sep, 2022
Given an array arr[] of size N. the task is to check whether there exists any subarray of size atleast 2 such that its sum is palindrome. If such a subarray exists, then print YES. Otherwise, print NO.
Examples:
Input: arr[] = {10, 6, 7, 9, 12}
Output: Yes
Explanation:
The subarray [6, 7, 9] with sum 22 is palindrome.
Input: arr[] = {15, 4, 8, 2}
Output: No
Explanation:
No such subarray exists.
Approach:
To solve the problem follow the steps below:
- Create a prefix sum array of the given array.
- Iterate over the array using nested for loops to denote start and end of subarrays. The sum of the subarray within the indices [x, y] can be obtained by pref[y] – pref[x – 1].
- Check if this sum is Palindrome or not. If any of the sum if palindrome print “Yes”, otherwise print “No”.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPalindrome( int 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 ;
}
void findSubarray( int ar[], int n)
{
int pref[n];
pref[0] = ar[0];
for ( int x = 1; x < n; x++)
pref[x] = pref[x - 1] + ar[x];
bool found = false ;
for ( int x = 0; x < n; x++) {
for ( int y = x + 1; y < n; y++) {
int sum = pref[y];
if (x > 0) {
sum -= pref[x - 1];
}
if (checkPalindrome(sum)) {
found = true ;
break ;
}
}
if (found)
break ;
}
if (found)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int ar[] = { 1, 11, 20, 35 };
int n = sizeof (ar) / sizeof (ar[0]);
findSubarray(ar, n);
return 0;
}
|
Java
class GFG{
static boolean checkPalindrome( int 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 ;
}
static void findSubarray( int []ar, int n)
{
int []pref = new int [n];
pref[ 0 ] = ar[ 0 ];
for ( int x = 1 ; x < n; x++)
pref[x] = pref[x - 1 ] + ar[x];
boolean found = false ;
for ( int x = 0 ; x < n; x++)
{
for ( int y = x + 1 ; y < n; y++)
{
int sum = pref[y];
if (x > 0 )
{
sum -= pref[x - 1 ];
}
if (checkPalindrome(sum))
{
found = true ;
break ;
}
}
if (found)
break ;
}
if (found)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String args[])
{
int []ar = { 1 , 11 , 20 , 35 };
int n = ar.length;
findSubarray(ar, n);
}
}
|
Python3
def checkPalindrome(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
def findSubarray(ar, n):
pref = [ 0 for i in range (n)]
pref[ 0 ] = ar[ 0 ]
for x in range ( 1 , n):
pref[x] = pref[x - 1 ] + ar[x]
found = False
for x in range (n):
for y in range (x + 1 , n, 1 ):
sum = pref[y]
if (x > 0 ):
sum - = pref[x - 1 ]
if (checkPalindrome( sum )):
found = True
break
if (found):
break
if (found):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
ar = [ 1 , 11 , 20 , 35 ]
n = len (ar)
findSubarray(ar, n)
|
C#
using System;
class GFG{
static bool checkPalindrome( int 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 ;
}
static void findSubarray( int []ar, int n)
{
int []pref = new int [n];
pref[0] = ar[0];
for ( int x = 1; x < n; x++)
pref[x] = pref[x - 1] + ar[x];
bool found = false ;
for ( int x = 0; x < n; x++)
{
for ( int y = x + 1; y < n; y++)
{
int sum = pref[y];
if (x > 0)
{
sum -= pref[x - 1];
}
if (checkPalindrome(sum))
{
found = true ;
break ;
}
}
if (found)
break ;
}
if (found)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main()
{
int []ar = { 1, 11, 20, 35 };
int n = ar.Length;
findSubarray(ar, n);
}
}
|
Javascript
<script>
function checkPalindrome(n)
{
var rev = 0;
for ( var x = n; x != 0; x = parseInt(x/10)) {
var d = x % 10;
rev = rev * 10 + d;
}
if (rev == n)
return true ;
else
return false ;
}
function findSubarray(ar, n)
{
var pref = Array(n).fill(0);
pref[0] = ar[0];
for ( var x = 1; x < n; x++)
pref[x] = pref[x - 1] + ar[x];
var found = false ;
for ( var x = 0; x < n; x++) {
for ( var y = x + 1; y < n; y++) {
var sum = pref[y];
if (x > 0) {
sum -= pref[x - 1];
}
if (checkPalindrome(sum)) {
found = true ;
break ;
}
}
if (found)
break ;
}
if (found)
document.write( "Yes" );
else
document.write( "No" );
}
var ar = [1, 11, 20, 35];
var n = ar.length;
findSubarray(ar, n);
</script>
|
Time Complexity: O(n2logn)
Auxiliary Space: O(n) because using extra space for pref array
Share your thoughts in the comments
Please Login to comment...