https://www.geeksforgeeks.org/minimum-elements-to-change-so-that-for-an-index-i-all-elements-on-the-left-are-ve-and-all-elements-on-the-right-are-ve/
Given an array arr of size n, the task is to find the minimum number of elements that should be changed (element value can be changed to anything) so that there exists an index 0 ≤ i ≤ n-2 such that:
- All the numbers in range 0 to i (inclusive) are < 0.
- All numbers in range i+1 to n-1 (inclusive) are > 0.
Examples:
Input: arr[] = {-1, -2, -3, 3, -5, 3, 4}
Output: 1
Change -5 to 5 and the array becomes {-1, -2, -3, 3, 5, 3, 4}Input: arr[] = {3, -5}
Output: 2
Change 3 to -3 and -5 to 5
Approach: Fix the value of i, what changes would we need to make index i the required index? Change all the positive elements on the left of i to negative and all negative elements to the right of i to positive. Hence the number of operations required would be:
(Number of positive terms on the left of i) + (Number of negative terms on the right of i)
To find the required terms, we can pre-compute them using suffix sum.
Hence, we try each i as the required index and choose the one which needs minimum changes.
(Number of positive terms on the left of i) + (Number of negative terms on the right of i)
To find the required terms, we can pre-compute them using suffix sum.
Hence, we try each i as the required index and choose the one which needs minimum changes.