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. XListView刷新

    package com.example.da; import java.util.ArrayList;import java.util.List; import com.badu.net.Networ ...

  2. dedecms为后台自定义菜单的完整方法

    dedecms为后台自定义菜单的完整方法 品味人生 dedeCMS 围观7330次 18 条评论 编辑日期:2014-06-14 字体:大 中 小   最近在给客户定制一个企业网站,客户要求使用ded ...

  3. CSS 高级语法

    选择器的分组 你可以对选择器进行分组,这样,被分组的选择器就可以分享相同的声明.用逗号将需要分组的选择器分开.在下面的例子中,我们对所有的标题元素进行了分组.所有的标题元素都是绿色的. h1,h2,h ...

  4. 1、java中常用名字规范

    包名:多个单词组成是所有单词字母小写. 类名.接口名:所有单词首字母大写. 变量名.函数名:多单词组成时第一个单词首字母小写,从第二个单词开始首字母大写. 常量名:所有字母大写,单词之间用 “_” 连 ...

  5. SSIS 目录

    微软 BI 系列随笔 - SSIS 2012 基础 - SSIS 目录 上一篇讲解了使用SSIS参数与环境,由于涉及到了SSIS目录的相关知识和概念,本篇将对其进行讲解. 注:在之前的版本中,是使用整 ...

  6. git 新建分支/切换分支/合并分支 使用方法

    我的源码在 阿里云的git上存储着呢 1. 在 code.aliyun.com 上 新建分支  fixbug 2. 通过在 phpstorm中 右键项目>>VSC>>Git&g ...

  7. 互斥锁(Mutex)

    互斥锁(Mutex)互斥锁是一个互斥的同步对象,意味着同一时间有且仅有一个线程可以获取它.互斥锁可适用于一个共享资源每次只能被一个线程访问的情况 函数://创建一个处于未获取状态的互斥锁Public ...

  8. Android学习笔记(一)

    活动(Actiity)是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用中可以包含零个或多个活动. 所有的自己写Activity都继承于Activity类.项目中的任何活动都应该改重写Ac ...

  9. angular.js input

    <!DOCTYPE html> <html ng-app="app"> <head> <meta charset="UTF-8& ...

  10. sql查询当前月内的所有日期

    ),),)) as dt from master..spt_values where type='P' ),),),,)')