
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Program to count number of points that lie on a line in Python
- Count number of Distinct Substring in a String in C++
- Max Points on a Line in C++
- Count the number of distinct values in MySQL?
- Count the total number of squares that can be visited by Bishop in one move in C++
- Write the decimal number represented by the points \( A, B, C \), D on the given number line."
- Count number of substrings with exactly k distinct characters in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Number of ordered points pair satisfying line equation in C++
- Count distinct elements in an array in C++
- Queries on count of points lie inside a circle in C++
- Count subsets having distinct even numbers in C++
- Program to count number of distinct substrings in s in Python
- Absolute distinct count in a sorted array in C++?
- Number of Distinct Islands in C++
