Check if K consecutive palindrome numbers are present in the Array
Last Updated :
18 Jul, 2023
Given an array, arr[], and an integer K, the task is to check whether K consecutive palindrome numbers are present or not.
Examples:
Input: arr[] = {15, 7, 11, 151, 23, 1}, K = 3
Output: true
Explanation: There are 3 consecutive palindromes numbers (7, 11, 151).
Input : arr[] = {19, 37, 51, 42}, K = 1
Output: false
Approach: This can be solved with the following idea:
Iterate through array, arr[], and use the concept of sliding window. Check for each number whether it’s palindrome or not.
Below are the steps involved in the implementation of the code:
- We have to iterate through each element.
- Whenever any palindrome element occurs, count the length of the consecutive palindromes starting from that element.
- If the count is greater than or equal to K, return true else return false.
Below is the implementation of the code:
C++14
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome( int num)
{
int val = num;
int curr = 0;
while (val != 0) {
int rem = val % 10;
curr = curr * 10 + rem;
val = val / 10;
}
return num == curr;
}
bool maxConsecutivePalindromes( int arr[], int n, int K)
{
int i = 0;
while (i < n) {
if (isPalindrome(arr[i])) {
int count = 0;
while (i < n && isPalindrome(arr[i])) {
count++;
i++;
}
if (count >= K)
return true ;
continue ;
}
i++;
}
return false ;
}
int main()
{
int arr[] = { 15, 7, 11, 151, 23, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 3;
bool ans = maxConsecutivePalindromes(arr, n, K);
cout << (ans ? "true" : "false" ) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static boolean
maxConsecutivePalindromes( int [] arr, int K)
{
int n = arr.length;
int i = 0 ;
while (i < n) {
if (isPalindrome(arr[i])) {
int count = 0 ;
while (i < n && isPalindrome(arr[i])) {
count++;
i++;
}
if (count >= K)
return true ;
continue ;
}
i++;
}
return false ;
}
public static boolean isPalindrome( int num)
{
int val = num;
int curr = 0 ;
while (val != 0 ) {
int rem = val % 10 ;
curr = curr * 10 + rem;
val = val / 10 ;
}
return num == curr;
}
public static void main(String[] args)
{
int [] arr = { 15 , 7 , 11 , 151 , 23 , 6 };
int K = 3 ;
boolean ans = maxConsecutivePalindromes(arr, K);
System.out.println(ans);
}
}
|
Python
def isPalindrome(num):
val = num
curr = 0
while val ! = 0 :
rem = val % 10
curr = curr * 10 + rem
val = val / / 10
return num = = curr
def maxConsecutivePalindromes(arr, n, K):
i = 0
while i < n:
if isPalindrome(arr[i]):
count = 0
while i < n and isPalindrome(arr[i]):
count + = 1
i + = 1
if count > = K:
return True
continue
i + = 1
return False
arr = [ 15 , 7 , 11 , 151 , 23 , 6 ]
n = len (arr)
K = 3
ans = maxConsecutivePalindromes(arr, n, K)
print ( "true" if ans else "false" )
|
C#
using System;
class GFG
{
public static bool MaxConsecutivePalindromes( int [] arr, int K)
{
int n = arr.Length;
int i = 0;
while (i < n)
{
if (IsPalindrome(arr[i]))
{
int count = 0;
while (i < n && IsPalindrome(arr[i]))
{
count++;
i++;
}
if (count >= K)
return true ;
continue ;
}
i++;
}
return false ;
}
public static bool IsPalindrome( int num)
{
int val = num;
int curr = 0;
while (val != 0)
{
int rem = val % 10;
curr = curr * 10 + rem;
val = val / 10;
}
return num == curr;
}
public static void Main( string [] args)
{
int [] arr = { 15, 7, 11, 151, 23, 6 };
int K = 3;
bool ans = MaxConsecutivePalindromes(arr, K);
Console.WriteLine(ans);
}
}
|
Javascript
function maxConsecutivePalindromes(arr, K) {
let n = arr.length;
let i = 0;
while (i < n) {
if (isPalindrome(arr[i])) {
let count = 0;
while (i < n && isPalindrome(arr[i])) {
count++;
i++;
}
if (count >= K)
return true ;
continue ;
}
i++;
}
return false ;
}
function isPalindrome(num) {
let val = num;
let curr = 0;
while (val !== 0) {
let rem = val % 10;
curr = curr * 10 + rem;
val = Math.floor(val / 10);
}
return num === curr;
}
let arr = [15, 7, 11, 151, 23, 6];
let K = 3;
let ans = maxConsecutivePalindromes(arr, K);
console.log(ans);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...