Sherlock and Array

Sort by

recency

|

59 Discussions

|

  • + 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"
    
  • + 1 comment

    This codes give the exact output for all test cases ,yet the compiler shows wrong output! Can anyone tell me whats wrong into this code`

    public static String balancedSums(List<Integer> array) {
        // Write your code here
            Integer[] arr=(Integer[])array.toArray(new Integer[array.size()]);
            int[] pref=new int[arr.length];
            pref[0]=arr[0];
            for(int i=1;i<arr.length;i++){
                pref[i]=pref[i-1]+arr[i];
            } 
            for(int i=0;i<arr.length;i++){
                if((pref[i]-arr[i])==pref[arr.length-1]-pref[i]){
                    return "Yes";
                }
            }
            
            return "NO";
        }