Related Articles

# Maximum value of X such that difference between any array element and X does not exceed K

• Difficulty Level : Basic
• Last Updated : 22 Apr, 2021

Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the maximum possible integer X, such that the absolute difference between any array element and X is at most K. If no such value of X exists, then print “-1”.

Examples:

Input: arr[] = {6, 4, 8, 5}, K = 2
Output: 6
Explanation: Considering X to be 6, the absolute difference between every array element and X(= 6) is at most K (= 2), as illustrated below:

• Absolute difference between arr[0](= 6) and X(= 6) = |6 – 6| = 0.
• Absolute difference between arr[1](= 4) and X(= 6) = |4 – 6| = 2.
• Absolute difference between arr[2](= 8) and X(= 6) = |8 – 6| = 2.
• Absolute difference between arr[3](= 5) and X(= 6) = |5 – 6| = 1.

Input: arr[] = {1, 2, 5}, K = 2
Output: 3

Approach: The given problem can be solved based on the following observations:

• Considering array elements to be arr[i], the value of |arr[i] – X| must be at most K.
• If arr[i] > X, then X ≤ (arr[i] – K). Otherwise, X ≤ (arr[i] + K).
• From the above two equations, the maximum value of X must be the sum of minimum value of arr[i] and K.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## 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

 ``
Output:
`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up