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
// C++ code for the above problem #include <bits/stdc++.h> using namespace std; // Function to detect the given // number is palindrome or not bool isPalindrome( int num) { int val = num; int curr = 0; // Reversing the number while (val != 0) { int rem = val % 10; curr = curr * 10 + rem; val = val / 10; } return num == curr; } // Functions to return maximum // consecutive palindrome numbers bool maxConsecutivePalindromes( int arr[], int n, int K) { int i = 0; while (i < n) { // Checking the current // element is palindrome or not if (isPalindrome(arr[i])) { int count = 0; // Count the consecutive // palindrome numbers while (i < n && isPalindrome(arr[i])) { count++; i++; } // If the count is more // than K if (count >= K) return true ; continue ; } i++; } return false ; } // Driver Code int main() { int arr[] = { 15, 7, 11, 151, 23, 6 }; int n = sizeof (arr) / sizeof (arr[0]); int K = 3; // Function call bool ans = maxConsecutivePalindromes(arr, n, K); cout << (ans ? "true" : "false" ) << endl; return 0; } |
Java
// Java code for the above problem import java.io.*; class GFG { // Functions to return maximum // consecutive palindrome numbers public static boolean maxConsecutivePalindromes( int [] arr, int K) { int n = arr.length; int i = 0 ; while (i < n) { // Checking the current // element is palindrome // or not if (isPalindrome(arr[i])) { int count = 0 ; // Count the consecutive // palindrome numbers while (i < n && isPalindrome(arr[i])) { count++; i++; } // If the count is more // than K if (count >= K) return true ; continue ; } i++; } return false ; } // Function to detect the given // number is palindrome or not public static boolean isPalindrome( int num) { int val = num; int curr = 0 ; // Reversing the number while (val != 0 ) { int rem = val % 10 ; curr = curr * 10 + rem; val = val / 10 ; } return num == curr; } // Driver Code public static void main(String[] args) { int [] arr = { 15 , 7 , 11 , 151 , 23 , 6 }; int K = 3 ; // Function call boolean ans = maxConsecutivePalindromes(arr, K); System.out.println(ans); } } |
Python
# Function to detect the given # number is palindrome or not def isPalindrome(num): val = num curr = 0 # Reversing the number while val ! = 0 : rem = val % 10 curr = curr * 10 + rem val = val / / 10 return num = = curr # Function to return maximum # consecutive palindrome numbers def maxConsecutivePalindromes(arr, n, K): i = 0 while i < n: # Checking the current # element is palindrome or not if isPalindrome(arr[i]): count = 0 # Count the consecutive # palindrome numbers while i < n and isPalindrome(arr[i]): count + = 1 i + = 1 # If the count is more # than K if count > = K: return True continue i + = 1 return False # Driver Code arr = [ 15 , 7 , 11 , 151 , 23 , 6 ] n = len (arr) K = 3 # Function call ans = maxConsecutivePalindromes(arr, n, K) print ( "true" if ans else "false" ) |
C#
// c# code for the above problem using System; class GFG { // Functions to return maximum // consecutive palindrome numbers public static bool MaxConsecutivePalindromes( int [] arr, int K) { int n = arr.Length; int i = 0; while (i < n) { // Checking if the current // element is palindrome or not if (IsPalindrome(arr[i])) { int count = 0; // Count the consecutive // palindrome numbers while (i < n && IsPalindrome(arr[i])) { count++; i++; } // If the count is more than K if (count >= K) return true ; continue ; } i++; } return false ; } // Function to detect if the given // number is a palindrome or not public static bool IsPalindrome( int num) { int val = num; int curr = 0; // Reversing the number while (val != 0) { int rem = val % 10; curr = curr * 10 + rem; val = val / 10; } return num == curr; } // Driver Code public static void Main( string [] args) { int [] arr = { 15, 7, 11, 151, 23, 6 }; int K = 3; // Function call bool ans = MaxConsecutivePalindromes(arr, K); Console.WriteLine(ans); } } |
Javascript
// JavaScript code for the above problem // Function to return maximum consecutive palindrome numbers function maxConsecutivePalindromes(arr, K) { let n = arr.length; let i = 0; while (i < n) { // Checking the current element is palindrome or not if (isPalindrome(arr[i])) { let count = 0; // Count the consecutive palindrome numbers while (i < n && isPalindrome(arr[i])) { count++; i++; } // If the count is more // than K if (count >= K) return true ; continue ; } i++; } return false ; } // Function to detect the given // number is palindrome or not function isPalindrome(num) { let val = num; let curr = 0; // Reversing the number while (val !== 0) { let rem = val % 10; curr = curr * 10 + rem; val = Math.floor(val / 10); } return num === curr; } // Driver Code let arr = [15, 7, 11, 151, 23, 6]; let K = 3; // Function call let ans = maxConsecutivePalindromes(arr, K); console.log(ans); //This code is contributed by Tushar Rokade |
true
Time Complexity: O(N)
Auxiliary Space: O(1)