Find the next number by adding natural numbers in order on alternating indices from last
Last Updated :
28 Oct, 2021
Given a numeric string S of size N, the task is to find the number formed by adding numbers 1, 2, 3, ... up to infinity to every alternative digit of the given numeric string S(starting from the last position). At any point, if the addition result is not a single digit, perform the repeated addition of digits until the result is a single digit.
Examples:
Input: S = "1345"
Output: 1546
Explanation:
Adding 1 to the last digit i.e., 5 will become 6 which modifies the string to "1346".
Adding 2 to the second last digit i.e., 3 will become 5 which modifies the string to "1546".
After the above steps, the resultant string formed is "1546".
Input: S = "789"
Output: 981
Approach: The idea to solve this problem lies over the part of repeated addition. There is actually no need to perform addition repeatedly until there is one digit. Instead, perform number % 9. If the number % 9 is equal to 9, then the sum of the digits will be equal to 9, else the sum of the digits will be equal to the number % 9. Follow the steps below to solve the problem:
- Initialize the variables temp and adding_number as 0 to store the position to be altered and the number to be added.
- Initialize the string variable result as an empty string to store the result.
- Iterate over the range [len-1, 0] where len is the length of the string, using the variable i and perform the following steps:
- Initialize the variable digit as the digit at the current position in the string.
- If temp%2 equals 0 then increase the value of adding_number by 1 and add it to the variable digit.
- If digit greater than equals to 10 then set the value of digit as digit%9 and if still, digit equals 0 then set its value as 9.
- Add the variable digit to the variable result in the beginning.
- After performing the above steps, print the value of result as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate the resultant
// number using the given criteria
string generateNumber(string number)
{
int temp = 0, adding_number = 0;
// Storing end result
string result = "";
// Find the length of numeric string
int len = number.size();
// Traverse the string
for (int i = len - 1; i >= 0; i--) {
// Storing digit at ith position
int digit = number[i] - '0';
// Checking that the number would
// be added or not
if (temp % 2 == 0) {
adding_number += 1;
digit += adding_number;
// Logic for summing the digits
// if the digit is greater than 10
if (digit >= 10) {
digit %= 9;
if (digit == 0)
digit = 9;
}
}
// Storing the result
result = to_string(digit) + result;
temp += 1;
}
// Returning the result
return result;
}
// Driver Code
int main()
{
string S = "1345";
cout << generateNumber(S);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to generate the resultant
// number using the given criteria
public static String generateNumber(String number) {
int temp = 0, adding_number = 0;
// Storing end result
String result = "";
// Find the length of numeric string
int len = number.length();
// Traverse the string
for (int i = len - 1; i >= 0; i--) {
// Storing digit at ith position
int digit = (int) number.charAt(i) - (int) '0';
// Checking that the number would
// be added or not
if (temp % 2 == 0) {
adding_number += 1;
digit += adding_number;
// Logic for summing the digits
// if the digit is greater than 10
if (digit >= 10) {
digit %= 9;
if (digit == 0)
digit = 9;
}
}
// Storing the result
result = digit + result;
temp += 1;
}
// Returning the result
return result;
}
// Driver Code
public static void main(String args[]) {
String S = "1345";
System.out.println(generateNumber(S));
}
}
// This code is contributed by gfgking.
Python3
# Python3 program for the above approach
# Function to generate the resultant
# number using the given criteria
def generateNumber(number) :
temp = 0; adding_number = 0;
# Storing end result
result = "";
# Find the length of numeric string
l = len(number);
# Traverse the string
for i in range(l - 1, -1, -1) :
# Storing digit at ith position
digit = ord(number[i]) - ord('0');
# Checking that the number would
# be added or not
if (temp % 2 == 0) :
adding_number += 1;
digit += adding_number;
# Logic for summing the digits
# if the digit is greater than 10
if (digit >= 10) :
digit %= 9;
if (digit == 0) :
digit = 9;
# Storing the result
result = str(digit) + result;
temp += 1;
# Returning the result
return result;
# Driver Code
if __name__ == "__main__" :
S = "1345";
print(generateNumber(S));
# This code is contributed by AnkThon
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to generate the resultant
// number using the given criteria
function generateNumber(number) {
let temp = 0, adding_number = 0;
// Storing end result
let result = "";
// Find the length of numeric string
let len = number.length;
// Traverse the string
for (let i = len - 1; i >= 0; i--) {
// Storing digit at ith position
let digit = parseInt(number[i]);
// Checking that the number would
// be added or not
if (temp % 2 == 0) {
adding_number += 1;
digit += adding_number;
// Logic for summing the digits
// if the digit is greater than 10
if (digit >= 10) {
digit %= 9;
if (digit == 0)
digit = 9;
}
}
// Storing the result
result = (digit).toString() + result;
temp += 1;
}
// Returning the result
return result;
}
// Driver Code
let S = "1345";
document.write(generateNumber(S));
// This code is contributed by Potta Lokesh
</script>
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to generate the resultant
// number using the given criteria
public static String generateNumber(string number) {
int temp = 0, adding_number = 0;
// Storing end result
string result = "";
// Find the length of numeric string
int len = number.Length;
// Traverse the string
for (int i = len - 1; i >= 0; i--) {
// Storing digit at ith position
int digit = (int)number[i] - (int)'0';
// Checking that the number would
// be added or not
if (temp % 2 == 0) {
adding_number += 1;
digit += adding_number;
// Logic for summing the digits
// if the digit is greater than 10
if (digit >= 10) {
digit %= 9;
if (digit == 0)
digit = 9;
}
}
// Storing the result
result = digit + result;
temp += 1;
}
// Returning the result
return result;
}
// Driver Code
public static void Main(string []args) {
string S = "1345";
Console.WriteLine(generateNumber(S));
}
}
// This code is contributed by AnkThon
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find if given number is sum of first n natural numbers Given a number s (1 <= s <= 1000000000). If this number is the sum of first n natural number then print n, otherwise print -1. Examples: Input: s = 10Output: n = 4Explanation: 1 + 2 + 3 + 4 = 10 Input: s = 17Output: n = -1Explanation: 17 can't be expressed as a sum of first n natural numbers a
8 min read
Sum of first N natural numbers with alternate signs Given an integer N, the task is to find the sum of first N natural numbers with alternate signs, i.e 1 - 2 + 3 - 4 + 5 - 6 + .... Examples: Input: N = 6 Output: -3 Explanation: 1 - 2 + 3 - 4 + 5 - 6 = -3 Therefore, the required output is -3. Input: N = 5 Output: 3 Explanation: 1 - 2 + 3 - 4 + 5 = 3
8 min read
Sum of series formed by difference between product and sum of N natural numbers Given a natural number N, the task is to find the sum of the series up to Nth term where the ith term denotes the difference between the product of first i natural numbers and sum of first i natural numbers, i.e., { 1 - 1 } + { (1 * 2) - (2 + 1) } + { (1 * 2 * 3) - (3 + 2 + 1) } + { (1 * 2 * 3 * 4)
10 min read
Value of k-th index of a series formed by append and insert MEX in middle Given two integers, n and k. Initially we have a sequence consisting of a single number 1. We need to consider series formed after n steps. In each step, we append the sequence to itself and insert the MEX(minimum excluded)(>0) value of the sequence in the middle. Perform (n-1) steps. Finally, fi
6 min read
Find Array obtained after adding terms of AP for Q queries Given an array A[] consisting of N integers and Q queries, say Query[][] of the form {L, R, a, d} such that each query represents the infinite AP with the first term as a and common difference d. The task is to print the updated array after performing the given queries such that for each query {L, R
14 min read