Array Manipulation – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Array Manipulation HackerRank Solution
#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;
}
Java Array Manipulation HackerRank Solution
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);
}
}
Python 3 Array Manipulation HackerRank Solution
#!/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)))
JavaScript Array Manipulation HackerRank Solution
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);
});
C Array Manipulation HackerRank Solution
#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("%ld\n",maxv);
return 0;
}
Leave a comment below