Count of pass required to visit same index again by moving arr [ i ] to index arr [ i ] | Function to find the number of moves required to visit the same index again for every array element ; Make given array 0 index based ; Stores the number of moves ; Store index value ; Update the value of cnt ; Make a pass ; Print the value of cnt ; Driver Code
const numberOfPasses = ( arr , N ) => { for ( let i = 0 ; i < N ; ++ i ) { -- arr [ i ] ; } for ( let i = 0 ; i < N ; ++ i ) { let cnt = 0 ; let k = i ; do { ++ cnt ; k = arr [ k ] ; } while ( k != i ) ; document . write ( ` ${ cnt } ` ) ; } } let arr = [ 4 , 6 , 2 , 1 , 5 , 3 ] ; let N = arr . length ; numberOfPasses ( arr , N ) ;