Minimum count of elements required to obtain the given Array by repeated mirror operations | Function to find minimum number of elements required to form A [ ] by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the final answer ; Driver Code
function minimumrequired ( A , N ) { let K = N ; let ans ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } let ispalindrome = true ; for ( let i = 0 ; i < parseInt ( K / 2 , 10 ) ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = false ; } if ( ispalindrome ) { ans = parseInt ( K / 2 , 10 ) ; K = parseInt ( K / 2 , 10 ) ; } else { ans = K ; break ; } } return ans ; } let a = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] ; let N = a . length ; document . write ( minimumrequired ( a , N ) ) ;