Array Manipulation

Sort by

recency

|

25 Discussions

|

  • + 0 comments

    Java 8 - This solution does require a sort and then a full pass through the queries, but the memory overhead is less and still meet so the time requirments.

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            long result = Integer.MIN_VALUE;
            
            queries.sort((q1,q2)->Integer.compare(q1.get(0), q2.get(0)));
            
            PriorityQueue<List<Integer>> expireQueue = new PriorityQueue<>(
                (q1,q2)->Integer.compare(q1.get(1), q2.get(1))
            ) ;
            long runningTotal = 0;
            
            for (List<Integer> query: queries) {
                int idx = query.get(0);            
                expireQueue.offer(query);
                
                runningTotal += query.get(2);
                
                while(expireQueue.peek().get(1)<idx) 
                    runningTotal -= expireQueue.poll().get(2) ;
                
                if (runningTotal>result)
                    result = runningTotal;
            }
            return result ;
        }
    
  • + 0 comments

    here is hackerrank array manipulation problem solution in Python Java c++ c and javascript

  • + 0 comments

    Java

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            Map<Integer, Long> sum = new TreeMap<>(); // order by key
            for (List<Integer> list : queries) {
                int a = list.get(0);
                int b = list.get(1);
                long k = list.get(2);
                sum.put(a, sum.getOrDefault(a, 0L) + k);
                sum.put(b + 1, sum.getOrDefault(b + 1, 0L) - k);
            }
    
            long max = 0L;
            long current = 0L;
            for (long value : sum.values()) {
                current += value;
                max = Math.max(max, current);
            }
            return max;
    
        }
    
  • + 0 comments
    def arrayManipulation(n, queries):
        """Finds the maximum value in an array after multiple range updates.
    
        Uses a difference array to efficiently track changes.
    
        Args:
            n (int): Array size.
            queries (list of list): List of [start, end, value] updates.
    
        Returns:
            int: Maximum value after all updates.
        """
    
        diff = [0] * (n + 2)  # Difference array (1-indexed with extra cell)
    
        for start, end, value in queries:
            diff[start] += value  # Increase at start
            diff[end + 1] -= value  # Cancel effect after end
    
        max_num = 0  # Track maximum value
        curr_sum = 0  # Track current prefix sum
        for i in range(1, n + 1):
            if diff[i] == 0:  # Skip if no change
                continue
            curr_sum += diff[i]  # Update prefix sum
            max_num = max(max_num, curr_sum)  # Update maximum
    
        return max_num
    
  • + 0 comments

    Single Pass O(N) Functional Sol'n

    def arrayManipulation(n: Int, queries: Array[Array[Int]]): Long = 
    
      queries.foldLeft(new Array[Long](n+1)) { 
        case (darr,q) => q match {
          case Array(start, end, value) =>
            darr(start - 1) += value
            darr(end) -= value  
            darr
        }}.scanLeft(0L)(_+_).max
    

    Understand the Difference Array Trick - super simple and effective!

    1. Make arr size n+1 so we can add to end if its the Last item (or ignore)

    2. only Mark the Starts with +query and mark the end+1 index with a -ve value [100, 500 , 0 , 200 , -100 , -500 , 0 , -200 ]

    3. Then simply compute the Prefix Array in a Single Pass