Length of Smallest subarray in range 1 to N with sum greater than a given value | C ++ implementation of the above implementation ; Function to return the count of minimum elements such that the sum of those elements is > S . ; Initialize currentSum = 0 ; Loop from N to 1 to add the numbers and check the condition . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumber ( int N , int S ) { int countElements = 0 ; int currSum = 0 ; while ( currSum <= S ) { currSum += N ; N -- ; countElements ++ ; } return countElements ; } int main ( ) { int N , S ; N = 5 ; S = 11 ; int count = countNumber ( N , S ) ; cout << count << endl ; return 0 ; }