Cpp & Java Code || 2 - Approach with simple explanation || O(n) time & O(1)space - optimized sol
Approach - 1 --> Time Complexity O(n) and Space Complexity O(n)\n\nStep-1 : Assign 1 candy to each children i.e in left and right array .\nStep-2 : Create a lef
**Approach - 1 --> Time Complexity O(n) and Space Complexity O(n)**\n\n**Step-1** : Assign 1 candy to each children i.e in left and right array .\n**Step-2** : Create a left array , that make sure to have a candy greater than left element only.\n                  if(arr [ i  ] > arr [ i - 1  ]   )  then left [ i ] = left [ i - 1 ] + 1;\n**Step-3** : Create a right array , that make sure to have a candy greater than right element only.\n\t\t\t\t if( arr [ i ] > arr [ i + 1] ) then right [ i ] = right [ i +1 ] + 1;\n**Step-4** : Calculate max of ( left[i] , right[i] )\n\nE.g :\n3    8    4   5   7  9\n1    1    1   1   1   1    --->  left array assigned with 1\n1    2    1   2   3   4   ---->  step-2\n1    1    1   1   1   1   ---> right array assigned with 1\n1    2    1   1   1   1   ----> step-3\ncalculate max of step 2 and step 3\n1    2    1   2   3   4   ----------------------> ANSWER = 1 + 2+ 1+ 2+3 +4 = 13\n\n**Note : You can also do it in a two loop with single extra array. **\n\n\n**Approach - 2 : We can apply the approach of valley and peak problem.**\npeek will have maximum candy while valley have 1 candy.\nInitialize candy_count = no. of childern ( bcz each children must have 1 candy )\nStart the looping from 1 to arr.size() -1\nstep -1 : if current value is equal to previous value then they have same number of Candy.\nstep -2 : if we have increasing slope , we increase the peak value by one for each increasing element \n``` while( arr [ i ] > arr [ i - 1 ] ) peak++;``` and add count_candy with peak value.\nstep - 3 : if we have decreasing slope , we increase the value of valley by one for each decreasing element\n```while ( arr [ i ] < arr [ i - 1 ] ) valley++``` and add count_candy with valley value.\nstep -4 : count_candy = count_candy - min ( peak , valley )\n\n**J A V A     S O L U T I O N**\n```\nclass Solution {\n    public int candy(int[] ratings) { \n        int peak = 0 , valley = 0 , size = ratings.length,candy_count = size ;\n        for(int i = 1 ; i< size ;){\n            if(ratings[i] == ratings[i-1]){ i++ ; continue;}\n            peak = 0 ;\n            valley = 0;\n            while( i< size && ratings[i] > ratings[i-1]){\n                peak++;\n                i++;\n                candy_count += peak;\n            }\n               \n            \n            while(i<size && ratings[i] < ratings[i-1]){\n                valley++;\n                i++;\n                candy_count += valley;\n            }\n               \n            candy_count -= Math.min(peak,valley);\n        }\n        return candy_count;\n    }\n}\n```\n**C P P  S O L U T I O N**\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        int peak = 0 , valley = 0 , size = ratings.size(),candy_count = size ;\n        for(int i = 1 ; i< size ;){\n            if(ratings[i] == ratings[i-1]){ i++ ; continue;}\n            peak = 0 ;\n            valley = 0;\n            while( i< size && ratings[i] > ratings[i-1]){\n                peak++;\n                i++;\n                candy_count += peak;\n            }\n               \n            \n            while(i<size && ratings[i] < ratings[i-1]){\n                valley++;\n                i++;\n                candy_count += valley;\n            }\n               \n            candy_count -= min(peak,valley);\n        }\n        return candy_count;\n    }\n};\n```\n\n**Upvote if you found it helpful**\n**
['C++', 'Java']