Sherlock and Array

Sort by

recency

|

55 Discussions

|

  • + 0 comments

    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";
        }
    
  • + 0 comments

    For arr s.t. arr == {1}, the left and right can't add up to zero. Even saying there is a "left" and a "right" is nonsensical. Null + Null != 0.

  • + 0 comments
    def balancedSums(arr):
        # Write your code here
        total_sum = sum(arr)
        right_acc = 0
        for index in range(len(arr)):
            total_sum -= arr[index]
            if (right_acc == total_sum):
                return "YES"
            right_acc += arr[index]
        return "NO"
    
  • + 0 comments

    def balancedSums(arr):

    left = 0
    right = sum(arr)
    for i in range(len(arr)):
        right -= arr[i]
        if left == right:
            return 'YES'
        left += arr[i]
    return 'NO'
    
  • + 0 comments

    TypeScript

    function balancedSums(arr: number[]): string {
      let totalSum = arr.reduce((acc, cur) => acc + cur);
    
      let leftSum = 0;
    
      for (let i = 0; i < arr.length; i++) {
        let rightSum = totalSum - leftSum - arr[i];
    
        if (rightSum === leftSum) return 'YES';
    
        leftSum += arr[i];
      }
    
      return 'NO';
    }