Find the index having sum of elements on its left equal to reverse of the sum of elements on its right
Given an array, arr[] size N, the task is to find the smallest index of the array such that the sum of all the array elements on the left side of the index equal to the reverse of the digits of the sum of all the array elements on the right side of that index. If no such index found then print -1.
Examples:
Input: arr[] = { 5, 7, 3, 6, 4, 9, 2 }
Output: 2
Explanation:
Sum of array elements on left side of index 2 = (5 + 7) = 12
Sum of array elements on right side of index 2 = (6 + 4 + 9 + 2) = 21
Since sum of array elements on left side of index 2 equal to the reverse of the digits of sum of elements on the right side of index 2. Therefore, the required output is 2.Input: arr[] = { 1, 3, 6, 8, 9, 2 }
Output: -1
Naive Approach: The simplest approach to solve this problem is to traverse the array and for each index, check if the sum of elements on the left side of the index is equal to the reverse of the digits of the sum of the array elements on the right side of that index or not. If found to be true, then print that index. Otherwise, if no such index is found, print -1.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is to use prefix-sum technique. Follow the steps below to solve the problem:
- Initialize a variable, say rightSum, to store the sum of the array elements on the right side of each index.
- Initialize a variable, say leftSum, to store the sum of array elements on the left side of each index.
- Traverse the array using variable i and update the value of rightSum -= arr[i], leftSum += arr[i] and check if leftSum equal to the reverse of digits of rightSum or not. If found to be true then print i.
- Otherwise, print -1.
Below is the implementation of the above approach.
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> 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
<script> // Javascript program for the above approach // Function to check if a number is equal // to the reverse of digits of other number function checkReverse(leftSum , rightSum) { // Stores reverse of // digits of rightSum var rev = 0; // Stores rightSum var temp = rightSum; // Calculate reverse of // digits of temp while (temp != 0) { // Update rev rev = (rev * 10) + (temp % 10); // Update temp temp = parseInt(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 function findIndex(arr , N) { // Stores sum of array elements // on right side of each index var rightSum = 0; // Stores sum of array elements // on left side of each index var leftSum = 0; // Traverse the array for (i = 0; i < N; i++) { // Update rightSum rightSum += arr[i]; } // Traverse the array for (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 var arr = [ 5, 7, 3, 6, 4, 9, 2 ]; var N = arr.length; document.write(findIndex(arr, N)); // This code contributed by aashish1995 </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)