- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count distinct points visited on the number line in C++

We are given a binary sequence of 0’s and 1’s. Also assume a person is sitting at a position or point stored in current_pos. Now starting from current_pos, if the binary sequence has 0 then he moves one step left ( current_pos - 1). If it’s 1 he moves one step right ( current_pos + 1). The goal is to find distinct positions or points he visited after the whole binary sequence is completed.

We will solve this using the number of times a point is visited. If frequency is non-zero, increase count of distinct points.

## Input

Path[]= “001100” current_pos=3

## Output

Distinct points visited on number line: 3

**Explanation** − starting from path[0] and position 3.

Path[0]: 0 → move left ...currpos=2 Path[1]: 0 → move left ...currpos=1 Path[2]: 1 → move right ...currpos=2 Path[3]: 1 → move left ...currpos=3 Path[4]: 0 → move left ...currpos=2 Path[5]: 0 → move left ...currpos=1

Total distinct positions are 3, which are 1,2,3

## Input

Path[]= “010101” current_pos=5

## Output

Distinct points visited on number line: 2

**Explanation** − starting from path[0] and position 5.

Path[0]: 0 → move left ...currpos=4 Path[1]: 1 → move right ...currpos=5 Path[2]: 0 → move left ...currpos=4 Path[3]: 1 → move right ...currpos=5 Path[4]: 0 → move left ...currpos=4 Path[5]: 1 → move right ...currpos=5

Total distinct positions are 2, which are 4 and 5

## Approach used in the below program is as follows

String of 0’s and 1’s is stored in the path.

Current_pos stores starting point.

Function getDistinctPoints(int current_pos, string path) takes current position and path as input and returns the count of distinct positions/points.

Variable len stores the length of path.

Array frequency[21] is used to store the frequency for the number of times the point is visited. Index represents the point. Total 0-20.

Start traversing the path string.

If current value is 0, move left ( current_pos -1 ). Update frequency of this point frequency[current_pos]++.

Else current value is 1, move right ( current_pos +1 ). Update frequency of this point frequency[current_pos]++.

Now traverse the frequency array and for each non zero value. Increase count.

Count contains distinct visited points.

- Return count as desired result.

## Example

#include <bits/stdc++.h> using namespace std; //distict points visited between 0 to 20 int getDistinctPoints(int current_pos, string path){ // Length of path int len = path.length(); int count=0; // Array to store number of times a point is visited int frequency[21]={0}; // For all the directions in path for (int i = 0; i < len; i++) { //for left direction if (path[i] == '0') { current_pos--; frequency[current_pos]++; //increase visit count } // If the direction is right else { current_pos++; frequency[current_pos]++; //increase visit count } } for(int i=0;i<21;i++) if(frequency[i]!=0) // if visted then frequency[i] is non zero count++; return count; } int main(){ int current_pos = 3; string path = "011101100"; cout << "Count of distinct points visited on the number line:<<(getDistinctPoints(current_pos, path)); return 0; }

## Output

Count of distinct points visited on the number line :5

- Related Questions & Answers
- Program to count number of points that lie on a line in Python
- Max Points on a Line in C++
- Count the number of distinct values in MySQL?
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Number of ordered points pair satisfying line equation in C++
- Count the total number of squares that can be visited by Bishop in one move in C++
- Count number of Distinct Substring in a String in C++
- Count distinct value in MongoDB?
- Program to count number of distinct substrings in s in Python
- Queries on count of points lie inside a circle in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Count number of substrings with exactly k distinct characters in C++
- How to count the distinct column in MySQL?
- Count the number of threads in a process on linux
- MySQL SELECT DISTINCT and count?