Subarray of length K whose concatenation forms a palindrome | Function to check if a number is Palindrome or not here i is the starting index and j is the last index of the subarray ; If the integer at i is not equal to j then the subarray is not palindrome ; Otherwise ; all a [ i ] is equal to a [ j ] then the subarray is palindrome ; Function to find a subarray whose concatenation forms a palindrome and return its starting index ; Iterating over subarray of length k and checking if that subarray is palindrome ; If no subarray is palindrome ; Driver Code
function palindrome ( a , i , j ) { while ( i < j ) { if ( a [ i ] != a [ j ] ) return false ; i ++ ; j -- ; } return true ; } function findSubArray ( arr , k ) { let n = arr . length ; for ( let i = 0 ; i <= n - k ; i ++ ) { if ( palindrome ( arr , i , i + k - 1 ) ) return i ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 1 , 3 ] ; let k = 4 ; let ans = findSubArray ( arr , k ) ; if ( ans == - 1 ) document . write ( - 1 + " " ) ; else { for ( let i = ans ; i < ans + k ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; }