Maximum occurred integers after M circular operations in given range | C ++ 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 ; Stores 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mostvisitedsector ( int N , vector < int > & A ) { int maxVisited = 0 ; map < int , int > mp ; for ( int i = 0 ; i < A . size ( ) - 1 ; i ++ ) { int start = A [ i ] % N ; int end = A [ i + 1 ] % N ; while ( start != end ) { if ( start == 0 ) { mp [ N ] ++ ; if ( mp [ N ] > maxVisited ) { maxVisited = mp [ N ] ; } } else { mp [ start ] ++ ; if ( mp [ start ] > maxVisited ) { maxVisited = mp [ start ] ; } } start = ( start + 1 ) % N ; } } mp [ A . back ( ) ] ++ ; if ( mp [ A . back ( ) ] > maxVisited ) { maxVisited = mp [ A . back ( ) ] ; } for ( auto x : mp ) { if ( x . second == maxVisited ) { cout << x . first << " ▁ " ; } } } int main ( ) { int N = 4 ; vector < int > arr = { 1 , 2 , 1 , 2 } ; mostvisitedsector ( N , arr ) ; return 0 ; }