Skip to main content

Command Palette

Search for a command to run...

Check if The Number is Fascinating

Published
3 min read
Check if The Number is Fascinating
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an integer n that consists of exactly 3 digits.

We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0's:

  • Concatenate n with the numbers 2 * n and 3 * n.

Return true if n is fascinating, or false otherwise.

Concatenating two numbers means joining them together. For example, the concatenation of 121 and 371 is 121371.

LeetCode Problem - 2729

class Solution {
    public boolean isFascinating(int n) {
        // Create a list to store digits and check for duplicate digits
        List<Integer> list = new ArrayList<>();

        // Check if the number n is a fascinating number
        if (facscinationNum(n)) {
            // Get the digits of n
            int[] temp = return3Num(n);
            // Add the digits of n to the list, ensuring no duplicates
            for (int i = 0; i < 3; i++) {
                if (!list.contains(temp[i])) {
                    list.add(temp[i]);
                } else {
                    return false; // Return false if a duplicate is found
                }
            }
        } else {
            return false; // Return false if n is not a fascinating number
        }

        // Check if 2 * n is a fascinating number
        if (facscinationNum(2 * n)) {
            // Get the digits of 2 * n
            int[] temp = return3Num(2 * n);
            // Add the digits of 2 * n to the list, ensuring no duplicates
            for (int i = 0; i < 3; i++) {
                if (!list.contains(temp[i])) {
                    list.add(temp[i]);
                } else {
                    return false; // Return false if a duplicate is found
                }
            }
        } else {
            return false; // Return false if 2 * n is not a fascinating number
        }

        // Check if 3 * n is a fascinating number
        if (facscinationNum(3 * n)) {
            // Get the digits of 3 * n
            int[] temp = return3Num(3 * n);
            // Add the digits of 3 * n to the list, ensuring no duplicates
            for (int i = 0; i < 3; i++) {
                if (!list.contains(temp[i])) {
                    list.add(temp[i]);
                } else {
                    return false; // Return false if a duplicate is found
                }
            }
        } else {
            return false; // Return false if 3 * n is not a fascinating number
        }

        return true; // Return true if all conditions are met
    }

    // Helper function to check if a number is fascinating (contains only non-zero digits)
    public boolean facscinationNum(int num) {
        List<Integer> list = new ArrayList<>();

        // Loop through the digits of the number (3 digits)
        for (int i = 0; i < 3; i++) {
            int temp = num % 10; // Extract the last digit
            // Add the digit to the list if it's not already present and not zero
            if (!list.contains(temp) && temp != 0) {
                list.add(temp);
            } else {
                return false; // Return false if a digit is repeated or is zero
            }
            num /= 10; // Remove the last digit
        }

        return true; // Return true if the number contains only non-zero unique digits
    }

    // Helper function to return the 3 digits of a number in an array
    public int[] return3Num(int num) {
        int[] result = new int[3];

        // Extract the last 3 digits of the number
        for (int i = 0; i < 3; i++) {
            result[i] = num % 10; // Extract the last digit
            num /= 10; // Remove the last digit
        }

        return result; // Return the 3 digits as an array
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.