Given an integer N, the task is to reduce the number and form a key by following the operations given below:
- Extract the Most Significant Digit of the number:
- If the digit is even: Add the consecutive digits until the sum of digits is odd.
- If the digit is odd: Add the consecutive digits until the sum of digits is even.
- Repeat the process for all the remaining digits.
- Finally, concatenate the sum computed together to get the key.
Examples:
Input: N = 1667848270
Output: 20290
Explanation:
Step 1: First Digit(= 1) is odd. So, add up the next digits until the sum is even.
Therefore, digits 1, 6, 6, and 7 are added up to form 20.
Step 2: Next digit(= 8) is even. So, add up the next digits until the sum is odd.
Therefore, digits 8, 4, 8, 2, and 7 are added up to form 29.
Step 3: Last digit(= 0) is even.
Therefore, the final answer after concatenating the results will be: 20290
Input: N = 7246262412
Output: 342
Explanation:
Step 1: First Digit(= 7) is odd. So, add up the next digits until the sum is even.
Therefore, digits 7, 2, 4, 6, 2, 6, 2, 4, and 1 are added up to form 34.
Step 2: Last digit(= 2) is even.
Therefore, the final answer after concatenating the results will be: 342.
Approach: The idea is to iterate the digits of the number and check the parity of the digit. If it is even, then proceed to the next digits until an odd digit is encountered. For odd digit, add consecutive digits until the sum of digits is even. Finally, concatenate the sum computed to get the desired key.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int key( int N)
{
string num = "" + to_string(N);
int ans = 0;
int j = 0;
for (j = 0; j < num.length(); j++)
{
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
for (i = j; j < num.length(); j++)
{
add += num[j] - 48;
if (add % 2 == 1)
break ;
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = ( int ) floor ( log10 (add) + 1);
ans *= ( pow (10, digit));
ans += add;
}
i = j;
}
else
{
int add = 0;
int i;
for (i = j; j < num.length(); j++)
{
add += num[j] - 48;
if (add % 2 == 0)
{
break ;
}
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = ( int ) floor (
log10 (add) + 1);
ans *= ( pow (10, digit));
ans += add;
}
i = j;
}
}
if (j + 1 >= num.length())
{
return ans;
}
else
{
return ans += num[num.length() - 1] - 48;
}
}
int main()
{
int N = 1667848271;
cout << key(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
static int key( int N)
{
String num = "" + N;
int ans = 0 ;
int j = 0 ;
for (j = 0 ; j < num.length(); j++) {
if ((num.charAt(j) - 48 ) % 2 == 0 ) {
int add = 0 ;
int i;
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48 ;
if (add % 2 == 1 )
break ;
}
if (add == 0 ) {
ans *= 10 ;
}
else {
int digit = ( int )Math.floor(
Math.log10(add) + 1 );
ans *= (Math.pow( 10 , digit));
ans += add;
}
i = j;
}
else {
int add = 0 ;
int i;
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48 ;
if (add % 2 == 0 ) {
break ;
}
}
if (add == 0 ) {
ans *= 10 ;
}
else {
int digit = ( int )Math.floor(
Math.log10(add) + 1 );
ans *= (Math.pow( 10 , digit));
ans += add;
}
i = j;
}
}
if (j + 1 >= num.length()) {
return ans;
}
else {
return ans += num.charAt(
num.length() - 1 )
- 48 ;
}
}
public static void main(String[] args)
{
int N = 1667848271 ;
System.out.print(key(N));
}
}
|
Python3
import math
def key(N) :
num = "" + str (N)
ans = 0
j = 0
while j < len (num) :
if (( ord (num[j]) - 48 ) % 2 = = 0 ) :
add = 0
i = j
while j < len (num) :
add + = ord (num[j]) - 48
if (add % 2 = = 1 ) :
break
j + = 1
if (add = = 0 ) :
ans * = 10
else :
digit = int (math.floor(math.log10(add) + 1 ))
ans * = ( pow ( 10 , digit))
ans + = add
i = j
else :
add = 0
i = j
while j < len (num) :
add + = ord (num[j]) - 48
if (add % 2 = = 0 ) :
break
j + = 1
if (add = = 0 ) :
ans * = 10
else :
digit = int (math.floor(math.log10(add) + 1 ))
ans * = ( pow ( 10 , digit))
ans + = add
i = j
j + = 1
if (j + 1 ) > = len (num) :
return ans
else :
ans + = ord (num[ len (num) - 1 ]) - 48
return ans
N = 1667848271
print (key(N))
|
C#
using System;
class GFG{
static int key( int N)
{
String num = "" + N;
int ans = 0;
int j = 0;
for (j = 0; j < num.Length; j++)
{
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
if (add % 2 == 1)
break ;
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = ( int )Math.Floor(
Math.Log10(add) + 1);
ans *= ( int )(Math.Pow(10, digit));
ans += add;
}
i = j;
}
else
{
int add = 0;
int i;
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
if (add % 2 == 0)
{
break ;
}
}
if (add == 0)
{
ans *= 10;
}
else {
int digit = ( int )Math.Floor(
Math.Log10(add) + 1);
ans *= ( int )(Math.Pow(10, digit));
ans += add;
}
i = j;
}
}
if (j + 1 >= num.Length)
{
return ans;
}
else
{
return ans += num[num.Length - 1] - 48;
}
}
public static void Main(String[] args)
{
int N = 1667848271;
Console.Write(key(N));
}
}
|
Javascript
<script>
function key(N)
{
let num = "" + N.toString();
let ans = 0;
let j = 0;
for (j = 0; j < num.length; j++)
{
if ((num[j].charCodeAt() - 48) % 2 == 0)
{
let add = 0;
let i;
for (i = j; j < num.length; j++)
{
add += num[j].charCodeAt() - 48;
if (add % 2 == 1)
break ;
}
if (add == 0)
{
ans *= 10;
}
else
{
let digit = Math.floor(Math.log10(add) + 1);
ans *= parseInt(Math.pow(10, digit), 10);
ans += add;
}
i = j;
}
else
{
let add = 0;
let i;
for (i = j; j < num.length; j++)
{
add += num[j].charCodeAt() - 48;
if (add % 2 == 0)
{
break ;
}
}
if (add == 0)
{
ans *= 10;
}
else {
let digit = Math.floor(Math.log10(add) + 1);
ans *= parseInt(Math.pow(10, digit), 10);
ans += add;
}
i = j;
}
}
if (j + 1 >= num.length)
{
return ans;
}
else
{
return ans += num[num.length - 1].charCodeAt() - 48;
}
}
let N = 1667848271;
document.write(key(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Apr, 2021
Like Article
Save Article