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
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
[tabby title="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 |
26971
Recommended Posts:
- Pseudo Random Number Generator (PRNG)
- Random number generator in arbitrary probability distribution fashion
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of times the largest perfect square number can be subtracted from N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Build Lowest Number by Removing n digits from a given number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- Minimum number of given powers of 2 required to represent a number
- Querying maximum number of divisors that a number in a given range has
- Print a number strictly less than a given number such that all its digits are distinct.
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.



