text: Rearrange array such that sum of same indexed elements is atmost K | Reverse array ; Function to rearrange array such that sum of similar indexed elements does not exceed K ; Sort the array B [ ] in descending order ; If condition fails ; Prlet the array ; Given arrays
code: function reverse ( a ) { let i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function rearrangeArray ( A , B , N , K ) { B . sort ( ) ; B = reverse ( B ) ; let flag = true ; for ( let i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ i ] > K ) { flag = false ; break ; } } if ( ! flag ) { document . write ( " " + " " ) ; } else { for ( let i = 0 ; i < N ; i ++ ) { document . write ( B [ i ] + " " ) ; } } } let A = [ 1 , 2 , 3 , 4 , 2 ] ; let B = [ 1 , 2 , 3 , 1 , 1 ] ; let N = A . length ; let K = 5 ; rearrangeArray ( A , B , N , K ) ;