text: Count of right shifts for each array element to be in its sorted position | Function to find the right shifts required for each element to reach its sorted array position in A [ ] ; Stores required number of shifts for each element ; If the element is at sorted position ; Otherwise ; Calculate right shift ; Prlet the respective shifts ; Driver Code
code: function findShifts ( A , N ) { let shift = Array . from ( { length : N } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( i == A [ i ] - 1 ) shift [ i ] = 0 ; else shift [ i ] = ( A [ i ] - 1 - i + N ) % N ; } for ( let i = 0 ; i < N ; i ++ ) document . write ( shift [ i ] + " " ) ; } let arr = [ 1 , 4 , 3 , 2 , 5 ] ; let N = arr . length ; findShifts ( arr , N ) ;