Disarium Number

Last Updated : 9 Jun, 2026

Given a number n, find if it is a Disarium number. A number is called a Disarium number if the sum of its digits raised to the power of their respective positions is equal to the number itself.

Examples: 

Input: n = 89
Output: true
Explanation: 81 + 92 = 8 + 81 = 89, which is equal to n. Therefore, 89 is a Disarium Number, so output is true.

Input: n = 81
Output: false
Explanation: 81 + 12 = 8 + 1 = 9, which is not equal to n. Therefore, 81 is not a Disarium Number, so output is false.

Try It Yourself
redirect icon

[Naive Approach] Storing Digits - O(d) Time and O(d) Space

Extract all digits of the number and store them in a container. Then traverse the stored digits in order, compute the powered sum using their positions, and compare the result with the original number.

Dry Run for n = 89:

  • The extracted digits are stored as [8, 9].
  • Compute the powered sum using their positions: 8¹ + 9² = 8 + 81 = 89.
  • The computed sum is equal to the original number 89.
  • Therefore, 89 is a Disarium number, and the function returns true.
C++
#include <bits/stdc++.h>
using namespace std;

// Check whether n is a Disarium number.
bool isDisarium(int n) {
    int original = n;
    vector<int> digits;

    // Store all digits of the number.
    while (n > 0) {
        digits.push_back(n % 10);
        n /= 10;
    }

    reverse(digits.begin(), digits.end());

    int sum = 0;

    // Compute the powered sum using the digit positions.
    for (int i = 0; i < digits.size(); i++) {
        sum += pow(digits[i], i + 1);
    }

    return sum == original;
}

int main() {
    int n = 89;

    cout << (isDisarium(n) ? "true" : "false");

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Collections;

class GFG {

    // Check whether n is a Disarium number.
    static boolean isDisarium(int n) {
        int original = n;
        ArrayList<Integer> digits = new ArrayList<>();

        // Store all digits of the number.
        while (n > 0) {
            digits.add(n % 10);
            n /= 10;
        }

        Collections.reverse(digits);

        int sum = 0;

        // Compute the powered sum using the digit positions.
        for (int i = 0; i < digits.size(); i++) {
            sum += (int) Math.pow(digits.get(i), i + 1);
        }

        return sum == original;
    }

    public static void main(String[] args) {
        int n = 89;

        System.out.println(isDisarium(n));
    }
}
Python
# Check whether n is a Disarium number.
def isDisarium(n):
    original = n
    digits = []

    # Store all digits of the number.
    while n > 0:
        digits.append(n % 10)
        n //= 10

    digits.reverse()

    sum_val = 0

    # Compute the powered sum using the digit positions.
    for i in range(len(digits)):
        sum_val += digits[i] ** (i + 1)

    return sum_val == original


if __name__ == "__main__":
    n = 89

    print("true" if isDisarium(n) else "false")
C#
using System;
using System.Collections.Generic;

class GFG {
    // Check whether n is a Disarium number.
    static bool isDisarium(int n) {
        int original = n;
        List<int> digits = new List<int>();

        // Store all digits of the number.
        while (n > 0) {
            digits.Add(n % 10);
            n /= 10;
        }

        digits.Reverse();

        int sum = 0;

        // Compute the powered sum using the digit positions.
        for (int i = 0; i < digits.Count; i++) {
            sum += (int)Math.Pow(digits[i], i + 1);
        }

        return sum == original;
    }

    static void Main() {
        int n = 89;

        Console.WriteLine(isDisarium(n) ? "true" : "false");
    }
}
JavaScript
// Check whether n is a Disarium number.
function isDisarium(n) {
    const original = n;
    const digits = [];

    // Store all digits of the number.
    while (n > 0) {
        digits.push(n % 10);
        n = Math.floor(n / 10);
    }

    digits.reverse();

    let sum = 0;

    // Compute the powered sum using the digit positions.
    for (let i = 0; i < digits.length; i++) {
        sum += Math.pow(digits[i], i + 1);
    }

    return sum === original;
}

const n = 89;

console.log(isDisarium(n) ? "true" : "false");

Output
true

[Expected Approach] Counting Digits - O(d) Time and O(1) Space

Instead of storing the digits separately, first count the total number of digits and then extract each digit from right to left. As each digit is processed, raise it to its corresponding position and add its contribution to the sum. If the final sum equals the original number, the number is Disarium.

Dry Run for n = 89:

  • The total number of digits is 2. Initialize sum = 0.
  • Extract the last digit 9 and add 9² = 81 to the sum. Now, sum = 81.
  • Extract the next digit 8 and add 8¹ = 8 to the sum. Now, sum = 81 + 8 = 89.
  • The computed sum equals the original number 89. Therefore, 89 is a Disarium number, and the function returns true.
C++
#include <bits/stdc++.h>
using namespace std;

// Return the number of digits in n.
int countDigits(int n) {
    int count = 0;

    while (n > 0) {
        n /= 10;
        count++;
    }
    
    return count;
}

// Check whether n is a Disarium number.
bool isDisarium(int n) {
    int digits = countDigits(n);
    int original = n;
    int sum = 0;

    while (n > 0) {

        // Add the current digit raised to its position.
        int digit = n % 10;
        sum += pow(digit, digits);

        n /= 10;
        digits--;
    }

    return sum == original;
}

int main() {
    int n = 89;

    cout << (isDisarium(n) ? "true" : "false");

    return 0;
}
Java
class GFG {

    // Return the number of digits in n.
    static int countDigits(int n) {
        int count = 0;

        while (n > 0) {
            n /= 10;
            count++;
        }

        return count;
    }

    // Check whether n is a Disarium number.
    static boolean isDisarium(int n) {
        int digits = countDigits(n);
        int original = n;
        int sum = 0;

        while (n > 0) {

            // Add the current digit raised to its position.
            int digit = n % 10;
            sum += (int)Math.pow(digit, digits);

            n /= 10;
            digits--;
        }

        return sum == original;
    }

    public static void main(String[] args) {
        int n = 89;

        System.out.println(isDisarium(n));
    }
}
Python
# Return the number of digits in n.
def countDigits(n):
    count = 0

    while n > 0:
        n //= 10
        count += 1

    return count


# Check whether n is a Disarium number.
def isDisarium(n):
    digits = countDigits(n)
    original = n
    sum_val = 0

    while n > 0:

        # Add the current digit raised to its position.
        digit = n % 10
        sum_val += digit ** digits

        n //= 10
        digits -= 1

    return sum_val == original


if __name__ == "__main__":
    n = 89

    print("true" if isDisarium(n) else "false")
C#
using System;

class GFG {
    // Return the number of digits in n.
    static int CountDigits(int n) {
        int count = 0;

        while (n > 0) {
            n /= 10;
            count++;
        }

        return count;
    }

    // Check whether n is a Disarium number.
    static bool isDisarium(int n) {
        int digits = CountDigits(n);
        int original = n;
        int sum = 0;

        while (n > 0) {
            // Add the current digit raised to its position.
            int digit = n % 10;
            sum += (int)Math.Pow(digit, digits);

            n /= 10;
            digits--;
        }

        return sum == original;
    }

    static void Main() {
        int n = 89;

        Console.WriteLine(isDisarium(n) ? "true" : "false");
    }
}
JavaScript
// Return the number of digits in n.
function countDigits(n) {
    let count = 0;

    while (n > 0) {
        n = Math.floor(n / 10);
        count++;
    }

    return count;
}

// Check whether n is a Disarium number.
function isDisarium(n) {
    const digits = countDigits(n);
    const original = n;
    let sum = 0;
    let position = digits;

    while (n > 0) {

        // Add the current digit raised to its position.
        const digit = n % 10;
        sum += Math.pow(digit, position);

        n = Math.floor(n / 10);
        position--;
    }

    return sum === original;
}

const n = 89;

console.log(isDisarium(n) ? "true" : "false");

Output
true


Comment