#K13951. Valid Row Formation
Valid Row Formation
Valid Row Formation
You are given an array of integers representing the heights of people and an integer \( k \). Your task is to determine if it is possible to form a valid row by arranging the people in non-decreasing order such that the difference between any two consecutive heights does not exceed \( k \). If the difference between any two adjacent heights is greater than \( k \), then it is not possible to form a valid row and you should output an empty array ([]
). Otherwise, output the arranged row.
Input/Output Requirement: Read input from stdin
and write output to stdout
. When a valid arrangement exists, output the row as space-separated integers on a single line. If no valid arrangement exists, output []
(without quotes).
Note: The input begins with an integer \( n \) denoting the number of people, followed by \( n \) space-separated integers representing the heights, and then an integer \( k \) on a new line.
inputFormat
The first line contains a single integer \( n \) which is the number of people.
The second line contains \( n \) space-separated integers representing the heights.
The third line contains the integer \( k \), the maximum allowed difference between any two consecutive heights.
outputFormat
If a valid row can be formed by sorting the heights in non-decreasing order such that the difference between every two consecutive elements is at most \( k \), then output the sorted row as space-separated integers on a single line.
If the condition is not met, output []
.
5
1 3 8 6 4
2
1 3 4 6 8
</p>