给定一个整数N和一个由[1, N]范围内的M 个整数组成的数组arr[ ] 。 (M – 1)操作需要执行。在每第i个操作中,循环遍历[1, N]范围内从arr[i]到arr[i + 1] 的每个连续元素。任务是在完成M 次操作后按排序顺序打印访问次数最多的元素。
例子:
Input: N = 4, arr[] = {1, 2, 1, 2}
Output: 1 2
Explanation:
Operation 1: 1–>2.
Operation 2: 2–>3–>4–>1.
Operation 3: 1–>2.
After completing the three operations, the maximum occurred integers are {1, 2}.
Therefore, print {1, 2}
Input: N = 6, arr[] = {1, 2, 1, 2, 3}
Output: 1 2 3
处理方法:按照以下步骤解决问题:
- 创建一个 Map 来计算元素被访问的次数和变量maxVisited来存储任何元素的最大访问次数。
- 遍历数组并执行以下操作:
- 从A[i] 中的当前元素开始,访问循环中的所有连续元素( mod N ),直到A[i+1] 。
- 在每次迭代期间,将其访问计数增加 1 ,并跟踪maxVisited变量中的最高访问计数。
- 完成每一轮后,将最后访问的元素的计数加 1 。
- 找到存储在 Map 中的最大频率(比如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
Javascript
输出:
1 2
时间复杂度: O(N*M)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live