Array equilibrium point

Source: Internet
Author: User

The equilibrium point of a sequence is like this. The sum of all elements on the left of a sequence should be equal to the sum of all elements on the right, for example, in sequence a below:

 

A [0] =-7 A [1] = 1 A [2] = 5A [3] = 2 A [4] =-4 A [5] = 3A [6] = 0

 

3 is a balance point because:

  • A [0] + A [1] + A [2] = A [4] + A [5] + A [6]

6 is also a balance point because:

  • A [0] + A [1] + A [2] + A [3] + A [4] + A [5] = 0

(The sum of zero elements is zero) index 7 is not a balance point because it is not a valid index of sequence.

If you are still not very clear, a clear definition is given here: 0 ≤ k <n andSum[I = 0] k-1A [I] =Sum[I = k + 1] n-1 A [I]. The integer k is the equilibrium point of the sequence a [0], a [1],..., a [n-1] $. Here we assume that the sum of zero elements is zero.

Please write a function

Int equi (int A [], int N );

Returns the equilibrium point of a given sequence (any one). If there is no equilibrium point, returns −1, assuming that the sequence can reach a very large value.

Assume that:

  • N is an integer in [0 .. 10,000,000;
  • Array a each element is an integer in the value range [− 2,147,483,648... 2,147,483,647.

Complexity:

  • Worst case-the expected time complexity is O (n );
  • Worst case-the expected space complexity is O (n), except for Input Storage (excluding the storage space required by input parameters ).

Elements in the input array can be modified.

C # the two methods are the same, but the writing method is a little different:

Method 1:

 1   Using  System;  2   Using  System. Collections. Generic;  3   Using  System. LINQ; 4   Class  Solution {  5     Public   Int Equi ( Int  [] ){  6       If (A. Length <= 2  )  7         Return - 1  ;  8      Long Summary = A. sum ();  9       Long Left = 0  ;  10   11       For ( Int I = 1 ; I <A. length; I ++ )  12   {  13 Left + = A [I-1  ];  14          If (A [I] = left & Summary-left-A [I] = A [I])  15               Return  I;  16   }  17        Return - 1  ;  18   }  19 }

Method 2:

 1   Using  System;  2   Using  System. Collections. Generic;  3   Using  System. LINQ;  4   Class  Solution {  5     Public   Int Equi ( Int [] ){  6       If (A. Length = 0  )  7                   Return - 1  ;  8        Long Summary = A. sum ();  9               10        Long Sum_left = 0 ;  11        For ( Int I = 0 ; I <A. length; I ++ )  12   {  13             Long Sum_right = Summary-sum_left- A [I];  14             If (Sum_left = Sum_right)  15  {  16                  Return  I;  17   }  18 Sum_left + = A [I];  19   }  20          Return - 1  ;  21   }  22 }

Let's take a look at the advantages and disadvantages of these two methods.

======================================== Gorgeous split line ================ ==================

Evaluation given on the test Website:

Method 1:

 

Analysis

 

Test Time Result
Example
Test from the task description
0.080 S. OK
Simple 0.080 S. OK
extreme_large_numbers
sequence with extremly large numbers testing Arithmetic overflow.
0.070 S. runtime error
tested program terminated unexpectedly
stdout:

 unhandled exception: system. overflowexception: Number overflow. at system. LINQ. enumerable. 
     
       M _ 5E (int32 A, int32 B) [0x00000] At system. LINQ. enumerable. sum [int32, int32] (ienumerable '1 source, system. func '3 selector) [0x00000] At system. LINQ. enumerable. sum (ienumerable '1 source) [0x00000] At solution. equi (system. int32 [] A) [0x00000] At solutionwrapper. run (system. string input, system. string output) [0x00000] At solutionwrapper. main (system. string [] ARGs) [0x00000] 
     
overflow_tests 0.080 S. runtime error
tested program terminated unexpectedly
stdout:

 unhandled exception: system. overflowexception: Number overflow. at system. LINQ. enumerable. 
     
       M _ 5E (int32 A, int32 B) [0x00000] At system. LINQ. enumerable. sum [int32, int32] (ienumerable '1 source, system. func '3 selector) [0x00000] At system. LINQ. enumerable. sum (ienumerable '1 source) [0x00000] At solution. equi (system. int32 [] A) [0x00000] At solutionwrapper. run (system. string input, system. string output) [0x00000] At solutionwrapper. main (system. string [] ARGs) [0x00000] 
     
One_large
One large number at the end of the sequence
0.070 S. Wrong answer 
Got-1, but equilibrium point exists, for example on position 0
Sum_0
Sequence with Sum = 0
0.070 S. OK
Single
Single Number
0.070 S. Wrong answer 
Got-1, but equilibrium point exists, for example on position 0
Empty
Empty Array
0.060 S. OK
Combinations_of_two
Multiple runs, all combinations of {-1, 0, 1} ^ 2
0.080 S. Wrong answer 
Got-1, but equilibrium point exists, for example on position 0
Combinations_of_three
Multiple runs, all combinations of {-1, 0, 1} ^ 3
0.080 S. Wrong answer 
Got-1, but equilibrium point exists, for example on position 0
Small_pyramid 0.070 S. Wrong answer 
Got-1, but equilibrium point exists, for example on Position 42
Large_long_sequence_of_ones 0.130 S. Wrong answer 
Got-1, but equilibrium point exists, for example on Position 50000
Large_long_sequence_of_minus_ones 0.110 S. Wrong answer 
Got-1, but equilibrium point exists, for example on Position 50002
Medium_pyramid 0.090 S. Wrong answer 
Got-1, but equilibrium point exists, for example on Position 402
Large_pyramid
Large performance test, O (N ^ 2) solutions shocould fail.
0.160 S. Wrong answer 
Got-1, but equilibrium point exists, for example on Position 898

Method 2:

Analysis

Detected time complexity:
O (N)

 

Test Time Result
Example
Test from the task description
0.100 S. OK
Simple 0.090 S. OK
extreme_large_numbers
sequence with extremly large numbers testing Arithmetic overflow.
0.070 S. runtime error
tested program terminated unexpectedly
stdout:

 unhandled exception: system. overflowexception: Number overflow. at system. LINQ. enumerable. 
     
       M _ 5E (int32 A, int32 B) [0x00000] At system. LINQ. enumerable. sum [int32, int32] (ienumerable '1 source, system. func '3 selector) [0x00000] At system. LINQ. enumerable. sum (ienumerable '1 source) [0x00000] At solution. equi (system. int32 [] A) [0x00000] At solutionwrapper. run (system. string input, system. string output) [0x00000] At solutionwrapper. main (system. string [] ARGs) [0x00000] 
     
Overflow_tests 0.070 S. Runtime error 
Tested Program terminated unexpectedly 
Stdout:

Unhandled exception: system. overflowexception: Number overflow. at system. LINQ. enumerable. <sum> M _ 5E (int32 A, int32 B) [0x00000] At system. LINQ. enumerable. sum [int32, int32] (ienumerable '1 source, system. func '3 selector) [0x00000] At system. LINQ. enumerable. sum (ienumerable '1 source) [0x00000] At solution. equi (system. int32 [] A) [0x00000] At solutionwrapper. run (system. string input, system. string output) [0x00000] At solutionwrapper. main (system. string [] ARGs) [0x00000]
One_large
One large number at the end of the sequence
0.070 S. OK
Sum_0
Sequence with Sum = 0
0.080 S. OK
Single
Single Number
0.070 S. OK
Empty
Empty Array
0.060 S. OK
Combinations_of_two
Multiple runs, all combinations of {-1, 0, 1} ^ 2
0.070 S. OK
Combinations_of_three
Multiple runs, all combinations of {-1, 0, 1} ^ 3
0.070 S. OK
Small_pyramid 0.070 S. OK
Large_long_sequence_of_ones 0.100 S. OK
Large_long_sequence_of_minus_ones 0.120 S. OK
Medium_pyramid 0.090 S. OK
Large_pyramid
Large performance test, O (N ^ 2) solutions shocould fail.
0.160 S. OK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.