Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Array Manipulation problem solution

YASH PAL, 31 July 2024

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

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes