给出的零索引数组&该数组的平衡指数

时间:2015-10-27 18:25:59

标签: java c++ arrays algorithm data-structures

给出了由N个整数组成的零索引数组A.该阵列的平衡指数是任何整数P,使得0≤P<1。 N和较低指数的要素总和等于较高指数要素的总和,即 A [0] + A [1] + ... + A [P-1] = A [P + 1] + ... + A [N-2] + A [N-1]。 假设零元素的总和等于0.如果P = 0或P = N-1,则会发生这种情况。

例如,考虑以下由N = 8个元素组成的数组A:

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

P = 1是该阵列的平衡指数,因为:

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

P = 3是该阵列的平衡指数,因为:

A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

P = 7也是一个均衡指数,因为:

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

并且没有索引大于7的元素。

P = 8不是平衡指数,因为它不满足条件0≤P<1。 Ñ

现在我必须写一个函数:

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

给定由N个整数组成的零索引数组A,返回其任何均衡指数。如果不存在均衡指数,该函数应返回-1。

例如,给定上面显示的数组A,函数可以返回1,3或7,如上所述。

假设:

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

这里有一些复杂性:

Elements of input arrays can be modified.

15 个答案:

答案 0 :(得分:6)

100%用c#评分

using System;
class Solution {
    public int solution(int[] A) {
        // First calculate sum of complete array as `sum_right`
        long sum_right = 0;
        for (int i = 0; i < A.Length; i++)
        {
            sum_right += A[i];
        }

        // start calculating sum from left side (lower index) as `sum_left`
        // in each iteration subtract A[i] from complete array sum - `sum_right`
        long sum_left = 0;
        for (int p = 0; p < A.Length; p++)
        {
            sum_left += p - 1 < 0 ? 0: A[p-1];
            sum_right -= A[p];
            if (sum_left == sum_right)
            {
                 return p;
            }
        }
        return -1;


    }
}

答案 1 :(得分:5)

100% - Java

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

    long sum = 0;
    for (int i = 0; i < A.length; i++) {
        sum += (long) A[i];
    }
    long leftSum = 0;
    long rightSum = 0;

    for (int i = 0; i < A.length; i++) {
        rightSum = sum - (leftSum + A[i]);
        if (leftSum == rightSum) {
            return i;
        }
        leftSum += A[i];
    }
    return -1;
}

}

答案 2 :(得分:3)

在C ++中(因为那是原始标签之一,虽然它看起来已经被删除了......)

int solution(int a[], int N){
    int left;
    int right;
    for(int i = 0; i < N; i++){
        left  = 0;
        right = 0;
        for(int t = 0; t < N; t++){
            if(t < i)      left  += a[t];
            else if(t > i) right += a[t];
            else continue;
        }
        if(left == right) return i;
    }
    return -1;
}

...

int demo[] = {-1, 3, -4, 5, 1, -6, 2, 1};
cout << solution(demo,sizeof(demo)/sizeof(*demo));

如果你想看到所有指数......

if(left == right) cout << "Equilibrium Index: " <<  i << endl;

我觉得奇怪的是它不需要返回一个索引数组;那说,如果你需要它,通过一些轻微的修改实现起来并不难:

答案 3 :(得分:3)

The answer is posted in this blog: http://blog.codility.com/2011/03/solutions-for-task-equi.html。为了避免O(N ^ 2)并实现O(N)性能: 更好的运行时间的关键观察是在恒定时间内更新左/右总和,而不是从头开始重新计算它们。

int equi(int arr[], int n) 
{
    if (n==0) return -1; 
    long long sum = 0;
    int i; 
    for(i=0;i<n;i++) sum+=(long long) arr[i]; 

    long long sum_left = 0;    
    for(i=0;i<n;i++) {
        long long sum_right = sum - sum_left - (long long) arr[i];
        if (sum_left == sum_right) return i;
        sum_left += (long long) arr[i];
    } 
    return -1; 
} 

答案 4 :(得分:2)

这是java等价的

public static int equilibriumIndex(int[] array) {
    int INDEX_NOT_FOUND = -1;
    int rSum = 0, lSum = 0;

    for (int index = 0; index < array.length; index++) {
        rSum += array[index];           
    }

    for (int index = 0; index < array.length; index++) {
        lSum += (index==0) ? 0 : array[index -1];// cumulative sum before (left sum) the current index
        rSum -= array[index]; // sum after (right sum) the current index onwards
        if (lSum == rSum) { // if both sums, cumulative sum before the current index and cumulative sum after the current index is equal, we got the equilibrium index 
            return index;
        }                       
    }
    return INDEX_NOT_FOUND;
}

以下是测试方法

@Test
public void equilibriumTest() {
    int result = ArrayUtils.equilibriumIndex(new int[]{1,2,3,1,6});
    assertThat(result, equalTo(3));
}

答案 5 :(得分:2)

Javascript中的100分

function solution(V) {

    var sum = 0;
    for (i=0; i < V.length; i++) {
      sum += V[i];   
    }

    var leftSum= 0;
    var rightSum = 0;

    for (j=0; j < V.length; j++) {
      rightSum = sum - (leftSum + V[j]);
      if(leftSum == rightSum) {
          return j;
      }
      leftSum += V[j];
    }
    return -1;
}

答案 6 :(得分:1)

直截了当的方法看起来如下。

首先,您需要计算数组所有元素的总和

例如,如果你有C ++中的数组

int a[] = { -1, 3, -4, 5, 1, -6, 2, 1 };

然后您可以使用普通循环来计算标题std::accumulate中声明的总和或标准算法<numeric>

例如

long long int right_sum = 
    std::accumulate( std::begin( a ), std::end( a ), 0ll );

左子序列的元素总和最初等于零

long long int left_sum = 0;

然后你可以使用适当的lambda表达式应用标准算法std::find_if,或者再次写一个普通的循环,例如

for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
    right_sum -= a[i];
    if ( left_sum == right_sum ) std::cout << i << ' ';
    left_sum += a[i];
}

结果将是

1 3 7

答案 7 :(得分:1)

动态编程方法。准时。 O(2N)空间。

  1. 保留两个表(数组),tableBefore和tableAfter。
  2. tableBefore总结为每个i的索引i;我 - &gt; 1至N.
  3. tableAfter总结为每个i的索引i;我 - &gt; N到1。
  4. 然后循环并比较tableBefore和tableAfter中的每个索引。如果它相等,那就是你的均衡指数。

    public static int EquilibriumIndex2(int[] a) {
    int len = a.length;
    int[] tableBefore = new int[len];
    int[] tableAfter = new int[len];
    
    tableBefore[0] = 0;
    for (int i = 1; i < len; i++) {
        tableBefore[i] = tableBefore[i - 1] + a[i - 1];
    }
    
    //System.out.println("tableBefore: " + Arrays.toString(tableBefore));
    tableAfter[len - 1] = 0;
    for (int i = len - 2; i >= 0; i--) {
        tableAfter[i] = tableAfter[i + 1] + a[i + 1];
    }
    //System.out.println("tableAfter: " + java.util.Arrays.toString(tableAfter));
    
    for (int j = 0; j < len; j++) {
        if (tableAfter[j] == tableBefore[j]) {
            return j;
        }
    }
    return -1;
    

    }

答案 8 :(得分:1)

您可以使用总和方法来解决此问题。每当从左边求和=右边的和,你就有一个平衡点。

public int solution(int[] A) {
    int[] sumLeft = new int[A.length];
    int[] sumRight = new int[A.length];

    sumLeft[0] = A[0];
    sumRight[A.length-1] = A[A.length-1];
    for (int i=1; i<A.length; i++){
        sumLeft[i] = A[i] + sumLeft[i-1];
    }
    for (int i=A.length-2; i>=0; i--) {
        sumRight[i] = sumRight[i+1] + A[i];
    }

    for (int i=0; i<A.length; i++) {
        if (sumLeft[i]==sumRight[i]) {
            return i;
        }
    }
    return -1;
}

答案 9 :(得分:1)

我在Swift 3.0中的回答

public func solution(_ A : inout [Int]) -> Int {

var nElements = A.count
var equilibriumIndexArray = [Int]() /* to store all possible indices */
var equilibriumIndex = -1

for fixedIndex in 0..<nElements{

    var sumLeft = 0
    var sumRight = 0

    //Sum the left part
    for index in 0..<fixedIndex
    {
        sumLeft += A[index]
    }

    //Sum the right part
    for index in fixedIndex+1..<nElements
    {
        sumRight += A[index]
    }

    //Check for equilibrium
    if sumLeft == sumRight
    {
        equilibriumIndexArray.append(fixedIndex)
    }

}

//pick a random element from the list of possible answers
if equilibriumIndexArray.count > 0
{
    let randomIndex = Int(arc4random_uniform(UInt32(equilibriumIndexArray.count)))
    equilibriumIndex = equilibriumIndexArray[randomIndex]
}

return equilibriumIndex

}

答案 10 :(得分:1)

python中的

:)

def solution(A):
    INDEX_NOT_FOUND = -1
    right_sum = 0 
    left_sum = 0

    for item in range(0, len(A)):
        right_sum += A[item]

    for item in range(0, len(A)):
        if item == 0:
            left_sum +=  0 
        else: 
            left_sum += A[item -1]
        right_sum -= A[item]
        if left_sum == right_sum:
            return item
    return INDEX_NOT_FOUND;

答案 11 :(得分:1)

100% - PHP

a.out

<强>输出:

function solution(array $a)
{
    $result = [];

    for($i = 0; $count = count($a), $i < $count; $i++) {
        if(sumLeft($a, $i-1) === sumRight($a, $i+1)) {
            $result[] = $i;
        }
    }

    return  count($result) ? $result : -1;
}

function sumRight(array $a, int $position): int
{
    return array_sum(array_slice($a, $position));;
}

function sumLeft(array $a, int $position): int
{
    return array_sum(array_slice($a, 0, $position + 1));
}

echo "<pre>";
print_r(solution([-1, 3, -4, 5, 1, -6, 2, 1]));

答案 12 :(得分:1)

简单解决方案:
步骤:

1)检查(Array = null)

然后打印“当阵列为NULL时,不存在平衡点”

2)检查(数组的长度= 1)

然后打印“Equilibrium_Index = 0”=&gt;数组中只存在单个元素,即平衡点

3)检查(阵列长度> 1)

循环(数组索引1到长度-1)

将每个指数视为均衡点

检查(低于平衡点的元素总和=高于平衡点的元素之和)

是=&gt; equilibrium_index = i(打破循环)

否=&gt;使用循环计数器的下一个值继续步骤3

4)如果没有从步骤3返回控制意味着环路中不存在平衡点

然后打印“数组中没有平衡点。”

请在下面找到相同的代码:

 public int findSum(int equillibrium,int a[],int flag)
 {


      /*Flag - It represents whether sum is required for left side of equilibrium 
       point or right side of equilibrium point
      *Flag = 0 => Sum is required for left side of equilibrium point
      *Flag = 1 => Sum is required for right side of equilibrium point
      */

     int lowlimit = 0, uplimit = a.length-1, i ,sum = 0;

     if(flag==0)
         uplimit = equillibrium - 1;
     else
         lowlimit = equillibrium + 1;

     for(i=lowlimit ; i<=uplimit; i++)
         sum = sum + a[i];

     return sum;
 }

 public int findEquillibriumPoint(int a[])
 {
     int i = 0; //Loop Counter

     //Since only one element is present it is at equilibrium only and index of equillibrium point is index of single element i.e 0
     if(a.length==1)
         return 0;
     else
     {
         for(i=1;i<a.length;i++)
         {
             if(findSum(i,a,0)==findSum(i,a,1)) //checking if some of lower half from equilibrium point is equal to sum of upper half
                 return i; //if equilibrium point is found return the index of equilibrium point
         }
     }

     return -1;//if equilibrium point is not present in array then return -1 
 }

答案 13 :(得分:0)

对于懒惰和PHP开发人员:

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

echo solution($A) . "\n";

function solution($A)
{
    $sum = 0;

    for ($i=0; $i < count($A); $i++) {
        $sum += $A[$i];
    }

    $sumRight = 0;
    $sumLeft = 0;

    for ($j=0; $j < count($A); $j++) {
        $sumRight = $sum - ($sumLeft + $A[$j]);
        if ($sumLeft == $sumRight) {
            return $j;
        }
        $sumLeft += $A[$j];
    }
    return -1;
}

复杂性O(N)

答案 14 :(得分:0)

Ruby中的100分

def equilibrium(a)
  sum = 0
  n = a.length
  left_sum = 0
  right_sum = 0

  n.times do |i|
    sum += a[i]
  end

  n.times do |i|
    right_sum = sum - left_sum - a[i]
    if right_sum == left_sum
      return i
    end
    left_sum += a[i]
  end
  return -1
end