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
Naive Approach:
The naive approach is that we traverse each element of the array and for each element find maximum element say max in the left side which is including itself also and find minimum element say min in the right side which is after ith element. Finding maximum and minimum will be requiring another loop traversal. Now, check if max is less than min or not. If lesser then increase the count of elements, else continue.
Algorithm:
- Define function countIndices(arr, n) that takes an integer array ‘arr‘ and its size ‘n’ as input.
- Initialize a variable ‘count‘ to 0
- Traverse the array ‘arr‘ from index 0 to n-1:
- Initialize a variable ‘max_left‘ to INT_MIN
- Traverse the left side of ‘arr‘ including the current element and find the maximum element among them, store it in ‘max_left‘
- Initialize a variable ‘min_right‘ to INT_MAX
- Traverse the right side of ‘arr‘ after the current element and find the minimum element among them, store it in ‘min_right‘
- If the value of ‘min_right‘ is not INT_MAX and ‘max_left‘ is less than ‘min_right‘, then increment the value of ‘count‘.
- Return the value of ‘count‘.
- Define the main function
- Initialize an integer array ‘arr‘ with given elements and its size ‘n’ to the size of the array
- Call the function ‘countIndices‘ with ‘arr‘ and ‘n‘ as input
- Print the returned value from the function
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countIndices( int arr[], int n) {
int count = 0;
for ( int i = 0; i < n; i++) {
int max_left = INT_MIN;
int j;
for (j = 0; j <= i; j++) {
max_left = max(max_left, arr[j]);
}
int min_right = INT_MAX;
for ( int k = i + 1; k < n; k++) {
min_right = min(min_right, arr[k]);
}
if (min_right != INT_MAX && max_left < min_right) {
count++;
}
}
return count;
}
int main() {
int arr[] = { 1, 5, 4, 2, 3, 8, 7, 9 };
int n = sizeof (arr) / sizeof ( int );
int result = countIndices(arr, n);
cout << result << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int countIndices( int arr[], int n) {
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int max_left = Integer.MIN_VALUE;
int j;
for (j = 0 ; j <= i; j++) {
max_left = Math.max(max_left, arr[j]);
}
int min_right = Integer.MAX_VALUE;
for ( int k = i + 1 ; k < n; k++) {
min_right = Math.min(min_right, arr[k]);
}
if (min_right != Integer.MAX_VALUE && max_left < min_right) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int arr[] = { 1 , 5 , 4 , 2 , 3 , 8 , 7 , 9 };
int n = arr.length;
int result = countIndices(arr, n);
System.out.println(result);
}
}
|
Javascript
function GFG(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
let max_left = -Infinity;
for (let j = 0; j <= i; j++) {
max_left = Math.max(max_left, arr[j]);
}
let min_right = Infinity;
for (let k = i + 1; k < arr.length; k++) {
min_right = Math.min(min_right, arr[k]);
}
if (min_right !== Infinity && max_left < min_right) {
count++;
}
}
return count;
}
const arr = [1, 5, 4, 2, 3, 8, 7, 9];
const result = GFG(arr);
console.log(result);
|
Time Complexity: O(n*n) where n is size of input array. This is because two nested for loops are being executed in function countIndices.
Auxiliary Space: O(1) as no extra space has been used.
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,
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSortedPoints( int * arr, int N)
{
int left[N];
int right[N];
int Min = INT_MAX;
int Max = INT_MIN;
int Count = 0;
for ( int i = 0; i < N; i++) {
Max = max(arr[i], Max);
left[i] = Max;
}
for ( int i = N - 1; i >= 0; i--) {
Min = min(arr[i], Min);
right[i] = Min;
}
for ( int i = 0; i < N - 1; i++) {
if (left[i] <= right[i + 1])
Count++;
}
return Count;
}
int main()
{
int arr[] = { 1, 5, 4, 2, 3, 8, 7, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countSortedPoints(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSortedPoints( int []arr, int N)
{
int []left = new int [N];
int []right = new int [N];
int Min = Integer.MAX_VALUE;
int Max = Integer.MIN_VALUE;
int Count = 0 ;
for ( int i = 0 ; i < N; i++) {
Max = Math.max(arr[i], Max);
left[i] = Max;
}
for ( int i = N - 1 ; i >= 0 ; i--) {
Min = Math.min(arr[i], Min);
right[i] = Min;
}
for ( int i = 0 ; i < N - 1 ; i++) {
if (left[i] <= right[i + 1 ])
Count++;
}
return Count;
}
public static void main (String[] args) {
int arr[] = { 1 , 5 , 4 , 2 , 3 , 8 , 7 , 9 };
int N = arr.length;
System.out.print(countSortedPoints(arr, N));
}
}
|
Python3
INT_MIN = - 2147483648
INT_MAX = 2147483647
def countSortedPoints(arr, N):
left = [ 0 for i in range (N)]
right = [ 0 for i in range (N)]
Min = INT_MAX
Max = INT_MIN
Count = 0
for i in range (N):
Max = max (arr[i], Max )
left[i] = Max
for i in range (N - 1 , - 1 , - 1 ):
Min = min (arr[i], Min )
right[i] = Min
for i in range ( 0 , N - 1 ):
if (left[i] < = right[i + 1 ]):
Count + = 1
return Count
arr = [ 1 , 5 , 4 , 2 , 3 , 8 , 7 , 9 ]
N = len (arr)
print (countSortedPoints(arr, N))
|
C#
using System;
class GFG
{
static int countSortedPoints( int []arr, int N)
{
int []left = new int [N];
int []right = new int [N];
int Min = Int32.MaxValue;
int Max = Int32.MinValue;
int Count = 0;
for ( int i = 0; i < N; i++) {
Max = Math.Max(arr[i], Max);
left[i] = Max;
}
for ( int i = N - 1; i >= 0; i--) {
Min = Math.Min(arr[i], Min);
right[i] = Min;
}
for ( int i = 0; i < N - 1; i++) {
if (left[i] <= right[i + 1])
Count++;
}
return Count;
}
public static void Main()
{
int []arr = { 1, 5, 4, 2, 3, 8, 7, 9 };
int N = arr.Length;
Console.Write(countSortedPoints(arr, N));
}
}
|
Javascript
<script>
const INT_MIN = -2147483647 - 1;
const INT_MAX = 2147483647;
const countSortedPoints = (arr, N) => {
let left = new Array(N).fill(0);
let right = new Array(N).fill(0);
let Min = INT_MAX;
let Max = INT_MIN;
let Count = 0;
for (let i = 0; i < N; i++) {
Max = Math.max(arr[i], Max);
left[i] = Max;
}
for (let i = N - 1; i >= 0; i--) {
Min = Math.min(arr[i], Min);
right[i] = Min;
}
for (let i = 0; i < N - 1; i++) {
if (left[i] <= right[i + 1])
Count++;
}
return Count;
}
let arr = [1, 5, 4, 2, 3, 8, 7, 9];
let N = arr.length;
document.write(countSortedPoints(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)