Find a partition point in array
Last Updated :
14 Mar, 2023
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++
// Simple C++ program to find a partition point in
// an array
#include <bits/stdc++.h>
using namespace std;
// Prints an element such than all elements on left
// are smaller and all elements on right are greater.
int FindElement(int A[], int n)
{
// traverse array elements
for (int i = 0; i < n; i++) {
// If we found that such number
int flag = 0;
// check All the elements on its left are smaller
for (int j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break;
}
// check All the elements on its right are Greater
for (int j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break;
}
// If flag == 0 indicates we found that number
if (flag == 0)
return A[i];
}
return -1;
}
// driver program to test above function
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
// Simple Java program to find
// a partition point in an array
import java.io.*;
class GFG {
// Prints an element such than all elements
// on left are smaller and all elements on
// right are greater.
static int FindElement(int[] A, int n)
{
// traverse array elements
for (int i = 0; i < n; i++) {
// If we found that such number
int flag = 0;
// check All the elements on
// its left are smaller
for (int j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break;
}
// check All the elements on
// its right are Greater
for (int j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break;
}
// If flag == 0 indicates we
// found that number
if (flag == 0)
return A[i];
}
return -1;
}
// Driver code
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));
}
}
// This code is contributed by vt_m
Python3
# Simple python 3 program to find a
# partition point in an array
# Prints an element such than all
# elements on left are smaller and
# all elements on right are greater.
def FindElement(A, n):
# traverse array elements
for i in range(0, n, 1):
# If we found that such number
flag = 0
# check All the elements on its
# left are smaller
for j in range(0, i, 1):
if (A[j] >= A[i]):
flag = 1
break
# check All the elements on its
# right are Greater
for j in range(i + 1, n, 1):
if (A[j] <= A[i]):
flag = 1
break
# If flag == 0 indicates we found
# that number
if (flag == 0):
return A[i]
return -1
# Driver Code
if __name__ == '__main__':
A = [4, 3, 2, 5, 8, 6, 7]
n = len(A)
print(FindElement(A, n))
# This code is contributed by
# Sanjit_Prasad
C#
// Simple C# program to find a
// partition point in an array
using System;
class GFG {
// Prints an element such than all
// elements on left are smaller and all
// elements on right are greater.
static int FindElement(int[] A, int n)
{
// traverse array elements
for (int i = 0; i < n; i++) {
// If we found that such number
int flag = 0;
// check All the elements on
// its left are smaller
for (int j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break;
}
// check All the elements on
// its right are Greater
for (int j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break;
}
// If flag == 0 indicates we
// found that number
if (flag == 0)
return A[i];
}
return -1;
}
// Driver code
static public void Main()
{
int[] A = { 4, 3, 2, 5, 8, 6, 7 };
int n = A.Length;
Console.WriteLine(FindElement(A, n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// Simple PHP program to find a partition
// point in an array
// Prints an element such than all elements
// on left are smaller and all elements on
// right are greater.
function FindElement($A, $n)
{
// traverse array elements
for ($i = 0; $i < $n; $i++)
{
// If we found that such number
$flag = 0;
// check All the elements on its
// left are smaller
for ( $j = 0; $j < $i; $j++)
if ($A[$j] >= $A[$i])
{
$flag = 1;
break;
}
// check All the elements on its
// right are Greater
for ( $j = $i + 1; $j < $n; $j++)
if ($A[$j] <= $A[$i])
{
$flag = 1;
break;
}
// If flag == 0 indicates we found
// that number
if ($flag == 0)
return $A[$i];
}
return -1;
}
// Driver Code
$A = array( 4, 3, 2, 5, 8, 6, 7 );
$n = count($A);
echo FindElement($A, $n);
// This code is contributed by
// Rajput-Ji
?>
JavaScript
<script>
// Simple JavaScript program to find
// a partition point in an array
// Prints an element such than all elements
// on left are smaller and all elements on
// right are greater.
function FindElement(A , n)
{
// traverse array elements
for (i = 0; i < n; i++) {
// If we found that such number
var flag = 0;
// check All the elements on
// its left are smaller
for (j = 0; j < i; j++)
if (A[j] >= A[i]) {
flag = 1;
break;
}
// check All the elements on
// its right are Greater
for (j = i + 1; j < n; j++)
if (A[j] <= A[i]) {
flag = 1;
break;
}
// If flag == 0 indicates we
// found that number
if (flag == 0)
return A[i];
}
return -1;
}
// Driver code
var A = [4, 3, 2, 5, 8, 6, 7];
var n = A.length;
document.write(FindElement(A, n));
// This code contributed by shikhasingrajput
</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++
// Simple C++ program to find
// a partition point in an array
#include <bits/stdc++.h>
using namespace std;
// Returns an element that has all
// the element to its left smaller and
// to its right greater
int FindElement(int A[], int n)
{
// Create an array 'SE[]' that will
// store smaller element on right side.
int SE[n];
// Create an another array 'GE[]' that will
// store greatest element on left side.
int GE[n];
// initialize first and last index of SE[], GE[]
GE[0] = A[0];
SE[n - 1] = A[n - 1];
// store greatest element from left to right
for (int i = 1; i < n; i++) {
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
// store smallest element from right to left
for (int i = n - 2; i >= 0; i--) {
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
// Now find a number which is greater than all
// elements at it's left and smaller than all
// then elements to it's right
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;
}
// Driver code
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
// Simple java program to find a
// partition point in an array
import java.io.*;
class GFG {
// Returns an element that has all
// the element to its left smaller
// and to its right greater
static int FindElement(int[] A, int n)
{
// Create an array 'SE[]' that will
// store smaller element on right side.
int[] SE = new int[n];
// Create an another array 'GE[]' that
// will store greatest element on left side.
int[] GE = new int[n];
// initialize first and last index of SE[], GE[]
GE[0] = A[0];
SE[n - 1] = A[n - 1];
// store greatest element from left to right
for (int i = 1; i < n; i++)
{
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
// store smallest element from right to left
for (int i = n - 2; i >= 0; i--)
{
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
// Now find a number which is greater than all
// elements at it's left and smaller than all
// then elements to it's right
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;
}
// Driver code
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));
}
}
// This code is contributed by vt_m.
Python3
# Python Program to implement
# the above approach
# Returns an element that has all
# the element to its left smaller and
# to its right greater
def FindElement(A, n) :
# Create an array 'SE[]' that will
# store smaller element on right side.
SE = [0] * n
# Create an another array 'GE[]' that will
# store greatest element on left side.
GE = [0] * n
# initialize first and last index of SE[], GE[]
GE[0] = A[0]
SE[n - 1] = A[n - 1]
# store greatest element from left to right
for i in range(1, n):
if (GE[i - 1] < A[i]) :
GE[i] = A[i]
else :
GE[i] = GE[i - 1]
# store smallest element from right to left
for i in range(n-2, -1, -1):
if (A[i] < SE[i + 1]) :
SE[i] = A[i]
else :
SE[i] = SE[i + 1]
# Now find a number which is greater than all
# elements at it's left and smaller than all
# then elements to it's right
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
# Driver code
A = [ 4, 3, 2, 5, 8, 6, 7 ]
n = len(A)
print(FindElement(A, n))
# This code is contributed by code_hunt.
C#
// Simple C# program to find
// a partition point in an array
using System;
class GFG {
// Returns an element that has all
// the element to its left smaller
// and to its right greater
static int FindElement(int[] A, int n)
{
// Create an array 'SE[]' that will
// store smaller element on right side.
int[] SE = new int[n];
// Create an another array 'GE[]' that will
// store greatest element on left side.
int[] GE = new int[n];
// initialize first and last index of SE[], GE[]
GE[0] = A[0];
SE[n - 1] = A[n - 1];
// store greatest element from left to right
for (int i = 1; i < n; i++)
{
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
// store smallest element from right to left
for (int i = n - 2; i >= 0; i--)
{
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
// Now find a number which is greater than all
// elements at it's left and smaller than all
// then elements to it's right
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;
}
// Driver code
static public void Main()
{
int[] A = {4, 3, 2, 5, 8, 6, 7};
int n = A.Length;
Console.WriteLine(FindElement(A, n));
}
}
// This code is contributed by vt_m .
PHP
<?php
// PHP program to find a partition point
// in an array
// Prints an element such than all elements
// on left are smaller and all elements on
// right are greater.
function FindElement($A, $n)
{
// traverse array elements
for ($i = 0; $i < $n; $i++)
{
// If we found that such number
$flag = 0;
// check All the elements on
// its left are smaller
for ($j = 0; $j < $i; $j++)
if ($A[$j] >= $A[$i])
{
$flag = 1;
break;
}
// check All the elements on
// its right are Greater
for ($j = $i + 1; $j < $n; $j++)
if ($A[$j] <= $A[$i])
{
$flag = 1;
break;
}
// If flag == 0 indicates we
// found that number
if ($flag == 0)
return $A[$i];
}
return -1;
}
// Driver code
$A = array(4, 3, 2, 5, 8, 6, 7);
$n = sizeof($A);
echo(FindElement($A, $n));
// This code is contributed
// by Mukul Singh
?>
JavaScript
<script>
// Simple Javascript program to find
// a partition point in an array
// Returns an element that has all
// the element to its left smaller
// and to its right greater
function FindElement(A, n)
{
// Create an array 'SE[]' that will
// store smaller element on right side.
let SE = new Array(n);
// Create an another array 'GE[]' that will
// store greatest element on left side.
let GE = new Array(n);
// initialize first and last index of SE[], GE[]
GE[0] = A[0];
SE[n - 1] = A[n - 1];
// store greatest element from left to right
for (let i = 1; i < n; i++)
{
if (GE[i - 1] < A[i])
GE[i] = A[i];
else
GE[i] = GE[i - 1];
}
// store smallest element from right to left
for (let i = n - 2; i >= 0; i--)
{
if (A[i] < SE[i + 1])
SE[i] = A[i];
else
SE[i] = SE[i + 1];
}
// Now find a number which is greater than all
// elements at it's left and smaller than all
// then elements to it's right
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)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read