📌  相关文章
📜  找到左边元素总和等于右边元素总和的索引

📅  最后修改于: 2021-09-06 06:19:30             🧑  作者: Mango

给定一个数组, arr[]大小为N ,任务是找到数组的最小索引,使得索引左侧的所有数组元素的总和等于所有元素之和的数字的倒数该索引右侧的数组元素。如果没有找到这样的索引,则打印-1

例子:

朴素的方法:解决这个问题最简单的方法是遍历数组,对于每个索引,检查索引左边元素的总和是否等于数组元素和的数字的倒数该索引的右侧与否。如果发现为真,则打印该索引。否则,如果找不到这样的索引,则打印-1

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

高效的方法:为了优化上述方法,想法是使用前缀和技术。请按照以下步骤解决问题:

  • 初始化一个变量,比如rightSum ,以存储每个索引右侧的数组元素的总和。
  • 初始化一个变量,比如leftSum ,以存储每个索引左侧的数组元素的总和。
  • 使用变量i遍历数组并更新rightSum -= arr[i], leftSum += arr[i] 的值并检查leftSum 是否等于rightSum的数字的倒数。如果发现是真的,则打印i
  • 否则,打印-1

下面是上述方法的实现。

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if a number is equal
// to the reverse of digits of other number
bool checkReverse(int leftSum, int rightSum)
{
    // Stores reverse of
    // digits of rightSum
    int rev = 0;
 
    // Stores rightSum
    int temp = rightSum;
 
    // Calculate reverse of
    // digits of temp
    while (temp != 0) {
 
        // Update rev
        rev = (rev * 10) + (temp % 10);
 
        // Update temp
        temp /= 10;
    }
 
    // If reverse of digits of
    // rightSum equal to leftSum
    if (rev == leftSum) {
        return true;
    }
 
    return false;
}
 
// Function to find the index
// that satisfies the condition
int findIndex(int arr[], int N)
{
 
    // Stores sum of array elements
    // on right side of each index
    int rightSum = 0;
 
    // Stores sum of array elements
    // on left side of each index
    int leftSum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update rightSum
        rightSum += arr[i];
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update rightSum
        rightSum -= arr[i];
 
        // If leftSum equal to
        // reverse of digits
        // of rightSum
        if (checkReverse(leftSum,
                         rightSum)) {
            return i;
        }
 
        // Update leftSum
        leftSum += arr[i];
    }
 
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 7, 3, 6, 4, 9, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findIndex(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to check if a number is equal
  // to the reverse of digits of other number
  static boolean checkReverse(int leftSum, int rightSum)
  {
    // Stores reverse of
    // digits of rightSum
    int rev = 0;
 
    // Stores rightSum
    int temp = rightSum;
 
    // Calculate reverse of
    // digits of temp
    while (temp != 0) {
 
      // Update rev
      rev = (rev * 10) + (temp % 10);
 
      // Update temp
      temp /= 10;
    }
 
    // If reverse of digits of
    // rightSum equal to leftSum
    if (rev == leftSum) {
      return true;
    }
 
    return false;
  }
 
  // Function to find the index
  // that satisfies the condition
  static int findIndex(int[] arr, int N)
  {
 
    // Stores sum of array elements
    // on right side of each index
    int rightSum = 0;
 
    // Stores sum of array elements
    // on left side of each index
    int leftSum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Update rightSum
      rightSum += arr[i];
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Update rightSum
      rightSum -= arr[i];
 
      // If leftSum equal to
      // reverse of digits
      // of rightSum
      if (checkReverse(leftSum,
                       rightSum)) {
        return i;
      }
 
      // Update leftSum
      leftSum += arr[i];
    }
 
    return -1;
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    int[] arr = { 5, 7, 3, 6, 4, 9, 2 };
    int N = arr.length;
    System.out.print(findIndex(arr, N));
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python3 Program to implement
# the above approach
 
# Function to check if a number is equal
# to the reverse of digits of other number
def checkReverse(leftSum, rightSum):
     
    # Stores reverse of
    # digits of rightSum
    rev = 0
 
    # Stores rightSum
    temp = rightSum
 
    # Calculate reverse of
    # digits of temp
    while (temp != 0):
 
        # Update rev
        rev = (rev * 10) + (temp % 10)
 
        # Update temp
        temp //= 10
 
    # If reverse of digits of
    # rightSum equal to leftSum
    if (rev == leftSum):
        return True
 
    return False
 
# Function to find the index
# that satisfies the condition
def findIndex(arr, N):
 
    # Stores sum of array elements
    # on right side of each index
    rightSum = 0
 
    # Stores sum of array elements
    # on left side of each index
    leftSum = 0
 
    # Traverse the array
    for i in range(N):
        
        # Update rightSum
        rightSum += arr[i]
 
    # Traverse the array
    for i in range(N):
 
        # Update rightSum
        rightSum -= arr[i]
 
        # If leftSum equal to
        # reverse of digits
        # of rightSum
        if (checkReverse(leftSum, rightSum)):
            return i
 
        # Update leftSum
        leftSum += arr[i]
    return -1
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 7, 3, 6, 4, 9, 2]
    N =  len(arr)
    print(findIndex(arr, N))
 
    # This code is contributed by mohit kumar 29


C#
// C# Program to implement
// the above approach
using System;
class GFG {
     
    // Function to check if a number is equal
    // to the reverse of digits of other number
    static bool checkReverse(int leftSum, int rightSum)
    {
        // Stores reverse of
        // digits of rightSum
        int rev = 0;
      
        // Stores rightSum
        int temp = rightSum;
      
        // Calculate reverse of
        // digits of temp
        while (temp != 0) {
      
            // Update rev
            rev = (rev * 10) + (temp % 10);
      
            // Update temp
            temp /= 10;
        }
      
        // If reverse of digits of
        // rightSum equal to leftSum
        if (rev == leftSum) {
            return true;
        }
      
        return false;
    }
      
    // Function to find the index
    // that satisfies the condition
    static int findIndex(int[] arr, int N)
    {
      
        // Stores sum of array elements
        // on right side of each index
        int rightSum = 0;
      
        // Stores sum of array elements
        // on left side of each index
        int leftSum = 0;
      
        // Traverse the array
        for (int i = 0; i < N; i++) {
      
            // Update rightSum
            rightSum += arr[i];
        }
      
        // Traverse the array
        for (int i = 0; i < N; i++) {
      
            // Update rightSum
            rightSum -= arr[i];
      
            // If leftSum equal to
            // reverse of digits
            // of rightSum
            if (checkReverse(leftSum,
                             rightSum)) {
                return i;
            }
      
            // Update leftSum
            leftSum += arr[i];
        }
      
        return -1;
    }
 
  // Driver code
  static void Main()
  {
    int[] arr = { 5, 7, 3, 6, 4, 9, 2 };
    int N = arr.Length;
    Console.Write(findIndex(arr, N));
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live