Maximum occurred integers after M circular operations in given range | Java program for the above approach ; 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
import java . io . * ; import java . util . * ; class GFG { static void mostvisitedsector ( int N , ArrayList < Integer > A ) { int maxVisited = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < A . size ( ) - 1 ; i ++ ) { int start = A . get ( i ) % N ; int end = A . get ( i + 1 ) % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . containsKey ( N ) ) mp . put ( N , mp . get ( N ) + 1 ) ; else mp . put ( N , 1 ) ; if ( mp . get ( N ) > maxVisited ) { maxVisited = mp . get ( N ) ; } } else { if ( mp . containsKey ( start ) ) mp . put ( start , mp . get ( start ) + 1 ) ; else mp . put ( start , 1 ) ; if ( mp . get ( start ) > maxVisited ) { maxVisited = mp . get ( start ) ; } } start = ( start + 1 ) % N ; } } int last = A . get ( A . size ( ) - 1 ) ; if ( mp . containsKey ( last ) ) mp . put ( last , mp . get ( last ) + 1 ) ; else mp . put ( last , 1 ) ; if ( mp . get ( last ) > maxVisited ) { maxVisited = mp . get ( last ) ; } for ( Map . Entry x : mp . entrySet ( ) ) { if ( ( int ) x . getValue ( ) == maxVisited ) { System . out . print ( x . getKey ( ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 4 ; ArrayList < Integer > arr = new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , 1 , 2 ) ) ; mostvisitedsector ( N , arr ) ; } }