📌  相关文章
📜  通过一次交换最大化最小和最大数组元素之间的距离

📅  最后修改于: 2021-09-07 04:47:53             🧑  作者: Mango

给定一个由[1, N]范围内的N 个元素组成的arr[] ,任务是通过一次交换最大化最小和最大数组元素之间的距离。
例子:

方法

  1. 找出数组中 1 和 N 的索引。
  2. minIdxmaxIdx分别为两个索引的最小值和最大值。
  3. 现在,maxIdx – minIdx 是两个元素之间的当前距离。它可以通过以下两个交换中的最大值最大化:
    • a[minIdx]a[0]交换,通过minIdx增加距离。
    • a[maxIdx]a[N – 1]交换,将距离增加N – 1 – maxIdx

下面是上述方法的实现:

C++
// C++ program maximize the
// distance between smallest
// and largest array element
// by a single swap
#include 
using namespace std;
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
int find_max_dist(int arr[], int N)
{
 
    int minIdx = -1, maxIdx = -1;
 
    for (int i = 0; i < N; i++) {
        if (arr[i] == 1 || arr[i] == N) {
            if (minIdx == -1)
                minIdx = i;
            else {
                maxIdx = i;
                break;
            }
        }
    }
 
    return maxIdx - minIdx
           + max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 4, 3, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << find_max_dist(arr, N) << endl;
    return 0;
}


Java
// Java program maximize the distance
// between smallest and largest array
// element by a single swap
import java.util.*;
 
class GFG{
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
static int find_max_dist(int arr[], int N)
{
    int minIdx = -1, maxIdx = -1;
 
    for(int i = 0; i < N; i++)
    {
       if (arr[i] == 1 || arr[i] == N)
       {
           if (minIdx == -1)
               minIdx = i;
           else
           {
               maxIdx = i;
               break;
           }
       }
    }
    return maxIdx - minIdx +
           Math.max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 4, 3, 2 };
    int N = arr.length;
     
    System.out.print(find_max_dist(arr, N) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program maximize the
# distance between smallest
# and largest array element
# by a single swap
 
# Function to maximize the distance
# between the smallest and largest
# array element by a single swap
def find_max_dist(arr, N):
 
    minIdx, maxIdx = -1, -1
 
    for i in range(N):
        if (arr[i] == 1 or arr[i] == N):
            if (minIdx == -1) :
                minIdx = i
                 
            else :
                maxIdx = i
                break
 
    return (maxIdx - minIdx +
        max(minIdx, N - 1 - maxIdx))
 
# Driver code
arr = [ 1, 4, 3, 2 ]
N = len(arr)
 
print(find_max_dist(arr, N))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program maximize the distance
// between smallest and largest array
// element by a single swap
using System;
 
class GFG{
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
static int find_max_dist(int []arr, int N)
{
    int minIdx = -1, maxIdx = -1;
 
    for(int i = 0; i < N; i++)
    {
       if (arr[i] == 1 || arr[i] == N)
       {
           if (minIdx == -1)
               minIdx = i;
           else
           {
               maxIdx = i;
               break;
           }
       }
    }
    return maxIdx - minIdx +
           Math.Max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 4, 3, 2 };
    int N = arr.Length;
     
    Console.Write(find_max_dist(arr, N) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
3

时间复杂度: O(N)

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