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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG {
static String nambiarNumber(String str, int i)
{
if (i >= str.length())
return "" ;
int firstDigit = (str.charAt(i) - '0' );
int digitParity = firstDigit % 2 ;
int sumDigits = 0 ;
while (i < str.length()) {
sumDigits += (str.charAt(i) - '0' );
int sumParity = sumDigits % 2 ;
if (digitParity != sumParity) {
break ;
}
i++;
}
return ( "" + sumDigits + nambiarNumber(str, i + 1 ));
}
public static void main(String[] args)
{
String str = "9880127431" ;
System.out.println(nambiarNumber(str, 0 ));
}
}
|
Python3
def nambiarNumber( Str , i):
if (i > = len ( Str )):
return ""
firstDigit = ord ( Str [i]) - ord ( '0' )
digitParity = firstDigit % 2
sumDigits = 0
while (i < len ( Str )):
sumDigits + = ( ord ( Str [i]) - ord ( '0' ))
sumParity = sumDigits % 2
if (digitParity ! = sumParity):
break
i + = 1
return ("" + str (sumDigits) +
nambiarNumber( Str , i + 1 ))
Str = "9880127431"
print (nambiarNumber( Str , 0 ))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static String nambiarNumber(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 ( "" + sumDigits + nambiarNumber(str, i + 1));
}
public static void Main(String[] args)
{
String str = "9880127431" ;
Console.WriteLine(nambiarNumber(str, 0));
}
}
|
Javascript
<script>
function nambiarNumber(str,i)
{
if (i >= str.length)
return "" ;
let firstDigit = (str[i].charCodeAt(0) - '0' .charCodeAt(0));
let digitParity = firstDigit % 2;
let sumDigits = 0;
while (i < str.length) {
sumDigits += (str[i].charCodeAt(0) - '0'.charCodeAt(0));
let sumParity = sumDigits % 2;
if (digitParity != sumParity) {
break ;
}
i++;
}
return ( "" + sumDigits + nambiarNumber(str, i + 1));
}
let str = "9880127431" ;
document.write(nambiarNumber(str, 0));
</script>
|
Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(n) where n is the length of the string
Last Updated :
15 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...