Hackerrank “Breaking the Record” solution

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
1
down vote

favorite












I have solved the hacker rank problem listed here. Given a sequence of scores, I need to count the number of times the records for the high and low scores have been broken.



Easiest way for me was to iterate the input for find the answer but i guess that would have meant O(n) complexity, so I thought of solving the problem using divide and conquer approach hoping it would reduce the complexity of the solution.



I have implemented the solution and it is working, but I am not sure whether I can reduce the complexity. Can you please review the code and provide feedback?



private static int minScore, maxScore;

static int breakingRecords(int score)
int firstScore = score[0];
int result = new int[2];
minScore = maxScore = firstScore;
count(score,1, score.length - 1, result);

return result;


private static void count(int scores, int start, int end, int result)






share|improve this question



























    up vote
    1
    down vote

    favorite












    I have solved the hacker rank problem listed here. Given a sequence of scores, I need to count the number of times the records for the high and low scores have been broken.



    Easiest way for me was to iterate the input for find the answer but i guess that would have meant O(n) complexity, so I thought of solving the problem using divide and conquer approach hoping it would reduce the complexity of the solution.



    I have implemented the solution and it is working, but I am not sure whether I can reduce the complexity. Can you please review the code and provide feedback?



    private static int minScore, maxScore;

    static int breakingRecords(int score)
    int firstScore = score[0];
    int result = new int[2];
    minScore = maxScore = firstScore;
    count(score,1, score.length - 1, result);

    return result;


    private static void count(int scores, int start, int end, int result)






    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have solved the hacker rank problem listed here. Given a sequence of scores, I need to count the number of times the records for the high and low scores have been broken.



      Easiest way for me was to iterate the input for find the answer but i guess that would have meant O(n) complexity, so I thought of solving the problem using divide and conquer approach hoping it would reduce the complexity of the solution.



      I have implemented the solution and it is working, but I am not sure whether I can reduce the complexity. Can you please review the code and provide feedback?



      private static int minScore, maxScore;

      static int breakingRecords(int score)
      int firstScore = score[0];
      int result = new int[2];
      minScore = maxScore = firstScore;
      count(score,1, score.length - 1, result);

      return result;


      private static void count(int scores, int start, int end, int result)






      share|improve this question













      I have solved the hacker rank problem listed here. Given a sequence of scores, I need to count the number of times the records for the high and low scores have been broken.



      Easiest way for me was to iterate the input for find the answer but i guess that would have meant O(n) complexity, so I thought of solving the problem using divide and conquer approach hoping it would reduce the complexity of the solution.



      I have implemented the solution and it is working, but I am not sure whether I can reduce the complexity. Can you please review the code and provide feedback?



      private static int minScore, maxScore;

      static int breakingRecords(int score)
      int firstScore = score[0];
      int result = new int[2];
      minScore = maxScore = firstScore;
      count(score,1, score.length - 1, result);

      return result;


      private static void count(int scores, int start, int end, int result)








      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 1 at 13:17









      200_success

      123k14142399




      123k14142399









      asked Mar 1 at 11:00









      Balkrishan Nagpal

      1165




      1165




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You should be consistent with your naming: int score vs. int scores.



          Avoid keeping state in static variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.



          You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.



          The following straightforward loop would be much preferable:



          public static int brokenRecords(int scores) 
          int highScore = scores[0],
          lowScore = scores[0],
          highRecords = 0,
          lowRecords = 0;
          for (int i = 1; i < scores.length; i++)
          if (scores[i] > highScore)
          highScore = scores[i];
          highRecords++;

          if (scores[i] < lowScore)
          lowScore = scores[i];
          lowRecords++;


          return new int highRecords, lowRecords ;






          share|improve this answer























          • Do you mean complexity of solution to this problem can't be less than O(n)?
            – Balkrishan Nagpal
            Mar 1 at 15:33










          • Is it possible to get the answer without considering all n numbers? I think not.
            – 200_success
            Mar 1 at 15:34










          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%2f188591%2fhackerrank-breaking-the-record-solution%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          You should be consistent with your naming: int score vs. int scores.



          Avoid keeping state in static variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.



          You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.



          The following straightforward loop would be much preferable:



          public static int brokenRecords(int scores) 
          int highScore = scores[0],
          lowScore = scores[0],
          highRecords = 0,
          lowRecords = 0;
          for (int i = 1; i < scores.length; i++)
          if (scores[i] > highScore)
          highScore = scores[i];
          highRecords++;

          if (scores[i] < lowScore)
          lowScore = scores[i];
          lowRecords++;


          return new int highRecords, lowRecords ;






          share|improve this answer























          • Do you mean complexity of solution to this problem can't be less than O(n)?
            – Balkrishan Nagpal
            Mar 1 at 15:33










          • Is it possible to get the answer without considering all n numbers? I think not.
            – 200_success
            Mar 1 at 15:34














          up vote
          3
          down vote



          accepted










          You should be consistent with your naming: int score vs. int scores.



          Avoid keeping state in static variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.



          You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.



          The following straightforward loop would be much preferable:



          public static int brokenRecords(int scores) 
          int highScore = scores[0],
          lowScore = scores[0],
          highRecords = 0,
          lowRecords = 0;
          for (int i = 1; i < scores.length; i++)
          if (scores[i] > highScore)
          highScore = scores[i];
          highRecords++;

          if (scores[i] < lowScore)
          lowScore = scores[i];
          lowRecords++;


          return new int highRecords, lowRecords ;






          share|improve this answer























          • Do you mean complexity of solution to this problem can't be less than O(n)?
            – Balkrishan Nagpal
            Mar 1 at 15:33










          • Is it possible to get the answer without considering all n numbers? I think not.
            – 200_success
            Mar 1 at 15:34












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          You should be consistent with your naming: int score vs. int scores.



          Avoid keeping state in static variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.



          You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.



          The following straightforward loop would be much preferable:



          public static int brokenRecords(int scores) 
          int highScore = scores[0],
          lowScore = scores[0],
          highRecords = 0,
          lowRecords = 0;
          for (int i = 1; i < scores.length; i++)
          if (scores[i] > highScore)
          highScore = scores[i];
          highRecords++;

          if (scores[i] < lowScore)
          lowScore = scores[i];
          lowRecords++;


          return new int highRecords, lowRecords ;






          share|improve this answer















          You should be consistent with your naming: int score vs. int scores.



          Avoid keeping state in static variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.



          You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.



          The following straightforward loop would be much preferable:



          public static int brokenRecords(int scores) 
          int highScore = scores[0],
          lowScore = scores[0],
          highRecords = 0,
          lowRecords = 0;
          for (int i = 1; i < scores.length; i++)
          if (scores[i] > highScore)
          highScore = scores[i];
          highRecords++;

          if (scores[i] < lowScore)
          lowScore = scores[i];
          lowRecords++;


          return new int highRecords, lowRecords ;







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Mar 1 at 15:13


























          answered Mar 1 at 13:27









          200_success

          123k14142399




          123k14142399











          • Do you mean complexity of solution to this problem can't be less than O(n)?
            – Balkrishan Nagpal
            Mar 1 at 15:33










          • Is it possible to get the answer without considering all n numbers? I think not.
            – 200_success
            Mar 1 at 15:34
















          • Do you mean complexity of solution to this problem can't be less than O(n)?
            – Balkrishan Nagpal
            Mar 1 at 15:33










          • Is it possible to get the answer without considering all n numbers? I think not.
            – 200_success
            Mar 1 at 15:34















          Do you mean complexity of solution to this problem can't be less than O(n)?
          – Balkrishan Nagpal
          Mar 1 at 15:33




          Do you mean complexity of solution to this problem can't be less than O(n)?
          – Balkrishan Nagpal
          Mar 1 at 15:33












          Is it possible to get the answer without considering all n numbers? I think not.
          – 200_success
          Mar 1 at 15:34




          Is it possible to get the answer without considering all n numbers? I think not.
          – 200_success
          Mar 1 at 15:34












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188591%2fhackerrank-breaking-the-record-solution%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

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

          C++11 CLH Lock Implementation