Find the missing number and repeated number

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

favorite












I am practising interview questions from here.




Problem : You are given a read only array of n integers from 1 to n.
Each integer appears exactly once except A which appears twice and B
which is missing. Return A and B such that output A should precede B.




My approach :



Sum(Natural numbers) = Sum(given numbers) - A + B
Sum_squares(Natural numbers) = Sum_squares(given numbers) - A*A + B*B


where :



Sum of n Natural numbers is given by : n(n+1)/2
Sum of squares of n Natural numbers is given by : n((n+1)/2)((2n+1)/3)


The above two equations can be simplified to :



(B-A) = Sum(Natural numbers) - Sum(given numbers) 
(B-A)*(B+A) = Sum_squares(Natural numbers) - Sum_squares(given numbers)


Solution:



def repeat_num_and_missing_num(array):
""" returns the value of repeated number and missing number in the given array
using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""
missing_num = 0
repeated_num = 0
x = len(array)
sum_of_num = 0
sum_of_squares = 0
sum_of_num_actual = (x*(x+1))/2
sum_of_squares_actual = ((x)*(x+1)*(2*x+1) ) / 6

for num in array:
sum_of_num += num
sum_of_squares += num*num


missing_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
+(sum_of_num_actual - sum_of_num))/2
repeated_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
-(sum_of_num_actual - sum_of_num))/2
return repeated_num, missing_num

#Test Cases
print repeat_num_and_missing_num([5,3,2,1,1]) == (1,4)

print repeat_num_and_missing_num([1,2,3,4,5,16,6,8,9,10,11,12,13,14,15,16]) == (16,7)

print repeat_num_and_missing_num([1,1]) == (1,2)


How can I make this code better?







share|improve this question

























    up vote
    6
    down vote

    favorite












    I am practising interview questions from here.




    Problem : You are given a read only array of n integers from 1 to n.
    Each integer appears exactly once except A which appears twice and B
    which is missing. Return A and B such that output A should precede B.




    My approach :



    Sum(Natural numbers) = Sum(given numbers) - A + B
    Sum_squares(Natural numbers) = Sum_squares(given numbers) - A*A + B*B


    where :



    Sum of n Natural numbers is given by : n(n+1)/2
    Sum of squares of n Natural numbers is given by : n((n+1)/2)((2n+1)/3)


    The above two equations can be simplified to :



    (B-A) = Sum(Natural numbers) - Sum(given numbers) 
    (B-A)*(B+A) = Sum_squares(Natural numbers) - Sum_squares(given numbers)


    Solution:



    def repeat_num_and_missing_num(array):
    """ returns the value of repeated number and missing number in the given array
    using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""
    missing_num = 0
    repeated_num = 0
    x = len(array)
    sum_of_num = 0
    sum_of_squares = 0
    sum_of_num_actual = (x*(x+1))/2
    sum_of_squares_actual = ((x)*(x+1)*(2*x+1) ) / 6

    for num in array:
    sum_of_num += num
    sum_of_squares += num*num


    missing_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
    +(sum_of_num_actual - sum_of_num))/2
    repeated_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
    -(sum_of_num_actual - sum_of_num))/2
    return repeated_num, missing_num

    #Test Cases
    print repeat_num_and_missing_num([5,3,2,1,1]) == (1,4)

    print repeat_num_and_missing_num([1,2,3,4,5,16,6,8,9,10,11,12,13,14,15,16]) == (16,7)

    print repeat_num_and_missing_num([1,1]) == (1,2)


    How can I make this code better?







    share|improve this question





















      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I am practising interview questions from here.




      Problem : You are given a read only array of n integers from 1 to n.
      Each integer appears exactly once except A which appears twice and B
      which is missing. Return A and B such that output A should precede B.




      My approach :



      Sum(Natural numbers) = Sum(given numbers) - A + B
      Sum_squares(Natural numbers) = Sum_squares(given numbers) - A*A + B*B


      where :



      Sum of n Natural numbers is given by : n(n+1)/2
      Sum of squares of n Natural numbers is given by : n((n+1)/2)((2n+1)/3)


      The above two equations can be simplified to :



      (B-A) = Sum(Natural numbers) - Sum(given numbers) 
      (B-A)*(B+A) = Sum_squares(Natural numbers) - Sum_squares(given numbers)


      Solution:



      def repeat_num_and_missing_num(array):
      """ returns the value of repeated number and missing number in the given array
      using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""
      missing_num = 0
      repeated_num = 0
      x = len(array)
      sum_of_num = 0
      sum_of_squares = 0
      sum_of_num_actual = (x*(x+1))/2
      sum_of_squares_actual = ((x)*(x+1)*(2*x+1) ) / 6

      for num in array:
      sum_of_num += num
      sum_of_squares += num*num


      missing_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
      +(sum_of_num_actual - sum_of_num))/2
      repeated_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
      -(sum_of_num_actual - sum_of_num))/2
      return repeated_num, missing_num

      #Test Cases
      print repeat_num_and_missing_num([5,3,2,1,1]) == (1,4)

      print repeat_num_and_missing_num([1,2,3,4,5,16,6,8,9,10,11,12,13,14,15,16]) == (16,7)

      print repeat_num_and_missing_num([1,1]) == (1,2)


      How can I make this code better?







      share|improve this question











      I am practising interview questions from here.




      Problem : You are given a read only array of n integers from 1 to n.
      Each integer appears exactly once except A which appears twice and B
      which is missing. Return A and B such that output A should precede B.




      My approach :



      Sum(Natural numbers) = Sum(given numbers) - A + B
      Sum_squares(Natural numbers) = Sum_squares(given numbers) - A*A + B*B


      where :



      Sum of n Natural numbers is given by : n(n+1)/2
      Sum of squares of n Natural numbers is given by : n((n+1)/2)((2n+1)/3)


      The above two equations can be simplified to :



      (B-A) = Sum(Natural numbers) - Sum(given numbers) 
      (B-A)*(B+A) = Sum_squares(Natural numbers) - Sum_squares(given numbers)


      Solution:



      def repeat_num_and_missing_num(array):
      """ returns the value of repeated number and missing number in the given array
      using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""
      missing_num = 0
      repeated_num = 0
      x = len(array)
      sum_of_num = 0
      sum_of_squares = 0
      sum_of_num_actual = (x*(x+1))/2
      sum_of_squares_actual = ((x)*(x+1)*(2*x+1) ) / 6

      for num in array:
      sum_of_num += num
      sum_of_squares += num*num


      missing_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
      +(sum_of_num_actual - sum_of_num))/2
      repeated_num = (((sum_of_squares_actual - sum_of_squares) /(sum_of_num_actual - sum_of_num))
      -(sum_of_num_actual - sum_of_num))/2
      return repeated_num, missing_num

      #Test Cases
      print repeat_num_and_missing_num([5,3,2,1,1]) == (1,4)

      print repeat_num_and_missing_num([1,2,3,4,5,16,6,8,9,10,11,12,13,14,15,16]) == (16,7)

      print repeat_num_and_missing_num([1,1]) == (1,2)


      How can I make this code better?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 14 at 16:42









      Latika Agarwal

      861216




      861216




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Removing trailing whitespace, renaming variables, reordering the code to group statements about the same concepts and introducing temporary variable to avoid writing and computing the same expressions multiple times and adding comments:



          def repeat_num_and_missing_num(array):
          """ Return the value of repeated number and missing number in the given array
          using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""

          sum_of_num = 0
          sum_of_squares = 0
          for num in array:
          sum_of_num += num
          sum_of_squares += num*num

          x = len(array)
          sum_of_num_expected = (x * (x+1)) / 2
          sum_of_squares_expected = ((x) * (x+1) * (2*x+1)) / 6

          # Assuming A is present twice and B is missing:
          # B - A
          b_minus_a = sum_of_num_expected - sum_of_num
          # B^2 - A^2 = (B-A) * (B+A)
          b2_minus_a2 = sum_of_squares_expected - sum_of_squares
          # B + A
          b_plus_a = b2_minus_a2 / b_minus_a

          a = (b_plus_a - b_minus_a) / 2
          b = (b_plus_a + b_minus_a) / 2
          return a, b





          share|improve this answer






























            up vote
            2
            down vote













            In addition to what @Josay has already mentioned, I'd advise making use of the inbuilt sum function.



            sum_of_num = sum(array)
            sum_of_squared = sum((n*n for n in array))


            which would be faster than a for-loop iterative solution.






            share|improve this answer





















              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%2f194379%2ffind-the-missing-number-and-repeated-number%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
              4
              down vote



              accepted










              Removing trailing whitespace, renaming variables, reordering the code to group statements about the same concepts and introducing temporary variable to avoid writing and computing the same expressions multiple times and adding comments:



              def repeat_num_and_missing_num(array):
              """ Return the value of repeated number and missing number in the given array
              using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""

              sum_of_num = 0
              sum_of_squares = 0
              for num in array:
              sum_of_num += num
              sum_of_squares += num*num

              x = len(array)
              sum_of_num_expected = (x * (x+1)) / 2
              sum_of_squares_expected = ((x) * (x+1) * (2*x+1)) / 6

              # Assuming A is present twice and B is missing:
              # B - A
              b_minus_a = sum_of_num_expected - sum_of_num
              # B^2 - A^2 = (B-A) * (B+A)
              b2_minus_a2 = sum_of_squares_expected - sum_of_squares
              # B + A
              b_plus_a = b2_minus_a2 / b_minus_a

              a = (b_plus_a - b_minus_a) / 2
              b = (b_plus_a + b_minus_a) / 2
              return a, b





              share|improve this answer



























                up vote
                4
                down vote



                accepted










                Removing trailing whitespace, renaming variables, reordering the code to group statements about the same concepts and introducing temporary variable to avoid writing and computing the same expressions multiple times and adding comments:



                def repeat_num_and_missing_num(array):
                """ Return the value of repeated number and missing number in the given array
                using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""

                sum_of_num = 0
                sum_of_squares = 0
                for num in array:
                sum_of_num += num
                sum_of_squares += num*num

                x = len(array)
                sum_of_num_expected = (x * (x+1)) / 2
                sum_of_squares_expected = ((x) * (x+1) * (2*x+1)) / 6

                # Assuming A is present twice and B is missing:
                # B - A
                b_minus_a = sum_of_num_expected - sum_of_num
                # B^2 - A^2 = (B-A) * (B+A)
                b2_minus_a2 = sum_of_squares_expected - sum_of_squares
                # B + A
                b_plus_a = b2_minus_a2 / b_minus_a

                a = (b_plus_a - b_minus_a) / 2
                b = (b_plus_a + b_minus_a) / 2
                return a, b





                share|improve this answer

























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  Removing trailing whitespace, renaming variables, reordering the code to group statements about the same concepts and introducing temporary variable to avoid writing and computing the same expressions multiple times and adding comments:



                  def repeat_num_and_missing_num(array):
                  """ Return the value of repeated number and missing number in the given array
                  using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""

                  sum_of_num = 0
                  sum_of_squares = 0
                  for num in array:
                  sum_of_num += num
                  sum_of_squares += num*num

                  x = len(array)
                  sum_of_num_expected = (x * (x+1)) / 2
                  sum_of_squares_expected = ((x) * (x+1) * (2*x+1)) / 6

                  # Assuming A is present twice and B is missing:
                  # B - A
                  b_minus_a = sum_of_num_expected - sum_of_num
                  # B^2 - A^2 = (B-A) * (B+A)
                  b2_minus_a2 = sum_of_squares_expected - sum_of_squares
                  # B + A
                  b_plus_a = b2_minus_a2 / b_minus_a

                  a = (b_plus_a - b_minus_a) / 2
                  b = (b_plus_a + b_minus_a) / 2
                  return a, b





                  share|improve this answer















                  Removing trailing whitespace, renaming variables, reordering the code to group statements about the same concepts and introducing temporary variable to avoid writing and computing the same expressions multiple times and adding comments:



                  def repeat_num_and_missing_num(array):
                  """ Return the value of repeated number and missing number in the given array
                  using the standard formulaes of Sum of n Natural numbers and sum of squares of n Natural Numbers"""

                  sum_of_num = 0
                  sum_of_squares = 0
                  for num in array:
                  sum_of_num += num
                  sum_of_squares += num*num

                  x = len(array)
                  sum_of_num_expected = (x * (x+1)) / 2
                  sum_of_squares_expected = ((x) * (x+1) * (2*x+1)) / 6

                  # Assuming A is present twice and B is missing:
                  # B - A
                  b_minus_a = sum_of_num_expected - sum_of_num
                  # B^2 - A^2 = (B-A) * (B+A)
                  b2_minus_a2 = sum_of_squares_expected - sum_of_squares
                  # B + A
                  b_plus_a = b2_minus_a2 / b_minus_a

                  a = (b_plus_a - b_minus_a) / 2
                  b = (b_plus_a + b_minus_a) / 2
                  return a, b






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited May 15 at 7:39


























                  answered May 14 at 17:18









                  Josay

                  23.8k13580




                  23.8k13580






















                      up vote
                      2
                      down vote













                      In addition to what @Josay has already mentioned, I'd advise making use of the inbuilt sum function.



                      sum_of_num = sum(array)
                      sum_of_squared = sum((n*n for n in array))


                      which would be faster than a for-loop iterative solution.






                      share|improve this answer

























                        up vote
                        2
                        down vote













                        In addition to what @Josay has already mentioned, I'd advise making use of the inbuilt sum function.



                        sum_of_num = sum(array)
                        sum_of_squared = sum((n*n for n in array))


                        which would be faster than a for-loop iterative solution.






                        share|improve this answer























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          In addition to what @Josay has already mentioned, I'd advise making use of the inbuilt sum function.



                          sum_of_num = sum(array)
                          sum_of_squared = sum((n*n for n in array))


                          which would be faster than a for-loop iterative solution.






                          share|improve this answer













                          In addition to what @Josay has already mentioned, I'd advise making use of the inbuilt sum function.



                          sum_of_num = sum(array)
                          sum_of_squared = sum((n*n for n in array))


                          which would be faster than a for-loop iterative solution.







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered May 15 at 6:38









                          hjpotter92

                          4,89611538




                          4,89611538






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194379%2ffind-the-missing-number-and-repeated-number%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?