Sort by

recency

|

2471 Discussions

|

  • + 0 comments

    Problem is not properly explained. Needed to get help of ChatGPT to properly understand this problem. @HackerRank Please try to explain problems efficiently.

  • + 1 comment

    Java working solution, with some test cases are failing because of "time limit exceed"

    #########################################################################

    public static long arrayManipulation(int n, List> queries) { int row=queries.size(); int col=queries.get(0).size(); //array to hold final combine result from row:0 to row:n long[] arr = new long[n]; long max=0, currMax=0;

        for(int i=0; i<row; i++) {
            int start=queries.get(i).get(0);
            int end=queries.get(i).get(1);
            int val=queries.get(i).get(2);
    
            for(int j=start-1; j<end; j++) {
                arr[j]=arr[j]+val;
                if(arr[j]>currMax){
                    currMax=arr[j];
                }
            }
            if(currMax>max){
                max=currMax;
            }
        }
        return max;
    }
    
    #
  • + 3 comments

    Javascript. does anyone know how I would make this faster??

    function arrayManipulation(n, queries) {
        let max = 0;
        let arr = Array(n).fill(0);
        
        for (let i=0; i<queries.length; i++){
            const [a,b,k] = queries[i];
            
            for (let j=a-1; j<b; j++){
                arr[j]+=k;
               max = arr[j] > max ? arr[j] : max;
        }
        }
      return max
    }
    
  • + 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;
        }
    
  • + 1 comment

    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))