Minimum Average Difference ( 96.81% of C++ ) - with comments
Intuition: We just have to take average difference and return the index giving minimum average difference.\n\nApproach:\n1) Take outtotalwhich will be sum of al
**Intuition:** We just have to take average difference and return the index giving minimum average difference.\n\n**Approach:**\n1) Take out` total `which will be sum of all n elements and `sum` which will be the sum till ith index.\n2) Calculate the the average till ith index and average till (n-1-i) th index.\n3) Now take absolute diff btw the average till ith index and average till (n-1-i) th index  and keep tracking the index giving minimum diff.\n4) Return the index giving minimum diff.\n\n```\nclass Solution {\npublic:\n    int minimumAverageDifference(vector<int>& nums) {\n       \n\t   //total will store the sum of all the elements of nums\n        long long total=0; \n\t\t//sum will store the sum upto ith index\n        long long sum=0;   \n\t   // mini stores the minimum average difference\n        long long mini=INT_MAX; \n\t\t//ans will store the index of minimum average difference\n        int ans=0; \n        \n        //finding the sum of all the elements of nums\n        for(int i=0;i<nums.size();i++){\n            total+=nums[i];\n        }\n        \n        for(int i=0;i<nums.size();i++){   \n            sum+=nums[i];\n            long long t=total-sum;\n            long long temp=nums.size()-i-1; //number of elements in the\n\t\t\t// last n - i - 1 elements\n            int avg=0;\n            if(temp==0){\n                avg=sum/(i+1); // average of the first i + 1 elements of nums\n            }\n            else{\n            //avg will store the absolute difference between the average of\n\t\t\t//the first i + 1 elements of nums and the average of the\n\t\t\t//last n - i - 1 elements\n               avg=(abs(sum/(i+1) - (t/temp)));\n            }\n            \n            //if mini is greater than the avg then insert avg in mini and \n\t\t\t//store the index in ans\n            if(mini>avg){\n                mini=avg;\n                ans=i;\n            }\n        }\n        \n        //returns the index of minimum average difference\n        return ans;\n    }\n};\n\n```\n\n**Time Complexity = O(n)\nSpace Complexity = O(1)**\n\n\n
['Array', 'C++']