Sort by

recency

|

2491 Discussions

|

  • + 0 comments

    I have used in Prefix method

    long [] arr= new long[n+2];

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

    }

  • + 0 comments

    import sys queries=sys.stdin.read().strip().split() it=iter(queries) n=int(next(it)) q=int(next(it)) arr = [0] * (n+2)

    for i in range(q): a = int(next(it)) b = int(next(it)) k = int(next(it)) arr[ a ] += k arr[ b +1] -= k

    max_value = 0 current = 0 for val in arr: current += val max_value = max(max_value, current)

    print(max_value)

  • + 0 comments

    import java.io.; import java.util.;

    class Result {

    /*
     * Complete the 'arrayManipulation' function below.
     *
     * The function is expected to return a LONG_INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. 2D_INTEGER_ARRAY queries
     */
    
    public static long arrayManipulation(int n, List<List<Integer>> queries) {
        // We use the difference array technique
        long[] arr = new long[n + 2];  // n+2 to avoid index issues
    
        for (List<Integer> query : queries) {
            int a = query.get(0);
            int b = query.get(1);
            int k = query.get(2);
    
            arr[a] += k;      // add k at start index
            arr[b + 1] -= k;  // subtract k after end index
        }
    
        long max = 0;
        long current = 0;
    
        for (int i = 1; i <= n; i++) {
            current += arr[i];
            if (current > max) {
                max = current;
            }
        }
    
        return max;
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
        int n = Integer.parseInt(firstMultipleInput[0]);
        int m = Integer.parseInt(firstMultipleInput[1]);
    
        List<List<Integer>> queries = new ArrayList<>();
    
        for (int i = 0; i < m; i++) {
            String[] queriesRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            List<Integer> queriesRowItems = new ArrayList<>();
    
            for (int j = 0; j < 3; j++) {
                int queriesItem = Integer.parseInt(queriesRowTempItems[j]);
                queriesRowItems.add(queriesItem);
            }
    
            queries.add(queriesRowItems);
        }
    
        long result = Result.arrayManipulation(n, queries);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    You cannot directly sum each item in range because you will get a Time Exceeded Exception.

    Instead, use prefix sum technique:

    // Swift
    func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
        // 1-indexed
        var operations: [Int] = Array(repeating: 0, count: n + 1)
    
        for query in queries {
            let a = query[0]
            let b = query[1]
            let k = query[2]
    
            operations[a - 1] += k
            operations[b] -= k
        }
        
        var maxValue = 0
        var currentSum = 0
        for value in operations {
            currentSum += value
            maxValue = max(maxValue, currentSum)
        }
        
        return maxValue
    }
    
  • + 0 comments

    tricky. Not explained very well.. KEY : prefix sum algorthim

        arr = [0] * (n+2)
    
        for i in range(len(queries)):
            a = queries[i][0]
            b = queries[i][1]
            k = queries[i][2]
            arr[ a ]   += k
            arr[ b +1] -= k
    
        max_value = 0
        current = 0
        for val in arr:
            current += val
            max_value = max(max_value, current)
    
        return (max_value)