📜  找到索引,其左侧的元素总数等于其右侧的元素总数之和

📅  最后修改于: 2021-05-17 23:13:14             🧑  作者: Mango

给定一个数组, arr []的大小为N ,任务是找到数组的最小索引,以使索引左侧的所有数组元素的总和等于所有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)