Count of positions j in Array such that arr[i] is maximum in index range [i, j] with end points as same
Last Updated :
17 Jul, 2021
Given an array arr[] consisting of N positive integers, the task is to find all j such that arr[j] = arr[i] and all the numbers in the range [min(j, i), max(j, i)] is less than or equal to arr[i] where 1 ≤ i ≤ N.
Examples:
Input: arr[] = {4, 7, 7, 9, 7}, N = 5
Output: 1 2 2 1 1
Explanation:
For i = 1, j = 1 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[1].
For i = 2, j = 2 and 3 are the elements such that arr[i] = arr[j] and no elements in the middle has value greater than arr[2].
For i = 3, j = 2 and 3 are the elements such that arr[i] = arr[j] and no elements in the middle has value greater than arr[3].
For i = 4, j = 4 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[4].
For i = 5, j = 5 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[5].
Input: arr[] = {1, 2, 1, 2, 4}, N = 5
Output: 1 2 1 2 1
Approach: The simplest way to solve the problem is to use two nested for loops to traverse the array and find the pairs such that arr[i] = arr[j] and no element in the range [i, j] is greater than arr[i]. Follow the steps below to solve the problem:
- Initialize an array, say ans[] that stores the answer for all the elements in the range [0, N-1].
- Iterate in the range[N-1, 0] using the variable i and perform the following steps:
- Iterate in the range[i, 0] using the variable j and perform the following steps:
- If arr[j] = arr[i], then increment the value of ans[i] by 1.
- If arr[j] > arr[i], then terminate the loop.
- Iterate in the range[i+1, N-1] using the variable j and perform the following steps:
- If arr[j] = arr[i], then increment the value of ans[i] by 1.
- If arr[j] > arr[i], then terminate the loop.
- After completing the above steps, print the array ans[] as the answer.
Below is the implementation of the above approach
C++
#include <iostream>
using namespace std;
void findElements( int arr[], int N)
{
int ans[N];
for ( int i = 0; i < N; i++) {
ans[i] = 0;
}
for ( int i = N - 1; i >= 0; i--) {
for ( int j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
ans[i]++;
}
else
if (arr[j] > arr[i])
break ;
}
for ( int j = i + 1; j < N; j++) {
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i]) {
break ;
}
}
}
for ( int i = 0; i < N; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 1, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
findElements(arr, N);
}
|
Java
public class GFG
{
static void findElements( int arr[], int N)
{
int ans[] = new int [N];
for ( int i = 0 ; i < N; i++) {
ans[i] = 0 ;
}
for ( int i = N - 1 ; i >= 0 ; i--) {
for ( int j = i; j >= 0 ; j--) {
if (arr[j] == arr[i]) {
ans[i]++;
}
else
if (arr[j] > arr[i])
break ;
}
for ( int j = i + 1 ; j < N; j++)
{
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i]) {
break ;
}
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(ans[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 2 , 4 };
int N = arr.length;
findElements(arr, N);
}
}
|
Python3
def findElements(arr, N):
ans = [ 0 for i in range (N)]
for i in range (N - 1 , - 1 , - 1 ):
for j in range (i, - 1 , - 1 ):
if arr[j] = = arr[i]:
ans[i] + = 1
else :
if arr[j] > arr[i]:
break
for j in range (i + 1 , N):
if arr[j] = = arr[i]:
ans[i] + = 1
else :
if arr[j] > arr[i]:
break
for i in range (N):
print (ans[i], end = " " )
return
if __name__ = = '__main__' :
arr = [ 1 , 2 , 1 , 2 , 4 ]
N = len (arr)
findElements(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findElements( int []arr, int N)
{
int []ans = new int [N];
for ( int i = 0; i < N; i++)
{
ans[i] = 0;
}
for ( int i = N - 1; i >= 0; i--)
{
for ( int j = i; j >= 0; j--)
{
if (arr[j] == arr[i])
{
ans[i]++;
}
else
if (arr[j] > arr[i])
break ;
}
for ( int j = i + 1; j < N; j++)
{
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i])
{
break ;
}
}
}
for ( int i = 0; i < N; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main()
{
int []arr = { 1, 2, 1, 2, 4 };
int N = arr.Length;
findElements(arr, N);
}
}
|
Javascript
<script>
function findElements(arr, N) {
let ans = new Array(N);
for (let i = 0; i < N; i++) {
ans[i] = 0;
}
for (let i = N - 1; i >= 0; i--) {
for (let j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
ans[i]++;
}
else
if (arr[j] > arr[i])
break ;
}
for (let j = i + 1; j < N; j++) {
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i]) {
break ;
}
}
}
for (let i = 0; i < N; i++) {
document.write(ans[i] + " " );
}
}
let arr = [1, 2, 1, 2, 4];
let N = arr.length
findElements(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...