Nearly Sorted (original) (raw)

Last Updated : 20 Dec, 2025

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 3

**Input: 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

Try It Yourselfredirect icon

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.

C++ `

#include #include #include using namespace std;

void nearlySorted(vector &arr, int k) {

// directly sort the array
sort(arr.begin(), arr.end());

}

int main() { vector arr = {2, 3, 1, 4};

int k = 2;

nearlySorted(arr, k);

for (int x : arr)
    cout << x << ' ';
return 0;

}

Java

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 + " ");
}

}

Python

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=" ")

C#

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 + " ");
}

}

JavaScript

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(" "));

`

[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.

C++ `

//Driver Code Starts #include #include #include #include using namespace std; //Driver Code Ends

void nearlySorted(vector &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 arr = {2, 3, 1, 4}; int k = 2; nearlySorted(arr, k); for (int x : arr) cout << x << ' '; return 0; } //Driver Code Ends

Java

//Driver Code Starts import java.util.PriorityQueue;

class GFG { //Driver Code Ends

static void nearlySorted(int[] arr, int k) {
    int n = arr.length;

    // Creating a min heap
    PriorityQueue<Integer> pq =  new PriorityQueue<>();

    // Pushing first k elements in pq
    for (int i = 0; i < k; i++) 
        pq.add(arr[i]);

    int i;
    for (i = k; i < n; i++) {
        pq.add(arr[i]);

        // Size becomes k+1 so pop it
        // and add minimum element in (i-k) index
        arr[i - k] = pq.poll();
    }

    // Putting remaining elements in array
    while (!pq.isEmpty()) {
        arr[i - k] = pq.poll();
        i++;
    }
}

//Driver Code Starts public static void main(String[] args) { int k = 2; int[] arr = {2, 3, 1, 4};

    nearlySorted(arr, k);

    for (int x : arr) {
        System.out.print(x + " ");
    }
}

}

//Driver Code Ends

Python

#Driver Code Starts import heapq #Driver Code Ends

def nearlySorted(arr, k):

n = len(arr)

# Creating a min heap
pq = []

# Pushing first k elements in pq
for i in range(k):
    heapq.heappush(pq, arr[i])

i = k
index = 0

while i < n:
    heapq.heappush(pq, arr[i])

    # Size becomes k+1 so pop it
    # and add minimum element in (index) position
    arr[index] = heapq.heappop(pq)
    i += 1
    index += 1

# Putting remaining elements in array
while pq:
    arr[index] = heapq.heappop(pq)
    index += 1

#Driver Code Starts if name == "main": k = 2 arr = [2, 3, 1, 4]

nearlySorted(arr, k)

print(" ".join(map(str, arr)))

#Driver Code Ends

C#

//Driver Code Starts using System; using System.Collections.Generic;

class MinHeap { private List heap;

public MinHeap() {
    heap = new List<int>();
}

public int Count {
    get { return heap.Count; }
}

public void Add(int val) {
    heap.Add(val);
    HeapifyUp(heap.Count - 1);
}

public int Pop() {
    if (heap.Count == 0) throw new InvalidOperationException("Heap is empty");

    int min = heap[0];
    heap[0] = heap[heap.Count - 1];
    heap.RemoveAt(heap.Count - 1);
    HeapifyDown(0);
    return min;
}

private void HeapifyUp(int index) {
    while (index > 0) {
        int parent = (index - 1) / 2;
        if (heap[parent] <= heap[index]) break;
        int temp = heap[parent];
        heap[parent] = heap[index];
        heap[index] = temp;
        index = parent;
    }
}

private void HeapifyDown(int index) {
    int left, right, smallest;
    while (true) {
        left = 2 * index + 1;
        right = 2 * index + 2;
        smallest = index;

        if (left < heap.Count && heap[left] < heap[smallest])
            smallest = left;
        if (right < heap.Count && heap[right] < heap[smallest])
            smallest = right;

        if (smallest == index) break;

        int temp = heap[index];
        heap[index] = heap[smallest];
        heap[smallest] = temp;

        index = smallest;
    }
}

}

class GFG { //Driver Code Ends

static void nearlySorted(int[] arr, int k) {
    int n = arr.Length;

    // creating a min heap
    MinHeap pq = new MinHeap();

    // pushing first k elements in pq
    for (int i = 0; i < k && i < n; i++)
        pq.Add(arr[i]);

    int index = 0;

    for (int i = k; i < n; i++) {
        pq.Add(arr[i]);

        // size becomes k+1 so pop it
        // and add minimum element in (index) position
        arr[index++] = pq.Pop();
    }

    // putting remaining elements in array
    while (pq.Count > 0) {
        arr[index++] = pq.Pop();
    }
}

//Driver Code Starts static void Main() { int[] arr = { 2, 3, 1, 4 }; int k = 2; nearlySorted(arr, k); foreach (int x in arr) Console.Write(x + " "); } }

//Driver Code Ends

JavaScript

//Driver Code Starts class MinHeap { constructor() { this.heap = []; }

push(val) {
    this.heap.push(val);
    this.heapifyUp();
}

pop() {
    if (this.heap.length === 1) return this.heap.pop();
    const min = this.heap[0];
    this.heap[0] = this.heap.pop();
    this.heapifyDown();
    return min;
}

heapifyUp() {
    let index = this.heap.length - 1;
    while (index > 0) {
        let parentIndex = Math.floor((index - 1) / 2);
        if (this.heap[parentIndex] <= this.heap[index]) break;
        [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
        index = parentIndex;
    }
}

heapifyDown() {
    let index = 0;
    while (true) {
        let left = 2 * index + 1;
        let right = 2 * index + 2;
        let smallest = index;

        if (left < this.heap.length && this.heap[left] < this.heap[smallest]) {
            smallest = left;
        }
        if (right < this.heap.length && this.heap[right] < this.heap[smallest]) {
            smallest = right;
        }
        if (smallest === index) break;

        [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
        index = smallest;
    }
}

isEmpty() {
    return this.heap.length === 0;
}

} //Driver Code Ends

// Function to sort a nearly sorted array function nearlySorted(arr, k) { let n = arr.length; let pq = new MinHeap();

// Pushing first k elements in pq
for (let i = 0; i < k; i++) {
    pq.push(arr[i]);
}

let index = 0;

for (let i = k; i < n; i++) {
    pq.push(arr[i]);

    // Size becomes k+1 so pop it
    // and add minimum element in (index) position
    arr[index] = pq.pop();
    index++;
}

// Putting remaining elements in array
while (!pq.isEmpty()) {
    arr[index] = pq.pop();
    index++;
}

}

//Driver Code Starts // Driver code let arr = [2, 3, 1, 4]; let k = 2;

nearlySorted(arr, k);

console.log(arr.join(" "));

//Driver Code Ends

`