Minimum jumps from either end to reach largest and smallest character in given String
Last Updated :
13 Dec, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int min_moves(string s, int n)
{
int maxpos = 0, minpos = 0;
for ( int i = 0; i < n; i++) {
if (s[i] > s[maxpos])
maxpos = i;
if (s[i] < s[minpos])
minpos = i;
}
int min_steps = min(maxpos, minpos);
int max_steps = max(maxpos, minpos);
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
int result;
result = min(ans1, min(ans2, ans3));
return result;
}
int main()
{
string str = "BACDEFHG" ;
int N = str.length();
cout << min_moves(str, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int min_moves(String s, int n)
{
int maxpos = 0 , minpos = 0 ;
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;
}
int min_steps = Math.min(maxpos, minpos);
int max_steps = Math.max(maxpos, minpos);
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1 ;
ans3 = min_steps + 1 + n - max_steps;
int result;
result = Math.min(ans1, Math.min(ans2, ans3));
return result;
}
public static void main (String[] args) {
String str = "BACDEFHG" ;
int N = str.length();
System.out.println(min_moves(str, N));
}
}
|
Python3
def min_moves(s, n):
maxpos = 0 ;
minpos = 0 ;
for i in range (n):
if (s[i] > s[maxpos]):
maxpos = i;
if (s[i] < s[minpos]):
minpos = i;
min_steps = min (maxpos, minpos);
max_steps = max (maxpos, minpos);
ans1, ans2, ans3 = 0 , 0 , 0 ;
ans1 = n - min_steps;
ans2 = max_steps + 1 ;
ans3 = min_steps + 1 + n - max_steps;
result = 0 ;
result = min (ans1, min (ans2, ans3));
return result;
if __name__ = = '__main__' :
str = "BACDEFHG" ;
N = len ( str );
print (min_moves( str , N));
|
C#
using System;
class GFG{
static int min_moves( string s, int n)
{
int maxpos = 0, minpos = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] > s[maxpos])
maxpos = i;
if (s[i] < s[minpos])
minpos = i;
}
int min_steps = Math.Min(maxpos, minpos);
int max_steps = Math.Max(maxpos, minpos);
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
int result;
result = Math.Min(ans1, Math.Min(ans2, ans3));
return result;
}
public static void Main( string [] args)
{
String str = "BACDEFHG" ;
int N = str.Length;
Console.WriteLine(min_moves(str, N));
}
}
|
Javascript
<script>
const min_moves = (s, n) => {
let maxpos = 0, minpos = 0;
for (let i = 0; i < n; i++)
{
if (s[i] > s[maxpos])
maxpos = i;
if (s[i] < s[minpos])
minpos = i;
}
let min_steps = Math.min(maxpos, minpos);
let max_steps = Math.max(maxpos, minpos);
let ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
let result;
result = Math.min(ans1, Math.min(ans2, ans3));
return result;
}
let str = "BACDEFHG" ;
let N = str.length;
document.write(min_moves(str, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...