Count distinct points visited on the number line

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.

Examples:

Input: current_pos = 5, path = “011101”
Output: 4
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”
Output: 3
3 -> 4 -> 5 -> 4 -> 5 -> 4 -> 3

Approach:

  • 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Utility function to return the number
// of distinct elements in an array
int countDistinct(int arr[], int len)
{
  
    set<int> hs;
  
    for (int i = 0; i < len; i++) {
        // add all the elements to the HashSet
        hs.insert(arr[i]);
    }
  
    // Return the size of hashset as
    // it consists of all unique elements
    return hs.size();
}
  
// Function to return the count of
// positions the person went to
int getDistinctPoints(int current_pos, string path)
{
  
    // Length of path
    int len = path.length();
  
    // Array to store all the points traveled
    int points[len + 1];
  
    // The first point is the current_pos
    points[0] = current_pos;
  
    // For all the directions in path
    for (int i = 0; i < len; i++) {
  
        // Get whether the direction was left or right
        char ch = path[i];
  
        // If the direction is left
        if (ch == '0') {
  
            // Decrement the current position by 1
            current_pos--;
  
            // Store the current position in array
            points[i + 1] = current_pos;
        }
  
        // If the direction is right
        else {
  
            // Increment the current position by 1
            current_pos++;
  
            // Store the current position in array
            points[i + 1] = current_pos;
        }
    }
  
    return countDistinct(points, len + 1);
}
  
// Driver code
int main()
{
    int current_pos = 5;
    string path = "011101";
  
    cout << (getDistinctPoints(current_pos, path));
  
    return 0;
}
// contributed by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Function to return the count of
    // positions the person went to
    public static int getDistinctPoints(int current_pos, String path)
    {
  
        // Length of path
        int len = path.length();
  
        // Array to store all the points traveled
        int points[] = new int[len + 1];
  
        // The first point is the current_pos
        points[0] = current_pos;
  
        // For all the directions in path
        for (int i = 0; i < len; i++) {
  
            // Get whether the direction was left or right
            char ch = path.charAt(i);
  
            // If the direction is left
            if (ch == '0') {
  
                // Decrement the current position by 1
                current_pos--;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
  
            // If the direction is right
            else {
  
                // Increment the current position by 1
                current_pos++;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
        }
  
        return countDistinct(points, len + 1);
    }
  
    // Utility function to return the number
    // of distinct elements in an array
    public static int countDistinct(int arr[], int len)
    {
  
        HashSet<Integer> hs = new HashSet<Integer>();
  
        for (int i = 0; i < len; i++) {
            // add all the elements to the HashSet
            hs.add(arr[i]);
        }
  
        // Return the size of hashset as
        // it consists of all unique elements
        return hs.size();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int current_pos = 5;
        String path = "011101";
  
        System.out.print(getDistinctPoints(current_pos, path));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Utility function to return the number
# of distinct elements in an array
def countDistinct(arr, Len):
  
    hs = dict()
  
    for i in range(Len):
          
        # add all the elements to the HashSet
        hs[arr[i]] = 1
  
    # Return the size of hashset as
    # it consists of all unique elements
    return len(hs)
  
# Function to return the count of
# positions the person went to
def getDistinctPoints(current_pos, path):
  
    # Length of path
    Len = len(path)
  
    # Array to store all the points traveled
    points = [0 for i in range(Len + 1)]
  
    # The first pois the current_pos
    points[0] = current_pos
  
    # For all the directions in path
    for i in range(Len):
  
        # Get whether the direction 
        # was left or right
        ch = path[i]
  
        # If the direction is left
        if (ch == '0'):
  
            # Decrement the current position by 1
            current_pos -= 1
  
            # Store the current position in array
            points[i + 1] = current_pos
  
        # If the direction is right
        else:
  
            # Increment the current position by 1
            current_pos += 1
  
            # Store the current position in array
            points[i + 1] = current_pos
          
    return countDistinct(points, Len + 1)
  
# Driver code
current_pos = 5
path = "011101"
  
print(getDistinctPoints(current_pos, path))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Function to return the count of
    // positions the person went to
    public static int getDistinctPoints(int current_pos,
                                        string path)
    {
  
        // Length of path
        int len = path.Length;
  
        // Array to store all the points traveled
        int[] points = new int[len + 1];
  
        // The first point is the current_pos
        points[0] = current_pos;
  
        // For all the directions in path
        for (int i = 0; i < len; i++) {
  
            // Get whether the direction was left or right
            char ch = path[i];
  
            // If the direction is left
            if (ch == '0') {
  
                // Decrement the current position by 1
                current_pos--;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
  
            // If the direction is right
            else {
  
                // Increment the current position by 1
                current_pos++;
  
                // Store the current position in array
                points[i + 1] = current_pos;
            }
        }
  
        return countDistinct(points, len + 1);
    }
  
    // Utility function to return the number
    // of distinct elements in an array
    public static int countDistinct(int[] arr, int len)
    {
  
        HashSet<int> hs = new HashSet<int>();
  
        for (int i = 0; i < len; i++) {
            // add all the elements to the HashSet
            hs.Add(arr[i]);
        }
  
        // Return the size of hashset as
        // it consists of all unique elements
        return hs.Count;
    }
  
    // Driver code
    public static void Main(string[] args)
    {
        int current_pos = 5;
        string path = "011101";
  
        Console.Write(getDistinctPoints(current_pos, path));
    }
}
  
// This code is contributed by shrikanth13

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Utility function to return the number 
// of distinct elements in an array 
function countDistinct($arr, $len
{  
    $hs = array(); 
  
    for ($i = 0; $i < $len; $i++) 
    
        // add all the elements to the HashSet 
        array_push($hs, $arr[$i]); 
    
  
    $hs = array_unique($hs);
      
    // Return the size of hashset as 
    // it consists of all unique elements 
    return count($hs); 
  
// Function to return the count of 
// positions the person went to 
function getDistinctPoints($current_pos, $path
  
    // Length of path 
    $len = strlen($path); 
  
    // Array to store all the points traveled 
    $points = array(); 
  
    // The first point is the current_pos 
    $points[0] = $current_pos
  
    // For all the directions in path 
    for ($i = 0; $i < $len; $i++)
    
  
        // Get whether the direction was left or right 
        $ch = $path[$i]; 
  
        // If the direction is left 
        if ($ch == '0')
        
  
            // Decrement the current position by 1 
            $current_pos--; 
  
            // Store the current position in array 
            $points[$i + 1] = $current_pos
        
  
        // If the direction is right 
        else 
        
  
            // Increment the current position by 1 
            $current_pos++; 
  
            // Store the current position in array 
            $points[$i + 1] = $current_pos
        
    
  
    return countDistinct($points, $len + 1); 
  
// Driver code 
$current_pos = 5; 
$path = "011101"
  
echo getDistinctPoints($current_pos, $path); 
  
// This code is contributed by Ryuga 
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.