Sort by

recency

|

2468 Discussions

|

  • + 0 comments

    Java Solution:-

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            // Write your code here
            long max = Long.MIN_VALUE;
            long currentValue = 0L;
    
            long[] list = new long[n];
    
            for (List<Integer> e : queries) {
                final int firstIndex = e.get(0) - 1;
                final int secondIndex = e.get(1) - 1;
                final int value = e.get(2);
                list[firstIndex] += value;
    
                if (secondIndex + 1 < list.length) {
                    list[secondIndex + 1] -= value;
                }
            }
    
            for (Long i : list) {
                currentValue += i;
                if (currentValue > max) {
                    max = currentValue;
                }
            }
            return max;
        }
    
  • + 0 comments

    What is the issue with this code? It takes 3.3 seconds in Windows and 1.3 seconds in Mac but Hackerank shows runtime error for 10 mill n elements

    def arrayManipulation(n, queries):

    temp_arr = [0]*(n+1)
    
    for a,d,k in queries:
        temp_arr[a-1] += k
        temp_arr[d] -=k
    
    for i in range(1,n):
        temp_arr[i] += temp_arr[i-1] 
    
    return(max(temp_arr))
    
  • + 0 comments

    Finding differential zero points to determine the maximum value. Since the exact location is not relevant, the maximum cumulative sum of differential values is sufficient

  • + 0 comments

    here's My solution:

    def arrayManipulation(n, queries):
        arr = [0] * (n )    
        
        for i in queries:
            l=i[0]-1 #made it 0th index
            r=i[1] #let it be 1 indexed
            arr[l]+=i[2]
            if r<n: #so that r doesn't cause out of index error
                arr[r]-=i[2]
                
        
        # Compute prefix sum and track the maximum value
        max_value = 0
        current_sum = 0
        for i in arr:
            current_sum += i
            if current_sum > max_value:
                max_value = current_sum
        
        return max_value
    
  • + 0 comments

    Data structures and arrays are fundamental concepts in computer science, forming the backbone of efficient data storage and manipulation. betbook250 register