👏Beats 98.57% of users with Java || ✅Simple & Easy Well Explained Solution without Sorting 🔥💥
Intuition\nFind First 3 Maximum number and 2 minimum numbers and return maximum of 3 max numbers or 2 min number multiply max number.\n\n# I Think This Can Help
# Intuition\nFind First 3 Maximum number and 2 minimum numbers and return maximum of 3 max numbers or 2 min number multiply max number.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/ca61190e-31c0-413b-8b2b-b1632beae1ca_1712215890.1777723.png)](https://leetcode.com/problems/maximum-product-of-three-numbers/submissions/1222855261/)\n\n# Approach\n1. Initilize 3 max pointers as min value to store first 3 max numbers.\n    ```\n    int maxp1 = Integer.MIN_VALUE, maxp2 = Integer.MIN_VALUE, maxp3 = Integer.MIN_VALUE;\n    ```\n2. Initilize 2 min pointers as max value to store first 2 min numbers.\n    ```\n    int minp1 = Integer.MAX_VALUE, minp2 = Integer.MAX_VALUE;\n    ```\n3. Iterate over the nums.(Below Code in the loop)\n    - Check if maxp1 is smaller than ele then replaced maxp3 to maxp2,maxp2 to maxp1 or maxp1 to ele.\n        ```\n        if(maxp1<=ele){\n            maxp3=maxp2;\n            maxp2=maxp1;\n            maxp1=ele;\n        }\n        ```\n    - Check if maxp2 is smaller than ele or ele smaller than maxp1 then replace maxp3 to maxp2 or maxp2 to ele.\n        ```\n        if(maxp2<=ele && ele<maxp1){\n            maxp3=maxp2;\n            maxp2=ele;\n        }\n        ```\n    - Check if maxp3 is smaller than ele or ele smaller than maxp2 then replace maxp3 to ele.\n        ```\n        if(maxp3<=ele && ele<maxp2){\n            maxp3=ele;\n        }\n        ```\n    - Check if minp1 is greater than ele then replace minp2 to minp1 or minp1 to ele.\n        ```\n        if(minp1>=ele){\n            minp2=minp1;\n            minp1=ele;\n        }\n        ```\n    - Check if minp2 is greater than ele or ele greater than minp1 then replace minp2 to ele.\n        ```\n        if(minp2>=ele && ele>minp1){\n            minp2=ele;\n        }\n        ```\n- Whole loop look like this\n    ```\n    for (int ele : nums) {\n        if(maxp1<=ele){\n            maxp3=maxp2;\n            maxp2=maxp1;\n            maxp1=ele;\n        }\n        if(maxp2<=ele && ele<maxp1){\n            maxp3=maxp2;\n            maxp2=ele;\n        }\n        if(maxp3<=ele && ele<maxp2){\n            maxp3=ele;\n        }\n        if(minp1>=ele){\n            minp2=minp1;\n            minp1=ele;\n        }\n        if(minp2>=ele && ele>minp1){\n            minp2=ele;\n        }\n    }\n    ```\n4. Return the max of 3 max pointers or 2 min pointers multiply max number.\n```\nreturn Math.max(maxp1 * maxp2 * maxp3, minp1 * minp2 * maxp1);\n```\n\n# Complexity\n- Time complexity:\nO(n)   --> We are going linear.\n\n- Space complexity:\nO(1)   --> We use Constant Space.\n\n# Code\n```\nclass Solution {\n    public int maximumProduct(int[] nums) {\n        int maxp1 = Integer.MIN_VALUE, maxp2 = Integer.MIN_VALUE, maxp3 = Integer.MIN_VALUE;\n        int minp1 = Integer.MAX_VALUE, minp2 = Integer.MAX_VALUE;\n        for (int ele : nums) {\n            if(maxp1<=ele){\n                maxp3=maxp2;\n                maxp2=maxp1;\n                maxp1=ele;\n            }\n            if(maxp2<=ele && ele<maxp1){\n                maxp3=maxp2;\n                maxp2=ele;\n            }\n            if(maxp3<=ele && ele<maxp2){\n                maxp3=ele;\n            }\n            if(minp1>=ele){\n                minp2=minp1;\n                minp1=ele;\n            }\n            if(minp2>=ele && ele>minp1){\n                minp2=ele;\n            }\n        }\n        return Math.max(maxp1 * maxp2 * maxp3, minp1 * minp2 * maxp1);\n    }\n}\n```\n\n![_94f47172-0dce-4692-964c-6b3dfe96ce1b.jpeg](https://assets.leetcode.com/users/images/9dd31a82-2164-4c19-946f-2d2d3c6f8d5d_1712216929.645511.jpeg)\n
['Array', 'Math', 'Sorting', 'Java']