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

📅  最后修改于: 2021-04-17 17:52:52             🧑  作者: 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 fisrt 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 fisrt 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 fisrt 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 fisrt 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


输出:
2

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