Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
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
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes