📌  相关文章
📜  在给定范围内进行M次循环运算后出现的最大整数

📅  最后修改于: 2021-05-14 07:57:12             🧑  作者: Mango

给定一个整数N和一个数组arr [] ,该数组arr [][1,N]范围内的M个整数组成。 (M – 1)个操作需要执行。在i操作中,循环遍历从arr [i]arr [i + 1]的范围[1,N]中的每个连续元素。任务是在完成M个操作后,按排序顺序打印访问量最大的元素。

例子:

方法:请按照以下步骤解决问题:

  • 创建一个Map来计算元素访问的次数,并创建变量maxVisited来存储任何元素的最大访问次数。
  • 遍历数组并执行以下操作:
    • A [i]中的当前元素开始,并访问circlel( mod N )中的所有连续元素,直到A [i + 1]为止。
    • 在每次迭代期间,将其访问计数增加1 ,并跟踪maxVisited变量中的最高访问计数。
    • 完成每个回合后,将最后访问的元素的计数加1
  • 找到存储在地图中的最大频率(例如maxFreq )。
  • 完成上述步骤后,迭代Map并打印频率为maxFreq的元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// occurred integer after completing
// all circular operations
void mostvisitedsector(int N, vector& A)
{
    // Stores the highest visit count
    // for any element
    int maxVisited = 0;
 
    // Stors the number of times an
    // element is visited
    map mp;
 
    // Iterate over the array
    for (int i = 0; i < A.size() - 1; i++) {
 
        int start = A[i] % N;
        int end = A[i + 1] % N;
 
        // Iterate over the range
        // circularly form start to end
        while (start != end) {
 
            // Count number of times an
            // element is visited
            if (start == 0) {
 
                // Increment frequency
                // of N
                mp[N]++;
 
                // Update maxVisited
                if (mp[N] > maxVisited) {
                    maxVisited = mp[N];
                }
            }
            else {
 
                // Increment frequency
                // of start
                mp[start]++;
 
                // Update maxVisited
                if (mp[start] > maxVisited) {
                    maxVisited = mp[start];
                }
            }
 
            // Increment the start
            start = (start + 1) % N;
        }
    }
 
    // Increment the count for the last
    // visited element
    mp[A.back()]++;
    if (mp[A.back()] > maxVisited) {
        maxVisited = mp[A.back()];
    }
 
    // Print most visited elements
    for (auto x : mp) {
        if (x.second == maxVisited) {
            cout << x.first << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int N = 4;
    vector arr = { 1, 2, 1, 2 };
 
    // Function Call
    mostvisitedsector(N, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum
// occurred integer after completing
// all circular operations
static void mostvisitedsector(int N,
                              ArrayList A)
{
     
    // Stores the highest visit count
    // for any element
    int maxVisited = 0;
 
    // Stors the number of times an
    // element is visited
    HashMap mp = new HashMap();
 
    // Iterate over the array
    for(int i = 0; i < A.size() - 1; i++)
    {
        int start = A.get(i) % N;
        int end = A.get(i + 1) % N;
 
        // Iterate over the range
        // circularly form start to end
        while (start != end)
        {
             
            // Count number of times an
            // element is visited
            if (start == 0)
            {
                 
                // Increment frequency
                // of N
                if (mp.containsKey(N))
                    mp.put(N, mp.get(N) + 1);
                else
                    mp.put(N, 1);
 
                // Update maxVisited
                if (mp.get(N) > maxVisited)
                {
                    maxVisited = mp.get(N);
                }
            }
            else
            {
                 
                // Increment frequency
                // of start
                if (mp.containsKey(start))
                    mp.put(start, mp.get(start) + 1);
                else
                    mp.put(start, 1);
 
                // Update maxVisited
                if (mp.get(start) > maxVisited)
                {
                    maxVisited = mp.get(start);
                }
            }
 
            // Increment the start
            start = (start + 1) % N;
        }
    }
 
    // Increment the count for the last
    // visited element
    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);
    }
 
    // Print most visited elements
    for(Map.Entry x : mp.entrySet())
    {
        if ((int)x.getValue() == maxVisited)
        {
            System.out.print(x.getKey() + " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
     
    ArrayList arr = new ArrayList(
        Arrays.asList(1, 2, 1, 2));
 
    // Function Call
    mostvisitedsector(N, arr);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to find the maximum
# occurred integer after completing
# all circular operations
def mostvisitedsector(N, A):
     
    # Stores the highest visit count
    # for any element
    maxVisited = 0
 
    # Stors the number of times an
    # element is visited
    mp = {}
 
    # Iterate over the array
    for i in range(0, len(A) - 1):
        start = A[i] % N
        end = A[i + 1] % N
 
        # Iterate over the range
        # circularly form start to end
        while (start != end):
 
            # Count number of times an
            # element is visited
            if (start == 0):
 
                # Increment frequency
                # of N
                if N in mp:
                    mp[N] = mp[N] + 1
                else:
                    mp[N] = 1
 
                # Update maxVisited
                if (mp[N] > maxVisited):
                    maxVisited = mp[N]
 
            else:
                 
                # Increment frequency
                # of start
                if start in mp:
                    mp[start] = mp[start] + 1
                else:
                    mp[start] = 1
 
                # Update maxVisited
                if (mp[start] > maxVisited):
                    maxVisited = mp[start]
 
            # Increment the start
            start = (start + 1) % N
 
    # Increment the count for the last
    # visited element
    if A[-1] in mp:
        mp[A[-1]] = mp[A[-1]] + 1
 
    if (mp[A[-1]] > maxVisited):
        maxVisited = mp[A[-1]]
 
    # Print most visited elements
    for x in mp:
        if (mp[x] == maxVisited):
            print(x, end = ' ')
 
# Driver Code
if __name__ == '__main__':
 
    N = 4
    arr = [ 1, 2, 1, 2 ]
 
    # Function Call
    mostvisitedsector(N, arr)
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the maximum
// occurred integer after completing
// all circular operations
static void mostvisitedsector(int N, ArrayList A)
{
     
    // Stores the highest visit count
    // for any element
    int maxVisited = 0;
 
    // Stors the number of times an
    // element is visited
    Dictionary mp = new Dictionary();
 
    // Iterate over the array
    for(int i = 0; i < A.Count - 1; i++)
    {
        int start = (int)A[i] % N;
        int end = (int)A[i + 1] % N;
 
        // Iterate over the range
        // circularly form start to end
        while (start != end)
        {
             
            // Count number of times an
            // element is visited
            if (start == 0)
            {
                 
                // Increment frequency
                // of N
                if (mp.ContainsKey(N))
                    mp[N] = mp[N] + 1;
                else
                    mp[N] = 1;
 
                // Update maxVisited
                if (mp[N] > maxVisited)
                {
                    maxVisited = mp[N];
                }
            }
            else
            {
 
                // Increment frequency
                // of start
                if (mp.ContainsKey(start))
                    mp[start] = mp[start] + 1;
                else
                    mp[start] = 1;
 
                // Update maxVisited
                if (mp[start] > maxVisited)
                {
                    maxVisited = mp[start];
                }
            }
 
            // Increment the start
            start = (start + 1) % N;
        }
    }
 
    // Increment the count for the last
    // visited element
    int last_element = (int)A[A.Count - 1];
 
    if (mp.ContainsKey(last_element))
        mp[last_element] = mp[last_element] + 1;
    else
        mp[last_element] = 1;
 
    if (mp[last_element] > maxVisited)
    {
        maxVisited = mp[last_element];
    }
 
    // Print most visited elements
    foreach(var x in mp)
    {
        if ((int)x.Value == maxVisited)
        {
            Console.Write(x.Key + " ");
        }
    }
}
 
// Driver Code
public static void Main()
{
    int N = 4;
    ArrayList arr = new ArrayList(){ 1, 2, 1, 2 };
 
    // Function Call
    mostvisitedsector(N, arr);
}
}
 
// This code is contributed by akhilsaini


输出:
1 2



时间复杂度: O(N * M)
辅助空间: O(N)