Problem Description:

    Given an array with N capacity, which store N integers from 1 to N-1 with one and only one integer

    occurs twice, please find that repeated integer. Note, limit the time complexity to O(n).

 

Solution:

int find(int a[], int N)

{

    int i, sum, base_sum;

 

    sum = 0;        // the sum of those N integers.

    for (i = 0; i < N; i++) {

        sum += a[i];

    }

    base_sum = (N*(N-1))/2;      // the sum of all the subscripts.

    return(sum - base_sum);

}

相关文章:

  • 2021-11-03
  • 2022-01-10
  • 2022-02-23
  • 2021-12-06
  • 2021-07-15
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2022-02-10
  • 2022-12-23
  • 2021-12-02
相关资源
相似解决方案