Interview Shuriken 52: Sort the Objects
You are given an array containing ‘n’ objects. On their creation, each object was given a creation number, which is a unique number from 1 to ‘n’. This means that an object with creation number ‘4’ was created just before the object with creation number ‘5’.
You have to sort the objects in-place on their creation number in O(n) and without any extra space. For simplicity, let’s assume you are given an integer array containing only the creation numbers, though each number is actually an object.
Example 1:
Input: [3, 1, 5, 4, 2]
Output: [1, 2, 3, 4, 5]
Example 2:
Input: [2, 6, 4, 3, 1, 5]
Output: [1, 2, 3, 4, 5, 6]
Example 3:
Input: [1, 5, 6, 4, 3, 2]
Output: [1, 2, 3, 4, 5, 6]
Input Format:
Constraints:
Output Format:
Sample Input 1:
Sample Output 1:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Read input as specified in the question. | |
## Print output as specified in the question. | |
n=int(input()) | |
arr=sorted(list(map(int,input().split()))) | |
for i in range(len(arr)): | |
print(arr[i],end=" ") |
Comments
Post a Comment
Please give us your valuable feedback