Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Next Greater Element In Sorted Array Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Next Greater Element In Sorted Array

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a sorted array and a number key, find the smallest array element which is greater than the key. If the key is greater than or equal to the largest element then return the key itself.

Naive Approach

The simple solution is to traverse the array and return the first element greater than key.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int getNextGreaterElement(vector<int> &arr, int key) {
    int n = arr.size();
	if (arr[n - 1] <= key) {
		return key;
	}
	for (int i = 0; i < n; i++) {
		if (arr[i] > key) {
			return arr[i];
		}
	}
}
Java
class Solution {
	int getNextGreaterElement (int[] arr, int key) {
		int n = arr.length;
		if (arr[n - 1] <= key) {
			return key;
		}
		for (int i = 0; i < n; i++) {
			if (arr[i] > key) {
				return arr[i];
			}
		}
		return 0;
	}
}

Optimal Approach

The problem can be solved efficiently using binary search to find the element just greater than the key.

Analysis

  • Time Complexity: O(log(n))
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int binarySearch (vector<int> &arr, int low, int high, int key) {
	if(low < high) {
		int mid = (high + low) / 2;
		if (arr[mid] <= key) {
			return binarySearch (arr, mid + 1, high, key);
		} else {
			return binarySearch (arr, low, mid, key);
		}
	}
	else {
		return arr[low];
	}
}
int getNextGreaterElement(vector<int> &arr, int key) {
    int n = arr.size();
	if (arr[n - 1] <= key) {
			return key;
	}
	return binarySearch (arr, 0, n - 1, key);
}
Java
class Solution {
	int binarySearch (int[] arr, int low, int high, int key) {
		if(low < high) {
			int mid = (high + low) / 2;
			if (arr[mid] <= key) {
				return binarySearch (arr, mid + 1, high, key);
			} else {
				return binarySearch (arr, low, mid, key);
			}
		}
		else {
			return arr[low];
		}
	}
	int getNextGreaterElement (int[] arr, int key) {
		int n = arr.length;
		if (arr[n - 1] <= key) {
			return key;
		}
		return binarySearch (arr, 0, n - 1, key);
	}
}
Related Content
Contains Element?
Insert Position in Sorted Array
Is Perfect Square
Matrix Search
Median of Row-wise Sorted Matrix
Negative numbers in sorted array
Non-Repeating Element
Search Range
Search Rotated Sorted Array
Square Root
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet