Interview Question Series | Season 2

Problem Explanation:

The approach works by multiplying the result by 2 iteratively, keeping track of the carry over each time, and storing the result in a string.

  1. Result Initialization: The string result starts with "1", which represents the initial value of 20=12^0 = 1.
  2. Loop for Exponentiation: For each exponent, we multiply the current result by 2, updating the result string, and handle the carry over for digits that exceed 9.
  3. Carry Handling: If a digit exceeds 9 after multiplication, it is carried over to the next digit.

C Code:

#include <stdio.h>

#define MAX_DIGITS 35

void two_power(int exponent) {
    // Array to hold the result, initialized with the string "1"
    char result[MAX_DIGITS] = "1";
    int size = 1;

    // Loop through the exponent, multiplying the result by 2 each time
    for (int i = 0; i < exponent; i++) {
        int carry = 0;

        // Multiply each digit by 2 and handle carry
        for (int j = 0; j < size; j++) {
            int num = (result[j] - '0') * 2 + carry;
            result[j] = (num % 10) + '0';  // Store the current digit
            carry = num / 10;  // Calculate carry for next iteration
        }

        // If there's a carry left after the last digit, add it
        if (carry > 0) {
            result[size] = carry + '0';
            size++;  // Increase the size of the result
        }
    }

    // Print the result in the correct order (most significant digit first)
    printf("result = ");
    for (int i = size - 1; i >= 0; i--) {
        printf("%c", result[i]);
    }
    printf("n");
}

int main() {
    int exponent = 100;
    two_power(exponent);
    return 0;
}

Explanation of Code:

  1. Initialization: The result array is initialized with "1", representing 20=12^0 = 1. The variable size keeps track of how many digits are in the result.

  2. Exponentiation Loop: The loop runs for exponent times (in this case, 100 times). During each iteration, the algorithm multiplies the number by 2:

    • For each digit of result, it calculates num = (result[j] - '0') * 2 + carry. This represents multiplying the digit by 2 and adding the carry from the previous digit multiplication.
    • The current digit is stored as result[j] = (num % 10) + '0' and the carry for the next iteration is updated as carry = num / 10.
  3. Carry Handling: After all digits are processed, if there’s any remaining carry, it is added as a new digit at the end of result, and the size is updated.

  4. Printing the Result: The result is printed from the most significant digit (stored in the last index of the array) to the least significant digit (stored in the first index).

Example Output:

When you run the program with exponent = 100, the output will be:

result = 1267650600228229401496703205376

This is the value of 21002^{100}, which cannot fit in a standard integer type due to its size. The program uses a string to store and manipulate the result, allowing it to handle large numbers without overflow.

Key Concepts:

  • String Manipulation: Storing large numbers as strings to bypass the limitations of integer overflow.
  • Carry Handling: Properly managing carry over when multiplying digits.
  • Efficiency: The algorithm runs in O(n) time, where n is the number of digits in the final result (about 31 digits for 21002^{100}).

This approach allows you to handle arbitrarily large numbers, as long as the array is large enough to hold the result.

Scroll to Top