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 the 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 the current character is ‘0’, then the person traveled left. So decrement the current position by 1 and store it in points[].
- If the 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 the number of distinct elements in an array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDistinct( int arr[], int len)
{
set< int > hs;
for ( int i = 0; i < len; i++) {
hs.insert(arr[i]);
}
return hs.size();
}
int getDistinctPoints( int current_pos, string path)
{
int len = path.length();
int points[len + 1];
points[0] = current_pos;
for ( int i = 0; i < len; i++) {
char ch = path[i];
if (ch == '0' ) {
current_pos--;
points[i + 1] = current_pos;
}
else {
current_pos++;
points[i + 1] = current_pos;
}
}
return countDistinct(points, len + 1);
}
int main()
{
int current_pos = 5;
string path = "011101" ;
cout << (getDistinctPoints(current_pos, path));
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int getDistinctPoints( int current_pos, String path)
{
int len = path.length();
int points[] = new int [len + 1 ];
points[ 0 ] = current_pos;
for ( int i = 0 ; i < len; i++) {
char ch = path.charAt(i);
if (ch == '0' ) {
current_pos--;
points[i + 1 ] = current_pos;
}
else {
current_pos++;
points[i + 1 ] = current_pos;
}
}
return countDistinct(points, len + 1 );
}
public static int countDistinct( int arr[], int len)
{
HashSet<Integer> hs = new HashSet<Integer>();
for ( int i = 0 ; i < len; i++) {
hs.add(arr[i]);
}
return hs.size();
}
public static void main(String[] args)
{
int current_pos = 5 ;
String path = "011101" ;
System.out.print(getDistinctPoints(current_pos, path));
}
}
|
Python3
def countDistinct(arr, Len ):
hs = dict ()
for i in range ( Len ):
hs[arr[i]] = 1
return len (hs)
def getDistinctPoints(current_pos, path):
Len = len (path)
points = [ 0 for i in range ( Len + 1 )]
points[ 0 ] = current_pos
for i in range ( Len ):
ch = path[i]
if (ch = = '0' ):
current_pos - = 1
points[i + 1 ] = current_pos
else :
current_pos + = 1
points[i + 1 ] = current_pos
return countDistinct(points, Len + 1 )
current_pos = 5
path = "011101"
print (getDistinctPoints(current_pos, path))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int getDistinctPoints( int current_pos,
string path)
{
int len = path.Length;
int [] points = new int [len + 1];
points[0] = current_pos;
for ( int i = 0; i < len; i++) {
char ch = path[i];
if (ch == '0' ) {
current_pos--;
points[i + 1] = current_pos;
}
else {
current_pos++;
points[i + 1] = current_pos;
}
}
return countDistinct(points, len + 1);
}
public static int countDistinct( int [] arr, int len)
{
HashSet< int > hs = new HashSet< int >();
for ( int i = 0; i < len; i++) {
hs.Add(arr[i]);
}
return hs.Count;
}
public static void Main( string [] args)
{
int current_pos = 5;
string path = "011101" ;
Console.Write(getDistinctPoints(current_pos, path));
}
}
|
PHP
<?php
function countDistinct( $arr , $len )
{
$hs = array ();
for ( $i = 0; $i < $len ; $i ++)
{
array_push ( $hs , $arr [ $i ]);
}
$hs = array_unique ( $hs );
return count ( $hs );
}
function getDistinctPoints( $current_pos , $path )
{
$len = strlen ( $path );
$points = array ();
$points [0] = $current_pos ;
for ( $i = 0; $i < $len ; $i ++)
{
$ch = $path [ $i ];
if ( $ch == '0' )
{
$current_pos --;
$points [ $i + 1] = $current_pos ;
}
else
{
$current_pos ++;
$points [ $i + 1] = $current_pos ;
}
}
return countDistinct( $points , $len + 1);
}
$current_pos = 5;
$path = "011101" ;
echo getDistinctPoints( $current_pos , $path );
?>
|
Javascript
<script>
function getDistinctPoints(current_pos, path)
{
let len = path.length;
let points =
Array.from({length: len + 1}, (_, i) => 0);
points[0] = current_pos;
for (let i = 0; i < len; i++) {
let ch = path[i];
if (ch == '0' ) {
current_pos--;
points[i + 1] = current_pos;
}
else {
current_pos++;
points[i + 1] = current_pos;
}
}
return countDistinct(points, len + 1);
}
function countDistinct(arr, len)
{
let hs = new Set();
for (let i = 0; i < len; i++) {
hs.add(arr[i]);
}
return hs.size;
}
let current_pos = 5;
let path = "011101" ;
document.write(getDistinctPoints(current_pos, path));
</script>
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(n), where n is the size of the given array.
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...