Given an array arr[] and an integer k, where every element is at most k positions away from its correct sorted position. This means that if the array were completely sorted, the element at index i in the given array can be at any index from i - k to i + k.
Examples:
Input: arr[]= [2, 3, 1, 4], k = 2
Output: [1, 2, 3, 4]
Explanation: All elements are at most k = 2 positions away from their correct positions.
Element 1 moves from index 2 to 0
Element 2 moves from index 0 to 1
Element 3 moves from index 1 to 2
Element 4 stays at index 3Input: arr[]= [1, 4, 5, 2, 3, 6, 7, 8, 9, 10], k = 2
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Explanation : The sorted array will be 1 2 3 4 5 6 7 8 9 10
Table of Content
[Naive Approach] Using Sorting - O(n log(n)) Time and O(1) Space
We can sort the array using any sorting algorithm to get the required order.
Note: This approach might accepts on online judges, but it ignores the given constraint, so it is not suitable for real interview scenarios where an optimized solution is expected.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void nearlySorted(vector<int> &arr, int k) {
// directly sort the array
sort(arr.begin(), arr.end());
}
int main() {
vector<int> arr = {2, 3, 1, 4};
int k = 2;
nearlySorted(arr, k);
for (int x : arr)
cout << x << ' ';
return 0;
}
import java.util.Arrays;
class GFG {
static void nearlySorted(int[] arr, int k) {
// directly sort the array
Arrays.sort(arr);
}
public static void main(String[] args) {
int[] arr = {2, 3, 1, 4};
int k = 2;
nearlySorted(arr, k);
for (int x : arr)
System.out.print(x + " ");
}
}
def nearlySorted(arr, k):
# directly sort the array
arr.sort()
if __name__ == '__main__':
arr = [2, 3, 1, 4]
k = 2
nearlySorted(arr, k)
for x in arr:
print(x, end=" ")
using System;
class GFG {
static void nearlySorted(int[] arr, int k) {
// directly sort the array
Array.Sort(arr);
}
static void Main() {
int[] arr = {2, 3, 1, 4};
int k = 2;
nearlySorted(arr, k);
foreach (int x in arr)
Console.Write(x + " ");
}
}
function nearlySorted(arr, k) {
// directly sort the array
arr.sort((a, b) => a - b);
}
// Driver Code
let arr = [2, 3, 1, 4];
let k = 2;
nearlySorted(arr, k);
console.log(arr.join(" "));
Output
1 2 3 4
[Expected Approach] Using Heap - O(n*log k) Time and O(k) Space
In this array, every element is at most k positions away from its correct spot. This means the element at index i could be anywhere between i - k and i + k in the sorted array. If we start placing the correct elements from left to right, then the element for the current position must be within the next k+1 elements, and we don’t need to check the elements to the left.
For these next k+1 elements, the best element to place at index i in the sorted array is the minimum element. Therefore, the problem reduces to finding the minimum element in a window of size k+1 for each position. To do this efficiently, we can use a min-heap, which allows us to quickly extract the minimum and insert the next element as we move through the array.
//Driver Code Starts
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
//Driver Code Ends
void nearlySorted(vector<int> &arr, int k) {
int n = arr.size();
// creating a min heap
priority_queue<int, vector<int>, greater<int>> pq;
// pushing first k elements in pq
for (int i = 0; i < k; i++)
pq.push(arr[i]);
int i;
for (i = k; i < n; i++) {
pq.push(arr[i]);
// size becomes k+1 so pop it
// and add minimum element in (i-k) index
arr[i - k] = pq.top();
pq.pop();
}
// puting remaining elements in array
while (!pq.empty()) {
arr[i - k] = pq.top();
pq.pop();
i++;
}
}
//Driver Code Starts
int main() {
vector<int> arr = {2, 3, 1, 4};
int k = 2;
nearlySorted(arr, k);
for (int x : arr)
cout << x << ' ';
return 0;
}
//Driver Code Ends
//Driver Code Starts
import java.util.PriorityQueue;
class GFG {
//Driver Code Ends
static void nearlySorted(int[] arr, int k) {
int n =