Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | C ++ program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CountWays ( int a , int b , int c ) { int x = b + c + 2 ; int arr [ x ] = { 0 } ; for ( int i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( int i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; cout << arr [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; return 0 ; }