Sherlock and Array

Sort by

recency

|

60 Discussions

|

  • + 0 comments
    public static String balancedSums(List<Integer> arr) {
    // Write your code here
        long total = 0l;
        for(Integer i : arr){
            total = total + i;
        }
        long leftSum =0L;
        long righSum = 0L;
        for(int i= 0; i < arr.size(); i++){
            righSum = total - leftSum - arr.get(i);
            if(righSum == leftSum){
                return "YES";
            }
            leftSum = leftSum + arr.get(i);
        }
        return "NO";
    }
    
  • + 0 comments

    Java solution using only one loop.

    Time complexity: O(n)

    Space complexity: O(1)

    I found a test case that HackerRank ignores: inputs like "0000...a" should return "YES", but they incorrectly accept "NO" as a valid result.

    public static String balancedSums(List<Integer> arr) {
            int n = arr.size();
            arr.add(0,0);
            arr.add(0);
            int i = 0;
            int j = n+1;
            int cSumL = arr.get(i);
            int cSumR = arr.get(j);
            while(j>=i+2){
                if(cSumL>cSumR){
                    j--;
                    cSumR += arr.get(j);    
                }else if(cSumL<cSumR){
                    i++;
                    cSumL += arr.get(i);
                }else{
                    if(j==i+2){
                        return "YES";
                    }else{
                        if(arr.get(j-1)==0){
                            j--;
                        }else{
                            i++;
                            cSumL += arr.get(i);
                            
                        }
                    }
                }
    
            }
            return "NO";
        }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def sherlock_and_array(arr):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(1)
        total_sum = 0
        for value in arr:
            total_sum += value
    
        left_sum = 0
        for value in arr:
            right_sum = total_sum - left_sum - value
            if left_sum == right_sum:
                return "YES"
            left_sum += value
    
        return "NO"
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn sherlock_and_array(arr: &[i32]) -> String {
    //Time complexity: O(n)
        //Space complexity (ignoring input): O(1)
        let total_sum: i32 = arr.iter().sum();
    
        let mut left_sum = 0;
        for value in arr {
            let right_sum = total_sum - left_sum - value;
            if left_sum == right_sum {
                return "YES".to_string();
            }
            left_sum += value;
        }
    
        "NO".to_string()
    
  • + 0 comments
    #Python 3
    def balancedSums(arr):
        left = 0
        right = sum(arr)
        for val in arr:
            right -=val
            if left == right:
                return "YES"
            left += val
        
        return "NO"