📌  相关文章
📜  X的最大值,以使任何数组元素和X之间的差不超过K

📅  最后修改于: 2021-05-17 04:36:21             🧑  作者: Mango

给定由N个正整数和一个正整数K组成的数组arr [] ,任务是找到最大可能的整数X ,以使任何数组元素和X之间的绝对差最大为K。如果不存在这样的X值,则打印“ -1”

例子:

方法:可以根据以下观察结果解决给定问题:

  • 考虑到数组元素为arr [i]| arr [i] – X |的值最多为K。
  • 如果arr [i]> X ,则X≤(arr [i] – K) 。否则, X≤(arr [i] + K)
  • 根据以上两个方程, X的最大值必须为arr [i]K的最小值之和。

请按照以下步骤解决问题:

  • 在变量S中找到给定数组arr []的最小元素。
  • X的最大值视为(S + K)
  • 遍历给定数组arr [] ,如果arr [i]X的绝对差值为K ,则将X更新为-1并退出循环。否则,请继续迭代。
  • 完成上述步骤后,打印X的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find maximum value
// of X such that |A[i] - X| ≤ K
int maximumNumber(int arr[], int N,
                  int K)
{
    // Stores the smallest array element
    int minimum = *min_element(arr,
                               arr + N);
 
    // Store the possible value of X
    int ans = minimum + K;
 
    // Traverse the array A[]
    for (int i = 0; i < N; i++) {
 
        // If required criteria is not satisfied
        if (abs(arr[i] - ans) > K) {
 
            // Update ans
            ans = -1;
            break;
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 5 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumNumber(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find maximum value
// of X such that |A[i] - X| ≤ K
static void maximumNumber(int arr[], int N,
                          int K)
{
     
    // Stores the smallest array element
    int minimum =  Arrays.stream(arr).min().getAsInt();
 
    // Store the possible value of X
    int ans = minimum + K;
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
         
        // If required criteria is not satisfied
        if (Math.abs(arr[i] - ans) > K)
        {
             
            // Update ans
            ans = -1;
            break;
        }
    }
 
    // Print the result
    System.out.print(ans);
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 2, 5 };
    int K = 2;
    int N = arr.length;
     
    maximumNumber(arr, N, K);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find maximum value
# of X such that |A[i] - X| ≤ K
def maximumNumber(arr, N, K):
     
    # Stores the smallest array element
    minimum = min(arr)
 
    # Store the possible value of X
    ans = minimum + K
 
    # Traverse the array A[]
    for i in range(N):
         
        # If required criteria is not satisfied
        if (abs(arr[i] - ans) > K):
             
            # Update ans
            ans = -1
            break
 
    # Print the result
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    arr =  [1, 2, 5]
    K = 2
    N = len(arr)
     
    maximumNumber(arr, N, K)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find maximum value
// of X such that |A[i] - X| ≤ K
static void maximumNumber(int []arr, int N,
                          int K)
{
     
    // Stores the smallest array element
    int mn = 100000000;
    for(int i = 0; i < N; i++)
    {
        if (arr[i] < mn)
          mn = arr[i];
    }
     
    // Store the possible value of X
    int ans = mn + K;
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
 
        // If required criteria is not satisfied
        if (Math.Abs(arr[i] - ans) > K)
        {
 
            // Update ans
            ans = -1;
            break;
        }
    }
 
    // Print the result
    Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 5 };
    int K = 2;
    int N = arr.Length;
     
    maximumNumber(arr, N, K);
}
}
 
// This code is contributed by ipg2016107


Javascript


输出:
3

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