📌  相关文章
📜  最小化在第一个和最后一个数组索引处放置最大和最小数组元素所需的交换

📅  最后修改于: 2021-09-06 05:59:23             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到重新排列数组元素所需的相邻交换的最小计数,以便最大和最小的数组元素分别出现在数组的第一个和最后一个索引上。

例子:

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

  • 遍历数组并计算第一次出现的最大数组元素X和最后一次出现的最小数组元素Y的索引。
  • 移动第一个索引处的最大数组元素所需的相邻交换计数等于X
  • 移动最后一个索引处的最小数组元素所需的相邻交换次数等于N – 1 – Y
  • 如果X > Y ,则一个相邻的交换共同用于移动第一个索引处的最大元素和最后一个索引处的最小元素。因此,所需的相邻交换的总数等于X + (N – 1 – Y) – 1
  • 否则,所需的相邻交换的总数等于X + (N – 1 – Y)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to  find the minimum count of adjacent
// swaps to move largest and smallest element at the
// first and the last index of the array, respectively
int minimumMoves(int* a, int n)
{
 
    // Stores the smallest array element
    int min_element = INT_MAX;
 
    // Stores the smallest array element
    int max_element = INT_MIN;
 
    // Stores the last occurrence of
    // the smallest array element
    int min_ind = -1;
 
    // Stores the first occurrence of
    // the largest array element
    int max_ind = -1;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // If a[i] is less than
        // min_element
        if (a[i] <= min_element) {
 
            // Update min_element
            min_element = a[i];
 
            // Update min_ind
            min_ind = i;
        }
 
        // If a[i] is greater than
        // max_element
        if (a[i] > max_element) {
 
            // Update max_element
            max_element = a[i];
 
            // Update max_ind
            max_ind = i;
        }
    }
 
    // If max_ind is equal
    // to min_ind
    if (max_ind == min_ind) {
        // Return 0
        return 0;
    }
 
    // If max_ind is greater than min_ind
    else if (max_ind > min_ind) {
 
        return max_ind + (n - min_ind - 2);
    }
 
    // Otherwise
    else {
 
        return max_ind + n - min_ind - 1;
    }
}
 
// Driver Code
int main()
{
 
    // Input
    int arr[] = { 35, 46, 17, 23 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Print the result
    cout << minimumMoves(arr, N) << endl;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to  find the minimum count of adjacent
// swaps to move largest and smallest element at the
// first and the last index of the array, respectively
static int minimumMoves(int []a, int n)
{
 
    // Stores the smallest array element
    int min_element = Integer.MAX_VALUE;
 
    // Stores the smallest array element
    int max_element = Integer.MIN_VALUE;
 
    // Stores the last occurrence of
    // the smallest array element
    int min_ind = -1;
 
    // Stores the first occurrence of
    // the largest array element
    int max_ind = -1;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++)
    {
 
        // If a[i] is less than
        // min_element
        if (a[i] <= min_element)
        {
 
            // Update min_element
            min_element = a[i];
 
            // Update min_ind
            min_ind = i;
        }
 
        // If a[i] is greater than
        // max_element
        if (a[i] > max_element) {
 
            // Update max_element
            max_element = a[i];
 
            // Update max_ind
            max_ind = i;
        }
    }
 
    // If max_ind is equal
    // to min_ind
    if (max_ind == min_ind) {
        // Return 0
        return 0;
    }
 
    // If max_ind is greater than min_ind
    else if (max_ind > min_ind) {
 
        return max_ind + (n - min_ind - 2);
    }
 
    // Otherwise
    else {
 
        return max_ind + n - min_ind - 1;
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Input
    int arr[] = { 35, 46, 17, 23 };
    int N = arr.length;
 
    // Print the result
    System.out.print(minimumMoves(arr, N) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for the above approach
import sys
 
# Function to  find the minimum count of adjacent
# swaps to move largest and smallest element at the
# first and the last index of the array, respectively
def minimumMoves(a, n):
   
    # Stores the smallest array element
    min_element = sys.maxsize
 
    # Stores the smallest array element
    max_element = -sys.maxsize - 1
 
    # Stores the last occurrence of
    # the smallest array element
    min_ind = -1
 
    # Stores the first occurrence of
    # the largest array element
    max_ind = -1
 
    # Traverse the array arr[]
    for i in range(n):
       
        # If a[i] is less than
        # min_element
        if (a[i] <= min_element):
           
            # Update min_element
            min_element = a[i]
 
            # Update min_ind
            min_ind = i
 
        # If a[i] is greater than
        # max_element
        if (a[i] > max_element):
           
            # Update max_element
            max_element = a[i]
 
            # Update max_ind
            max_ind = i
 
    # If max_ind is equal
    # to min_ind
    if (max_ind == min_ind):
       
        # Return 0
        return 0
 
    # If max_ind is greater than min_ind
    elif(max_ind > min_ind):
        return max_ind + (n - min_ind - 2)
 
    # Otherwise
    else:
        return max_ind + n - min_ind - 1
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    arr =  [35, 46, 17, 23]
    N = len(arr)
 
    # Print the result
    print(minimumMoves(arr, N))
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to  find the minimum count of adjacent
  // swaps to move largest and smallest element at the
  // first and the last index of the array, respectively
  static int minimumMoves(int []a, int n)
  {
 
    // Stores the smallest array element
    int min_element = int.MaxValue;
 
    // Stores the smallest array element
    int max_element = int.MinValue;
 
    // Stores the last occurrence of
    // the smallest array element
    int min_ind = -1;
 
    // Stores the first occurrence of
    // the largest array element
    int max_ind = -1;
 
    // Traverse the array []arr
    for (int i = 0; i < n; i++)
    {
 
      // If a[i] is less than
      // min_element
      if (a[i] <= min_element)
      {
 
        // Update min_element
        min_element = a[i];
 
        // Update min_ind
        min_ind = i;
      }
 
      // If a[i] is greater than
      // max_element
      if (a[i] > max_element)
      {
 
        // Update max_element
        max_element = a[i];
 
        // Update max_ind
        max_ind = i;
      }
    }
 
    // If max_ind is equal
    // to min_ind
    if (max_ind == min_ind)
    {
 
      // Return 0
      return 0;
    }
 
    // If max_ind is greater than min_ind
    else if (max_ind > min_ind)
    {
      return max_ind + (n - min_ind - 2);
    }
 
    // Otherwise
    else
    {
      return max_ind + n - min_ind - 1;
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Input
    int []arr = { 35, 46, 17, 23 };
    int N = arr.Length;
 
    // Print the result
    Console.Write(minimumMoves(arr, N) +"\n");
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live