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.
Input: N = 1667848270
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
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:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the number of operations required to reduce the given number
- Minimum number of given operations required to reduce the array to 0 element
- Reduce N to 1 with minimum number of given operations
- Reduce a number to 1 by performing given operations | Set 2
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Min operations to reduce N by multiplying by any number or taking square root
- Minimum number of operations required to reduce N to 0
- Minimum number of operations required to reduce N to 1
- Count operations of the given type required to reduce N to 0
- Minimum steps to reduce N to 0 by given operations
- Reduce N to 0 or less by given X and Y operations
- Reduce the fraction to its lowest form
- Find maximum operations to reduce N to 1
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M
- Minimum division by 10 and multiplication by 2 required to reduce given number to 1
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Reduce the number to minimum multiple of 4 after removing the digits
- Reduce the array to a single element with the given operation
- Reduce given three Numbers by decrementing in Pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.