Count number of substrings with numeric value greater than X | C ++ implementation of the approach ; Function that counts valid sub - strings ; Only take those numbers that do not start with '0' . ; converting the sub - string starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( string S , int X ) { int count = 0 ; const int N = S . length ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] != '0' ) { for ( int len = 1 ; ( i + len ) <= N ; ++ len ) { if ( stoi ( S . substr ( i , len ) ) > X ) count ++ ; } } } return count ; } int main ( ) { string S = "2222" ; int X = 97 ; cout << count ( S , X ) ; return 0 ; }