What is Noble integer ?

Noble Integer in Simple Terms:
A noble integer is a special type of number that has an interesting property when it comes to its digits.

Explanation with Examples:
Imagine you have a number, like 123. This number has three digits: 1, 2, and 3. Now, if the number of digits that are smaller than the current digit is exactly equal to the value of the current digit, then the number is called a noble integer.

Here’s what that means with the number 123:

  • The first digit is 1. The number of digits smaller than 1 is 0. But the value of the current digit, which is 1, is also 1. Since 0 is equal to 1, 123 is not a noble integer.

Let’s try another number, 231:

  • The first digit is 2. The number of digits smaller than 2 is 1 (which is 1). The value of the current digit, which is 2, is not equal to 1. So, 231 is not a noble integer.

Now, let’s check the number 312:

  • The first digit is 3. The number of digits smaller than 3 is 2 (which is 1 and 2). The value of the current digit, which is 3, is equal to 3. So, 312 is a noble integer!

In simple terms, a noble integer is a number where each digit “feels special” because the count of digits smaller than it is exactly the same as the digit itself.

Remember, noble integers are a bit of a mathematical curiosity and might not come up very often, but they can be fun to explore!

Here’s a Java program to check if a given number is a noble integer:

import java.util.Scanner;

public class NobleInteger {

    public static int countSmallerDigits(int[] arr, int num) {
        int count = 0;
        for (int digit : arr) {
            if (digit < num) {
                count++;
            }
        }
        return count;
    }

    public static boolean isNobleInteger(int number) {
        String numStr = Integer.toString(number);
        int[] digits = new int[numStr.length()];
        for (int i = 0; i < numStr.length(); i++) {
            digits[i] = Character.getNumericValue(numStr.charAt(i));
        }

        for (int i = 0; i < digits.length; i++) {
            if (countSmallerDigits(digits, digits[i]) != digits[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (isNobleInteger(num)) {
            System.out.println(num + " is a noble integer!");
        } else {
            System.out.println(num + " is not a noble integer.");
        }
    }
}

Copy and paste this code into a Java IDE or a text editor, and then compile and run it. When you run the program, enter the number you want to check, and it will let you know if it’s a noble integer or not.

Here’s a simple Python program to check if a given number is a noble integer:

def count_smaller_digits(arr, num):
    count = 0
    for digit in arr:
        if digit < num:
            count += 1
    return count

def is_noble_integer(number):
    digits = [int(digit) for digit in str(number)]

    for i, digit in enumerate(digits):
        if count_smaller_digits(digits[:i], digit) != digit:
            return False
    return True

# Taking input from the user
num = int(input("Enter a number: "))

if is_noble_integer(num):
    print(f"{num} is a noble integer!")
else:
    print(f"{num} is not a noble integer.")

Copy and paste this code into a Python interpreter or a script file, and it will allow you to check if a given number is a noble integer. When you run the program, enter the number you want to check, and it will tell you if it’s a noble integer or not.

Noble integer in an Array

Imagine you have a group of numbers, like a list of your friends’ ages. An integer is just a fancy word for a whole number like 1, 5, 10, and so on. Now, a noble integer is a special kind of number in this group.

Here’s the rule for a number to be noble: The number of integers in the list that are greater than the given number should be the same as the number itself.

For example, let’s say you have these ages: 4, 7, 2, and 9. Now, let’s take the number 2 from the list. In the list, there are two numbers greater than 2, which are 4 and 7. And guess what? The number 2 is also 2 itself! So, in this case, 2 is a noble integer because the count of numbers greater than 2 is the same as 2.

But let’s look at the number 4. There are two numbers greater than 4 (which are 7 and 9), but 4 is not equal to 2 (the count of numbers greater than 4). So, 4 is not a noble integer.

Here’s another example: Let’s take the number 3. In the list, there are three numbers greater than 3 (which are 4, 7, and 9). And guess what? The number 3 is also 3 itself! So, 3 is a noble integer in this case.

In simple terms, a noble integer is like a number that “rules” over the other numbers. It’s special because it has a relationship with the count of bigger numbers in the list.

Remember, not all groups of numbers have a noble integer, and sometimes they might have more than one!

Here’s a simple Java program to find a noble integer in an array:

public class NobleInteger {

    // Function to check if a number is a noble integer
    static boolean isNoble(int[] arr, int num) {
        int count = 0;

        // Count how many elements are greater than 'num'
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > num) {
                count++;
            }
        }

        // Check if the count matches the noble condition
        return count == num;
    }

    public static void main(String[] args) {
        int[] arr = {2, 4, 3, 7, 6, 9, 1};

        // Check each number in the array
        for (int num : arr) {
            if (isNoble(arr, num)) {
                System.out.println(num + " is a noble integer.");
                return; // No need to continue after finding one noble integer
            }
        }

        System.out.println("No noble integer found.");
    }
}

In this program, the isNoble function takes an array of integers and a number as parameters. It counts how many elements in the array are greater than the given number. If the count matches the number itself, the function returns true, indicating that the number is noble.

In the main method, we iterate through each number in the array and use the isNoble function to check if it’s a noble integer. If we find a noble integer, we print it and stop the loop. If no noble integer is found after checking all numbers, a message is printed indicating that no noble integer was found.

Thank You!

LEAVE A REPLY

Please enter your comment!
Please enter your name here