📌  相关文章
📜  计数顺时针数组旋转,以最大化存在于与其值相同的索引处的数组元素的计数

📅  最后修改于: 2021-10-27 08:21:42             🧑  作者: Mango

给定一个由前N 个自然数排列组成的数组arr[] ,任务是找到使满足条件arr[i] = i ( 1-基于索引),其中1 ≤ i ≤ N。

例子:

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

  • 初始化两个整数maxians ,以及两个数组new_arr[]freq[]
  • 为每个元素遍历数组arr[] an,计算将其与其正确位置分开的索引数量,即|(arr[i] – i + N) % N| .
  • 将每个数组元素的计数存储在新数组new_arr[] 中
  • new_arr[]中每个元素的频率计数存储在数组freq[] 中
  • 打印元素 i 最大频率作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// clockwise array rotations required
// to maximize count of array elements
// present at indices same as their value
void find_min_rot(int arr[], int n)
{
    // Stores count of indices separating
    // elements from its correct position
    int new_arr[n + 1];
    int maxi = 1, ans = 0;
 
    // Stores frequencies of counts of
    // indices separating
    int freq[n + 1];
    for (int i = 1; i <= n; i++) {
        freq[i] = 0;
    }
 
    // Count indices separating each
    // element from its correct position
    for (int i = 1; i <= n; i++) {
 
        new_arr[i] = (arr[i] - i + n) % n;
    }
 
    // Update frequencies of counts obtained
    for (int i = 1; i <= n; i++) {
 
        freq[new_arr[i]]++;
    }
 
    // Find the count with maximum frequency
    for (int i = 1; i <= n; i++) {
        if (freq[i] > maxi) {
            maxi = freq[i];
            ans = i;
        }
    }
 
    // Print the answer
    cout << ans << endl;
}
 
// Driver Code
int main()
{
 
    int N = 5;
    int arr[] = { -1, 3, 4, 1, 5, 2 };
 
    // Find minimum number of
    // array rotations required
    find_min_rot(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to count the number of
// clockwise array rotations required
// to maximize count of array elements
// present at indices same as their value
static void find_min_rot(int arr[], int n)
{
     
    // Stores count of indices separating
    // elements from its correct position
    int[] new_arr = new int[n + 1];
    int maxi = 1, ans = 0;
  
    // Stores frequencies of counts of
    // indices separating
    int[] freq = new int[n + 1];
    for(int i = 1; i <= n; i++)
    {
        freq[i] = 0;
    }
  
    // Count indices separating each
    // element from its correct position
    for(int i = 1; i <= n; i++)
    {
        new_arr[i] = (arr[i] - i + n) % n;
    }
  
    // Update frequencies of counts obtained
    for(int i = 1; i <= n; i++)
    {
        freq[new_arr[i]]++;
    }
  
    // Find the count with maximum frequency
    for(int i = 1; i <= n; i++)
    {
        if (freq[i] > maxi)
        {
            maxi = freq[i];
            ans = i;
        }
    }
  
    // Print the answer
    System.out.print(ans);
}
    
// Driver Code
public static void main(String[] args)
{
    int N = 5;
    int[] arr = { -1, 3, 4, 1, 5, 2 };
  
    // Find minimum number of
    // array rotations required
    find_min_rot(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
  
# Function to count the number of
# clockwise array rotations required
# to maximize count of array elements
# present at indices same as their value
def find_min_rot(arr, n):
     
    # Stores count of indices separating
    # elements from its correct position
    new_arr = [0] * (n + 1)
    maxi = 1
    ans = 0
  
    # Stores frequencies of counts of
    # indices separating
    freq = [0] * (n + 1)
    for i in range(1, n + 1):
        freq[i] = 0
  
    # Count indices separating each
    # element from its correct position
    for i in range(1, n + 1):
         new_arr[i] = (arr[i] - i + n) % n
  
    # Update frequencies of counts obtained
    for i in range(1, n + 1):
        freq[new_arr[i]] += 1
  
    # Find the count with maximum frequency
    for i in range(1, n + 1):
        if (freq[i] > maxi):
            maxi = freq[i]
            ans = i
             
    # Print the answer
    print(ans)
  
# Driver Code
if __name__ == '__main__':
  
    N = 5
    arr = [ -1, 3, 4, 1, 5, 2 ]
  
    # Find minimum number of
    # array rotations required
    find_min_rot(arr, N)
     
# This code is contributed by jana_sayantan


C#
// C# program for the above approach
using System;
 
class GFG
{
    
// Function to count the number of
// clockwise array rotations required
// to maximize count of array elements
// present at indices same as their value
static void find_min_rot(int []arr, int n)
{
     
    // Stores count of indices separating
    // elements from its correct position
    int[] new_arr = new int[n + 1];
    int maxi = 1, ans = 0;
  
    // Stores frequencies of counts of
    // indices separating
    int[] freq = new int[n + 1];
    for(int i = 1; i <= n; i++)
    {
        freq[i] = 0;
    }
  
    // Count indices separating each
    // element from its correct position
    for(int i = 1; i <= n; i++)
    {
        new_arr[i] = (arr[i] - i + n) % n;
    }
  
    // Update frequencies of counts obtained
    for(int i = 1; i <= n; i++)
    {
        freq[new_arr[i]]++;
    }
  
    // Find the count with maximum frequency
    for(int i = 1; i <= n; i++)
    {
        if (freq[i] > maxi)
        {
            maxi = freq[i];
            ans = i;
        }
    }
  
    // Print the answer
    Console.Write(ans);
}
    
// Driver Code
public static void Main(String[] args)
{
    int N = 5;
    int[] arr = { -1, 3, 4, 1, 5, 2 };
  
    // Find minimum number of
    // array rotations required
    find_min_rot(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程