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.
-
Iterate in the range[i, 0] using the variable j and perform the following steps:
- After completing the above steps, print the array ans[] as the answer.
Below is the implementation of the above approach
// C++ program for the above approach #include <iostream> using namespace std;
// Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] void findElements( int arr[], int N)
{ // To Store answer
int ans[N];
// Initialize the value of ans[i] as 0
for ( int i = 0; i < N; i++) {
ans[i] = 0;
}
// Traverse the array in reverse direction
for ( int i = N - 1; i >= 0; i--) {
// Traverse in the range [i, 0]
for ( int j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break ;
}
// Traverse in the range [i+1, N-1]
for ( int j = i + 1; j < N; j++) {
// Increment ans[i] if arr[i] = arr[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] << " " ;
}
} // Driver Code int main()
{ // Given Input
int arr[] = { 1, 2, 1, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
// Function Call
findElements(arr, N);
} |
// Java program for the above approach public class GFG
{ // Function to find j such that arr[j]=arr[i]
// and no element is greater than arr[i] in the
// range [j, i]
static void findElements( int arr[], int N)
{
// To Store answer
int ans[] = new int [N];
// Initialize the value of ans[i] as 0
for ( int i = 0 ; i < N; i++) {
ans[i] = 0 ;
}
// Traverse the array in reverse direction
for ( int i = N - 1 ; i >= 0 ; i--) {
// Traverse in the range [i, 0]
for ( int j = i; j >= 0 ; j--) {
if (arr[j] == arr[i]) {
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break ;
}
// Traverse in the range [i+1, N-1]
for ( int j = i + 1 ; j < N; j++)
{
// Increment ans[i] if arr[i] = arr[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] + " " );
}
}
// Driver code
public static void main(String[] args)
{
// Given Input
int arr[] = { 1 , 2 , 1 , 2 , 4 };
int N = arr.length;
// Function Call
findElements(arr, N);
}
} // This code is contributed by abhinavjain194 |
# Python3 program for the above approach # Function to find j such that arr[j]=arr[i] # and no element is greater than arr[i] in the # range [j, i] def findElements(arr, N):
# Initialising a list to store ans
# Initialize the value of ans[i] as 0
ans = [ 0 for i in range (N)]
# Traverse the array in reverse direction
for i in range (N - 1 , - 1 , - 1 ):
# Traverse in the range [i, 0]
for j in range (i, - 1 , - 1 ):
if arr[j] = = arr[i]:
# Increment ans[i] if arr[j] = arr[i]
ans[i] + = 1
else :
# Terminate the loop
if arr[j] > arr[i]:
break
# Traverse in the range [i+1, N-1]
for j in range (i + 1 , N):
# Increment ans[i] if arr[j] = arr[i]
if arr[j] = = arr[i]:
ans[i] + = 1
else :
if arr[j] > arr[i]:
break
# Print the ans
for i in range (N):
print (ans[i], end = " " )
return
# Driver Code if __name__ = = '__main__' :
# Given Input
arr = [ 1 , 2 , 1 , 2 , 4 ]
N = len (arr)
# Function call
findElements(arr, N)
# This code is contributed by MuskanKalra1 |
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] static void findElements( int []arr, int N)
{ // To Store answer
int []ans = new int [N];
// Initialize the value of ans[i] as 0
for ( int i = 0; i < N; i++)
{
ans[i] = 0;
}
// Traverse the array in reverse direction
for ( int i = N - 1; i >= 0; i--)
{
// Traverse in the range [i, 0]
for ( int j = i; j >= 0; j--)
{
if (arr[j] == arr[i])
{
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break ;
}
// Traverse in the range [i+1, N-1]
for ( int j = i + 1; j < N; j++)
{
// Increment ans[i] if arr[i] = arr[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] + " " );
}
} // Driver Code public static void Main()
{ // Given Input
int []arr = { 1, 2, 1, 2, 4 };
int N = arr.Length;
// Function Call
findElements(arr, N);
} } // This code is contributed by SURENDRA_GANGWAR |
<script> // JavaScript program for the above approach // Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] function findElements(arr, N) {
// To Store answer
let ans = new Array(N);
// Initialize the value of ans[i] as 0
for (let i = 0; i < N; i++) {
ans[i] = 0;
}
// Traverse the array in reverse direction
for (let i = N - 1; i >= 0; i--) {
// Traverse in the range [i, 0]
for (let j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break ;
}
// Traverse in the range [i+1, N-1]
for (let j = i + 1; j < N; j++) {
// Increment ans[i] if arr[i] = arr[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] + " " );
}
} // Driver Code // Given Input let arr = [1, 2, 1, 2, 4]; let N = arr.length // Function Call findElements(arr, N); </script> |
1 2 1 2 1
Time Complexity: O(N2)
Auxiliary Space: O(N)