HackerRank Array Manipulation problem solution

In this HackerRank Array Manipulation Interview preparation kit problem solution we have a Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.

HackerRank Array Manipulation Interview preparation kit solution

Problem solution in Python programming.

#!/usr/bin/env python

import itertools, sys


if __name__ == '__main__':
    N, M = list(map(int, sys.stdin.readline().split()))
    x = [0] * N

    for _ in range(M):
        a, b, k = list(map(int, sys.stdin.readline().split()))

        x[a - 1] += k
        if b < N:
            x[b] -= k

    print(max(itertools.accumulate(x)))

Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long size = scanner.nextLong();

        Map<Long, Long> map = new HashMap<>();
        long operations = scanner.nextLong();

        for (long i = 0; i < operations; i++) {
            long start = scanner.nextLong();
            long end = scanner.nextLong();
            long value = scanner.nextLong();

            map.put(start, (map.containsKey(start) ? map.get(start) : 0) + value);
            map.put(end + 1, (map.containsKey(end + 1) ? map.get(end + 1) : 0) - value);
        }

        long max = 0;
        long value = 0;
        for (long i = 0; i < size; i++) {
            value += (map.containsKey(i + 1) ? map.get(i + 1) : 0);
            max = Math.max(max, value);
        }

        System.out.println(max);
    }
}

Problem solution in C++ programming.

#include <iostream>

using namespace std;
const int NMAX = 1e7+2;
long long a[NMAX];
int main()
{
    int n, m;
    cin >> n >> m;
    for(int i=1;i<=m;++i){
        int x, y, k;
        cin >> x >> y >> k;
        a[x] += k;
        a[y+1] -= k;
    }
    long long x = 0,sol=-(1LL<<60);
    for(int i=1;i<=n;++i){
        x += a[i];
        sol = max(sol,x);
    }
    cout<<sol<<"n";
    return 0;
}

Problem solution in C programming.

#include<stdio.h>
long A[10000009]={0},CF[10000009+1]={0};
int main()
{
 
  long N,Q;
  
    
    long val,left,right,i,count=0;
   long maxv=-1;
    scanf("%ld%ld",&N,&Q);
  for(i=0;i<Q;i++)
  {
   scanf("%ld%ld%ld",&left,&right,&val);
   CF[left-1]+=val;
   CF[right]-=val;
  }
    
  for(i=0;i<N;i++)
  {
   count+=CF[i];
   A[i]=count;
      if(count>maxv) maxv=count;
  }
       printf("%ldn",maxv);
 return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
  var splitInput = input.split("n");
  var listSize = parseInt(splitInput[0].split(" ")[0]);
  var numInserts = parseInt(splitInput[0].split(" ")[1]);
  var max = 0;
  var amounts = Array(listSize);

  for (var i = 0; i < numInserts; i++) {
    // input is 1 based
    var start = parseInt(splitInput[i+1].split(" ")[0]) - 1;
    var end = parseInt(splitInput[i+1].split(" ")[1]);
    var amount = parseInt(splitInput[i+1].split(" ")[2]);

    amounts[start] = amounts[start] || 0;
    amounts[start] = amounts[start] + amount;

    amounts[end] = amounts[end] || 0;
    amounts[end] = amounts[end] - amount;
  }
  
  var current = 0;
  for (var i = 0; i < listSize; i++) {
    current += (amounts[i] || 0);
    if (current > max) {
      max = current;
    }
  }

  console.log(max);
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

1 thought on “HackerRank Array Manipulation problem solution”

Comments are closed.