Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Image typed out: Let SS(i,j), ij denote the set of all stack-sortable permutatio

ID: 3603274 • Letter: I

Question

Image typed out:

Let SS(i,j), ij denote the set of all stack-sortable permutations of numbers between i and j (SS(i,j) is empty for i>j). Prove that SS(1,n) consists exactly of all strings obtained by concatenation of strings in SS(i, n-1) and SS(1,i-1), separated by n, for all i=1..n (Example: <4,5,3, 6, 1,2> in SS(1,6); it should be easy to see that 1,3,2 is not in SS(1,3).)

Let SSi ) i denote the set of all stack-sortable permutations of numbers between i and J (SS(ij) is empty for Dj). Prove that SS(1,n) consists exactly of all strings obtained by concatenation of strings in SS(i, n1) and SS(1,i-1), separated by n, for all -1.n (Example: in SS(6); t should be easy to see that 1,3,2 is not in SS(3).)

Explanation / Answer

#include <bits/stdc++.h>

using namespace std;

// Utility method to get dth digit of number N

char getDigit(int N, int d)

{

    string str;

    stringstream ss;

    ss << N;

    ss >> str;

    return str[d - 1];

}

// Method to return Nth character in concatenated

// decimal string

char getNthChar(int N)

{

    // sum will store character escaped till now

    int sum = 0, nine = 9;

    // dist will store numbers escaped till now

    int dist = 0, len;

    // loop for number lengths

    for (len = 1; ; len++)

    {

        // nine*len will be incremented characters

        // and nine will be incremented numbers

        sum += nine*len;

        dist += nine;

        if (sum >= N)

        {

            // restore variables to previous correct state

            sum -= nine*len;

            dist -= nine;

            N -= sum;

            break;

        }

        nine *= 10;

    }

    // get distance from last one digit less maximum

    // number

    int diff = ceil((double)N / len);

    // d will store dth digit of current number

    int d = N % len;

    if (d == 0)

        d = len;

    // method will return dth numbered digit

    // of (dist + diff) number

    return getDigit(dist + diff, d);

}

// Driver code to test above methods

int main()

{

    int N = 251;

    cout << getNthChar(N) << endl;

    return 0;

}