Here is a simple program test task, it doesn't have very diffcult logic:

A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 7 elements:

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

P = 3 is an equilibrium index of this array, because:

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

P = 6 is also an equilibrium index, because:

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

and there are no elements with indices greater than 6.

P = 7 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function

int solution(int A[], int N);

int solution(NSMutableArray *A);

int solution(const vector<int> &A);

class Solution { int solution(int[] A); }

class Solution { public int solution(int[] A); }

object Solution { def solution(A: Array[Int]): Int }

function solution(A);

function solution(A)

function solution($A);

function solution(A: array of longint; N: longint): longint;

def solution(A)

sub solution { my (@A)=@_; ... }

def solution(a)

Private Function solution ( A As Integer() ) as Integer

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

Assume that:

  • N is an integer within the range [0..10,000,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

For example, given array A such that

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

the function may return 3 or 6, as explained above.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

At very beginning, I have wrotten down this:

// 62 of 100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
if(A.Length <=1)
return -1;
//abc
int sumA = 0;
foreach(int i in A){
sumA += i;
} int j = 0;
int tempSum = A[0];
for(j = 1; j<A.Length; j++){ //A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
//return P
if(sumA - tempSum - A[j] == tempSum){
break;
}
tempSum += A[j];
}
if (j == A.Length || j == A.Length - 1)
return -1;
else
return j;
}
}

I got 62 points of 100. because it missed border check and one misunderstanding.

So I updated it to this one:

//100 of 100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
//N is an integer within the range [0..10,000,000];
if(A.Length <1 || A.Length >10000000)
return -1;
//single number
if(A.Length == 1)
return 0; //each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]
Int64 sumA = 0;
foreach(int i in A){
sumA += i;
} int j = 0; //each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]
Int64 tempSum = 0;
for(; j<A.Length; j++){
//A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
//return P
if(sumA - tempSum - A[j] == tempSum){
break;
}
tempSum += A[j];
}
//P = 7 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N
if (j == A.Length)
return -1;
else
return j;
}
}

Now, I got 100 points.

This task is from a test website.

Find the equipment indices的更多相关文章

  1. elasticsearch auto delete old indices

    定在crontab 每天执行 crontab -e * 2 * * * ~/autodelete.py Python 代码如下 #!/usr/bin/env python # encoding:utf ...

  2. Equipment Box[HDU1110]

    Equipment Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 转载:监控每个节点(Indices部分)

    集群的健康只是一个方面,它是对整个集群所有方面的一个很高的概括.节点状态的api是另外一个方面,它提供了关于你的集群中每个节点令你眼花缭乱的统计数据. 节点的状态提供了那么多的统计数据,在你很熟悉它们 ...

  4. HDOJ 3177 Crixalis&#39;s Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...

  6. HDU ACM 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. 杭电 3177 Crixalis&#39;s Equipment

    http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...

  8. Hdu 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板

    <pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...

随机推荐

  1. iOS常用设计模式和机制之代理

    Delegate : 1 代理设计模式的使用我们首先需要明白三个要素 *委托方:委托别人去执行某些操作的人(对象) *代理方:被委托区执行某些操作的人(对象) *协议:(protocol)委托方需要代 ...

  2. iOS9适配中的各种问题

    1.http在ios9上不能在使用.需要进行配置. the resource could not be loaded because the app transport security policy ...

  3. [原创]cocos2d-x研习录-第二阶 概念类之摄相机类(CCCamera)

    在Cocos2D-x中,每个CCNode都拥有一个摄像机类CCCamera.只有通过CCCamera,CCNode才会被渲染出来.当CCNode发生缩放.旋转和位置变化时,都需要覆盖CCCamera, ...

  4. cdr创建样式与样式集的方法

    样式是一组定义对象属性的格式化属性,如轮廓或填充.例如,要定义轮廓样式,您可以指定轮廓宽度.颜色和线条类型等属性.要定义字符样式,您可以指定字体类型.字体样式和大小.文本颜色和背景色.字符位置.大写等 ...

  5. flash cs6导入某些mp3不能的解决办法

    安装最新的quicktime 另外还有一个很恶心的办法,可以不用装quicktime. 1.用adobe audio打开一个没问题的mp3, 2.再打开有问题的MP3,全选,复制: 3.切换到没问题的 ...

  6. C# 多线程的等待所有线程结束 用 ManualResetEvent 控制

    using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApplication ...

  7. php_sapi_name详解

    php_sapi_name : — 返回 web 服务器和 PHP 之间的接口类型. 返回描述 PHP 所使用的接口类型(the Server API, SAPI)的小写字符串. 例如,CLI 的 P ...

  8. mfc_随机数生成器

    vc++6.0 内涵图

  9. MATLAB那些常见的命令

    1.clear :清除内存变量和函数,也就是把WorkSpace的变量给清空: 2.clc :清楚当前Matlab命令窗口的内容:相当于清楚屏幕吧,重新开始,简洁一点,注意clc是不清 楚变量的.一般 ...

  10. No.3__C#

    起步的迷思 周二:今天起来,天上下着小雨,气温降低了许多.从上周的二十多度又回到了七八度的样子.多穿了一件衣服,顶着寒风就出门了,确实是有点冷.到了公司,已经八点四十几了.有些小疲倦,头晕晕沉沉的,不 ...