#include using namespace std; void minSwapsKthLargest(string str, int k){ string res = str; for (int i = 0; i < k; i++) { next_permutation( str.begin(), str.end()); } int swap_count = 0; for (int i = 0; i < res.length(); i++) { if (res[i] != str[i]) { int start = i + 1; while (res[i] != str[start]) { start++; } while (i != start) { swap(str[start], str[start - 1]); start--; swap_count++; } } } cout

Continue ReadingGiven an array arr[] of N integers, the task is to find the lexicographically largest permutation by sequentially inserting the array elements to the front or the back of another array.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and […]

Continue Reading#include using namespace std; bool possible(int x, int S, int N){ int minSum = (x * (x + 1)) / 2; int maxSum = (x * ((2 * N) – x + 1)) / 2; if (S < minSum || S > maxSum) { return false; } return true;} void findPermutation(int N, int L, int R, int S){ int x = R – L + […]

Continue ReadingGiven an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array.Examples:Input: arr[ ] = {5, 5, 4, 4, 2}Output: 1 3Explanation: For all numbers in the range [1, […]

Continue ReadingGiven an array arr[] of length N and an integer K, the task is to find the maximize the length of the array by appending at most K elements such that the array becomes a permutation of consecutive numbers starting from 1. Once K elements are added, sum of two or more array elements can […]

Continue ReadingGiven a string find the number of unique permutations that can be obtained by swapping two indices such that the elements at these indices are distinct.NOTE: Swapping is always performed in the original string.Examples:Input: str = “sstt”Output: 5Explaination: Swap str[0] with str[2], string obtained “tsst” which is valid (str[0]!=str[2])Swap str[0] with str[3]. string obtained “tsts”Swap str[1] […]

Continue ReadingGenerate a permutation of first N natural numbers from an array of differences between adjacent elementsGiven an array arr[] consisting of (N – 1), the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] – P[i]). If there exists no such permutation, then […]

Continue ReadingCount ways to place ‘+’ and ‘-‘ in front of array elements to obtain sum KGiven an array A[] consisting of N non-negative integers, and an integer K, the task is to find the number of ways ‘+’ and ‘-‘ operators can be placed in front of elements of the array A[] such that the […]

Continue Readingdef Swap(x, y, p, posOfCurrNum): posOfCurrNum[p[x]], posOfCurrNum[p[y]] = posOfCurrNum[p[y]], posOfCurrNum[p[x]] p[x], p[y] = p[y], p[x] return p, posOfCurrNumdef sortArray(p, n): posOfCurrNum = [0] * (n + 1) for i in range(1, n + 1): posOfCurrNum[p[i]] = i ans = [] for i in range(1, n + 1): if (p[i] == i): continue j = posOfCurrNum[i] if (abs(i – j) * 2 >= n): p, posOfCurrNum = Swap(i, j, […]

Continue ReadingCount inversions in a permutation of first N natural numbersGiven an array, arr[] of size N denoting a permutation of numbers from 1 to N, the task is to count the number of inversions in the array. Note: Two array elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.Examples:Input: arr[] […]

Continue Reading