[LinkedIn]find balance (equilibrium) point (index) in an array (unsorted)
来源:互联网 发布:美图秀秀mac官方 编辑:程序博客网 时间:2024/06/10 18:51
Code From Here (g4g)
/**1) Initialize leftsum as 02) Get the total sum of the array as sum3) 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] 4) return -1 **/int equilibrium(int arr[], int n){ int sum = 0; // initialize sum of whole array int leftsum = 0; // initialize leftsum int i; /* Find sum of the whole array */ for (i = 0; i < n; ++i) sum += arr[i]; for( i = 0; i < n; ++i) { sum -= arr[i]; // sum is now right sum for index i if(leftsum == sum) return i; leftsum += arr[i]; } /* If no equilibrium index found, then return 0 */ return -1;}
0 0
- [LinkedIn]find balance (equilibrium) point (index) in an array (unsorted)
- [LinkedIn] Find all triangles in an array
- Find the kth largest element in an unsorted array
- Find the k-th minimal element of an unsorted array
- Find the smallest positive number missing from an unsorted array
- Get nth Max number in an array of unsorted integers.
- the kth largest element in an unsorted array.
- Find duplicates in an array
- Facebook面试题 find the peek or valley index in an array
- [LinkedIn] Find target number in 2D sorted array (matrix)
- First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo
- Given an unsorted integer array, find the first missing positive integer.
- find kth largest number in an array
- Find consecutive elements in an array
- Find the repeated item in an array
- find ith smallest element in an array
- Find local minima in an array
- PIQ03: Find Majority Element in an Array
- [LinkedIn]Intersection of two sorted array
- MATLAB indexing question
- 浅解闭包概念
- 我的第一个C语言程序
- Xsolla和亚马逊支付合作,提供游戏内计费方案
- [LinkedIn]find balance (equilibrium) point (index) in an array (unsorted)
- Valid Sudoku
- Remove duplicates from sorted array
- Remove duplicates from sorted array II
- Trapping Rain Water 双扫
- air max femme found that the world's largest network of black market "Silk Road" creator and operat
- Candy 双扫
- 阿公一艘额石头人
- C语言网络编程