Count operations to sort given Permutation Array in increasing order
Last Updated :
23 Jul, 2025
Given an unsorted permutation array of size N and an integer K ( 1 ≤ K ≤ N ), the task is to sort this permutation array in increasing order in the minimum number of steps, where in each step K elements have to be removed from the array and inserted at the front in increasing order and remaining elements should be in arbitrary order.
Examples:
Input: N = 5, P[] = [ 2, 4, 1, 5, 3 ], K = 2
Output: 2
Explanation: Following are the operations that can be performed to sort the permutation in increasing order.
- Operation1: Choose elements 3, 4 remove them from P then sort them in increasing order - 4, 3 and finally add them in front of P, So, P becomes [3, 4, 2, 1, 5].
- Operation 2: Choose elements 1, 2 remove them from P then sort them in increasing order - 1, 2 and finally add them in front of P, So P becomes [1, 2, 3, 4, 5}.
So the minimum number of operations required to sort the permutation P in increasing order is 2.
Input : N = 3, P[] = [3, 1, 2], K = 1
Output: 2
Explanation: Following are the operations that can be performed to sort the permutation in increasing order.
- Operation 1: Choose element 2 remove it from P then sort it in increasing order - 2 and finally add it in front of P, So P becomes [2, 3, 1].
- Operation 2: Choose element 3 remove it from P then sort it in increasing order - 1 and finally add it in front of P, So P becomes [ 1, 2, 3 ].
So the minimum number of operations required to sort the permutation P in increasing order is 2.
Approach: This can be solved by the following idea:
Suppose x elements do not participate in any operation means they are already sorted in increasing order. And since this x must be maximized to minimize the number of operations, we need to find the maximal subsequence of the numbers [...2, 1]. Let this sequence have w numbers, then the answer is ceil((n − w) / k).
Follow the steps to solve the problem:
- Initialize correct_pos with 0.
- Initialize counter with 0.
- Run a loop from i: N-1 to 0 and check:
- If ( P[i] == counter ) then increment correct_pos with 1 and counter with 1.
- Calculate the Number of wrong_pos elements by ( N - correct_pos ).
- Calculate minimum number operation by taking ceil of ( wrong_pos / K ).
- Return the minimum number of operations.
Below is the implementation of the above approach:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function performing calculation
int min_operation_to_sort(int N, vector<int>& P, int K)
{
// Initializing correct_pos
// varaible with 0
int correct_pos = 0;
// Initializing counter with 1
int counter = 1;
for (int i = N - 1; i >= 0; i--) {
// If counter is equal to P[i] then
// correct_pos is incremented with 1.
if (P[i] == counter) {
correct_pos++;
// Counter is incremented
counter++;
}
}
// Number of wrong_pos is calculated
int wrong_pos = N - correct_pos;
// Number of operation calculated
int min_operation = (wrong_pos + K - 1) / K;
// Returning minimum number
// of operations
return min_operation;
}
// Driver Code
int main()
{
int N = 5;
vector<int> P = { 2, 4, 1, 5, 3 };
// Number of elements to be choosen
int K = 2;
// Calling function performing operation
// and printing the result.
cout << min_operation_to_sort(N, P, K);
return 0;
}
Java
import java.util.*;
public class Main {
// Function performing calculation
public static int minOperationToSort(int N, List<Integer> P, int K) {
// Initializing correct_pos variable with 0
int correct_pos = 0;
// Initializing counter with 1
int counter = 1;
for (int i = N - 1; i >= 0; i--) {
// If counter is equal to P[i] then
// correct_pos is incremented with 1.
if (P.get(i) == counter) {
correct_pos++;
// Counter is incremented
counter++;
}
}
// Number of wrong_pos is calculated
int wrong_pos = N - correct_pos;
// Number of operation calculated
int min_operation = (wrong_pos + K - 1) / K;
// Returning minimum number of operations
return min_operation;
}
// Driver Code
public static void main(String[] args) {
int N = 5;
List<Integer> P = new ArrayList<>(Arrays.asList(2, 4, 1, 5, 3));
// Number of elements to be chosen
int K = 2;
// Calling function performing operation
// and printing the result.
System.out.println(minOperationToSort(N, P, K));
}
}
//This code is contributed by Akash Jha
Python3
# Python3 code for the above approach
# Function performing calculation
def min_operation_to_sort(N, P, K):
# Initializing correct_pos
# variable with 0
correct_pos = 0
# Initializing counter with 1
counter = 1
for i in range(N - 1, -1, -1):
# If counter is equal to P[i] then
# correct_pos is incremented with 1.
if P[i] == counter:
correct_pos += 1
# Counter is incremented
counter += 1
# Number of wrong_pos is calculated
wrong_pos = N - correct_pos
# Number of operation calculated
min_operation = (wrong_pos + K - 1) // K
# Returning minimum number
# of operations
return min_operation
# Driver Code
N = 5
P = [2, 4, 1, 5, 3]
# Number of elements to be chosen
K = 2
# Calling function performing operation
# and printing the result.
print(min_operation_to_sort(N, P, K))
# This code is contributed by Prajwal Kandekar
C#
using System;
using System.Collections.Generic;
class Program {
// Function performing calculation
static int min_operation_to_sort(int N, List<int> P, int K) {
// Initializing correct_pos variable with 0
int correct_pos = 0;
// Initializing counter with 1
int counter = 1;
for (int i = N - 1; i >= 0; i--) {
// If counter is equal to P[i] then
// correct_pos is incremented with 1.
if (P[i] == counter) {
correct_pos++;
// Counter is incremented
counter++;
}
}
// Number of wrong_pos is calculated
int wrong_pos = N - correct_pos;
// Number of operation calculated
int min_operation = (wrong_pos + K - 1) / K;
// Returning minimum number of operations
return min_operation;
}
// Driver Code
static void Main(string[] args) {
int N = 5;
List<int> P = new List<int>() { 2, 4, 1, 5, 3 };
// Number of elements to be chosen
int K = 2;
// Calling function performing operation
// and printing the result.
Console.WriteLine(min_operation_to_sort(N, P, K));
}
}
// This code is contributed by Akash Jha
JavaScript
function min_operation_to_sort(N, P, K) {
// Initializing correct_pos variable with 0
let correct_pos = 0;
// Initializing counter with 1
let counter = 1;
for (let i = N - 1; i >= 0; i--) {
// If counter is equal to P[i] then
// correct_pos is incremented with 1.
if (P[i] == counter) {
correct_pos++;
// Counter is incremented
counter++;
}
}
// Number of wrong_pos is calculated
let wrong_pos = N - correct_pos;
// Number of operation calculated
let min_operation = Math.ceil(wrong_pos / K);
// Returning minimum number of operations
return min_operation;
}
// Driver Code
let N = 5;
let P = [2, 4, 1, 5, 3];
// Number of elements to be chosen
let K = 2;
// Calling function performing operation
// and printing the result.
console.log(min_operation_to_sort(N, P, K));
// This code is contributed by Akash Jha
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
By using the cycle: A cycle in a permutation is a sequence of elements that can be cyclically permuted, which means that the elements can be shifted to the right or left within the sequence without changing the order of the elements. For example, the permutation [3, 1, 4, 2] has two cycles: [3, 4, 2, 1] and [1].
This can sort the permutation by performing a series of cyclic permutations, where each cyclic permutation involves only elements that belong to the same cycle. In each cycle, sort the elements by performing a series of swaps. The number of swaps required to sort a cycle is equal to the length of the cycle minus one.
Follow the steps to solve the problem:
- Initialize a list of visited elements to all False.
- Initialize a variable for the number of operations to zero.
- For each element in the permutation, if the element has not been visited, then:
a. Start a new cycle with this element.
b. While the cycle has not returned to the starting element:
i. Mark the current element as visited.
ii. Move to the next element in the cycle.
iii. If the next element has not been visited, add it to the cycle.
c. Sort the cycle by performing a series of swaps, and increment the number of operations by the number of swaps. - Return the number of operations.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int find_min_operations(int N, vector<int>& P, int K)
{
vector<bool> visited(N, false);
int num_ops = 0;
for (int i = 0; i < N; i++) {
if (!visited[i]) {
int cycle_start = i;
int cycle_length = 0;
while (!visited[cycle_start]) {
visited[cycle_start] = true;
cycle_start = P[cycle_start] - 1;
cycle_length++;
}
num_ops += ceil((double)(cycle_length - 1) / K);
}
}
return num_ops;
}
int main()
{
int N = 5, K = 2;
vector<int> P = { 2, 4, 1, 5, 3 };
int min_ops = find_min_operations(N, P, K);
cout << "Minimum number of operations: " << min_ops
<< endl;
return 0;
}
Java
import java.util.*;
public class Main {
public static int
findMinOperations(int N, List<Integer> P, int K)
{
// Keep track of visited indices
boolean[] visited = new boolean[N];
int numOps = 0;
for (int i = 0; i < N; i++) {
if (!visited[i]) {
// Found a cycle starting from i
int cycleStart = i;
int cycleLength = 0;
while (!visited[cycleStart]) {
// Mark the current index as visited and
// move to the next index in the cycle
visited[cycleStart] = true;
cycleStart = P.get(cycleStart) - 1;
cycleLength++;
}
// Calculate the number of operations needed
// to sort this cycle
numOps += Math.ceil(
(double)(cycleLength - 1) / K);
}
}
return numOps;
}
// Driver code
public static void main(String[] args)
{
int N = 5, K = 2;
List<Integer> P = Arrays.asList(2, 4, 1, 5, 3);
int minOps = findMinOperations(N, P, K);
System.out.println("Minimum number of operations: "
+ minOps);
}
}
Python3
import math
# function to find the minimum number of operations required
def find_min_operations(N, P, K):
# list to visited elements
visited = [False] * N
num_ops = 0
for i in range(N):
# if the element is not visited, it is the start of a new cycle
if not visited[i]:
cycle_start = i
cycle_length = 0
# keep following the permutation until we reach the start of the cycle
while not visited[cycle_start]:
visited[cycle_start] = True
cycle_start = P[cycle_start] - 1
cycle_length += 1
# calculate the number of operations required for this cycle and add it to the total
num_ops += math.ceil((cycle_length - 1) / K)
# return the answer
return num_ops
# example inputs
N = 5
K = 2
P = [2, 4, 1, 5, 3]
# call the function and print the result
min_ops = find_min_operations(N, P, K)
print("Minimum number of operations:", min_ops)
C#
using System;
class Program
{
static void Main()
{
// Example inputs
int N = 5;
int K = 2;
int[] P = { 2, 4, 1, 5, 3 };
// Call the function and print the result
int minOps = FindMinOperations(N, P, K);
Console.WriteLine("Minimum number of operations: " + minOps);
}
// Function to find the minimum number of operations required
static int FindMinOperations(int N, int[] P, int K)
{
// Array to store visited elements
bool[] visited = new bool[N];
int numOps = 0;
for (int i = 0; i < N; i++)
{
// If the element is not visited, it is the start of a new cycle
if (!visited[i])
{
int cycleStart = i;
int cycleLength = 0;
// Keep following the permutation until we reach the start of the cycle
while (!visited[cycleStart])
{
visited[cycleStart] = true;
cycleStart = P[cycleStart] - 1;
cycleLength += 1;
}
// Calculate the number of operations required for this cycle and add it to the total
numOps += (int)Math.Ceiling((cycleLength - 1) / (double)K);
}
}
// Return the answer
return numOps;
}
}
JavaScript
function find_min_operations(N, P, K) {
// list to visited elements
var visited = new Array(N).fill(false);
var num_ops = 0;
for (var i = 0; i < N; i++) {
// if the element is not visited, it is the start of a new cycle
if (!visited[i]) {
cycle_start = i;
cycle_length = 0;
// keep following the permutation until we reach
// the start of the cycle
while (!visited[cycle_start]) {
visited[cycle_start] = true;
cycle_start = P[cycle_start] - 1;
cycle_length++;
}
// calculate the number of operations required for
// this cycle and add it to the total
num_ops += Math.ceil((cycle_length - 1) / K);
}
}
// return the answer
return num_ops;
}
// example inputs
var N = 5;
var K = 2;
var P = [2, 4, 1, 5, 3];
// call the function and print the result
var min_ops = find_min_operations(N, P, K);
console.log("Minimum number of operations:", min_ops);
// This code is contribtued by Tapesh(tapeshdua420)
OutputMinimum number of operations: 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem