Open In App
Related Articles

Minimum jumps from either end to reach largest and smallest character in given String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string str, the task is to find the minimum number of moves required to reach lexicographically largest and smallest characters. In one move, a jump can be made from leftmost side or the rightmost side of given string.

Examples: 

Input: str = AEDCB, N = 5  
Output: 2
Explanation: take two steps from leftmost side to reach A and E

Input: str = BACDEFHG, N = 8
Output: 4
Explanation: take two steps from leftmost side to reach A and 2 steps from rightmost side to reach H(2+2=4)

Input: str = CDBA, N = 4
Output: 3
Explanation: take  three steps from rightmost side to reach A and  then D

 

Approach: This problem is implementation-based. Follow the steps below to solve the given problem. 

  • Find the index of maximum and minimum elements in the string
  • Calculate the minimum and maximum of these indexes to find the min_steps and max_steps  that can be taken
  • There are only three possible ways to reach both elements
    • traverse from the start and cover both elements i.e min_steps+1
    • traverse from the last  and cover both elements  i.e n-max_steps  
    • traverse from both start and end i.e min_steps+1+n-max_steps
  • The final answer is a minimum of all possible three ways of traversal.

Below is the implementation of the above approach:

C++

// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Minimum number of moves required  to
// reach and largest and smallest ASCII values
int min_moves(string s, int n)
{
    int maxpos = 0, minpos = 0;
 
    // Finding index of maximum
    // and minimum element in string
    for (int i = 0; i < n; i++) {
 
        if (s[i] > s[maxpos])
            maxpos = i;
 
        if (s[i] < s[minpos])
            minpos = i;
    }
 
    // Calculating minimum ans maximum steps
    // that can be taken
 
    int min_steps = min(maxpos, minpos);
    int max_steps = max(maxpos, minpos);
 
    // Only three possible ways
    // to reach both elements
    int ans1, ans2, ans3;
 
    ans1 = n - min_steps;
 
    ans2 = max_steps + 1;
 
    ans3 = min_steps + 1 + n - max_steps;
 
    int result;
 
    // Minimum steps in all three ways
    result = min(ans1, min(ans2, ans3));
 
    // Return the final result
    return result;
}
 
// Driver code
int main()
{
 
    string str = "BACDEFHG";
    int N = str.length();
 
    cout << min_moves(str, N);
 
    return 0;
}

                    

Java

// Java code for the above approach
import java.io.*;
class GFG
{
   
// Minimum number of moves required  to
// reach and largest and smallest ASCII values
static int min_moves(String s, int n)
{
    int maxpos = 0, minpos = 0;
 
    // Finding index of maximum
    // and minimum element in string
    for (int i = 0; i < n; i++) {
 
        if (s.charAt(i) > s.charAt(maxpos))
            maxpos = i;
 
        if (s.charAt(i)  < s.charAt(minpos))
            minpos = i;
    }
 
    // Calculating minimum ans maximum steps
    // that can be taken
 
    int min_steps = Math.min(maxpos, minpos);
    int max_steps = Math.max(maxpos, minpos);
 
    // Only three possible ways
    // to reach both elements
    int ans1, ans2, ans3;
 
    ans1 = n - min_steps;
 
    ans2 = max_steps + 1;
 
    ans3 = min_steps + 1 + n - max_steps;
 
    int result;
 
    // Minimum steps in all three ways
    result = Math.min(ans1, Math.min(ans2, ans3));
 
    // Return the final result
    return result;
}
 
// Driver code
    public static void main (String[] args) {
       String str = "BACDEFHG";
    int N = str.length();
 
    
        System.out.println(min_moves(str, N));
    }
}
 
// This code is contributed by Potta Lokesh

                    

Python3

# Python code for the above approach
 
# Minimum number of moves required to
# reach and largest and smallest ASCII values
def min_moves(s, n):
    maxpos = 0;
    minpos = 0;
 
    # Finding index of maximum
    # and minimum element in string
    for i in range(n):
 
        if (s[i] > s[maxpos]):
            maxpos = i;
 
        if (s[i] < s[minpos]):
            minpos = i;
 
    # Calculating minimum ans maximum steps
    # that can be taken
    min_steps = min(maxpos, minpos);
    max_steps = max(maxpos, minpos);
 
    # Only three possible ways
    # to reach both elements
    ans1, ans2, ans3 = 0,0,0;
    ans1 = n - min_steps;
    ans2 = max_steps + 1;
    ans3 = min_steps + 1 + n - max_steps;
 
    result=0;
 
    # Minimum steps in all three ways
    result = min(ans1, min(ans2, ans3));
 
    # Return the final result
    return result;
 
# Driver code
if __name__ == '__main__':
    str = "BACDEFHG";
    N = len(str);
 
    print(min_moves(str, N));
 
# This code is contributed by shikhasingrajput

                    

C#

// C# program for above approach
using System;
 
class GFG{
 
// Minimum number of moves required  to
// reach and largest and smallest ASCII values
static int min_moves(string s, int n)
{
    int maxpos = 0, minpos = 0;
 
    // Finding index of maximum
    // and minimum element in string
    for(int i = 0; i < n; i++)
    {
        if (s[i] > s[maxpos])
            maxpos = i;
 
        if (s[i] < s[minpos])
            minpos = i;
    }
 
    // Calculating minimum ans maximum steps
    // that can be taken
    int min_steps = Math.Min(maxpos, minpos);
    int max_steps = Math.Max(maxpos, minpos);
 
    // Only three possible ways
    // to reach both elements
    int ans1, ans2, ans3;
 
    ans1 = n - min_steps;
    ans2 = max_steps + 1;
    ans3 = min_steps + 1 + n - max_steps;
 
    int result;
 
    // Minimum steps in all three ways
    result = Math.Min(ans1, Math.Min(ans2, ans3));
 
    // Return the final result
    return result;
}
 
// Driver code
public static void Main(string[] args)
{
    String str = "BACDEFHG";
    int N = str.Length;
 
    Console.WriteLine(min_moves(str, N));
}
}
 
// This code is contributed by ukasp

                    

Javascript

<script>
 
// JavaScript program for above approach
 
// Minimum number of moves required to
// reach and largest and smallest ASCII values
const min_moves = (s, n) => {
     
    let maxpos = 0, minpos = 0;
 
    // Finding index of maximum
    // and minimum element in string
    for(let i = 0; i < n; i++)
    {
        if (s[i] > s[maxpos])
            maxpos = i;
 
        if (s[i] < s[minpos])
            minpos = i;
    }
 
    // Calculating minimum ans maximum steps
    // that can be taken
    let min_steps = Math.min(maxpos, minpos);
    let max_steps = Math.max(maxpos, minpos);
 
    // Only three possible ways
    // to reach both elements
    let ans1, ans2, ans3;
 
    ans1 = n - min_steps;
    ans2 = max_steps + 1;
    ans3 = min_steps + 1 + n - max_steps;
 
    let result;
 
    // Minimum steps in all three ways
    result = Math.min(ans1, Math.min(ans2, ans3));
 
    // Return the final result
    return result;
}
 
// Driver code
let str = "BACDEFHG";
let N = str.length;
 
document.write(min_moves(str, N));
 
// This code is contributed by rakeshsahni
 
</script>

                    

Output
4

Time Complexity: O(N)   
Auxiliary Space: O(1)


 



Last Updated : 13 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads