Find all Array elements that are smaller than all elements to their right
Given an array arr[] containing N positive integers. The task is to find all the elements which are smaller than all the elements to their right.
Examples:
Input: arr[] = {6, 14, 13, 21, 17, 19}
Output: [6, 13, 17, 19]
Explanation: All the elements in the output are following the condition.
Input: arr[] = {10, 3, 4, 8, 7}
Output: [3, 4, 7]
Naive approach: This approach uses two loops. For each element, traverse the array to its right and check if any smaller or equal element exists or not. If all elements in the right part of the array are greater than it, then print this element.
Steps for implementation-
- Traverse the input array/vector to pick elements one by one
- Run an inner loop for every element from its next element to the end
- Now if our element is greater than or equal to any element on its right then break the inner loop
- If the inner loop is broken then leave that element else print that element
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void FindDesiredElements(vector< int >& arr)
{
int n = arr.size();
for ( int i = 0; i < n; i++) {
int j = i + 1;
while (j < n) {
if (arr[i] >= arr[j]) {
break ;
}
j++;
}
if (j == n) {
cout << arr[i] << " " ;
}
}
}
int main()
{
vector< int > arr = { 6, 14, 13, 21, 17, 19 };
FindDesiredElements(arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class GFG {
static void findDesiredElements(List<Integer> arr) {
int n = arr.size();
for ( int i = 0 ; i < n; i++) {
int j = i + 1 ;
while (j < n) {
if (arr.get(i) >= arr.get(j)) {
break ;
}
j++;
}
if (j == n) {
System.out.print(arr.get(i) + " " );
}
}
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
arr.add( 6 );
arr.add( 14 );
arr.add( 13 );
arr.add( 21 );
arr.add( 17 );
arr.add( 19 );
findDesiredElements(arr);
}
}
|
Python3
def find_desired_elements(arr):
n = len (arr)
for i in range (n):
j = i + 1
while j < n:
if arr[i] > = arr[j]:
break
j + = 1
if j = = n:
print (arr[i], end = ' ' )
if __name__ = = '__main__' :
arr = [ 6 , 14 , 13 , 21 , 17 , 19 ]
find_desired_elements(arr)
|
C#
using System;
using System.Collections.Generic;
class Program {
static void Main( string [] args) {
List< int > arr = new List< int > { 6, 14, 13, 21, 17, 19 };
FindDesiredElements(arr);
}
static void FindDesiredElements(List< int > arr) {
int n = arr.Count;
for ( int i = 0; i < n; i++) {
int j = i + 1;
while (j < n) {
if (arr[i] >= arr[j]) {
break ;
}
j++;
}
if (j == n) {
Console.Write(arr[i] + " " );
}
}
}
}
|
Javascript
function findDesiredElements(arr) {
var n = arr.length;
for ( var i = 0; i < n; i++) {
var j = i + 1;
while (j < n) {
if (arr[i] >= arr[j]) {
break ;
}
j++;
}
if (j == n) {
process.stdout.write(arr[i] + " " );
}
}
}
var arr = [6, 14, 13, 21, 17, 19];
findDesiredElements(arr);
|
Output-
6 13 17 19
Time complexity: O(N*N), because of two nested for loops
Auxiliary Space: O(1),because no extra space has been used
Efficient approach: In the efficient approach, the idea is to use a Stack. Follow the steps mentioned below:
- Iterate the array from the beginning of the array.
- For every element in the array, pop all the elements present in the stack that are greater than it and then push it into the stack.
- If no element is greater than it, then the current element is an answer.
- At last, the stack remains with elements that are smaller than all elements present to their right.
Below is the code implementation of the above approach.
C++
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
void FindDesiredElements(vector< int > const & arr)
{
stack< int > stk;
for ( int i : arr) {
while (!stk.empty() && stk.top() > i) {
stk.pop();
}
stk.push(i);
}
while (!stk.empty()) {
cout << stk.top() << " " ;
stk.pop();
}
}
int main()
{
vector< int > arr = { 6, 14, 13, 21, 17, 19 };
FindDesiredElements(arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Stack;
class GFG {
static void FindDesiredElements(ArrayList<Integer> arr)
{
Stack<Integer> stk = new Stack<Integer>();
for ( int i : arr)
{
while (!stk.empty() && stk.peek() > i) {
stk.pop();
}
stk.push(i);
}
while (!stk.empty()) {
System.out.print(stk.peek() + " " );
stk.pop();
}
}
public static void main(String args[])
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 6 );
arr.add( 14 );
arr.add( 13 );
arr.add( 21 );
arr.add( 17 );
arr.add( 19 );
FindDesiredElements(arr);
}
}
|
Python3
def FindDesiredElements(arr):
stk = []
for i in arr :
while ( len (stk)! = 0 and stk[ len (stk) - 1 ] > i):
stk.pop()
stk.append(i)
while ( len (stk) ! = 0 ):
print (stk[ len (stk) - 1 ],end = " " )
stk.pop()
arr = []
arr.append( 6 )
arr.append( 14 )
arr.append( 13 )
arr.append( 21 )
arr.append( 17 )
arr.append( 19 )
FindDesiredElements(arr)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void FindDesiredElements(List< int > arr) {
Stack< int > stk = new Stack< int >();
foreach ( int i in arr) {
while (stk.Count!=0 && stk.Peek() > i) {
stk.Pop();
}
stk.Push(i);
}
while (stk.Count>0) {
Console.Write(stk.Peek() + " " );
stk.Pop();
}
}
public static void Main(String []args) {
List< int > arr = new List< int >();
arr.Add(6);
arr.Add(14);
arr.Add(13);
arr.Add(21);
arr.Add(17);
arr.Add(19);
FindDesiredElements(arr);
}
}
|
Javascript
<script>
function FindDesiredElements( arr) {
var stk = [];
for ( var i of arr) {
while (stk.length!=0 && stk[stk.length-1] > i) {
stk.pop();
}
stk.push(i);
}
while (stk.length != 0) {
document.write(stk[stk.length-1] + " " );
stk.pop();
}
}
var arr = [];
arr.push(6);
arr.push(14);
arr.push(13);
arr.push(21);
arr.push(17);
arr.push(19);
FindDesiredElements(arr);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Space Optimized approach: The idea is to traverse the array from right to left (reverse order) and maintain an auxiliary variable that stores the minimum element found so far. This approach will neglect the use of stack. Follow the below steps:
- Start iterating from the end of the array.
- If the current element is smaller than the minimum so far, then the element is found. Update the value storing minimum value so far. Print all such values as the answer.
Below is the implementation of the above approach.
C++
#include <iostream>
#include <limits.h>
using namespace std;
void FindDesiredElements( int arr[], int n)
{
int min_so_far = INT_MAX;
for ( int j = n - 1; j >= 0; j--) {
if (arr[j] <= min_so_far) {
min_so_far = arr[j];
cout << arr[j] << " " ;
}
}
}
int main()
{
int arr[] = { 6, 14, 13, 21, 17, 19 };
int N = sizeof (arr) / sizeof (arr[0]);
FindDesiredElements(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void FindDesiredElements( int [] arr, int n)
{
int min_so_far = Integer.MAX_VALUE;
for ( int j = n - 1 ; j >= 0 ; j--)
{
if (arr[j] <= min_so_far) {
min_so_far = arr[j];
System.out.print(arr[j] + " " );
}
}
}
public static void main(String[] args)
{
int [] arr = { 6 , 14 , 13 , 21 , 17 , 19 };
int N = arr.length;
FindDesiredElements(arr, N);
}
}
|
Python3
def FindDesiredElements(arr, n):
min_so_far = 10 * * 9 ;
for j in range (n - 1 , - 1 , - 1 ):
if (arr[j] < = min_so_far):
min_so_far = arr[j];
print (arr[j], end = " " )
arr = [ 6 , 14 , 13 , 21 , 17 , 19 ];
N = len (arr)
FindDesiredElements(arr, N);
|
C#
using System;
class GFG {
static void FindDesiredElements( int [] arr, int n)
{
int min_so_far = Int32.MaxValue;
for ( int j = n - 1; j >= 0; j--) {
if (arr[j] <= min_so_far) {
min_so_far = arr[j];
Console.Write(arr[j] + " " );
}
}
}
public static void Main()
{
int [] arr = { 6, 14, 13, 21, 17, 19 };
int N = arr.Length;
FindDesiredElements(arr, N);
}
}
|
Javascript
<script>
function FindDesiredElements(arr, n)
{
let min_so_far = Number.MAX_VALUE;
for (let j = n - 1; j >= 0; j--)
{
if (arr[j] <= min_so_far) {
min_so_far = arr[j];
document.write(arr[j] + " " );
}
}
}
let arr = [6, 14, 13, 21, 17, 19];
let N = arr.length;
FindDesiredElements(arr, N);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
01 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...