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:
- Find the smallest element of the given array arr[] in a variable, say S.
- Consider the maximum value of X as (S + K).
- Traverse the given array arr[] and if the value of the absolute difference of arr[i] and X is K, then update X to -1 and break out of the loop. Otherwise, continue the iteration.
- After completing the above steps, print the value of X as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> 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 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 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# 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 |
<script> // Javascript program for
// the above approach
// Function to find maximum value
// of X such that |A[i] - X| ? K
function maximumNumber(arr, N, K)
{
// Stores the smallest
// array element
let minimum = Math.min(...arr)
// Store the possible value of X
let ans = minimum + K;
// Traverse the array A[]
for (let 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
document.write(ans)
}
// Driver Code
let arr = [1, 2, 5]
let K = 2
let N = arr.length
maximumNumber(arr, N, K);
// This code is contributed by Hritik
</script>
|
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2: Binary Search:
Another approach to solve this problem is to use binary search. We can find the range of possible values of X using the smallest and largest elements of the array. Then, we can perform a binary search in this range to find the maximum value of X that satisfies the given condition.
Here is the implementation of this approach:
#include <bits/stdc++.h> 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 and largest array elements
int minimum = *min_element(arr, arr + N);
int maximum = *max_element(arr, arr + N);
// Store the range of possible values of X
int low = minimum + K;
int high = maximum - K;
// Perform binary search to find
// maximum value of X that satisfies
// the given condition
while (low <= high) {
int mid = low + (high - low) / 2;
bool possible = true ;
for ( int i = 0; i < N; i++) {
if ( abs (arr[i] - mid) > K) {
possible = false ;
break ;
}
}
if (possible) {
low = mid + 1;
} else {
high = mid - 1;
}
}
// Return the maximum value of X
return high;
} // Driver Code int main()
{ int arr[] = {1, 2, 5};
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
int ans = maximumNumber(arr, N, K);
if (ans == -1) {
cout << "No such X exists" ;
} else {
cout << ans;
}
return 0;
} |
import java.util.*;
public class Main {
// Function to find maximum value // of X such that |A[i] - X| ? K public static int maximumNumber( int arr[], int N, int K) {
// Stores the smallest and largest array elements
int minimum = Arrays.stream(arr).min().getAsInt();
int maximum = Arrays.stream(arr).max().getAsInt();
// Store the range of possible values of X
int low = minimum + K;
int high = maximum - K;
// Perform binary search to find
// maximum value of X that satisfies
// the given condition
while (low <= high) {
int mid = low + (high - low) / 2 ;
boolean possible = true ;
for ( int i = 0 ; i < N; i++) {
if (Math.abs(arr[i] - mid) > K) {
possible = false ;
break ;
}
}
if (possible) {
low = mid + 1 ;
} else {
high = mid - 1 ;
}
}
// Return the maximum value of X
return high;
} // Driver Code public static void main(String[] args) {
int arr[] = { 1 , 2 , 5 };
int K = 2 ;
int N = arr.length;
int ans = maximumNumber(arr, N, K);
if (ans == - 1 ) {
System.out.println( "No such X exists" );
} else {
System.out.println(ans);
}
} } |
# Function to find maximum value # of X such that |A[i] - X| ? K def maximumNumber(arr, N, K):
# Stores the smallest and largest array elements
minimum = min (arr)
maximum = max (arr)
# Store the range of possible values of X
low = minimum + K
high = maximum - K
# Perform binary search to find
# maximum value of X that satisfies
# the given condition
while low < = high:
mid = low + (high - low) / / 2
possible = True
for i in range (N):
if abs (arr[i] - mid) > K:
possible = False
break
if possible:
low = mid + 1
else :
high = mid - 1
# Return the maximum value of X
return high
# Driver Code arr = [ 1 , 2 , 5 ]
K = 2
N = len (arr)
ans = maximumNumber(arr, N, K)
if ans = = - 1 :
print ( "No such X exists" )
else :
print (ans)
|
using System;
using System.Linq;
class MainClass {
public static int maximumNumber( int [] arr, int N, int K)
{
// Stores the smallest and largest array elements
int minimum = arr.Min();
int maximum = arr.Max();
// Store the range of possible values of X
int low = minimum + K;
int high = maximum - K;
// Perform binary search to find
// maximum value of X that satisfies
// the given condition
while (low <= high) {
int mid = low + (high - low) / 2;
bool possible = true ;
for ( int i = 0; i < N; i++) {
if (Math.Abs(arr[i] - mid) > K) {
possible = false ;
break ;
}
}
if (possible) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// Return the maximum value of X
return high;
}
public static void Main()
{
int [] arr = { 1, 2, 5 };
int K = 2;
int N = arr.Length;
int ans = maximumNumber(arr, N, K);
if (ans == -1) {
Console.WriteLine( "No such X exists" );
}
else {
Console.WriteLine(ans);
}
}
} // This code is contributed by sarojmcy2e |
// Function to find maximum value // of X such that |A[i] - X| ? K function maximumNumber(arr, K) {
const N = arr.length;
// Stores the smallest and largest array elements
const minimum = Math.min(...arr);
const maximum = Math.max(...arr);
// Store the range of possible values of X
let low = minimum + K;
let high = maximum - K;
// Perform binary search to find
// maximum value of X that satisfies
// the given condition
while (low <= high) {
const mid = low + Math.floor((high - low) / 2);
let possible = true ;
for (let i = 0; i < N; i++) {
if (Math.abs(arr[i] - mid) > K) {
possible = false ;
break ;
}
}
if (possible) {
low = mid + 1;
} else {
high = mid - 1;
}
}
// Return the maximum value of X
return high;
} // Driver Code const arr = [1, 2, 5]; const K = 2; const ans = maximumNumber(arr, K); if (ans == -1) {
console.log( "No such X exists" );
} else {
console.log(ans);
} // This code is contributed by Sundaram |
3
Time Complexity: O(N Log N)
Auxiliary Space: O(1)