📌  相关文章
📜  每个数组元素处于其排序位置时的右移计数

📅  最后修改于: 2021-05-17 03:27:27             🧑  作者: Mango

给定大小为N的数组arr []包含范围为[1,N]的元素,任务是计算如果对数组进行排序,则每个元素到达其各自位置所需的右移次数。
例子:

方法:
想法是计算数组的每个元素的实际位置和排序位置之间的差。由于元素从1N,因此可以在不对数组进行排序的情况下确定每个元素的排序位置。每个元素的排序位置由(arr [i] -1)给出。因此,右移次数由(arr [i] – 1 – i + N)%N给出。

下面是上述方法的实现:

C++
// C++ Program to implement
// the approach
#include 
using namespace std;
  
// Function to find the right
// shifts required for each
// element to reach its sorted
// array position in A[]
void findShifts(int A[], int N)
{
    // Stores required number of
    // shifts for each element
    int shift[N];
  
    for (int i = 0; i < N; i++) {
  
        // If the element is
        // at sorted position
        if (i == A[i] - 1)
            shift[i] = 0;
  
        // Otherwise
        else
  
            // Calculate right shift
            shift[i]
                = (A[i] - 1 - i + N)
                  % N;
    }
  
    // Print the respective shifts
    for (int i = 0; i < N; i++)
        cout << shift[i] << " ";
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 4, 3, 2, 5 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    findShifts(arr, N);
  
    return 0;
}


Java
// Java program to implement 
// the approach
class GFG{
      
// Function to find the right 
// shifts required for each 
// element to reach its sorted 
// array position in A[] 
public static void findShifts(int[] A, int N) 
{ 
      
    // Stores required number of 
    // shifts for each element 
    int[] shift = new int[N]; 
  
    for(int i = 0; i < N; i++)
    { 
  
        // If the element is 
        // at sorted position 
        if (i == A[i] - 1) 
            shift[i] = 0; 
  
        // Otherwise 
        else
  
            // Calculate right shift 
            shift[i] = (A[i] - 1 - i + N) % N; 
    } 
  
    // Print the respective shifts 
    for(int i = 0; i < N; i++) 
        System.out.print(shift[i] + " ");
} 
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 4, 3, 2, 5 }; 
    int N = arr.length; 
  
    findShifts(arr, N); 
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 Program to implement
# the approach
  
# Function to find the right
# shifts required for each
# element to reach its sorted
# array position in A[]
def findShifts(A, N):
  
    # Stores required number of
    # shifts for each element
    shift = [0 for i in range(N)]
  
    for i in range(N):
  
        # If the element is
        # at sorted position
        if (i == A[i] - 1):
            shift[i] = 0
  
        # Otherwise
        else:
  
            # Calculate right shift
            shift[i] = (A[i] - 1 - i + N) % N
  
    # Print the respective shifts
    for i in range(N):
        print(shift[i], end = " ")
  
# Driver Code
if __name__ == '__main__':
  
    arr = [ 1, 4, 3, 2, 5 ]
    N = len(arr)
  
    findShifts(arr, N)
  
# This code is contributed by mohit kumar 29


C#
// C# program to implement 
// the approach 
using System;
  
class GFG{ 
      
// Function to find the right 
// shifts required for each 
// element to reach its sorted 
// array position in []A 
public static void findShifts(int[] A, int N) 
{ 
      
    // Stores required number of 
    // shifts for each element 
    int[] shift = new int[N]; 
  
    for(int i = 0; i < N; i++) 
    { 
  
        // If the element is 
        // at sorted position 
        if (i == A[i] - 1) 
            shift[i] = 0; 
  
        // Otherwise 
        else
  
            // Calculate right shift 
            shift[i] = (A[i] - 1 - i + N) % N; 
    } 
  
    // Print the respective shifts 
    for(int i = 0; i < N; i++) 
        Console.Write(shift[i] + " "); 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    int []arr = { 1, 4, 3, 2, 5 }; 
    int N = arr.Length; 
  
    findShifts(arr, N); 
} 
}
  
// This code is contributed by amal kumar choubey


输出:
0 2 0 3 0

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