# Nambiar Number Generator

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

First digit Next valid consecutive digits Resultant number
9880127431 9880127431 26
9880127431 9880127431 269
9880127431 9880127431 2697
9880127431 9880127431 26971

Input: N = 9866364552
Output: 32157

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 `

Output:

```26971
```

My Personal Notes arrow_drop_up Im a student of Shiv Nadar University pursuing Electronics and Communication Engineering Skills C, Data Structures, Java, HTML, CSS, JavaScript

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.