Find 132 Pattern from given Array
Given an array arr[] of size N. The task is to check if the array has 3 elements in indices i, j and k such that i < j < k and arr[i] < arr[j] > arr[k] and arr[i] < arr[k].
Examples:
Input: N = 6, arr[] = {4, 7, 11, 5, 13, 2}
Output: True
Explanation: [4, 7, 5] fits the condition.
Input: N = 4, arr[] = {11, 11, 12, 9}
Output: False
Explanation: No 3 elements fit the given condition.
Naive Approach: The simplest approach is to have 3 nested loops, each loop represents the possible moves of their indexes, i.e. the first loop shows the all possible indices values of i, the second nested loop shows the possible indices of j, for each value of i, and the third nested loop tells the all possible values of k index, for each value of j and i. Then, check the given condition, arr[i] < arr[k] < arr[j], if it’s true then return true, else return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool recreationalSpot( int a[], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
if (a[k] < a[j] && a[k] > a[i])
return true ;
}
}
}
return false ;
}
int main()
{
int arr[] = { 4, 7, 11, 5, 13, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
if (recreationalSpot(arr, N)) {
cout << "True" ;
}
else {
cout << "False" ;
}
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static boolean recreationalSpot( int [] a, int n) {
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
for ( int k = j + 1 ; k < n; k++) {
if (a[k] < a[j] && a[k] > a[i]) {
return true ;
}
}
}
}
return false ;
}
public static void main(String[] args) {
int [] arr = { 4 , 7 , 11 , 5 , 13 , 2 };
int N = arr.length;
if (recreationalSpot(arr, N)) {
System.out.println( "True" );
} else {
System.out.println( "False" );
}
}
}
|
Python
def recreationalSpot(a, n):
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
if a[k] < a[j] and a[k] > a[i]:
return True
return False
arr = [ 4 , 7 , 11 , 5 , 13 , 2 ]
N = len (arr)
if recreationalSpot(arr, N):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class Program
{
static bool RecreationalSpot( int [] a, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
for ( int k = j + 1; k < n; k++)
{
if (a[k] < a[j] && a[k] > a[i])
{
return true ;
}
}
}
}
return false ;
}
static void Main()
{
int [] arr = { 4, 7, 11, 5, 13, 2 };
int N = arr.Length;
if (RecreationalSpot(arr, N))
{
Console.WriteLine( "True" );
}
else
{
Console.WriteLine( "False" );
}
}
}
|
Javascript
function recreationalSpot(a, n) {
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
if (a[k] < a[j] && a[k] > a[i]) {
return true ;
}
}
}
}
return false ;
}
const arr = [4, 7, 11, 5, 13, 2];
const N = arr.length;
if (recreationalSpot(arr, N)) {
console.log( "True" );
} else {
console.log( "False" );
}
|
Time Complexity: O(N3).
Auxiliary Space: O(1).
Efficient Approach: The problem can be solved using the following idea:
Traverse the array from N-1 to 0 and check for every ith element if the greatest element on the right which is smaller than ith element is greater than the smallest element on the left of i then true else false.
To find the greatest element smaller than ith element we can use Next Greater Element
Follow the steps mentioned below to implement the idea:
- Create a vector small[].
- Traverse the array arr[] and maintain a min value that is the smallest value of arr[0, . . ., i].
- If there is no element smaller than Arr[i] store -1 else store min.
- Initialize an empty stack (say S). Run a loop from N-1 to 0:
- If stack is not empty and top element in stack <= small[i], then pop the element;
- If stack is not empty and small[i] < Top element in stack < arr[i] then return true.
- Otherwise, push arr[i] into stack.
- If the condition is not satisfied, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool recreationalSpot( int arr[], int n)
{
vector< int > small;
int min1 = arr[0];
for ( int i = 0; i < n; i++) {
if (min1 >= arr[i]) {
min1 = arr[i];
small.push_back(-1);
}
else {
small.push_back(min1);
}
}
stack< int > s;
for ( int i = n - 1; i > 0; i--) {
while (!s.empty() && s.top() <= small[i]) {
s.pop();
}
if (!s.empty() && small[i] != -1
&& s.top() < arr[i])
return true ;
s.push(arr[i]);
}
return false ;
}
int main()
{
int arr[] = { 4, 7, 11, 5, 13, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
if (recreationalSpot(arr, N)) {
cout << "True" ;
}
else {
cout << "False" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static boolean recreationalSpot( int [] arr, int n)
{
List<Integer> small = new ArrayList<>();
int min1 = arr[ 0 ];
for ( int i = 0 ; i < n; i++) {
if (min1 >= arr[i]) {
min1 = arr[i];
small.add(- 1 );
}
else {
small.add(min1);
}
}
Stack<Integer> s = new Stack<>();
for ( int i = n - 1 ; i > 0 ; i--) {
while (!s.isEmpty()
&& s.peek() <= small.get(i)) {
s.pop();
}
if (!s.isEmpty() && small.get(i) != - 1
&& s.peek() < arr[i]) {
return true ;
}
s.push(arr[i]);
}
return false ;
}
public static void main(String[] args)
{
int [] arr = { 4 , 7 , 11 , 5 , 13 , 2 };
int N = arr.length;
if (recreationalSpot(arr, N)) {
System.out.print( "True" );
}
else {
System.out.print( "False" );
}
}
}
|
Python3
def recreationalSpot(arr, n) :
small = [];
min1 = arr[ 0 ];
for i in range (n) :
if (min1 > = arr[i]) :
min1 = arr[i];
small.append( - 1 );
else :
small.append(min1);
s = [];
for i in range (n - 1 , 0 , - 1 ) :
while ( len (s) ! = 0 and s[ - 1 ] < = small[i]) :
s.pop();
if ( len (s) ! = 0 and small[i] ! = - 1 and s[ - 1 ] < arr[i]) :
return True ;
s.append(arr[i]);
return False ;
if __name__ = = "__main__" :
arr = [ 4 , 7 , 11 , 5 , 13 , 2 ];
N = len (arr);
if (recreationalSpot(arr, N)) :
print ( "True" );
else :
print ( "False" );
|
C#
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class GFG {
static bool recreationalSpot( int [] arr, int n)
{
List< int > small = new List< int >();
int min1 = arr[0];
for ( int i = 0; i < n; i++) {
if (min1 >= arr[i]) {
min1 = arr[i];
small.Add(-1);
}
else {
small.Add(min1);
}
}
Stack s = new Stack();
for ( int i = n - 1; i > 0; i--) {
while (s.Count > 0 && arr[( int )s.Peek()] <= small[i]) {
s.Pop();
}
if (s.Count > 0 && small[i] != -1
&& Convert.ToInt32(s.Peek()) < arr[i]) {
return true ;
}
s.Push(arr[i]);
}
return false ;
}
public static void Main()
{
int [] arr = { 4, 7, 11, 5, 13, 2 };
int N = arr.Length;
if (recreationalSpot(arr, N)) {
Console.Write( "True" );
}
else {
Console.Write( "False" );
}
}
}
|
Javascript
<script>
function recreationalSpot(arr, n)
{
small = [];
let min1 = arr[0];
for (let i = 0; i < n; i++) {
if (min1 >= arr[i]) {
min1 = arr[i];
small.push(-1);
}
else {
small.push(min1);
}
}
s = [];
for (let i = n - 1; i > 0; i--) {
while (s.length>0 && s[s.length-1] <= small[i]) {
s.pop();
}
if (s.length>0 && small[i] != -1
&& s[s.length-1] < arr[i])
return true ;
s.push(arr[i]);
}
return false ;
}
let arr = [ 4, 7, 11, 5, 13, 2 ];
let N = arr.length;
if (recreationalSpot(arr, N) == true ) {
console.log( "True" );
}
else {
console.log( "False" );
}
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N).
Last Updated :
13 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...