Nambiar Number Generator | C ++ implementation of the approach ; Function to return the Nambiar number of the given number ; If there is no digit to choose ; Choose the first digit ; Chosen digit 's parity ; To store the sum of the consecutive digits starting from the chosen digit ; While there are digits to choose ; Update the sum ; If the parity differs ; Return the current sum concatenated with the Numbiar number for the rest of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string numbiarNumber ( string str , int i ) { if ( i > str . length ( ) ) return " " ; int firstDigit = str [ i ] - '0' ; int digitParity = firstDigit % 2 ; int sumDigits = 0 ; while ( i < str . length ( ) ) { sumDigits += ( str [ i ] - '0' ) ; int sumParity = sumDigits % 2 ; if ( digitParity != sumParity ) break ; i ++ ; } return ( to_string ( sumDigits ) + numbiarNumber ( str , i + 1 ) ) ; } int main ( ) { string str = "9880127431" ; cout << numbiarNumber ( str , 0 ) << endl ; return 0 ; }