Sort by

recency

|

2479 Discussions

|

  • + 0 comments

    bit ugly but it does the job in scala (60)

    trick is to avoid inner loops for the queries and instead ADD value at the start index and SUBTRACT it at the end index + 1 (due to inclusive bounds)

    also make sure to return long

        def arrayManipulation(n: Int, queries: Array[Array[Int]]): Long = {
          val arr = Array.fill(n)(0)
    
          for (query <- queries) {
            val start = query(0) - 1
            val _end = query(1)
            val value = query(2)
    
            
            arr(start) = arr(start) + value
            if (_end != n) {
              arr(_end) = arr(_end) - value
            }
          }
          var res = 0L
          var run_res = 0L
          for (i <- 0 until n by 1) {
            run_res = run_res + arr(i)
            if (run_res > res){
              res = run_res
            }
          }
    					
    			
          res
        }
    
  • + 0 comments

    Here's my approch

    long arrayManipulation(int n, vector> queries) { vector arr(n,0); long a=0,max=0; long value,start,end;

    for(int i=0; imax)max=arr[j]; } } return max; }

  • + 0 comments

    My Javascript solution:

    function arrayManipulation(n, queries) {
        // creating an array with the values needed to form the result array
        let sumArray = Array(n + 1).fill(0)
        queries.forEach(q => {
            let a = q[0] - 1
            let b = q[1]
            let k = q[2]
            
            sumArray[a] += k
            sumArray[b] -= k
        })
        
        // actually creating the result array
        let currentValue = 0
        sumArray.forEach((s, i) => {
            currentValue += s
            sumArray[i] = currentValue
        })
        
        // extracting the maximum value
        let maxValue = 0
        sumArray.forEach(s => {
            if(s > maxValue) {
                maxValue = s
            }
        })
        
        return maxValue
    }
    

    The key point to understand from this solution is that the goal is to reduce the big O, and the way we do this is by avoiding the most logical way to approach this problem, which could be a nested loop. For example, we could loop through the queries array and then, inside it, loop n times to perform each operation in order to form the final array. But this would result in O(mn) time complexity.

    Now, it's worth noticing that it doesn't really matter how many loops we go through, as long as they're not nested.

    That's why in my solution, I don't iterate the queries array in order to form the final array, but instead, I go through it to form an array that allows me to form the final array by iterating it afterwards.

    Let me give you an example. Let's say that I get this input: n **= 5 **a b k 2 4 2

    Then, my resulting array after iterating the queries array would be: [0, 2, 0, 0, -2, 0] (remember that my array size is n+1 to store the last substraction, but is not relevant)

    That doesn't tell us much at first glance, but if we iterate that array assigning the value of the summatory to each position, the result array would be: [0, 2, 2, 2, 0, 0]

    Here's how: 0 0 + 2 = 2 2 + 0 = 2 2 + 0 = 2 2 + (-2) = 0 0 + 0 = 0 // this last element is just for calculation purposes

    And... That's how we got the resultant array without nested loops!

    Once I have the resultant array I just need to find the Max value of it.

    As you can see I used 3 loops which results in O(a + b + c) but as you know, we dont care about less dominant terms so that can be reduced to O(n).

    Hope this explanation helps to understand the solution.

  • + 0 comments

    My Java 8 Solution;

    public static long arrayManipulation(int n, List<List<Integer>> queries) {
        long[] arr = new long[n + 2]; 
    
        for (List<Integer> query : queries) {
            int a = query.get(0);
            int b = query.get(1);
            int k = query.get(2);
    
            arr[a] += k;
            arr[b + 1] -= k;
        }
    
        long max = 0;
        long current = 0;
        for (int i = 1; i <= n; i++) {
            current += arr[i];
            if (current > max) {
                max = current;
            }
        }
    
        return max;
        }
    
  • + 1 comment

    long arrayManipulation(int n, vector> queries) {

       long long int** arr = new long long int*[queries.size()+1];
    
    // Step 2: Allocate each row with 'cols' elements
    for (long int i = 0; i < queries.size()+1; ++i) {
        arr[i] = new long long int[n];
    
        // Optional: initialize all elements to 0
        for (long long int j = 0; j < n; ++j) {
            arr[i][j] = 0;
        }
    }
    
            long long int l =1;
    
            long long int maxElement =0;
           // cout<<n << queries.size();
    
        for (long long int i = 0; i < queries.size(); ++i) 
        {
    
                long long int indexI = queries[i][0]-1;
                long long int indexJ = queries[i][1]-1;
                long long int value = queries[i][2];
    
    
               for(long long int m = 0 ; m < indexI ; ++m)
               {
                        arr[l][m] = arr[l-1][m];
               }
                for(long long int m =indexI ; m <= indexJ ; m++)
                {
                         arr[l][m] = value + arr[l-1][m];
    
                        if(arr[l][m] >= maxElement)
                        {
                          maxElement =  arr[l][m];
                        }
                }
    
                for(long long int m = indexJ +1 ; m < n ; ++m)
               {
                        arr[l][m] = arr[l-1][m];
               }
    
                l++;
    
        }
    
    
                for(long long int i = 0 ; i < queries.size() +1  ; i++)
                {
    
                    for(long long int j = 0 ; j < n ; j++)
                    {
                        cout<<arr[i][j] << " ";
                    }
    
                    cout<<std::endl;
                }
    
    return  maxElement;
    

    }