Given a 3-digit number N, the task is to find if N is an Osiris number or not. Osiris numbers are the numbers that are equal to the sum of permutations of sub-samples of their own digits. For example, 132 is an Osiris number as it is equal to 12 + 21 + 13 + 31 + 23 + 32.
Examples:Â
Input: N = 132Â
Output: YesÂ
12 + 21 + 13 + 31 + 23 + 32 = 132Input: N = 154Â
Output: NoÂ
Â
Approach:Â Â
If n = 132,Â
132 = 12 + 21 + 13 + 31 + 23 + 32Â
132 = 2 * 11 + 2 * 22 + 2 * 33Â
132 = 22 + 44 + 66Â
132 = (2 + 4 + 6) * 11Â
132 = 2 * (1 + 2 + 3) * 11, each digit of 132 occurs twice in the ones and tens position of the sums.Â
The same rule applies for every 3-digit Osiris number and can be reciprocated to check whether a number is an Osiris number or not.Â
For a 3-digit number N to be considered as an Osiris number, N must be equal to 2 * (sum of digits) * 11Â
Â
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; Â
// Function that returns true if // n is an Osiris number bool isOsiris( int n) {     // 3rd digit     int a = n % 10; Â
    // 2nd digit     int b = (n / 10) % 10; Â
    // 1st digit     int c = n / 100; Â
    int digit_sum = a + b + c; Â
    // Check the required condition     if (n == (2 * (digit_sum)*11)) {         return true ;     } Â
    return false ; } Â
// Driver code int main() {     int n = 132;     if (isOsiris(n))         cout << "Yes" ;     else         cout << "No" ; Â
    return 0; } |
Java
// Java implementation of the approach class GFG {      // Function that returns true if // n is an Osiris number static boolean isOsiris( int n) {     // 3rd digit     int a = n % 10 ; Â
    // 2nd digit     int b = (n / 10 ) % 10 ; Â
    // 1st digit     int c = n / 100 ; Â
    int digit_sum = a + b + c; Â
    // Check the required condition     if (n == ( 2 * (digit_sum)* 11 ))     {         return true ;     } Â
    return false ; } Â
// Driver code public static void main(String args[]) {     int n = 132 ;     if (isOsiris(n))         System.out.println( "Yes" );     else         System.out.println( "No" ); } } Â
// This code is contributed by Akanksha Rai |
Python3
# Python implementation of the approach Â
# Function that returns true if # n is an Osiris number def isOsiris(n):          # 3rd digit     a = n % 10          # 2nd digit     b = (n / / 10 ) % 10          # 1st digit     c = n / / 100 Â
    digit_sum = a + b + c Â
    # Check the required condition     if (n = = ( 2 * (digit_sum) * 11 )):         return True          return False Â
# Driver code if __name__ = = '__main__' : Â Â Â Â n = 132 Â Â Â Â if isOsiris(n): Â Â Â Â Â Â Â Â print ( "Yes" ) Â Â Â Â else : Â Â Â Â Â Â Â Â print ( "No" ) |
C#
// C# implementation of the approach using System; Â
class GFG { // Function that returns true if // n is an Osiris number static bool isOsiris( int n) {     // 3rd digit     int a = n % 10; Â
    // 2nd digit     int b = (n / 10) % 10; Â
    // 1st digit     int c = n / 100; Â
    int digit_sum = a + b + c; Â
    // Check the required condition     if (n == (2 * (digit_sum)*11))     {         return true ;     } Â
    return false ; } Â
// Driver code static void Main() {     int n = 132;     if (isOsiris(n))         Console.WriteLine( "Yes" );     else         Console.WriteLine( "No" ); } } Â
// This code is contributed by mits |
PHP
<?php // PHP implementation of the approach Â
// Function that returns true if // n is an Osiris number function isOsiris( $n ) {     // 3rd digit     $a = $n % 10; Â
    // 2nd digit     $b = floor ( $n / 10) % 10; Â
    // 1st digit     $c = floor ( $n / 100); Â
    $digit_sum = $a + $b + $c ; Â
    // Check the required condition     if ( $n == (2 * ( $digit_sum ) * 11))     {         return true;     } Â
    return false; } Â
// Driver code $n = 132; if (isOsiris( $n ))     echo "Yes" ; else     echo "No" ;      // This code is contributed by Ryuga ?> |
Javascript
<script> Â
// Javascript implementation of the approach Â
// Function that returns true if // n is an Osiris number function isOsiris(n) {          // 3rd digit     let a = n % 10; Â
    // 2nd digit     let b = parseInt((n / 10) % 10); Â
    // 1st digit     let c = parseInt(n / 100); Â
    let digit_sum = a + b + c; Â
    // Check the required condition     if (n == (2 * (digit_sum) * 11))     {         return true ;     }     return false ; } Â
// Driver code let n = 132; Â
if (isOsiris(n))     document.write( "Yes" ); else     document.write( "No" );      // This code is contributed by souravmahato348 Â
</script> |
Yes
Â
Time Complexity: O(1)Â
Space Complexity: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!