Saturday, 4 Dec 2021
Category: permutation

#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

#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 + […]

Given 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] […]

Generate 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 […]

def 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, […]