Open In App

Find the next number by adding natural numbers in order on alternating indices from last

Last Updated : 28 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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

Output: 
1546

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Next Article

Similar Reads