Reduce a given number to form a key by the given operations
Last Updated :
22 Apr, 2021
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++
// C++ program of the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the key
// of the given number
int key(int N)
{
// Convert the integer
// to String
string num = "" + to_string(N);
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for(j = 0; j < num.length(); j++)
{
// Check if digit is even or odd
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for(i = j; j < num.length(); j++)
{
add += num[j] - 48;
// Check if sum becomes odd
if (add % 2 == 1)
break;
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = (int)floor(log10(add) + 1);
ans *= (pow(10, digit));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else
{
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for(i = j; j < num.length(); j++)
{
add += num[j] - 48;
// Check if sum becomes even
if (add % 2 == 0)
{
break;
}
}
if (add == 0)
{
ans *= 10;
}
else
{
int digit = (int)floor(
log10(add) + 1);
ans *= (pow(10, digit));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.length())
{
return ans;
}
else
{
return ans += num[num.length() - 1] - 48;
}
}
// Driver code
int main()
{
int N = 1667848271;
cout << key(N);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program of the
// above approach
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
// Function to find the key
// of the given number
static int key(int N)
{
// Convert the integer
// to String
String num = "" + N;
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for (j = 0; j < num.length(); j++) {
// Check if digit is even or odd
if ((num.charAt(j) - 48) % 2 == 0) {
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48;
// Check if sum becomes odd
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));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else {
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length(); j++) {
add += num.charAt(j) - 48;
// Check if sum becomes even
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));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.length()) {
return ans;
}
else {
return ans += num.charAt(
num.length() - 1)
- 48;
}
}
// Driver Code
public static void main(String[] args)
{
int N = 1667848271;
System.out.print(key(N));
}
}
Python3
# Python3 program of the
# above approach
import math
# Function to find the key
# of the given number
def key(N) :
# Convert the integer
# to String
num = "" + str(N)
ans = 0
j = 0
# Iterate the num-string
# to get the result
while j < len(num) :
# Check if digit is even or odd
if ((ord(num[j]) - 48) % 2 == 0) :
add = 0
# Iterate until odd sum
# is obtained by adding
# consecutive digits
i = j
while j < len(num) :
add += ord(num[j]) - 48
# Check if sum becomes odd
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))
# Add the result in ans
ans += add
# Assign the digit index
# to num string
i = j
else :
# If the number is odd
add = 0
# Iterate until odd sum
# is obtained by adding
# consecutive digits
i = j
while j < len(num) :
add += ord(num[j]) - 48
# Check if sum becomes even
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))
# Add the result in ans
ans += add
# assign the digit index
# to main numstring
i = j
j += 1
# Check if all digits
# are visited or not
if (j + 1) >= len(num) :
return ans
else :
ans += ord(num[len(num) - 1]) - 48
return ans
N = 1667848271
print(key(N))
# This code is contributed by divyesh072019
C#
// C# program of the
// above approach
using System;
class GFG{
// Function to find the key
// of the given number
static int key(int N)
{
// Convert the integer
// to String
String num = "" + N;
int ans = 0;
int j = 0;
// Iterate the num-string
// to get the result
for (j = 0; j < num.Length; j++)
{
// Check if digit is even or odd
if ((num[j] - 48) % 2 == 0)
{
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
// Check if sum becomes odd
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));
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else
{
// If the number is odd
int add = 0;
int i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.Length; j++)
{
add += num[j] - 48;
// Check if sum becomes even
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));
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.Length)
{
return ans;
}
else
{
return ans += num[num.Length - 1] - 48;
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 1667848271;
Console.Write(key(N));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program of the above approach
// Function to find the key
// of the given number
function key(N)
{
// Convert the integer
// to String
let num = "" + N.toString();
let ans = 0;
let j = 0;
// Iterate the num-string
// to get the result
for (j = 0; j < num.length; j++)
{
// Check if digit is even or odd
if ((num[j].charCodeAt() - 48) % 2 == 0)
{
let add = 0;
let i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length; j++)
{
add += num[j].charCodeAt() - 48;
// Check if sum becomes odd
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);
// Add the result in ans
ans += add;
}
// Assign the digit index
// to num string
i = j;
}
else
{
// If the number is odd
let add = 0;
let i;
// Iterate until odd sum
// is obtained by adding
// consecutive digits
for (i = j; j < num.length; j++)
{
add += num[j].charCodeAt() - 48;
// Check if sum becomes even
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);
// Add the result in ans
ans += add;
}
// assign the digit index
// to main numstring
i = j;
}
}
// Check if all digits
// are visited or not
if (j + 1 >= num.length)
{
return ans;
}
else
{
return ans += num[num.length - 1].charCodeAt() - 48;
}
}
let N = 1667848271;
document.write(key(N));
// This code is contributed by mukesh07.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Reduce a number to 1 by performing given operations | Set 2
Given an integer N. The task is to reduce the given number N to 1 in minimum number of given operations. You can perform any one of the below operations in each step. If the number is even then you can divide the number by 2.If the number is odd then you are allowed to perform either (N + 1) or (N -
7 min read
Reduce a number to 1 by performing given operations
Given a number N. The task is to reduce the given number N to 1 in the minimum number of steps. You can perform any one of the below operations in each step.Operation 1: If the number is even then you can divide the number by 2.Operation 2: If the number is odd then you are allowed to perform either
7 min read
Reduce a number to 1 by performing given operations | Set 3
Given an integer N, the task is to find the number of steps required to reduce the given number N to 1 by performing the following operations: If the number is a power of 2, then divide the number by 2.Otherwise, subtract the greatest power of 2 smaller than N from N. Examples: Input: N = 2 Output:
10 min read
Reduce N to 1 by given operations
Given an integer N. Then your task is to output a minimum number of operations to reduce N into 1. You can below operations to do the same: Subtract 1 from NUpdate N to N/2, if N is divisible by 2Update N to N/3, if N is divisible by 3Examples: Input: N = 10Output: 3Explanation: The operations are p
5 min read
Reduce N to 1 with minimum number of given operations
Given an integer N, the task is to reduce N to 1 with the following two operations: 1 can be subtracted from each of the digits of the number only if the digit is greater than 0 and the resultant number doesn't have any leading 0s.1 can be subtracted from the number itself. The task is to find the m
6 min read
Minimum steps to reduce N to 0 by given operations
Give an integer N, the task is to find the minimum number of moves to reduce N to 0 by one of the following operations: Reduce N by 1.Reduce N to (N/2), if N is divisible by 2.Reduce N to (N/3), if N is divisible by 3. Examples: Input: N = 10Output: 4Explanation: Here N = 10Step 1: Reducing N by 1 i
11 min read
Minimum Steps to obtain N from 1 by the given operations
Given an integer N, the task is to find the minimum number of operations needed to obtain the number N starting from 1. Below are the operations: Add 1 to the current number.Multiply the current number by 2.Multiply the current number by 3. Print the minimum number of operations required and the cor
15+ min read
Number of operations to reduce Kth element to 0
Given an array arr[] of size N and an integer K, the task is to find the number of operations to reduce the Kth element(0-indexed) to 0. In one operation, the front element, that is arr[0] is decremented by 1 and is removed from the front of the array. If the removed element is still greater than 0,
8 min read
Minimum number of given operations required to reduce a number to 2
Given a positive integer N, the task is to reduce N to 2 by performing the following operations minimum number of times: Operation 1: Divide N by 5, if N is exactly divisible by 5.Operation 2: Subtract 3 from N. If it is not possible, print -1. Examples: Input: N = 28Output: 3Explanation: Operation
7 min read
Minimum number of steps required to obtain the given Array by the given operations
Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only. Select any index i and increment all the elements at the indices [i, N - 1] by 1.Select any index i and decrease a
12 min read