Problem Statement: Minimize the Maximum

You are given an array of integers and an integer K. For each array element, you can adjust it by increasing or decreasing it by a value of K. Your goal is to minimize the difference between the maximum and minimum elements of the array after these modifications.

Input:

The first line of input contains an integer T, denoting the number of test cases.
For each test case:
The first line contains an integer N, the number of elements in the array.
The second line contains N integers representing the elements of the array.
The third line contains an integer K.

Output:

For each test case, output the minimum possible difference between the maximum and minimum elements of the array after modifications.

Example:

Input:
2
4
1 5 8 10
2
3
3 9 12
4
Output:
5
5

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 10^5
  • 0 <= arr[i] <= 10^5
  • 0 <= K <= 10^5
  • Time limit: 1 second

Note:

You are not required to print the output. Just return the answer.
Be the first one to answer
Select
Add answer anonymously...

Top System Engineer Interview Questions Asked at TCS

Q. How do you handle frames in Selenium?
Q. What synchronization methods have you used in Selenium?
Q. What is Java?
System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits