# Array Manipulation

# Array Manipulation

+ 0 comments So many engineers struggle with the English language. Writing can take many styles and forms, but at the very least, it should succinctly convey the story in a manner that is digestible. Most of the challenges in HackerRank unfold as though they were written by children. If I were sufficiently incentivized, I would offer my time to review and edit the majority of these ill-defined problem sets.

+ 0 comments This question feels disingenuous.

For people who have trouble understanding it, here's a simplified explanation.

You get 2 variables as input. N (an int) and Queries (a 2D array of ints).N is the size of your base array, you create that.

Queries is a list of operations to perform on the array.

There's 3 numbers in queries. The first is the starting point, the second is the ending point, and the third is the number you add.The idea is you take your base array, then add the third number to the entries in that array between the index of N[Queries[0]] and N[Queries[1]].

What feels disingenuous is you can't actually do that. Writing the algorithm in the way they explain will give you the correct results, but between tests 7 and 13 you'll end up failing to execute fast enough.

There's a different method you'll have to use (you can google it). This question wouldn't be half as difficult if they didn't mislead you in the explanation.

+ 0 comments guys, my code is producing a segmentation fault using the following funciton. Please help me, not sure what i'm doing wrong.

` long arrayManipulation(long n, vector> queries, long m) {

`long i, j, result[n], max = INT_MIN; for(i = 0; i < n; i++) { result[i] = 0; } for(i = 0; i < m; i++) { for(j = queries[i][0] - 1; j <= queries[i][1] - 1; j++) { //result[j] = 0; result[j] = result[j] + queries[i][2]; if(result[j] > max) { max = result[j]; } } } return max;`

} `

+ 0 comments Using Prefix Sum :)

#include <bits/stdc++.h> using namespace std; const int N = 1e7+10; long long int arr[N]; int main(){ int n, q; cin >> n>> q; while(q--){ int a, b, k; cin >>a >>b >>k; arr[a]=arr[a]+k; arr[b+1]=arr[b+1]-k; } for(int i=1; i<=n; i++){ arr[i]=arr[i]+arr[i-1]; } cout << (*max_element(arr,arr+n+1))<< endl; return 0; }

+ 0 comments n,q = input().rstrip().split() zero_list = [ 0 for _ in range(int(n)+2)] for i in range(int(q)): n = list(map(int,input().rstrip().split()))[:3] zero_list[n[0]] += n[2] zero_list[n[1]+1] = zero_list[n[1]+1] - n[2] addition,max_val = 0,0 for i in range(len(zero_list)): addition += zero_list[i] if addition > max_val: max_val = addition print(max_val)

Sort 2182 Discussions, By:

Please Login in order to post a comment