We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Java solution with O(n) time complexity and O(1) space complexity:
publicstaticStringbalancedSums(List<Integer>arr){// goal: find element of arr where sum of elements to left equals sum of elements to the rightif(arr.size()==1)return"YES";//left = 0 = rightinttotalSum=arr.stream().mapToInt(Integer::intValue).sum();intleftSum=0;for(inti=0;i<arr.size();i++){intrightSum=totalSum-leftSum-arr.get(i);if(leftSum==rightSum)return"YES";leftSum+=arr.get(i);}return"NO";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Array
You are viewing a single comment's thread. Return to all comments →
My Java solution with O(n) time complexity and O(1) space complexity: