Disarium Number | Practice | GeeksforGeeks

Given a number N, find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself. Output 1 if it's Disarium, and 0 if not.
 
Example 1:
Input:
N = 89
Output:
1
Explanation:
8^1+9^2 = 89 thus output is 1.
Example 2:
Input:
N = 81
Output:
0
Explanation:
8^1+1^2 = 9 thus output is 0. 
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDisarium() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)
 
Constraints:
0 <= N <= 108
0
ayushnautiyal11103 months ago
C++ Implementation:)
Time 0.0/1.5
 
int isDisarium(int N) {       string n=to_string(N);       int res=0;       for(int i=0;i<n.length();i++){           int m=n[i]-'0';           res+=pow(m,i+1);       }       return res==N?1:0;   }
+1
pketul22125 months ago
class Solution {
    static int isDisarium(int N) {
        
        int k = N, sum = 0, count = 0;
        
        while(k != 0) {
            count++;
            k /= 10;
        }
        
        k = N;
        
        while(k != 0) {
            sum += Math.pow((k % 10), count);
            k /= 10;
            count--;
        }
        
        if(sum == N)
            return 1;
        return 0;
    }
}
0
Ashutosh Patel8 months ago
Ashutosh Patel
//Straight forward/simple approach --> count the no. of digits-->Summation of digits^power(count) from last ---> decrement count.//Code in C++//int isDisarium(int N) {        int count=0;        int num=N,n=N;        while(N!=0){            count++;            N=N/10;        }        int sum=0;        while(n!=0){            int rem=n%10;            sum=sum+pow(rem,count);            count--;            n=n/10;        }        if(sum==num)  return 1;        else          return 0;    }
0
Nitin Gangwar9 months ago
Nitin Gangwar
def isDisarium(self, N):        # code here         sum=0        j=1        s=str(N)        for i in s:            sum+=pow(int(i),j)            j+=1         if(sum==N):            return 1        return 0
0
martial9 months ago
martial
https://uploads.disquscdn.c... https://uploads.disquscdn.c...
0
Raushan Kumar10 months ago
Raushan Kumar
class Solution {    static int isDisarium(int N) {        // code here        int sum=0;        int temp=N;        int v=N;        int count=0;        int i=1;        while(temp!=0)        {            count++;            temp/=10;        }        // System.out.println(count);        while(count!=0){            count-=1;        int m=(int)(v/(Math.pow(10,count)));        v=(int)(v%(Math.pow(10,count)));        // System.out.print(v+" ");        sum=(int)(sum+Math.pow(m,i));        i++;
        }       // System.out.println(sum);        if(N==sum)                    return 1;        else                    return 0;    }};
0
Gurucharan Rajput1 year ago
Gurucharan Rajput
java simple solution
static int isDisarium(int N) {      int c=0;      int y,res;        int num = N;        int temp=N;;        int sum = 0;        while(N>0)        {            res=N%10;            c++;            N/=10;        }
        while(num > 0)        {             y=num%10;            sum =(int)(sum+Math.pow(y,c));            c--;            num /= 10;        }
        return sum == temp ? 1 : 0;    }};
0
Annanya Mathur1 year ago
Annanya Mathur
int isDisarium(int n) {        int c=floor(log10(n)+1);        int num=n,s=0;        while(num)        {            s+=pow((num%10),c--);            num=num/10;        }        return s==n;    }
0
Drigger1 year ago
Drigger
Sol in Java: https://ide.geeksforgeeks.o...
0
Myron Dsilva1 year ago
Myron Dsilva
        cop=N        l=0        while(cop!=0):            r=cop%10            l=l+1            cop=cop//10         copy=N        dis=0        while(copy>0):            rem=copy%10            dis=dis+int(rem**l)            l=l-1            copy=copy//10        if(dis==N):            return 1        else:            return 0
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.