Simple and Efficient In O(n) time
\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nIn this we take difference with the minimum element found. We keep track of minimu
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn this we take difference with the minimum element found. We keep track of minimum element and maximum difference. And at each iteration we keep updating. And difference can be negative if the first element is largest so in that case we return -1.<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:**O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int maximumDifference(int[] arr) {\n        int arr_size=arr.length;\n        int max_diff = arr[1] - arr[0];\n\t\tint min_element = arr[0];\n\t\tint i;\n\t\tfor (i = 1; i < arr_size; i++)\n\t\t{\n\t\t\tif (arr[i] - min_element > max_diff)\n\t\t\t\tmax_diff = arr[i] - min_element;\n\t\t\tif (arr[i] < min_element)\n\t\t\t\tmin_element = arr[i];\n\t\t}\n\t\tif (max_diff>0)\n            return max_diff; \n        else\n            return -1;\n    }\n}\n```
['Java']