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:
- Count maximum points on same line
- Count the total number of squares that can be visited by Bishop in one move
- Number of horizontal or vertical line segments to connect 3 points
- Number of ordered points pair satisfying line equation
- Number of Distinct Meeting Points on a Circular Road
- Count number of Distinct Substring in a String
- Count number of distinct substrings of a given length
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Represent a given set of points by the best possible straight line
- Count number of distinct pairs whose sum exists in the given array
- Find points at a given distance on a line of given slope
- Find X and Y intercepts of a line passing through the given points
- Program to count number of distinct Squares and Cubes upto N
- Maximum number of leaf nodes that can be visited within the given budget
- Prime points (Points that split a number into two primes)
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.