zoukankan      html  css  js  c++  java
  • Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A:
    
    A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0
    
    3 is an equilibrium index, because:
    A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
    
    6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0
    
    7 is not an equilibrium index, because it is not a valid index of array A.
    
    Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.

    Method 1 (Simple but inefficient)
    Use two loops. Outer loop iterates through all the element and inner loop finds out whether the current index picked by the outer loop is equilibrium index or not. Time complexity of this solution is O(n^2).

    Method 2 (Tricky and Efficient)
    The idea is to get total sum of array first. Then Iterate through the array and keep updating the left sum which is initialized as zero. In the loop, we can get right sum by subtracting the elements one by one. Thanks to Sambasiva for suggesting this solution and providing code for this.

    O(n) time complexity

    1) Initialize leftsum  as 0
    2) Get the total sum of the array as sum
    3) Iterate through the array and for each index i, do following.
        a)  Update sum to get the right sum.  
               sum = sum - arr[i] 
           // sum is now right sum
        b) If leftsum is equal to sum, then return current index. 
        c) leftsum = leftsum + arr[i] // update leftsum for next iteration.
    4) return -1 // If we come out of loop without returning then
                 // there is no equilibrium index

     1 #include <stdio.h>
     2  
     3 int equilibrium(int arr[], int n)
     4 {
     5    int sum = 0;      // initialize sum of whole array
     6    int leftsum = 0; // initialize leftsum
     7    int i;
     8  
     9    /* Find sum of the whole array */
    10    for (i = 0; i < n; ++i)
    11         sum += arr[i];
    12  
    13    for( i = 0; i < n; ++i)
    14    {
    15       sum -= arr[i]; // sum is now right sum for index i
    16  
    17       if(leftsum == sum)
    18         return i;
    19  
    20       leftsum += arr[i];
    21    }
    22  
    23     /* If no equilibrium index found, then return 0 */
    24     return -1;
    25 }
    26  
    27 int main()
    28 {
    29   int arr[] = {-7, 1, 5, 2, -4, 3, 0};
    30   int arr_size = sizeof(arr)/sizeof(arr[0]);
    31   printf("First equilibrium index is %d
    ", equilibrium(arr, arr_size));
    32  
    33   getchar();
    34   return 0;
    35 }
  • 相关阅读:
    洛谷1012 拼数
    洛谷1012 拼数
    洛谷 1155 (NOIp2008)双栈排序——仔细分析不合法的条件
    bzoj 3566 [SHOI2014]概率充电器——树型
    bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
    洛谷 1291 [SHOI2002]百事世界杯之旅
    洛谷 1365 WJMZBMR打osu! / Easy
    洛谷 1297 [国家集训队]单选错位——期望
    洛谷 1099 ( bzoj 1999 ) [Noip2007]Core树网的核
    洛谷 2827 蚯蚓——相邻两个比较的分析
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4199081.html
Copyright © 2011-2022 走看看