- 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 number of substrings with numeric value greater than X in C++

We are given a string of numbers 0 to 9. The string represents a decimal number. The goal is to find all substrings that represent a decimal number which is greater than number X. Condition is that substring should not start with 0. I.e in “2021”, “02”, “021”. “0” will not be included.

We will do this by checking the first value of all substrings, if it is greater than 0 then start making substrings from that index by converting them into integers using stoi(). If substring>X increment count.

Let us understand with examples.

**Input** − str=”123” X=12

**Output** − Count of number of substrings with numeric value greater than X are − 2

**Explanation** Substrings > 12 are 123 and 23.

**Input** − str=”111” X=100

**Output** − Count of even decimal value substrings in a binary string are − 1

**Explanation** Only 111 is greater than 100.

## Approach used in the below program is as follows

We take a string str as a string of numbers only.

Store the length of str in len=str.length()

Function greater_X(string str, int x) takes the string and its length and returns a count of substrings that form a decimal number greater than X.

Traverse string using FOR loop

Starting from index i=0 to i<len, reading from left to right.

If any str[i]!=’0’ means all substrings starting from it will be valid.

Starting from index j=1 to i+j<len, for length of substring.

Convert substring str.substr(i,j) to decimal using stoi(). If it is more than X. Increment count.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int greater_X(string str, int x){ int count = 0; int len = str.length(); for (int i = 0; i < len; ++i){ if(str[i] != '0'){ for (int j=1; (i + j) <= len; ++j){ if (stoi(str.substr(i, j)) > x){ count++; } } } } return count; } int main(){ string str = "987"; int x = 100; cout<<"Count of number of substrings with numeric value greater than X are: "<<greater_X(str, x); return 0; }

## Output

If we run the above code it will generate the following output −

Count of number of substrings with numeric value greater than X are: 1

- Related Questions & Answers
- Count smaller values whose XOR with x is greater than x in C++
- C++ code to count number of even substrings of numeric string
- Count of alphabets having ASCII value less than and greater than k in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Count subarrays with all elements greater than K in C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Find the Number of segments where all elements are greater than X using C++
- Count number of substrings with exactly k distinct characters in C++
- Count the number of words having sum of ASCII values less than and greater than k in C++
- How to find the smallest number greater than x in Python?
- Number of nodes greater than a given value in n-ary tree in C++
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Count substrings that starts with character X and ends with character Y in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Count the number of pairs that have column sum greater than row sum in C++