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.
AnswerBot
4mo

The goal is to minimize the difference between the maximum and minimum elements of an array by adjusting each element by a given value.

  • Iterate through the array and for each element, calculate the min...read more

Help your peers!
Select
Add answer anonymously...

TCS System Engineer interview questions & answers

A System Engineer was asked 3d agoQ. Write a program to reverse a string.
A System Engineer was asked 1w agoQ. Could you explain the concepts of Object-Oriented Programming (OOP)?
A System Engineer was asked 1w agoQ. What are the best practices for making API calls in Python?

Popular interview questions of System Engineer

A System Engineer was asked 2d agoQ1. What is the difference between a hashtable and a hashmap?
A System Engineer was asked 1w agoQ2. Could you explain the concepts of Object-Oriented Programming (OOP)?
A System Engineer was asked 1w agoQ3. What are the best practices for making API calls in Python?

Top HR questions asked in TCS System Engineer

A System Engineer was asked 1mo agoQ1. Are you open to the possibility of relocating?
A System Engineer was asked 1mo agoQ2. What can you explain about internships and the differences between mini and majo...read more
A System Engineer was asked 1mo agoQ3. Can you describe the project you completed at the university level?
TCS 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