Count of matchsticks required to represent the given number | C ++ implementation of the approach ; stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sticks [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int countSticks ( string str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str [ i ] - '0' ] ) ; } return cnt ; } int main ( ) { string str = "56" ; int n = str . length ( ) ; cout << countSticks ( str , n ) ; return 0 ; }