100% Faster || Easy explanation
Runtime: 0 ms, faster than 100.00% of Java online submissions\nMemory Usage: 41.4 MB, less than 96.55% of Java online submissions\n\n\nHere we are travesing thr
Runtime: 0 ms, faster than 100.00% of Java online submissions\nMemory Usage: 41.4 MB, less than 96.55% of Java online submissions\n\n\nHere we are travesing through array only once,\nwhile doing so we are performing 2 operations,\n1st:-    finding minimum in an array,\n2nd:-  finding max value of (current value-minimum value we found till now)\n\nif statement in code ensures that we wont subtract current number when it is being stored as min.\n\n\n\n    public int maximumDifference(int[] nums) {\n       int minn=nums[0];\n        int maxn=-1;\n        for(int i=0;i<nums.length;i++){\n            \n            if(minn>=nums[i])              //1st Part\n                minn=nums[i];\n            else {if(maxn<nums[i]-minn)    //2nd Part\n                 maxn=nums[i]-minn;\n                 }\n        }return maxn;\n    }\n\n\n\nsame code can also be written as shown below using Math.max and Math.min functions.\n\n\n    public int maximumDifference(int[] nums) {\n       int minn=nums[0];\n        int maxn=-1;\n        for(int i=0;i<nums.length;i++){\n            \n\t\t\tminn=Math.min(minn,nums[i]);\n            if(minn<nums[i])\n            maxn=Math.max(maxn,nums[i]-minn);\n            \n\t\t\t}return maxn;\n    }\n
['Java']