# Nambiar Number Generator

• Difficulty Level : Easy
• Last Updated : 28 May, 2022

M. Nambiar has devised a mechanism to process any given number and thus generating a new resultant number. He calls this mechanism as the “Nambiar Number Generator” and the resultant number is referred to as the “Nambiar Number”.
Mechanism: In the given number, starting with the first digit, keep on adding all subsequent digits till the state (even or odd) of the sum of the digits is opposite to the state (odd or even) of the first digit. Continue this form the subsequent digit till the last digit of the number is reached. Concatenating the sums thus generates the Nambiar Number.
Examples:

Input: N = 9880127431
Output: 26971

Input: N = 9866364552
Output: 32157

Approach: For the first unused digit from the left check whether it is even or odd. If the digit is even then find the sum of consecutive digits starting at the current digit which is odd (even sum if the first digit was odd). Concatenate this sum to the resultant number and repeat the whole process starting from the first unused digit from the left.
Below is the implementation of the above approach:

## C++

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

## Java

 `// Java implementation of the approach``class` `GFG {` `    ``// Function to return the Nambiar``    ``// number of the given number``    ``static` `String nambiarNumber(String str, ``int` `i)``    ``{` `        ``// If there is no digit to choose``        ``if` `(i >= str.length())``            ``return` `""``;` `        ``// Choose the first digit``        ``int` `firstDigit = (str.charAt(i) - ``'0'``);` `        ``// Chosen digit's parity``        ``int` `digitParity = firstDigit % ``2``;` `        ``// To store the sum of the consecutive``        ``// digits starting from the chosen digit``        ``int` `sumDigits = ``0``;` `        ``// While there are digits to choose``        ``while` `(i < str.length()) {` `            ``// Update the sum``            ``sumDigits += (str.charAt(i) - ``'0'``);``            ``int` `sumParity = sumDigits % ``2``;` `            ``// If the parity differs``            ``if` `(digitParity != sumParity) {``                ``break``;``            ``}``            ``i++;``        ``}` `        ``// Return the current sum concatenated with the``        ``// Numbiar number for the rest of the string``        ``return` `(``""` `+ sumDigits + nambiarNumber(str, i + ``1``));``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String str = ``"9880127431"``;``        ``System.out.println(nambiarNumber(str, ``0``));``    ``}``}`

## Python3

 `# Java implementation of the approach` `# Function to return the Nambiar``# number of the given number``def` `nambiarNumber(``Str``,i):` `    ``# If there is no digit to choose``    ``if` `(i >``=` `len``(``Str``)):``        ``return` `""` `    ``# Choose the first digit``    ``firstDigit ``=``ord``(``Str``[i])``-``ord``(``'0'``)` `    ``# Chosen digit's parity``    ``digitParity ``=` `firstDigit ``%` `2` `    ``# To store the sum of the consecutive``    ``# digits starting from the chosen digit``    ``sumDigits ``=` `0` `    ``# While there are digits to choose``    ``while` `(i < ``len``(``Str``)):` `        ``# Update the sum``        ``sumDigits ``+``=` `(``ord``(``Str``[i]) ``-` `ord``(``'0'``))``        ``sumParity ``=` `sumDigits ``%` `2` `        ``# If the parity differs``        ``if` `(digitParity !``=` `sumParity):``            ``break``        ``i ``+``=` `1` `    ``# Return the current sum concatenated with the``    ``# Numbiar number for the rest of the String``    ``return` `("" ``+` `str``(sumDigits) ``+``                 ``nambiarNumber(``Str``, i ``+` `1``))` `# Driver code``Str` `=` `"9880127431"``print``(nambiarNumber(``Str``, ``0``))` `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the approach.``using` `System;``using` `System.Collections.Generic;``    ` `class` `GFG``{` `    ``// Function to return the Nambiar``    ``// number of the given number``    ``static` `String nambiarNumber(String str, ``int` `i)``    ``{` `        ``// If there is no digit to choose``        ``if` `(i >= str.Length)``            ``return` `""``;` `        ``// Choose the first digit``        ``int` `firstDigit = (str[i] - ``'0'``);` `        ``// Chosen digit's parity``        ``int` `digitParity = firstDigit % 2;` `        ``// To store the sum of the consecutive``        ``// digits starting from the chosen digit``        ``int` `sumDigits = 0;` `        ``// While there are digits to choose``        ``while` `(i < str.Length)``        ``{` `            ``// Update the sum``            ``sumDigits += (str[i] - ``'0'``);``            ``int` `sumParity = sumDigits % 2;` `            ``// If the parity differs``            ``if` `(digitParity != sumParity)``            ``{``                ``break``;``            ``}``            ``i++;``        ``}` `        ``// Return the current sum concatenated with the``        ``// Numbiar number for the rest of the string``        ``return` `(``""` `+ sumDigits + nambiarNumber(str, i + 1));``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``String str = ``"9880127431"``;``        ``Console.WriteLine(nambiarNumber(str, 0));``    ``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output:

`26971`

Time Complexity: O(n) where n is the length of the string

Auxiliary Space: O(n) where n is the length of the string

My Personal Notes arrow_drop_up