📜  数组平衡索引的Java程序

📅  最后修改于: 2022-05-13 01:55:09.369000             🧑  作者: Mango

数组平衡索引的Java程序

数组的平衡索引是这样一个索引,使得较低索引处的元素之和等于较高索引处的元素之和。例如,在数组 A 中:

例子 :

写一个函数int balance(int[] arr, int n) ;给定大小为 n 的序列 arr[],返回平衡索引(如果有),如果不存在平衡索引,则返回 -1。

方法一(简单但低效)
使用两个循环。外循环遍历所有元素,内循环确定外循环选取的当前索引是否为平衡索引。该解决方案的时间复杂度为 O(n^2)。

Java
// Java program to find equilibrium
// index of an array
 
class EquilibriumIndex {
    int equilibrium(int arr[], int n)
    {
        int i, j;
        int leftsum, rightsum;
 
        /* Check for indexes one by one until
           an equilibrium index is found */
        for (i = 0; i < n; ++i) {
 
            /* get left sum */
            leftsum = 0; 
            for (j = 0; j < i; j++)
                leftsum += arr[j];
 
            /* get right sum */
            rightsum = 0;
            for (j = i + 1; j < n; j++)
                rightsum += arr[j];
 
            /* if leftsum and rightsum are same,
               then we are done */
            if (leftsum == rightsum)
                return i;
        }
 
        /* return -1 if no equilibrium index is found */
        return -1;
    }
    // Driver code
    public static void main(String[] args)
    {
        EquilibriumIndex equi = new EquilibriumIndex();
        int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
        int arr_size = arr.length;
        System.out.println(equi.equilibrium(arr, arr_size));
    }
}
 
// This code has been contributed by Mayank Jaiswal


Java
// Java program to find equilibrium
// index of an array
 
class EquilibriumIndex {
    int equilibrium(int arr[], int n)
    {
        int sum = 0; // initialize sum of whole array
        int leftsum = 0; // initialize leftsum
 
        /* Find sum of the whole array */
        for (int i = 0; i < n; ++i)
            sum += arr[i];
 
        for (int 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;
    }
 
   // Driver code
    public static void main(String[] args)
    {
        EquilibriumIndex equi = new EquilibriumIndex();
        int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
        int arr_size = arr.length;
        System.out.println("First equilibrium index is " +
                          equi.equilibrium(arr, arr_size));
    }
}
 
// This code has been contributed by Mayank Jaiswal


Java


java
// Java program to find equilibrium
// index of an array
class GFG{
 
static int equilibrium(int a[], int n)
{
    if (n == 1)
        return (0);
     
    int[] front = new int[n];
    int[] back = new int[n];
 
    // Taking the prefixsum from front end array
    for (int i = 0; i < n; i++)
    {
        if (i != 0)
        {
            front[i] = front[i - 1] + a[i];
        }
        else
        {
            front[i] = a[i];
        }
    }
   
    // Taking the prefixsum from back end of array
    for (int i = n - 1; i > 0; i--)
    {
        if (i <= n - 2)
        {
            back[i] = back[i + 1] + a[i];
        }
        else
        {
            back[i] = a[i];
        }
    }
     
    // Checking for equilibrium index by
    //comparing front and back sums
    for(int i = 0; i < n; i++)
    {
        if (front[i] == back[i])
        {
            return i;
        }
    }
     
    // If no equilibrium index found,then return -1
    return -1;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
    int arr_size = arr.length;
     
    System.out.println("First Point of equilibrium " +
                       "is at index " +
                       equilibrium(arr, arr_size));
}
}
 
// This code is contributed by Lovish Aggarwal


输出
3

时间复杂度: O(n^2)

方法2(棘手且高效)
这个想法是首先获得数组的总和。然后遍历数组并不断更新初始化为零的左和。在循环中,我们可以通过将元素一一相减得到正确的和。感谢 Sambasiva 提出这个解决方案并为此提供代码。

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. 
       // update leftsum for next iteration.
    c) leftsum = leftsum + arr[i]
4) return -1 
// If we come out of loop without returning then
// there is no equilibrium index

下图显示了上述方法的试运行:

下面是上述方法的实现:

Java

// Java program to find equilibrium
// index of an array
 
class EquilibriumIndex {
    int equilibrium(int arr[], int n)
    {
        int sum = 0; // initialize sum of whole array
        int leftsum = 0; // initialize leftsum
 
        /* Find sum of the whole array */
        for (int i = 0; i < n; ++i)
            sum += arr[i];
 
        for (int 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;
    }
 
   // Driver code
    public static void main(String[] args)
    {
        EquilibriumIndex equi = new EquilibriumIndex();
        int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
        int arr_size = arr.length;
        System.out.println("First equilibrium index is " +
                          equi.equilibrium(arr, arr_size));
    }
}
 
// This code has been contributed by Mayank Jaiswal
输出
First equilibrium index is 3

输出:
第一个均衡指数是 3

时间复杂度: O(n)

方法3:

这是一种非常简单直接的方法。这个想法是两次获取数组的前缀和。一次来自阵列的前端,另一个来自阵列的后端。

在获取两个前缀和后,运行一个循环并检查一些 i,如果来自一个数组的两个前缀和都等于来自第二个数组的前缀和,那么该点可以被认为是平衡点。

Java

Java

// Java program to find equilibrium
// index of an array
class GFG{
 
static int equilibrium(int a[], int n)
{
    if (n == 1)
        return (0);
     
    int[] front = new int[n];
    int[] back = new int[n];
 
    // Taking the prefixsum from front end array
    for (int i = 0; i < n; i++)
    {
        if (i != 0)
        {
            front[i] = front[i - 1] + a[i];
        }
        else
        {
            front[i] = a[i];
        }
    }
   
    // Taking the prefixsum from back end of array
    for (int i = n - 1; i > 0; i--)
    {
        if (i <= n - 2)
        {
            back[i] = back[i + 1] + a[i];
        }
        else
        {
            back[i] = a[i];
        }
    }
     
    // Checking for equilibrium index by
    //comparing front and back sums
    for(int i = 0; i < n; i++)
    {
        if (front[i] == back[i])
        {
            return i;
        }
    }
     
    // If no equilibrium index found,then return -1
    return -1;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
    int arr_size = arr.length;
     
    System.out.println("First Point of equilibrium " +
                       "is at index " +
                       equilibrium(arr, arr_size));
}
}
 
// This code is contributed by Lovish Aggarwal
输出
First Point of equilibrium is at index 3

时间复杂度: O(N)

空间复杂度: O(N)

有关详细信息,请参阅有关数组的 Equilibrium index 的完整文章!