📌  相关文章
📜  要附加的最小整数以将给定的数组转换为平衡

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

要附加的最小整数以将给定的数组转换为平衡

给定一个长度为N的数组A[] ,任务是找到要在数组的任何一端附加的最小整数以使其平衡。

例子:

方法:可以使用如下所述的两个指针方法来解决该问题:

插图:

下面是上述方法的实现:

C++
// C++ program of above approach.
 
#include 
using namespace std;
 
// Function to find minimum integer
// to be added either in front or at end
// of the array to make it equilibrium
int makeEquilibrium(int arr[], int n)
{
    int i = 1, j = n - 2;
 
    // Initialize left and right sum with
    // first and last value of array
    int leftSum = arr[0], rightSum = arr[n - 1];
 
    while (i <= j) {
 
        // If there is only one element present
        // between i and j, return
        // absolute value of left and right sum
        // which generate ans
        if (j - i < 1)
            return abs(leftSum - rightSum);
 
        // If left sum is less
        // increment i and add
        // element from front
        if (leftSum < rightSum) {
            leftSum += arr[i++];
        }
 
        // If right sum is less
        // decrement j and add
        // element from end
        else if (leftSum > rightSum) {
            rightSum += arr[j--];
        }
 
        // when both sum become equal
        else {
            leftSum += arr[i++];
            rightSum += arr[j--];
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 2, 6, 4, 3, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << makeEquilibrium(arr, N);
    return 0;
}


Java
// JAVA program of above approach.
import java.util.*;
class GFG
{
 
  // Function to find minimum integer
  // to be added either in front or at end
  // of the array to make it equilibrium
  public static int makeEquilibrium(int arr[], int n)
  {
    int i = 1, j = n - 2;
 
    // Initialize left and right sum with
    // first and last value of array
    int leftSum = arr[0], rightSum = arr[n - 1];
 
    while (i <= j) {
 
      // If there is only one element present
      // between i and j, return
      // absolute value of left and right sum
      // which generate ans
      if (j - i < 1)
        return Math.abs(leftSum - rightSum);
 
      // If left sum is less
      // increment i and add
      // element from front
      if (leftSum < rightSum) {
        leftSum += arr[i++];
      }
 
      // If right sum is less
      // decrement j and add
      // element from end
      else if (leftSum > rightSum) {
        rightSum += arr[j--];
      }
 
      // when both sum become equal
      else {
        leftSum += arr[i++];
        rightSum += arr[j--];
      }
    }
    return 0;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { 5, 2, 6, 4, 3, 2 };
    int N = arr.length;
    System.out.print(makeEquilibrium(arr, N));
  }
}
 
// This code is contributed by Taranpreet


Python3
# python3 program of above approach.
 
# Function to find minimum integer
# to be added either in front or at end
# of the array to make it equilibrium
def makeEquilibrium(arr, n):
 
    i, j = 1, n - 2
 
    #  left and right sum with
    # first and last value of array
 
    leftSum, rightSum = arr[0], arr[n - 1]
 
    while (i <= j):
 
                # If there is only one element present
                # between i and j, return
                # absolute value of left and right sum
                # which generate ans
 
        if (j - i < 1):
            return abs(leftSum - rightSum)
 
            # If left sum is less
            # increment i and add
            # element from front
        if (leftSum < rightSum):
            leftSum += arr[i]
            i += 1
 
            # If right sum is less
            # decrement j and add
            # element from end
        elif (leftSum > rightSum):
            rightSum += arr[j]
            j -= 1
 
            # when both sum become equal
        else:
            leftSum += arr[i]
            i += 1
            rightSum += arr[j]
            j -= 1
 
# Driver code
if __name__ == "__main__":
 
    arr = [5, 2, 6, 4, 3, 2]
    N = len(arr)
    print(makeEquilibrium(arr, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program of above approach.
using System;
 
public class GFG{
 
  // Function to find minimum integer
  // to be added either in front or at end
  // of the array to make it equilibrium
  static int makeEquilibrium(int[] arr, int n)
  {
    int i = 1, j = n - 2;
 
    // Initialize left and right sum with
    // first and last value of array
    int leftSum = arr[0], rightSum = arr[n - 1];
 
    while (i <= j) {
 
      // If there is only one element present
      // between i and j, return
      // absolute value of left and right sum
      // which generate ans
      if (j - i < 1)
        return Math.Abs(leftSum - rightSum);
 
      // If left sum is less
      // increment i and add
      // element from front
      if (leftSum < rightSum) {
        leftSum += arr[i++];
      }
 
      // If right sum is less
      // decrement j and add
      // element from end
      else if (leftSum > rightSum) {
        rightSum += arr[j--];
      }
 
      // when both sum become equal
      else {
        leftSum += arr[i++];
        rightSum += arr[j--];
      }
    }
    return 0;
  }
 
  // Driver code
  static public void Main ()
  {
 
    int[] arr = { 5, 2, 6, 4, 3, 2 };
    int N = arr.Length;
    Console.Write(makeEquilibrium(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
2

时间复杂度: O(N)
辅助空间: O(1)。