2) Complete the below code by making the reuse of the existing functionality. Find the index of the equilibrium element in the given array. In an array equilibrium element is the one where the sum of all the elements to the left side is equal to the sum of all the elements on the right side. Return Value: 1) Return -1 if no equilibrium element is found 2) In case there is more than one equilibrium element, return the element with the least index value. You are supposed to complete the given code by reusing the existing function. You can click on Compile & run anytime to check the compilation/execution status of the program you can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all the test cases. Code approach For the question: Correct the given implementation. Do not modify the approach. Test Case: a[] = {1,2,3,4,3,3}. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its right side sum (3+3) #include // Return the sum of elements from index 0 to (idx - 1) int left_side_sum(int a[], int n, int idx) { int sum = 0, i; for(i = 0; i < idx; i++) { sum += a[i]; } return sum; } // Return the sum of elements from index (idx + 1) to (n - 1) int right_side_sum(int a[], int n, int idx) { int sum = 0, i; for(i = idx + 1; i < n; i++) { sum += a[i]; } return sum; } // returns -1 if no equilibrium index found int findEquilibriumIndex(int a[], int n) { // Type your code here } int main() { //code int a[10], n, i; // get the elements count scanf("%d", &n); // get the array elements for(i=0; i
Find the index of the equilibrium element in the given array. In an array equilibrium element is the one where the sum of all the elements to the left side is equal to the sum of all the elements on the right side.
Return Value:
1) Return -1 if no equilibrium element is found
2) In case there is more than one equilibrium element, return the element with the least index value.
You are supposed to complete the given code by reusing the existing function. You can click on Compile & run anytime to check the compilation/execution status of the program you can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all the test cases.
Code approach For the question:
Correct the given implementation.
Do not modify the approach.
Test Case:
a[] = {1,2,3,4,3,3}. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its right side sum (3+3)
#include <stdio.h> // Return the sum of elements from index 0 to (idx - 1) int left_side_sum(int a[], int n, int idx) { int sum = 0, i; for(i = 0; i < idx; i++) { sum += a[i]; } return sum; } // Return the sum of elements from index (idx + 1) to (n - 1) int right_side_sum(int a[], int n, int idx) { int sum = 0, i; for(i = idx + 1; i < n; i++) { sum += a[i]; } return sum; } // returns -1 if no equilibrium index found int findEquilibriumIndex(int a[], int n) { // Type your code here } int main() { //code int a[10], n, i; // get the elements count scanf("%d", &n); // get the array elements for(i=0; i<n; i++) { scanf("%d", &a[i]); } int equiIndex = findEquilibriumIndex(a, n); if(equiIndex != -1) { printf("%d", a[equiIndex]); } return 0; }
Step by step
Solved in 5 steps with 3 images