Count of indices in Array having all prefix elements less than all in suffix
Given an array arr[], the task is to calculate the total number of indices where all elements in the left part is less than all elements in the right part of the array.
Examples:
Input: arr[] = {1, 5, 4, 2, 3, 8, 7, 9}
Output: 3
Explanation:
- Lets consider left part = [1], right part = [5, 4, 2, 3, 8, 7, 9]
Here, leftMax (1) < rightMin (2). So, it can be considered as sorted point.- Again, If we consider left part = [1, 5, 4, 2, 3], right part = [8, 7, 9]
Here also, leftMax < rightMin, So, it can also be considered as sorted point.- Similarly, If we consider left part = [1, 5, 4, 2, 3, 8, 7], right part = [9]
Here, leftMax < rightMin, So, it can also be considered as sorted point.Hence, total 3 sorted points are found.
Input: arr[] = {5, 2, 3, 4, 1}
Output: 0
Approach: The approach is based on the following idea:
The idea to solve the problem is by traversing the array and initialize two arrays to store the left part of the array and the right part of the array.
Then check if the maximum element of the left part of the array is less than the minimum element of the right part of the array.
If this condition is satisfied it is the sorted point and hence, increment the count by one and so on.
Follow the steps below to solve the given problem:
- Initialize Max = INT_MIN, Min = INT_MAX and Count = 0
- Now, create two arrays left and right of size N.
- Run one loop from start to end.
- In each iteration update Max as Max = max(Max, arr[i]) and also assign left[i] = Max
- Run another loop from end to start.
- In each iteration update Min as Min = min(Min, arr[i]) and also assign right[i] = Min
- Traverse the array from start to end.
- If, left[i] <= right[i+1], then a sorted point is achieved,
- Increment Count by 1
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to return total count // of sorted points in the array int countSortedPoints( int * arr, int N) { int left[N]; int right[N]; // Initialize the variables int Min = INT_MAX; int Max = INT_MIN; int Count = 0; // Make Maximum array for ( int i = 0; i < N; i++) { Max = max(arr[i], Max); left[i] = Max; } // Make Minimum array for ( int i = N - 1; i >= 0; i--) { Min = min(arr[i], Min); right[i] = Min; } // Count of sorted points for ( int i = 0; i < N - 1; i++) { if (left[i] <= right[i + 1]) Count++; } // Return count of sorted points return Count; } // Driver Code int main() { int arr[] = { 1, 5, 4, 2, 3, 8, 7, 9 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << countSortedPoints(arr, N); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to return total count // of sorted points in the array static int countSortedPoints( int []arr, int N) { int []left = new int [N]; int []right = new int [N]; // Initialize the variables int Min = Integer.MAX_VALUE; int Max = Integer.MIN_VALUE; int Count = 0 ; // Make Maximum array for ( int i = 0 ; i < N; i++) { Max = Math.max(arr[i], Max); left[i] = Max; } // Make Minimum array for ( int i = N - 1 ; i >= 0 ; i--) { Min = Math.min(arr[i], Min); right[i] = Min; } // Count of sorted points for ( int i = 0 ; i < N - 1 ; i++) { if (left[i] <= right[i + 1 ]) Count++; } // Return count of sorted points return Count; } // Driver Code public static void main (String[] args) { int arr[] = { 1 , 5 , 4 , 2 , 3 , 8 , 7 , 9 }; int N = arr.length; // Function call System.out.print(countSortedPoints(arr, N)); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python3 implementation of the approach INT_MIN = - 2147483648 INT_MAX = 2147483647 # Function to return total count # of sorted points in the array def countSortedPoints(arr, N): left = [ 0 for i in range (N)] right = [ 0 for i in range (N)] # Initialize the variables Min = INT_MAX Max = INT_MIN Count = 0 # Make Maximum array for i in range (N): Max = max (arr[i], Max ) left[i] = Max # Make Minimum array for i in range (N - 1 , - 1 , - 1 ): Min = min (arr[i], Min ) right[i] = Min # Count of sorted points for i in range ( 0 , N - 1 ): if (left[i] < = right[i + 1 ]): Count + = 1 # Return count of sorted points return Count # Driver Code arr = [ 1 , 5 , 4 , 2 , 3 , 8 , 7 , 9 ] N = len (arr) # Function call print (countSortedPoints(arr, N)) # This code is contributed by shinjanpatra |
C#
// C# program for the above approach using System; class GFG { // Function to return total count // of sorted points in the array static int countSortedPoints( int []arr, int N) { int []left = new int [N]; int []right = new int [N]; // Initialize the variables int Min = Int32.MaxValue; int Max = Int32.MinValue; int Count = 0; // Make Maximum array for ( int i = 0; i < N; i++) { Max = Math.Max(arr[i], Max); left[i] = Max; } // Make Minimum array for ( int i = N - 1; i >= 0; i--) { Min = Math.Min(arr[i], Min); right[i] = Min; } // Count of sorted points for ( int i = 0; i < N - 1; i++) { if (left[i] <= right[i + 1]) Count++; } // Return count of sorted points return Count; } // Driver Code public static void Main() { int []arr = { 1, 5, 4, 2, 3, 8, 7, 9 }; int N = arr.Length; // Function call Console.Write(countSortedPoints(arr, N)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript program for the above approach const INT_MIN = -2147483647 - 1; const INT_MAX = 2147483647; // Function to return total count // of sorted points in the array const countSortedPoints = (arr, N) => { let left = new Array(N).fill(0); let right = new Array(N).fill(0); // Initialize the variables let Min = INT_MAX; let Max = INT_MIN; let Count = 0; // Make Maximum array for (let i = 0; i < N; i++) { Max = Math.max(arr[i], Max); left[i] = Max; } // Make Minimum array for (let i = N - 1; i >= 0; i--) { Min = Math.min(arr[i], Min); right[i] = Min; } // Count of sorted points for (let i = 0; i < N - 1; i++) { if (left[i] <= right[i + 1]) Count++; } // Return count of sorted points return Count; } // Driver Code let arr = [1, 5, 4, 2, 3, 8, 7, 9]; let N = arr.length; // Function call document.write(countSortedPoints(arr, N)); // This code is contributed by rakeshsahni </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(N)