
- 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
Check if a cell can be visited more than once in a String in C++
Suppose we have a string with dots (.) and a number, a dot indicates the cell is empty, and if there is a number x in any cell, it indicates that we can move x steps to the right or left within the string. Our task is to check whether we can visit a cell more than once or not. For example, if a string is like “. 2 . . . 2 . .”, then we can visit 4th cell in two different ways. From second cell to two step to right, or from two step left from cell 6.
We will use one array called visited[] to keep track of the number of times ith cell of the string can be visited. Now traverse the string, and check if the current character is dot, or a number. For dot, do nothing, for x, increase the number, and increase the count of visits in the visited array within the range [i – x, i + x] by 1. By traversing visited array, if we get some room is visited more than one time or not.
Example
#include <iostream> #include <queue> using namespace std; class Node { public: int key; Node *left, *right; }; Node* getNode(int key) { Node* newNode = new Node; newNode->key = key; newNode->left = newNode->right = NULL; return newNode; } bool isLevelWiseSorted(Node* root) { int prevMax = INT_MIN; int min_val, max_val; int levelSize; queue<Node*> q; q.push(root); while (!q.empty()) { levelSize = q.size(); min_val = INT_MAX; max_val = INT_MIN; while (levelSize > 0) { root = q.front(); q.pop(); levelSize--; min_val = min(min_val, root->key); max_val = max(max_val, root->key); if (root->left) q.push(root->left); if (root->right) q.push(root->right); } if (min_val <= prevMax) return false; prevMax = max_val; } return true; } int main() { Node* root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); if (isLevelWiseSorted(root)) cout << "Tree is levelwise Sorted"; else cout << "Tree is Not levelwise sorted"; }
Output
Tree is level wise Sorted
- Related Articles
- Check if a string can be repeated to make another string in Python
- addEventListener() not working more than once with a button in JavaScript?
- Insert more than one element at once in a C# List
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- How to check if a string can be converted to float in Python?
- Check if a string can be rearranged to form special palindrome in Python
- Check If a String Can Break Another String in C++
- Check if a Queen can attack a given cell on chessboard in Python
- MySQL query to find a value appearing more than once?
- Check if a two-character string can be made using given words in Python
- Array elements that appear more than once in C?
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Array elements that appear more than once?
