Given a person who is at position current_pos and a binary string path which is the moves the person took, if path[i] = ‘0’ then the person moved one step left and if path[i] = ‘1’ then person moved one step to the right. The task is to find the count of distinct positions the person visited.
Input: current_pos = 5, path = “011101”
Given moves are left, right, right, right, left and right
i.e. 5 -> 4 -> 5 -> 6 -> 7 -> 6 -> 7
The number of distinct positions are 4 (4, 5, 6 and 7).
Input: current_pos = 3, path = “110100”
3 -> 4 -> 5 -> 4 -> 5 -> 4 -> 3
- Declare an array points to store all the points the person goes through.
- Initialize the first position of this array to the current position current_pos.
- Traverse the string path and do the following:
- If current character is ‘0’, then the person traveled left. So decrement the current position by 1 and store it in points.
- If current character is ‘1’, then the person traveled right. So increment the current position by 1 and store it in points.
- Count the total number of distinct elements in points. Refer Count distinct elements in an array for different methods of counting number of distinct elements in an array.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Count the total number of squares that can be visited by Bishop in one move
- Maximum number of leaf nodes that can be visited within the given budget
- Count maximum points on same line
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Number of horizontal or vertical line segments to connect 3 points
- Number of ordered points pair satisfying line equation
- Steps required to visit M points in order on a circular ring of N points
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Number of Distinct Meeting Points on a Circular Road
- Represent a given set of points by the best possible straight line
- Find points at a given distance on a line of given slope
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Find X and Y intercepts of a line passing through the given points
- Check whether a straight line can be formed using N co-ordinate points
- Slope of the line parallel to the line with the given slope
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum Squares possible parallel to both axes from N distinct points
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count Integral points inside a Triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.