[Java] [Detailed Explanation] Simple to understand with explanation
The important catch for this problem was that it can have only multiples of 2, 3 and 5. Once we deep dive into this, we can conclude following point:\n\n1. Numb
The important catch for this problem was that it can have only multiples of 2, 3 and 5. Once we deep dive into this, we can conclude following point:\n\n1. Number will be of the form ```2^x * 3^y * 5^z``` \n2. First number will always be 1 (x = 0, y = 0, z = 0)\n3. If 2^x == 3^y then we should definetly increment x & y, similarly for y & z and x & z\n\nOnce we have understood the all 3 points, here comes the observation part. Notice that ```numbers``` will only have the integers of the form ```2^x * 3^y * 5^z``` . If we want to find the next multiple that should be multiplied with either 2, 3 or 5, it can be found in the numbers list. But how? Answer will be in the pointer approach.\n\nNotice that all x, y & z powers starts with 0. Let\'s have 3 pointers ```c2, c3, c5```. For initial steps you can write it down in paper:\n```\n// Initially 1 is pushed to array numbers\nnumbers list becomes = [1]\n2 = 1*2 // Here c2 = 0 so we fetch that index number i.e. 1 from numbers and c2 is incremented by 1\nnumbers list becomes = [1, 2]\n3 = 1*3 // Here c3 = 0 so we fetch that index number i.e. 1 from numbers and c3 is incremented by 1\nnumbers list becomes = [1, 2, 3]\n4 = 2*2 // Here c2 = 1 so we fetch that index number i.e. 2 from numbers and c2 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4]\n5 = 1*5 // Here c5 = 0 so we fetch that index number i.e. 1 from numbers and c5 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5]\n6 = 3*2 // Here c2 = 2 so we fetch that index number i.e. 3 from numbers and c2 is incremented by 1\n6 = 2*3 // Here c3 = 1 so we fetch that index number i.e. 2 from numbers and c3 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5, 6]\n8 = 4*2 // Here c2 = 3 so we fetch that index number i.e. 4 from numbers and c2 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5, 6, 8]\n9 = 3*3 // Here c3 = 2 so we fetch that index number i.e. 3 from numbers and c3 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5, 6, 8, 9]\n10 = 5*2 // Here c2 = 4 so we fetch that index number i.e. 5 from numbers and c2 is incremented by 1\n10 = 2*5 // Here c5 = 1 so we fetch that index number i.e. 2 from numbers and c5 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5, 6, 8, 9, 10]\n12 = 6*2 // Here c2 = 5 so we fetch that index number i.e. 6 from numbers and c2 is incremented by 1\n12 = 4*3 // Here c3 = 3 so we fetch that index number i.e. 4 from numbers and c3 is incremented by 1\nnumbers list becomes = [1, 2, 3, 4, 5, 6, 8, 9, 10, 12]\n................. and the rest is history\n```\nCheck out the following implementation for this logic: \n```\nclass Solution {\n    ArrayList<Integer> numbers;\n    Solution() {\n        numbers = new ArrayList<>();\n        numbers.add(1);\n        int c2 = 0, c3 = 0, c5 = 0;\n        for(int i = 1;i<1690;i++){\n            int min = Math.min(numbers.get(c2)*2, Math.min(numbers.get(c3)*3, numbers.get(c5)*5));\n            numbers.add(min);\n            if(min==numbers.get(c2)*2) c2++;\n            if(min==numbers.get(c3)*3) c3++;\n            if(min==numbers.get(c5)*5) c5++;\n        }\n    }\n    public int nthUglyNumber(int n) {\n        return numbers.get(n-1);\n    }\n}\n```
['Java']