Minimum Jumps from Either End to Reach Largest and Smallest Character in given String


In this problem, we require to find the minimum jumps required to make to reach the largest and smallest character in the given string. We can move to the next or previous character in one jump.

We can solve the problem by finding the position of the lexicographically largest and smallest character in the given string. After that, we can find the minimum jumps required to the found indexes from the left and right sides.

Problem statement – We have a string str of length N containing the alphabetical characters in the uppercase. We need to find the minimum number of moves required to reach the lexicographically smallest and largest character from the left or right side of the string.

Sample examples

Input

alpha = "ASDFDJF";

Output

2

Explanation

  • The lexicographically smallest character is ‘A’, which is at index 0.

  • The lexicographically largest character is ‘S’, which is at index 1.

So, if we jump to index 1, we can also reach index 0. So, we require two jumps. One is from the left side to index 0, and another is from index 0 to index 1.

Input

alpha = "OPQRSTUV";

Output

2

Explanation

  • The lexicographically smallest character is ‘O’, which is at index 0.

  • The lexicographically largest character is ‘P’, which is at index 7.

We can reach index 0 with 1 move from the left side and index 7 with 1 move from the right side. So, we require total 2 moves.

Input

alpha = "CDABNFGH"

Output

5

Explanation – If we make the 5 jumps from the left side, we can convert the ‘A’ and ‘N’, which are lexicographically smallest and largest characters, respectively.

Approach 1

There are only three ways to reach both characters. The first way to reach both characters is from the left side. The second way to reach both characters is from the right side. The third way is to reach one character from the left and one from the right. We need to take the minimum value from above all three solutions.

Algorithm

Step 1 – Define the largeInd and smallInd variables to store the index of the lexicographically smallest and largest characters.

Step 2 – Use the loop to traverse the string. In the loop, if the character at the pth index is larger than the character at the ‘largeInd’ index, update ‘largeInd’ by p.

Step 3 – If the character at the pth index is smaller than the character at the ‘smallInd’ index, update the value of the ‘smallInd’ by p.

Step 4 – Declare the ‘minMoves’ and ‘maxMoves’ variables and store the minimum and maximum of largeInd and smallInd in both variables, respectively.

Step 5 – Declare the sol1, sol2, and sol3 variables to store all three solutions.

Step 6 – In the sol1, store the string length – minMoves to reach both indexes from the right.

Step 7 – In sol2, store the maxMoves + 1 to reach both indexes from the left side.

Step 8 – In the sol3 variable, store the minMoves + 1 + str_len – maxMoves to reach one index from the left and one from the right.

Step 9 – Take the minimum value from sol1, sol2, and sol3 and return the answer.

Example

#include <bits/stdc++.h>
using namespace std;

int totalMoves(string alpha, int str_len) {
    // To store positions of the largest and smallest character
    int largeInd = 0, smallInd = 0;
    // Find the index of the largest and smallest character
    for (int p = 0; p < str_len; p++) {
        if (alpha[p] > alpha[largeInd])
            largeInd = p;
        if (alpha[p] < alpha[smallInd])
            smallInd = p;
    }
    // Get the min and max moves
    int minMoves = min(largeInd, smallInd);
    int maxMoves = max(largeInd, smallInd);
    // Finding all possible solutions
    int sol1, sol2, sol3;
    // Jumping to both elements from the right side
    sol1 = str_len - minMoves;
    // Jumping to both elements from the left side
    sol2 = maxMoves + 1;
    // Jumping to min from the left and max from right
    sol3 = minMoves + 1 + str_len - maxMoves;
    // Take a minimum of all possible solutions
    int answer = min(sol1, min(sol2, sol3));
    return answer;
}
int main() {
    string alpha = "ASDFDJF";
    int str_len = alpha.length();
    cout << "The number of minimum moves required to reach the largest and smallest character is - " << totalMoves(alpha, str_len);
    return 0;
}

Output

The number of minimum moves required to reach the largest and smallest character is - 2

Time complexity – O(N) as we find an index of lexicographically largest and smallest character.

Space complexity – O(1), as we use constant space to store solutions.

Programmers can solve the problem in which we require to find the minimum jumps require to reach the second smallest and largest character. The logic to reach the index is the same but changes in finding the index of the second smallest and largest character.

Updated on: 25-Aug-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements