![]() |
VOOZH | about |
Given an array a[] of size N which consists of both positive and negative integers, the task is to find the index of the array for which the average of all the even elements on its LHS is Greater than the average of all the odd elements on its RHS (Follow 0-based Indexing).
Note:
- The First Index and Last Index Cannot be valid answers because the first index will not have any elements on its LHS as well as last index will not have any elements on its RHS.
- If there are multiple indices satisfying the above condition then return the smallest valid index as an answer.
- Else if there is no such index in the given array then return -1.
Examples:
Input: a[] = {4, 6, 1, 6, 5, 3}
Output: 1
Explanation: The average of even numbers in the array before index 1 is 4 (elements = {4}) and the average of odd numbers in array a[] after index 1 (elements = { 1, 3, 5} ) is 3. Here Floor Value Is Considered While calculating the average hence smallest valid index is 1.Input: a[] = { 1,5,3,4,6}
Output: -1
Explanation: In the given array there is no such index that satisfies the above condition because
- LHS Even average for every index in the given array is {0, 0, 0, 0, 4}
- RHS Odd average for every index in the given array is {2, 3, 0, 0, 0}
- There is no such index that satisfies the given condition.
- Hence - 1 is the answer.
Approach: To solve the problem follow the below steps:
Below is the code to implement the above approach:
1
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(N), where N is the length of the array.