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

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Published on 03-Aug-2020 13:45:21
Advertisements