Maximum occurred integers after M circular operations in given range | Function to find the maximum occurred integer after completing all circular operations ; Stores the highest visit count for any element ; Stors the number of times an element is visited ; Iterate over the array ; Iterate over the range circularly form start to end ; Count number of times an element is visited ; Increment frequency of N ; Update maxVisited ; Increment frequency of start ; Update maxVisited ; Increment the start ; Increment the count for the last visited element ; Print most visited elements ; Driver Code ; Function Call
function mostvisitedsector ( N , A ) { var maxVisited = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < A . length - 1 ; i ++ ) { var start = A [ i ] % N ; var end = A [ i + 1 ] % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . has ( N ) ) mp . set ( N , mp . get ( N ) + 1 ) ; else mp . set ( N , 1 ) ; if ( mp . get ( N ) > maxVisited ) { maxVisited = mp . get ( N ) ; } } else { if ( mp . has ( start ) ) mp . set ( start , mp . get ( start ) + 1 ) else mp . set ( start , 1 ) ; if ( mp . get ( start ) > maxVisited ) { maxVisited = mp . get ( start ) ; } } start = ( start + 1 ) % N ; } } var back = A [ A . length - 1 ] ; if ( mp . has ( back ) ) mp . set ( back , mp . get ( back ) + 1 ) else mp . set ( back , 1 ) ; if ( mp . get ( back ) > maxVisited ) { maxVisited = mp . get ( back ) ; } mp . forEach ( ( value , key ) => { if ( value == maxVisited ) { document . write ( key + " " ) ; } } ) ; } var N = 4 ; var arr = [ 1 , 2 , 1 , 2 ] ; mostvisitedsector ( N , arr ) ;