Java | Find max and min numbers | Beats > 97% | Simple | Clean code
Complexity\r\n- Time complexity: O(n)\r\n Add your time complexity here, e.g. O(n) \r\n\r\n- Space complexity: O(1)\r\n Add your space complexity here, e.g. O(n
# Complexity\r\n- Time complexity: $$O(n)$$\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: $$O(1)$$\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution {\r\n  public int maxProductDifference(int[] nums) {\r\n    int max1 = 0, max2 = 0, min1 = 10001, min2 = 10001;\r\n\r\n    for (var n : nums) {\r\n      if (n > max1) {\r\n        max2 = max1;\r\n        max1 = n;\r\n      } else if (n > max2) {\r\n        max2 = n;\r\n      }\r\n      if (n < min1) {\r\n        min2 = min1;\r\n        min1 = n;\r\n      } else if (n < min2) {\r\n        min2 = n;\r\n      }\r\n    }\r\n    return (max1 * max2) - (min1 * min2);\r\n  }\r\n}\r\n```\r\nIf you like my solution, please upvote it!
['Array', 'Java']