给定arr []由[1,N]范围内的N个元素组成,任务是通过一次交换使最小和最大数组元素之间的距离最大化。
例子:
Input: arr[] = {1, 4, 3, 2}
Output: 3
Explanation:
Swapping of arr[1] and arr[3] maximizes the distance.
Input: arr[] = {1, 6, 5, 3, 4, 7, 2}
Output: 6
Explanation:
Swappinf of arr[5] and arr[6] maximizes the distance.
方法
- 在数组中找到索引1和N。
- 令minIdx和maxIdx分别为两个索引的最小值和最大值。
- 现在,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)