Find the farthest smaller number in the right side
Last Updated :
22 Nov, 2022
Given an array arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1.
Examples:
Input: arr[] = {3, 1, 5, 2, 4}
Output: 3 -1 4 -1 -1
arr[3] is the farthest smallest element to the right of arr[0].
arr[4] is the farthest smallest element to the right of arr[2].
And for the rest of the elements, there is no smaller element to their right.
Input: arr[] = {1, 2, 3, 4, 0}
Output: 4 4 4 4 -1
Approach 1 : (Brute Force Method)
A brute force approach to this problem can be, keep a variable idx = -1 from beginning and for each element start traversing the same array from the backward upto (i+1)th index. And, if at any index j find smaller element from the current element, i.e. (a[i] > a[j]) break from the loop.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void farthest_min( int a[], int n)
{
for ( int i = 0; i < n; i++) {
int idx = -1;
for ( int j = n - 1; j > i; j--) {
if (a[i] > a[j]) {
idx = j;
break ;
}
}
cout << idx << " " ;
}
}
int main()
{
int a[] = { 3, 1, 5, 2, 4 };
int n = sizeof (a) / sizeof (a[0]);
farthest_min(a, n);
return 0;
}
|
Java
public class GFG {
static void farthest_min( int [] a, int n)
{
for ( int i = 0 ; i < n; i++) {
int idx = - 1 ;
for ( int j = n - 1 ; j > i; j--) {
if (a[i] > a[j]) {
idx = j;
break ;
}
}
System.out.print(idx + " " );
}
}
public static void main(String[] args)
{
int [] a = { 3 , 1 , 5 , 2 , 4 };
int n = a.length;
farthest_min(a, n);
}
}
|
Python3
def farthest_min(a, n):
for i in range (n):
idx = - 1
j = n - 1
while j > i:
if a[i] > a[j]:
idx = j
break
j - = 1
print (idx,end = " " )
a = [ 3 , 1 , 5 , 2 , 4 ]
n = len (a)
farthest_min(a, n)
"This Code is contributed by rajatkumargla19"
|
C#
using System;
class GFG {
static void farthest_min( int [] a, int n)
{
for ( int i = 0; i < n; i++) {
int idx = -1;
for ( int j = n - 1; j > i; j--) {
if (a[i] > a[j]) {
idx = j;
break ;
}
}
Console.Write(idx + " " );
}
}
public static void Main()
{
int [] a = { 3, 1, 5, 2, 4 };
int n = a.Length;
farthest_min(a, n);
}
}
|
Javascript
function farthest_min(a, n)
{
for (let i = 0; i < n; i++) {
let idx = -1;
for (let j = n - 1; j > i; j--) {
if (a[i] > a[j]) {
idx = j;
break ;
}
}
console.log(idx + " " );
}
}
let a = [ 3, 1, 5, 2, 4 ];
let n = a.length;
farthest_min(a, n);
|
Time Complexity : O(N^2)
Auxiliary Space : O(1)
Approach 2 : (Optimized Method)
An efficient approach is to create a suffix_min[] array where suffix_min[i] stores the minimum element from the subarray arr[i … N – 1]. Now for any element arr[i], binary search can be used on the subarray suffix_min[i + 1 … N – 1] to find the farthest smallest element to the right of arr[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void farthest_min( int a[], int n)
{
int suffix_min[n];
suffix_min[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--) {
suffix_min[i] = min(suffix_min[i + 1], a[i]);
}
for ( int i = 0; i < n; i++) {
int low = i + 1, high = n - 1, ans = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (suffix_min[mid] < a[i]) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
cout << ans << " " ;
}
}
int main()
{
int a[] = { 3, 1, 5, 2, 4 };
int n = sizeof (a) / sizeof (a[0]);
farthest_min(a, n);
return 0;
}
|
Java
class GFG {
static void farthest_min( int [] a, int n)
{
int [] suffix_min = new int [n];
suffix_min[n - 1 ] = a[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--) {
suffix_min[i]
= Math.min(suffix_min[i + 1 ], a[i]);
}
for ( int i = 0 ; i < n; i++) {
int low = i + 1 , high = n - 1 , ans = - 1 ;
while (low <= high) {
int mid = (low + high) / 2 ;
if (suffix_min[mid] < a[i]) {
ans = mid;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
System.out.print(ans + " " );
}
}
public static void main(String[] args)
{
int [] a = { 3 , 1 , 5 , 2 , 4 };
int n = a.length;
farthest_min(a, n);
}
}
|
Python3
def farthest_min(a, n):
suffix_min = [ 0 for i in range (n)]
suffix_min[n - 1 ] = a[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
suffix_min[i] = min (suffix_min[i + 1 ], a[i])
for i in range (n):
low = i + 1
high = n - 1
ans = - 1
while (low < = high):
mid = (low + high) / / 2
if (suffix_min[mid] < a[i]):
ans = mid
low = mid + 1
else :
high = mid - 1
print (ans, end = " " )
a = [ 3 , 1 , 5 , 2 , 4 ]
n = len (a)
farthest_min(a, n)
|
C#
using System;
class GFG {
static void farthest_min( int [] a, int n)
{
int [] suffix_min = new int [n];
suffix_min[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--) {
suffix_min[i]
= Math.Min(suffix_min[i + 1], a[i]);
}
for ( int i = 0; i < n; i++) {
int low = i + 1, high = n - 1, ans = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (suffix_min[mid] < a[i]) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
Console.Write(ans + " " );
}
}
public static void Main()
{
int [] a = { 3, 1, 5, 2, 4 };
int n = a.Length;
farthest_min(a, n);
}
}
|
Javascript
Javascript<script>
function farthest_min(a, n)
{
let suffix_min = new Array(n);
suffix_min[n - 1] = a[n - 1];
for (let i = n - 2; i >= 0; i--) {
suffix_min[i] = Math.min(suffix_min[i + 1], a[i]);
}
for (let i = 0; i < n; i++) {
let low = i + 1, high = n - 1, ans = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (suffix_min[mid] < a[i]) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
document.write(ans + " " );
}
}
let a = [ 3, 1, 5, 2, 4 ];
let n = a.length;
farthest_min(a, n);
</script>
|
Time Complexity: O(N* log(N) )
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...