Check if final remainder is present in original Array by reducing it based on given conditions | Javascript program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
function Reduced ( a , n ) { let original_array = [ ] ; original_array = a ; while ( a . length != 2 ) { let mid = Math . floor ( a . length / 2 ) ; let mid_ele = a [ mid ] ; let start = a [ 0 ] ; a . shift ( ) ; let end = a [ a . length - 1 ] ; a . pop ( ) ; let rmd = ( start * end ) % mid_ele ; a . push ( rmd ) ; let remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( let i = 0 ; i < original_array . length ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } let x = Reduced ( Arr , N ) ; if ( x ) document . write ( " " ) ; else document . write ( " " ) ;