Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Kth Permutation Sequence Editorial

DSA Editorial, Solution and Code

Practice Problem Link: https://workat.tech/problem-solving/practice/kth-permutation-sequence

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given two integers n and k, find the kth permutation sequence of numbers from 1 to n.

Naive Approach

The naive approach is to generate all the permutations of the sequence, and then print the kth sequence in the list.

Analysis

  • Time Complexity: O(n * n!)
  • Space Complexity: O(n * n!)

Implementation

C++

vector<string> result;
void permute(string s, string possible) {
	if(s.size() == 0) {
		result.push_back(possible);
		return;
	}
	for(int i = 0; i < s.size(); i++) {
		string left = s.substr(0, i);
		string right = s.substr(i + 1, s.size() - i - 1);
		string total = left + right;
		permute(total, possible + s[i]);
	}
}
string getKthPermutation(int n, int k) {
    string permutation = "";
	result.clear();
	for(int i = 1; i <= n; i++) {
		permutation += to_string(i);
	}
	permute(permutation, "");
	return result[k - 1];
}

Java

class Solution {
	void permute(String s, String possible, ArrayList<String> result) {
		if(s.length() == 0) {
			result.add(possible);
			return;
		}
		for(int i = 0; i < s.length(); i++) {
			String left = s.substring(0, i);
			String right = s.substring(i + 1);
			String total = left + right;
			permute(total, possible + s.charAt(i), result);
		}
	}
	String getKthPermutation(int n, int k) {
	    String permutation = "";
		for(int i = 1; i <= n; i++) {
			permutation += (Integer.toString(i));
		}
		ArrayList<String> result = new ArrayList<>();
		permute(permutation, "", result);
		return result.get(k - 1);
	}
}

Optimal Approach

We know that the total number of possible permutations is n!. Considering k to be 0 - based, we can try to find which number will be in the first position. The number at the first position should be k / (n - 1)! + 1 th integer. We can modify k with this algorithm, and try to find the next numbers at the remaining positions, till we have used up all the numbers.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Implementation

C++
int getFactorial(int n) {
	int factorial = 1;
	for(int i = 2; i <= n; i++) {
		factorial *= i;
	}
	return factorial;
}
string getKthPermutation(int n, int k) {
    string result = "", auxiliary = "";
	for(int i = 1; i <= n; i++) {
		auxiliary += (i + '0');
	}
	long long length = getFactorial(n);
	k--;
	while(!auxiliary.empty()) {
		length /= n;
		long long index = k / length;
		k %= length;
		result += auxiliary[index];
		auxiliary.erase(index, 1);
		n--;
	}
	return result;
}
Java
class Solution {
	long getFactorial(int n) {
		long factorial = 1;
		for(int i = 2; i <= n; i++) {
			factorial *= i;
		}
		return factorial;
	}
	String getKthPermutation(int n, int k) {
	    StringBuilder result = new StringBuilder();
		StringBuilder auxiliary = new StringBuilder();
		for(int i = 1; i <= n; i++) {
			auxiliary.append((char)(i + '0'));
		}
		long length = getFactorial(n);
		k--;
		while(auxiliary.length() > 0) {
			length /= n;
			long index = k / length;
			k %= length;
			result.append(auxiliary.charAt((int)index));
			auxiliary.delete((int)index, (int)index + 1);
			n--;
		}
		return new String(result);
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Generate Parentheses
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet