Nambiar Number Generator - GeeksforGeeks

08 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the Nambiar// number of the given numberstring 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 codeint main(){    string str = "9880127431";    cout << numbiarNumber(str, 0) << endl;    return 0;} // This code is contributed by// sanjeev2552
// Java implementation of the approachclass 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));    }}
# Java implementation of the approach # Function to return the Nambiar# number of the given numberdef 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 codeStr = "9880127431"print(nambiarNumber(Str, 0)) # This code is contributed by Mohit Kumar
// 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
<script>// Javascript implementation of the approach // Function to return the Nambiar    // number of the given numberfunction nambiarNumber(str,i){    // If there is no digit to choose        if (i >= str.length)            return "";           // Choose the first digit        let firstDigit = (str[i].charCodeAt(0) - '0'.charCodeAt(0));           // Chosen digit's parity        let digitParity = firstDigit % 2;           // To store the sum of the consecutive        // digits starting from the chosen digit        let sumDigits = 0;           // While there are digits to choose        while (i < str.length) {               // Update the sum            sumDigits += (str[i].charCodeAt(0) - '0'.charCodeAt(0));            let 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 codelet str = "9880127431";document.write(nambiarNumber(str, 0));     // This code is contributed by unknown2108</script>
26971
 
mohit kumar 29
Rajput-Ji
sanjeev2552
unknown2108
number-theory
Numbers
Mathematical
number-theory
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Find first and last digits of a number
Count ways to reach the n'th stair
Program to convert a given number to words
Find Union and Intersection of two unsorted arrays