Sort by

recency

|

2489 Discussions

|

  • + 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)
    
  • + 0 comments
    function arrayManipulation(n, queries) {
        // Write your code here
        const arr = new Array(n).fill(0);
        let max = 0;
        
        for(let i = 0; i < queries.length; i++) {
            const [a,b,k] = queries[i];
            
            // -1 because arrays are indexed 0, but a & b constraints 1<=a<=b<=n
            for(let j = a - 1; j <= b - 1 ; j++) {
                arr[j] += k;
    
                max = Math.max(arr[j], max);
            }
        }
        
        return max;
    }
    
  • + 1 comment

    public static long arrayManipulation(int n, List> queries) { long[] arr = new long[n];
    long max = 0;

        for(int i=0; i < queries.Count; i++)
        {
            int a = queries[i][0] - 1;
            int b = queries[i][1] - 1;
            int k = queries[i][2];
    
            for(int col=a;col<=b;col++)
            {
                arr[col] += k;
                max = Math.Max(max,arr[col]);
            }
        }
    
        return max;
    }