原文:

给定一个由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成对。 如果是,则以两个元素之间最短距离的最大值作为答案,将其与前一对元素的最短距离进行比较,并以这两个元素中的最小值作为答案。 循环结束时,我们将获得所需的输出。

有效方法:显然,最短距离是指距左端的距离和距右端的距离,即min(1 i, n-i)。 让我们将第 i 个元素的最短距离表示为di。 还有另一种情况,即重复选定对中的一个元素,然后在该元素出现的所有最短距离中选择最小的一个。 循环运行,并将所有数组元素中最短的距离存储在另一个数组中(将其设为d[])。 现在,我们得到了所有元素的最短距离。

运行for循环。 如果选取的元素是x,则另一个元素应该是k-x。 用max(d[x], d[k-x])更新ans,并在每次更新时,选择先前和当前答案中的最小值。 如果k-x不在数组中,则d[k-x]将为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