Find a partition point in array
Given an unsorted array of integers. Find an element such that all the elements to its left are smaller and to its right are greater. Print -1 if no such element exists.
Note that there can be more than one such elements. For example an array which is sorted in increasing order all elements follow the property. We need to find only one such element.
Examples :
Input : A[] = {4, 3, 2, 5, 8, 6, 7}
Output : 5
Input : A[] = {5, 6, 2, 8, 10, 9, 8}
Output : -1
Simple solution takes O(n2). Idea is to pick each array element one by one and for each element we have to check it is greater than all the elements to its left side and smaller than all the elements to its right side.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int FindElement( int A[], int n)
{
for ( int i = 0; i < n; i++) {
int flag = 0;
for ( int j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break ;
}
for ( int j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break ;
}
if (flag == 0)
return A[i];
}
return -1;
}
int main()
{
int A[] = { 4, 3, 2, 5, 8, 6, 7 };
int n = sizeof (A) / sizeof (A[0]);
cout << FindElement(A, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int FindElement( int [] A, int n)
{
for ( int i = 0 ; i < n; i++) {
int flag = 0 ;
for ( int j = 0 ; j < i; j++)
if (A[j] >= A[i]) {
flag = 1 ;
break ;
}
for ( int j = i + 1 ; j < n; j++)
if (A[j] <= A[i]) {
flag = 1 ;
break ;
}
if (flag == 0 )
return A[i];
}
return - 1 ;
}
static public void main(String[] args)
{
int [] A = { 4 , 3 , 2 , 5 , 8 , 6 , 7 };
int n = A.length;
System.out.println(FindElement(A, n));
}
}
|
Python3
def FindElement(A, n):
for i in range ( 0 , n, 1 ):
flag = 0
for j in range ( 0 , i, 1 ):
if (A[j] > = A[i]):
flag = 1
break
for j in range (i + 1 , n, 1 ):
if (A[j] < = A[i]):
flag = 1
break
if (flag = = 0 ):
return A[i]
return - 1
if __name__ = = '__main__' :
A = [ 4 , 3 , 2 , 5 , 8 , 6 , 7 ]
n = len (A)
print (FindElement(A, n))
|
C#
using System;
class GFG {
static int FindElement( int [] A, int n)
{
for ( int i = 0; i < n; i++) {
int flag = 0;
for ( int j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break ;
}
for ( int j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break ;
}
if (flag == 0)
return A[i];
}
return -1;
}
static public void Main()
{
int [] A = { 4, 3, 2, 5, 8, 6, 7 };
int n = A.Length;
Console.WriteLine(FindElement(A, n));
}
}
|
PHP
<?php
function FindElement( $A , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$flag = 0;
for ( $j = 0; $j < $i ; $j ++)
if ( $A [ $j ] >= $A [ $i ])
{
$flag = 1;
break ;
}
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $A [ $j ] <= $A [ $i ])
{
$flag = 1;
break ;
}
if ( $flag == 0)
return $A [ $i ];
}
return -1;
}
$A = array ( 4, 3, 2, 5, 8, 6, 7 );
$n = count ( $A );
echo FindElement( $A , $n );
?>
|
Javascript
<script>
function FindElement(A , n)
{
for (i = 0; i < n; i++) {
var flag = 0;
for (j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break ;
}
for (j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break ;
}
if (flag == 0)
return A[i];
}
return -1;
}
var A = [4, 3, 2, 5, 8, 6, 7];
var n = A.length;
document.write(FindElement(A, n));
</script>
|
Time complexity: O(n2)
Space Complexity: O(1)
Efficient solution take O(n) time.
- Create an auxiliary array ‘GE[]’. GE[] should store the element which is greater than A[i] and is on left side of A[i].
- Create an another Auxiliary array ‘SE[]’. SE[i] should store the element which is smaller than A[i] and is on right side of A[i].
- Find element in array that hold condition GE[i-1] < A[i] < SE[i+1].
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int FindElement( int A[], int n)
{
int SE[n];
int GE[n];
GE[0] = A[0];
SE[n - 1] = A[n - 1];
for ( int i = 1; i < n; i++) {
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
for ( int i = n - 2; i >= 0; i--) {
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
for ( int j = 0; j < n; j++)
{
if ((j == 0 && A[j] < SE[j + 1]) ||
(j == n - 1 && A[j] > GE[j - 1]) ||
(A[j] < SE[j + 1] && A[j] > GE[j - 1]))
return A[j];
}
return -1;
}
int main()
{
int A[] = {4, 3, 2, 5, 8, 6, 7};
int n = sizeof (A) / sizeof (A[0]);
cout << FindElement(A, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int FindElement( int [] A, int n)
{
int [] SE = new int [n];
int [] GE = new int [n];
GE[ 0 ] = A[ 0 ];
SE[n - 1 ] = A[n - 1 ];
for ( int i = 1 ; i < n; i++)
{
if (GE[i - 1 ] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1 ];
}
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (A[i] < SE[i + 1 ])
SE[i] = A[i];
else
SE[i] = SE[i + 1 ];
}
for ( int j = 0 ; j < n; j++)
{
if ((j == 0 && A[j] < SE[j + 1 ]) ||
(j == n - 1 && A[j] > GE[j - 1 ]) ||
(A[j] < SE[j + 1 ] && A[j] > GE[j - 1 ]))
return A[j];
}
return - 1 ;
}
static public void main(String[] args)
{
int [] A = { 4 , 3 , 2 , 5 , 8 , 6 , 7 };
int n = A.length;
System.out.println(FindElement(A, n));
}
}
|
Python3
def FindElement(A, n) :
SE = [ 0 ] * n
GE = [ 0 ] * n
GE[ 0 ] = A[ 0 ]
SE[n - 1 ] = A[n - 1 ]
for i in range ( 1 , n):
if (GE[i - 1 ] < A[i]) :
GE[i] = A[i]
else :
GE[i] = GE[i - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
if (A[i] < SE[i + 1 ]) :
SE[i] = A[i]
else :
SE[i] = SE[i + 1 ]
for j in range (n):
if ((j = = 0 and A[j] < SE[j + 1 ]) or
(j = = n - 1 and A[j] > GE[j - 1 ]) or
(A[j] < SE[j + 1 ] and A[j] > GE[j - 1 ])):
return A[j]
return - 1
A = [ 4 , 3 , 2 , 5 , 8 , 6 , 7 ]
n = len (A)
print (FindElement(A, n))
|
C#
using System;
class GFG {
static int FindElement( int [] A, int n)
{
int [] SE = new int [n];
int [] GE = new int [n];
GE[0] = A[0];
SE[n - 1] = A[n - 1];
for ( int i = 1; i < n; i++)
{
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
for ( int i = n - 2; i >= 0; i--)
{
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
for ( int j = 0; j < n; j++)
{
if ((j == 0 && A[j] < SE[j + 1]) ||
(j == n - 1 && A[j] > GE[j - 1]) ||
(A[j] < SE[j + 1] && A[j] > GE[j - 1]))
return A[j];
}
return -1;
}
static public void Main()
{
int [] A = {4, 3, 2, 5, 8, 6, 7};
int n = A.Length;
Console.WriteLine(FindElement(A, n));
}
}
|
PHP
<?php
function FindElement( $A , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$flag = 0;
for ( $j = 0; $j < $i ; $j ++)
if ( $A [ $j ] >= $A [ $i ])
{
$flag = 1;
break ;
}
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $A [ $j ] <= $A [ $i ])
{
$flag = 1;
break ;
}
if ( $flag == 0)
return $A [ $i ];
}
return -1;
}
$A = array (4, 3, 2, 5, 8, 6, 7);
$n = sizeof( $A );
echo (FindElement( $A , $n ));
?>
|
Javascript
<script>
function FindElement(A, n)
{
let SE = new Array(n);
let GE = new Array(n);
GE[0] = A[0];
SE[n - 1] = A[n - 1];
for (let i = 1; i < n; i++)
{
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
for (let i = n - 2; i >= 0; i--)
{
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
for (let j = 0; j < n; j++)
{
if ((j == 0 && A[j] < SE[j + 1]) ||
(j == n - 1 && A[j] > GE[j - 1]) ||
(A[j] < SE[j + 1] && A[j] > GE[j - 1]))
return A[j];
}
return -1;
}
let A = [4, 3, 2, 5, 8, 6, 7];
let n = A.length;
document.write(FindElement(A, n));
</script>
|
Time complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
14 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...