HackerRank - Array Manipulation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












This is my implementation for this hacker rank problem. (And a follow-up.)



Problem




You are given a list(1-indexed) of size n, initialized with zeroes.
You have to perform m operations on the list and output the maximum of
final values of all the elements in the list. For every operation,
you are given three integers a, b and and you have to add value k to
all the elements ranging from index to (both inclusive).




Example input



5 3
1 2 100
2 5 100
3 4 100


Expected output



200


Explanation



After first update list will be:



100 100 0 0 0


After second update list will be:



100 200 100 100 100


After third update list will be:



100 200 200 200 100


So the required answer will be: 200



First solution



My first attempt I think is quite readable (ignoring the way input and output is handle in hacker rank)



#include <bits/stdc++.h>
#include <algorithm>

using namespace std;

int main()
int n, m;
cin >> n >> m;
vector<long long> v(n);
for(int a0 = 0; a0 < m; a0++)
int start, end, val;
cin >> start >> end >> val;
auto it_start = v.begin()+(start-1);
auto it_end = v.begin()+(end);
transform(it_start, it_end , it_start, [k](long long &x)return x+=k;);

cout << *max_element(v.begin(),v.end());
return 0;



Final solution



But this approach, although readable, is too slow for what it is actually being ask, which is the maximum value that it would be achieved. So I wrote this, which passed the tests, but which is more difficult to understand in my opinion.



#include <algorithm>
#include <vector>
#include <iostream>

int main()
int n; int m;
std::cin >> n >> m;
using val_type = long long;
std::vector<val_type> v(n);
while(m--)
val_type start, end, val;
std::cin >> start >> end >> val;
auto it_start = v.begin()+(start-1);
auto it_end = v.begin()+ end;
*it_start += val;
*it_end -= val;

val_type max0;
auto accumulate_max_val = [x=val_type(0),&max](val_type y) mutablex+=y; if (x>max) max=x;;
std::for_each(v.begin(),v.end(),accumulate_max_val);
std::cout << max;
return 0;



What would you do to improve it? Am I using lambdas and the for_each appropriately, or is there a clearer way to express what I want to do?







share|improve this question



























    up vote
    2
    down vote

    favorite












    This is my implementation for this hacker rank problem. (And a follow-up.)



    Problem




    You are given a list(1-indexed) of size n, initialized with zeroes.
    You have to perform m operations on the list and output the maximum of
    final values of all the elements in the list. For every operation,
    you are given three integers a, b and and you have to add value k to
    all the elements ranging from index to (both inclusive).




    Example input



    5 3
    1 2 100
    2 5 100
    3 4 100


    Expected output



    200


    Explanation



    After first update list will be:



    100 100 0 0 0


    After second update list will be:



    100 200 100 100 100


    After third update list will be:



    100 200 200 200 100


    So the required answer will be: 200



    First solution



    My first attempt I think is quite readable (ignoring the way input and output is handle in hacker rank)



    #include <bits/stdc++.h>
    #include <algorithm>

    using namespace std;

    int main()
    int n, m;
    cin >> n >> m;
    vector<long long> v(n);
    for(int a0 = 0; a0 < m; a0++)
    int start, end, val;
    cin >> start >> end >> val;
    auto it_start = v.begin()+(start-1);
    auto it_end = v.begin()+(end);
    transform(it_start, it_end , it_start, [k](long long &x)return x+=k;);

    cout << *max_element(v.begin(),v.end());
    return 0;



    Final solution



    But this approach, although readable, is too slow for what it is actually being ask, which is the maximum value that it would be achieved. So I wrote this, which passed the tests, but which is more difficult to understand in my opinion.



    #include <algorithm>
    #include <vector>
    #include <iostream>

    int main()
    int n; int m;
    std::cin >> n >> m;
    using val_type = long long;
    std::vector<val_type> v(n);
    while(m--)
    val_type start, end, val;
    std::cin >> start >> end >> val;
    auto it_start = v.begin()+(start-1);
    auto it_end = v.begin()+ end;
    *it_start += val;
    *it_end -= val;

    val_type max0;
    auto accumulate_max_val = [x=val_type(0),&max](val_type y) mutablex+=y; if (x>max) max=x;;
    std::for_each(v.begin(),v.end(),accumulate_max_val);
    std::cout << max;
    return 0;



    What would you do to improve it? Am I using lambdas and the for_each appropriately, or is there a clearer way to express what I want to do?







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      This is my implementation for this hacker rank problem. (And a follow-up.)



      Problem




      You are given a list(1-indexed) of size n, initialized with zeroes.
      You have to perform m operations on the list and output the maximum of
      final values of all the elements in the list. For every operation,
      you are given three integers a, b and and you have to add value k to
      all the elements ranging from index to (both inclusive).




      Example input



      5 3
      1 2 100
      2 5 100
      3 4 100


      Expected output



      200


      Explanation



      After first update list will be:



      100 100 0 0 0


      After second update list will be:



      100 200 100 100 100


      After third update list will be:



      100 200 200 200 100


      So the required answer will be: 200



      First solution



      My first attempt I think is quite readable (ignoring the way input and output is handle in hacker rank)



      #include <bits/stdc++.h>
      #include <algorithm>

      using namespace std;

      int main()
      int n, m;
      cin >> n >> m;
      vector<long long> v(n);
      for(int a0 = 0; a0 < m; a0++)
      int start, end, val;
      cin >> start >> end >> val;
      auto it_start = v.begin()+(start-1);
      auto it_end = v.begin()+(end);
      transform(it_start, it_end , it_start, [k](long long &x)return x+=k;);

      cout << *max_element(v.begin(),v.end());
      return 0;



      Final solution



      But this approach, although readable, is too slow for what it is actually being ask, which is the maximum value that it would be achieved. So I wrote this, which passed the tests, but which is more difficult to understand in my opinion.



      #include <algorithm>
      #include <vector>
      #include <iostream>

      int main()
      int n; int m;
      std::cin >> n >> m;
      using val_type = long long;
      std::vector<val_type> v(n);
      while(m--)
      val_type start, end, val;
      std::cin >> start >> end >> val;
      auto it_start = v.begin()+(start-1);
      auto it_end = v.begin()+ end;
      *it_start += val;
      *it_end -= val;

      val_type max0;
      auto accumulate_max_val = [x=val_type(0),&max](val_type y) mutablex+=y; if (x>max) max=x;;
      std::for_each(v.begin(),v.end(),accumulate_max_val);
      std::cout << max;
      return 0;



      What would you do to improve it? Am I using lambdas and the for_each appropriately, or is there a clearer way to express what I want to do?







      share|improve this question













      This is my implementation for this hacker rank problem. (And a follow-up.)



      Problem




      You are given a list(1-indexed) of size n, initialized with zeroes.
      You have to perform m operations on the list and output the maximum of
      final values of all the elements in the list. For every operation,
      you are given three integers a, b and and you have to add value k to
      all the elements ranging from index to (both inclusive).




      Example input



      5 3
      1 2 100
      2 5 100
      3 4 100


      Expected output



      200


      Explanation



      After first update list will be:



      100 100 0 0 0


      After second update list will be:



      100 200 100 100 100


      After third update list will be:



      100 200 200 200 100


      So the required answer will be: 200



      First solution



      My first attempt I think is quite readable (ignoring the way input and output is handle in hacker rank)



      #include <bits/stdc++.h>
      #include <algorithm>

      using namespace std;

      int main()
      int n, m;
      cin >> n >> m;
      vector<long long> v(n);
      for(int a0 = 0; a0 < m; a0++)
      int start, end, val;
      cin >> start >> end >> val;
      auto it_start = v.begin()+(start-1);
      auto it_end = v.begin()+(end);
      transform(it_start, it_end , it_start, [k](long long &x)return x+=k;);

      cout << *max_element(v.begin(),v.end());
      return 0;



      Final solution



      But this approach, although readable, is too slow for what it is actually being ask, which is the maximum value that it would be achieved. So I wrote this, which passed the tests, but which is more difficult to understand in my opinion.



      #include <algorithm>
      #include <vector>
      #include <iostream>

      int main()
      int n; int m;
      std::cin >> n >> m;
      using val_type = long long;
      std::vector<val_type> v(n);
      while(m--)
      val_type start, end, val;
      std::cin >> start >> end >> val;
      auto it_start = v.begin()+(start-1);
      auto it_end = v.begin()+ end;
      *it_start += val;
      *it_end -= val;

      val_type max0;
      auto accumulate_max_val = [x=val_type(0),&max](val_type y) mutablex+=y; if (x>max) max=x;;
      std::for_each(v.begin(),v.end(),accumulate_max_val);
      std::cout << max;
      return 0;



      What would you do to improve it? Am I using lambdas and the for_each appropriately, or is there a clearer way to express what I want to do?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Aug 13 at 18:41









      Deduplicator

      9,9071844




      9,9071844









      asked Jan 17 at 15:04









      WooWapDaBug

      348214




      348214




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          • Kudos for figuring out the correct algorithm.



            However you can streamline it by not using a v vector:



            You correctly treated an operation a, b, k as a pair of operations: add k from a to the end, and subtract k from b+1 to the end. Now, instead of storing them in v, collect decoupled operations in a vector of their own. Sort it by index. std::partial_sum it, and find the maximum in the resulting array.



            This will drive the space complexity down from $O(n)$ to $O(m)$, and change the time complexity from $O(n+m)$ to $O(mlog m)$. According to constraints, the time complexity seems to be better. One should also keep in mind that accesses to v could be all over the place with no particular order, and a well crafted sequence of operations may incur too many cache misses. I didn't profile though.



          • It is possible that spelling the loop out (rather than using for_each and lambda) would improve readability.


          • The algorithm would fail if k was allowed to be negative. Even it is not the case, it still is a good habit to initialize max and x to v[0], and start the loop at v.begin() + 1.






          share|improve this answer























          • Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
            – WooWapDaBug
            Jan 18 at 7:35










          • @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
            – vnp
            Jan 18 at 8:24











          • Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
            – WooWapDaBug
            Jan 18 at 8:26

















          up vote
          0
          down vote













          What I recommend to improve to author - is to move calculation out of IO routines and make reusable function similar to listed below. Below is a Swift version which looks a bit different due Swift syntax. But C/C++ code can be nearly the same with the difference of array allocation.




          Swift 4 version.



          According to documentation for Array data type the complexity is O(1).




          Reading an element from an array is $O(1)$. Writing is $O(1)$ unless the
          array’s storage is shared with another array, in which case writing is
          O(n), where n is the length of the array.




          See: The Apple documentation for subscript()



          So, first we can safely allocate array of needed size. Then we can store intermediate sums at provided boundaries. In second part of calculation we finding maximum by summing intermediate values.



          func arrayManipulation(n: Int, queries: [[Int]]) -> Int 

          var result = Array(repeating: 0, count: n)

          for query in queries
          let left = query[0] - 1
          let right = query[1] - 1
          let amount = query[2]
          result[left] += amount
          if (right + 1) < n
          result[right + 1] -= amount



          var max = 0
          var x = 0
          for i in 0 ..< n
          x += result[i]
          if max < x
          max = x



          return max






          share|improve this answer



















          • 3




            Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
            – Toby Speight
            Aug 13 at 15:50










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185320%2fhackerrank-array-manipulation%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted











          • Kudos for figuring out the correct algorithm.



            However you can streamline it by not using a v vector:



            You correctly treated an operation a, b, k as a pair of operations: add k from a to the end, and subtract k from b+1 to the end. Now, instead of storing them in v, collect decoupled operations in a vector of their own. Sort it by index. std::partial_sum it, and find the maximum in the resulting array.



            This will drive the space complexity down from $O(n)$ to $O(m)$, and change the time complexity from $O(n+m)$ to $O(mlog m)$. According to constraints, the time complexity seems to be better. One should also keep in mind that accesses to v could be all over the place with no particular order, and a well crafted sequence of operations may incur too many cache misses. I didn't profile though.



          • It is possible that spelling the loop out (rather than using for_each and lambda) would improve readability.


          • The algorithm would fail if k was allowed to be negative. Even it is not the case, it still is a good habit to initialize max and x to v[0], and start the loop at v.begin() + 1.






          share|improve this answer























          • Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
            – WooWapDaBug
            Jan 18 at 7:35










          • @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
            – vnp
            Jan 18 at 8:24











          • Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
            – WooWapDaBug
            Jan 18 at 8:26














          up vote
          2
          down vote



          accepted











          • Kudos for figuring out the correct algorithm.



            However you can streamline it by not using a v vector:



            You correctly treated an operation a, b, k as a pair of operations: add k from a to the end, and subtract k from b+1 to the end. Now, instead of storing them in v, collect decoupled operations in a vector of their own. Sort it by index. std::partial_sum it, and find the maximum in the resulting array.



            This will drive the space complexity down from $O(n)$ to $O(m)$, and change the time complexity from $O(n+m)$ to $O(mlog m)$. According to constraints, the time complexity seems to be better. One should also keep in mind that accesses to v could be all over the place with no particular order, and a well crafted sequence of operations may incur too many cache misses. I didn't profile though.



          • It is possible that spelling the loop out (rather than using for_each and lambda) would improve readability.


          • The algorithm would fail if k was allowed to be negative. Even it is not the case, it still is a good habit to initialize max and x to v[0], and start the loop at v.begin() + 1.






          share|improve this answer























          • Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
            – WooWapDaBug
            Jan 18 at 7:35










          • @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
            – vnp
            Jan 18 at 8:24











          • Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
            – WooWapDaBug
            Jan 18 at 8:26












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted







          • Kudos for figuring out the correct algorithm.



            However you can streamline it by not using a v vector:



            You correctly treated an operation a, b, k as a pair of operations: add k from a to the end, and subtract k from b+1 to the end. Now, instead of storing them in v, collect decoupled operations in a vector of their own. Sort it by index. std::partial_sum it, and find the maximum in the resulting array.



            This will drive the space complexity down from $O(n)$ to $O(m)$, and change the time complexity from $O(n+m)$ to $O(mlog m)$. According to constraints, the time complexity seems to be better. One should also keep in mind that accesses to v could be all over the place with no particular order, and a well crafted sequence of operations may incur too many cache misses. I didn't profile though.



          • It is possible that spelling the loop out (rather than using for_each and lambda) would improve readability.


          • The algorithm would fail if k was allowed to be negative. Even it is not the case, it still is a good habit to initialize max and x to v[0], and start the loop at v.begin() + 1.






          share|improve this answer
















          • Kudos for figuring out the correct algorithm.



            However you can streamline it by not using a v vector:



            You correctly treated an operation a, b, k as a pair of operations: add k from a to the end, and subtract k from b+1 to the end. Now, instead of storing them in v, collect decoupled operations in a vector of their own. Sort it by index. std::partial_sum it, and find the maximum in the resulting array.



            This will drive the space complexity down from $O(n)$ to $O(m)$, and change the time complexity from $O(n+m)$ to $O(mlog m)$. According to constraints, the time complexity seems to be better. One should also keep in mind that accesses to v could be all over the place with no particular order, and a well crafted sequence of operations may incur too many cache misses. I didn't profile though.



          • It is possible that spelling the loop out (rather than using for_each and lambda) would improve readability.


          • The algorithm would fail if k was allowed to be negative. Even it is not the case, it still is a good habit to initialize max and x to v[0], and start the loop at v.begin() + 1.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jan 17 at 20:57


























          answered Jan 17 at 20:48









          vnp

          36.6k12991




          36.6k12991











          • Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
            – WooWapDaBug
            Jan 18 at 7:35










          • @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
            – vnp
            Jan 18 at 8:24











          • Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
            – WooWapDaBug
            Jan 18 at 8:26
















          • Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
            – WooWapDaBug
            Jan 18 at 7:35










          • @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
            – vnp
            Jan 18 at 8:24











          • Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
            – WooWapDaBug
            Jan 18 at 8:26















          Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
          – WooWapDaBug
          Jan 18 at 7:35




          Thank you for your feedback. How do you know this is the "correct" algorithm? And how do you compute the time complexity to know that with your proposed method is O(m log m)? I thought your suggestions could be implemented with a map, I don't know if I understood right what you are trying to tell me.
          – WooWapDaBug
          Jan 18 at 7:35












          @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
          – vnp
          Jan 18 at 8:24





          @WooWapDaBug It is hard to answer how. It feels right (call it experience). It doesn't brute force. It passes the test cases, which makes it right. $O(mlog m)$ comes from the sorting. I don't think map may change the bottomline: if the map gives better performance, it would mean you can sort faster.
          – vnp
          Jan 18 at 8:24













          Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
          – WooWapDaBug
          Jan 18 at 8:26




          Oh, I didn't mean it would change anything, I just thought it was an easy way to implement the sorting + indexing in just one small change
          – WooWapDaBug
          Jan 18 at 8:26












          up vote
          0
          down vote













          What I recommend to improve to author - is to move calculation out of IO routines and make reusable function similar to listed below. Below is a Swift version which looks a bit different due Swift syntax. But C/C++ code can be nearly the same with the difference of array allocation.




          Swift 4 version.



          According to documentation for Array data type the complexity is O(1).




          Reading an element from an array is $O(1)$. Writing is $O(1)$ unless the
          array’s storage is shared with another array, in which case writing is
          O(n), where n is the length of the array.




          See: The Apple documentation for subscript()



          So, first we can safely allocate array of needed size. Then we can store intermediate sums at provided boundaries. In second part of calculation we finding maximum by summing intermediate values.



          func arrayManipulation(n: Int, queries: [[Int]]) -> Int 

          var result = Array(repeating: 0, count: n)

          for query in queries
          let left = query[0] - 1
          let right = query[1] - 1
          let amount = query[2]
          result[left] += amount
          if (right + 1) < n
          result[right + 1] -= amount



          var max = 0
          var x = 0
          for i in 0 ..< n
          x += result[i]
          if max < x
          max = x



          return max






          share|improve this answer



















          • 3




            Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
            – Toby Speight
            Aug 13 at 15:50














          up vote
          0
          down vote













          What I recommend to improve to author - is to move calculation out of IO routines and make reusable function similar to listed below. Below is a Swift version which looks a bit different due Swift syntax. But C/C++ code can be nearly the same with the difference of array allocation.




          Swift 4 version.



          According to documentation for Array data type the complexity is O(1).




          Reading an element from an array is $O(1)$. Writing is $O(1)$ unless the
          array’s storage is shared with another array, in which case writing is
          O(n), where n is the length of the array.




          See: The Apple documentation for subscript()



          So, first we can safely allocate array of needed size. Then we can store intermediate sums at provided boundaries. In second part of calculation we finding maximum by summing intermediate values.



          func arrayManipulation(n: Int, queries: [[Int]]) -> Int 

          var result = Array(repeating: 0, count: n)

          for query in queries
          let left = query[0] - 1
          let right = query[1] - 1
          let amount = query[2]
          result[left] += amount
          if (right + 1) < n
          result[right + 1] -= amount



          var max = 0
          var x = 0
          for i in 0 ..< n
          x += result[i]
          if max < x
          max = x



          return max






          share|improve this answer



















          • 3




            Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
            – Toby Speight
            Aug 13 at 15:50












          up vote
          0
          down vote










          up vote
          0
          down vote









          What I recommend to improve to author - is to move calculation out of IO routines and make reusable function similar to listed below. Below is a Swift version which looks a bit different due Swift syntax. But C/C++ code can be nearly the same with the difference of array allocation.




          Swift 4 version.



          According to documentation for Array data type the complexity is O(1).




          Reading an element from an array is $O(1)$. Writing is $O(1)$ unless the
          array’s storage is shared with another array, in which case writing is
          O(n), where n is the length of the array.




          See: The Apple documentation for subscript()



          So, first we can safely allocate array of needed size. Then we can store intermediate sums at provided boundaries. In second part of calculation we finding maximum by summing intermediate values.



          func arrayManipulation(n: Int, queries: [[Int]]) -> Int 

          var result = Array(repeating: 0, count: n)

          for query in queries
          let left = query[0] - 1
          let right = query[1] - 1
          let amount = query[2]
          result[left] += amount
          if (right + 1) < n
          result[right + 1] -= amount



          var max = 0
          var x = 0
          for i in 0 ..< n
          x += result[i]
          if max < x
          max = x



          return max






          share|improve this answer















          What I recommend to improve to author - is to move calculation out of IO routines and make reusable function similar to listed below. Below is a Swift version which looks a bit different due Swift syntax. But C/C++ code can be nearly the same with the difference of array allocation.




          Swift 4 version.



          According to documentation for Array data type the complexity is O(1).




          Reading an element from an array is $O(1)$. Writing is $O(1)$ unless the
          array’s storage is shared with another array, in which case writing is
          O(n), where n is the length of the array.




          See: The Apple documentation for subscript()



          So, first we can safely allocate array of needed size. Then we can store intermediate sums at provided boundaries. In second part of calculation we finding maximum by summing intermediate values.



          func arrayManipulation(n: Int, queries: [[Int]]) -> Int 

          var result = Array(repeating: 0, count: n)

          for query in queries
          let left = query[0] - 1
          let right = query[1] - 1
          let amount = query[2]
          result[left] += amount
          if (right + 1) < n
          result[right + 1] -= amount



          var max = 0
          var x = 0
          for i in 0 ..< n
          x += result[i]
          if max < x
          max = x



          return max







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Aug 13 at 16:54









          Sam Onela

          5,88461545




          5,88461545











          answered Aug 13 at 15:15









          Vlad

          1093




          1093







          • 3




            Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
            – Toby Speight
            Aug 13 at 15:50












          • 3




            Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
            – Toby Speight
            Aug 13 at 15:50







          3




          3




          Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
          – Toby Speight
          Aug 13 at 15:50




          Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original.
          – Toby Speight
          Aug 13 at 15:50












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185320%2fhackerrank-array-manipulation%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?