给定一个由N个整数和一个整数K组成的数组,请选择两个不同的元素,其和为K,并找出所选择的元素与端点之间的最大最短距离。
例子:
Input : a[] = {2, 4, 3, 2, 1}
k = 5.
Output : 2
Explanation:
Select the pair(4, 1).
Shortest distance of 4 from ends = 2
Shortest distance of 1 from ends = 1
Hence, answer is max(2, 1) = 2
Input : a[] = {2, 4, 1, 9, 5}
k = 3
Output : 3
Explanation:
Select the pair (2, 1)
Shortest distance of 2 from ends = 1
Shortest distance of 1 from ends = 3
Hence, answer is max(1, 3) = 3.
注意:末端元素到末端的距离是1而不是0。
天真的方法:该方法是运行两个循环,并在内部循环中检查两个元素是否与和k成对。如果是,则以两个元素的最短距离中的最大值作为答案,将其与前一对的答案进行比较,并以这两个元素中的最小值作为答案。当循环结束时,我们将获得所需的输出。
高效的方法:显然,最短距离是指距左端的距离和距右端的距离,即 。让我们将第i个元素的最短距离表示为 。在另一种情况下,所选对中的一个元素被重复,然后在该元素出现的所有最短距离中选择最小的一个。运行一个循环并将所有数组元素的最短距离存储在另一个数组中(让它成为 )。现在,我们得到了所有元素的最短距离。
运行for循环。如果选择的元素是x ,则另一个元素应该是kx 。用更新ans 并在每次更新时,选择先前和当前答案中的最小值。如果kx不在数组中,则将为INFINITE,它将已经初始化。
C++
// C++ code to find maximum shortest distance
// from endpoints
#include
using namespace std;
// function to find maximum shortest distance
int find_maximum(int a[], int n, int k)
{
// stores the shortest distance of every
// element in original array.
unordered_map b;
for (int i = 0; i < n; i++) {
int x = a[i];
// shortest distance from ends
int d = min(1 + i, n - i);
if (b.find(x) == b.end())
b[x] = d;
else
/* if duplicates are found, b[x]
is replaced with minimum of the
previous and current position's
shortest distance*/
b[x] = min(d, b[x]);
}
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
int x = a[i];
// similar elements ignore them
// cause we need distinct elements
if (x != k - x && b.find(k - x) != b.end())
ans = min(max(b[x], b[k - x]), ans);
}
return ans;
}
// driver code
int main()
{
int a[] = { 3, 5, 8, 6, 7 };
int K = 11;
int n = sizeof(a) / sizeof(a[0]);
cout << find_maximum(a, n, K) << endl;
return 0;
}
Java
// Java code to find maximum shortest distance
// from endpoints
import java.util.*;
class GFG
{
static void makePermutation(int []a, int n)
{
// Store counts of all elements.
HashMap count = new HashMap();
for (int i = 0; i < n; i++)
{
if(count.containsKey(a[i]))
{
count.put(a[i], count.get(a[i]) + 1);
}
else
{
count.put(a[i], 1);
}
}
}
// function to find maximum shortest distance
static int find_maximum(int a[], int n, int k)
{
// stores the shortest distance of every
// element in original array.
HashMap b = new HashMap();
for (int i = 0; i < n; i++)
{
int x = a[i];
// shortest distance from ends
int d = Math.min(1 + i, n - i);
if (!b.containsKey(x))
b.put(x, d);
else
{
/* if duplicates are found, b[x]
is replaced with minimum of the
previous and current position's
shortest distance*/
b. put(x, Math.min(d, b.get(x)));
}
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
{
int x = a[i];
// similar elements ignore them
// cause we need distinct elements
if (x != k - x && b.containsKey(k - x))
ans = Math.min(Math.max(b.get(x),
b.get(k - x)), ans);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 3, 5, 8, 6, 7 };
int K = 11;
int n = a.length;
System.out.println(find_maximum(a, n, K));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 code to find maximum shortest
# distance from endpoints
# function to find maximum shortest distance
def find_maximum(a, n, k):
# stores the shortest distance of every
# element in original array.
b = dict()
for i in range(n):
x = a[i]
# shortest distance from ends
d = min(1 + i, n - i)
if x not in b.keys():
b[x] = d
else:
# if duplicates are found, b[x]
# is replaced with minimum of the
# previous and current position's
# shortest distance*/
b[x] = min(d, b[x])
ans = 10**9
for i in range(n):
x = a[i]
# similar elements ignore them
# cause we need distinct elements
if (x != (k - x) and (k - x) in b.keys()):
ans = min(max(b[x], b[k - x]), ans)
return ans
# Driver code
a = [3, 5, 8, 6, 7]
K = 11
n = len(a)
print(find_maximum(a, n, K))
# This code is contributed by mohit kumar
C#
// C# code to find maximum shortest distance
// from endpoints
using System;
using System.Collections.Generic;
class GFG
{
static void makePermutation(int []a, int n)
{
// Store counts of all elements.
Dictionary count = new Dictionary();
for (int i = 0; i < n; i++)
{
if(count.ContainsKey(a[i]))
{
count[a[i]] = count[a[i]] + 1;
}
else
{
count.Add(a[i], 1);
}
}
}
// function to find maximum shortest distance
static int find_maximum(int []a, int n, int k)
{
// stores the shortest distance of every
// element in original array.
Dictionary b = new Dictionary();
for (int i = 0; i < n; i++)
{
int x = a[i];
// shortest distance from ends
int d = Math.Min(1 + i, n - i);
if (!b.ContainsKey(x))
b.Add(x, d);
else
{
/* if duplicates are found, b[x]
is replaced with minimum of the
previous and current position's
shortest distance*/
b[x] = Math.Min(d, b[x]);
}
}
int ans = int.MaxValue;
for (int i = 0; i < n; i++)
{
int x = a[i];
// similar elements ignore them
// cause we need distinct elements
if (x != k - x && b.ContainsKey(k - x))
ans = Math.Min(Math.Max(b[x],
b[k - x]), ans);
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int []a = { 3, 5, 8, 6, 7 };
int K = 11;
int n = a.Length;
Console.WriteLine(find_maximum(a, n, K));
}
}
// This code is contributed by Princi Singh
输出:
2