📌  相关文章
📜  最小化使第一个和最后一个元素分别成为数组中最大和最小元素所需的交换

📅  最后修改于: 2021-05-18 01:14:33             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到使数组中的第一个和最后一个元素分别成为数组中最大和最小元素所需的最小相邻交换数。

例子:

方法:请按照以下步骤解决给定的问题:

  • 初始化一个变量,例如count ,以存储所需交换的总数。
  • 找到数组的最小和最大元素,并将其存储在变量中,分别说minElementmaxElement
  • 如果发现最小和最大元素相同,则该数组由单个不同的元素组成。因此,将两个元素分别放在其正确位置上,将它们打印为0
  • 否则,遍历数组并找到第一次出现的maxElement和最后一次出现的minElement的索引,并将其存储在变量中,分别说minIndexmaxIndex
  • count的值更新为maxIndex(N – 1 – minIndex)的总和,作为将最大和最小元素分别设为数组的第一个和最后一个元素所需的交换操作数。
  • 如果minIndex小于maxIndex ,则将count减少1 ,因为一个交换操作将重叠。
  • 完成上述步骤后,将count的值打印为所需的最小交换次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of swaps required to make the first
// and the last elements the largest
// and smallest element in the array
int minimum_swaps(int arr[], int n)
{
    // Stores the count of swaps
    int count = 0;
 
    // Stores the maximum element
    int max_el = *max_element(arr,
                              arr + n);
 
    // Stores the minimum element
    int min_el = *min_element(arr,
                              arr + n);
 
    // If the array contains
    // a single distinct element
    if (min_el == max_el)
        return 0;
 
    // Stores the indices of the
    // maximum and minimum elements
    int index_max = -1;
    int index_min = -1;
 
    for (int i = 0; i < n; i++) {
 
        // If the first index of the
        // maximum element is found
        if (arr[i] == max_el
            && index_max == -1) {
            index_max = i;
        }
 
        // If current index has
        // the minimum element
        if (arr[i] == min_el) {
            index_min = i;
        }
    }
 
    // Update the count of operations to
    // place largest element at the first
    count += index_max;
 
    // Update the count of operations to
    // place largest element at the last
    count += (n - 1 - index_min);
 
    // If smallest element is present
    // before the largest element initially
    if (index_min < index_max)
        count -= 1;
 
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 1, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minimum_swaps(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
import java.util.Collections;
 
class GFG{
 
// Function to find the minimum number
// of swaps required to make the first
// and the last elements the largest
// and smallest element in the array
static int minimum_swaps(Integer arr[], int n)
{
     
    // Stores the count of swaps
    int count = 0;
     
    // Stores the maximum element
    int max_el = Collections.max(Arrays.asList(arr));
 
    // Stores the minimum element
    int min_el = Collections.min(Arrays.asList(arr));
 
    // If the array contains
    // a single distinct element
    if (min_el == max_el)
        return 0;
 
    // Stores the indices of the
    // maximum and minimum elements
    int index_max = -1;
    int index_min = -1;
 
    for(int i = 0; i < n; i++)
    {
         
        // If the first index of the
        // maximum element is found
        if (arr[i] == max_el &&
            index_max == -1)
        {
            index_max = i;
        }
 
        // If current index has
        // the minimum element
        if (arr[i] == min_el)
        {
            index_min = i;
        }
    }
 
    // Update the count of operations to
    // place largest element at the first
    count += index_max;
 
    // Update the count of operations to
    // place largest element at the last
    count += (n - 1 - index_min);
 
    // If smallest element is present
    // before the largest element initially
    if (index_min < index_max)
        count -= 1;
 
    return count;
}
 
// Driver Code
public static void main (String[] args)
{
    Integer arr[] = { 2, 4, 1, 6, 5 };
    int N = arr.length;
     
    System.out.println(minimum_swaps(arr, N));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of swaps required to make the first
# and the last elements the largest
# and smallest element in the array
def minimum_swaps(arr, n):
     
    # Stores the count of swaps
    count = 0
 
    # Stores the maximum element
    max_el = max(arr)
 
    # Stores the minimum element
    min_el = min(arr)
 
    # If the array contains
    # a single distinct element
    if (min_el == max_el):
        return 0
 
    # Stores the indices of the
    # maximum and minimum elements
    index_max = -1
    index_min = -1
 
    for i in range(n):
         
        # If the first index of the
        # maximum element is found
        if (arr[i] == max_el and
            index_max == -1):
            index_max = i
 
        # If current index has
        # the minimum element
        if (arr[i] == min_el):
            index_min = i
 
    # Update the count of operations to
    # place largest element at the first
    count += index_max
 
    # Update the count of operations to
    # place largest element at the last
    count += (n - 1 - index_min)
 
    # If smallest element is present
    # before the largest element initially
    if (index_min < index_max):
        count -= 1
 
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [2, 4, 1, 6, 5]
    N = len(arr)
     
    print(minimum_swaps(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Linq;
 
class GFG{
 
// Function to find the minimum number
// of swaps required to make the first
// and the last elements the largest
// and smallest element in the array
static int minimum_swaps(int[] arr, int n)
{
     
    // Stores the count of swaps
    int count = 0;
 
    // Stores the maximum element
    int max_el = arr.Max();
 
    // Stores the minimum element
    int min_el = arr.Min();
 
    // If the array contains
    // a single distinct element
    if (min_el == max_el)
        return 0;
 
    // Stores the indices of the
    // maximum and minimum elements
    int index_max = -1;
    int index_min = -1;
 
    for(int i = 0; i < n; i++)
    {
         
        // If the first index of the
        // maximum element is found
        if (arr[i] == max_el &&
            index_max == -1)
        {
            index_max = i;
        }
 
        // If current index has
        // the minimum element
        if (arr[i] == min_el)
        {
            index_min = i;
        }
    }
 
    // Update the count of operations to
    // place largest element at the first
    count += index_max;
 
    // Update the count of operations to
    // place largest element at the last
    count += (n - 1 - index_min);
 
    // If smallest element is present
    // before the largest element initially
    if (index_min < index_max)
        count -= 1;
 
    return count;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 2, 4, 1, 6, 5 };
    int N = arr.Length;
 
    Console.WriteLine(minimum_swaps(arr, N));
}
}
 
// This code is contributed by ukasp


输出:
4

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