🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this blog post, we explore a problem that is commonly encountered in array processing and data structures: finding the nearest greater element to the left of every element in an array. This problem is a variation of the Next Greater Element problem and can be efficiently solved using a stack.
Problem
Given an array of integers, the task is to find the nearest greater element on the left side for every element in the array. If no such element exists, print -1.
2. Solution Steps
1. Create a stack to keep track of elements for which the nearest greater element on the left is not yet found.
2. Iterate through the array from left to right.
3. For each element:
a. Pop elements from the stack until the stack is empty or the top element of the stack is greater than the current element.
b. If the stack is empty, there is no greater element on the left. Print -1.
c. If the stack has elements, the top element is the nearest greater element on the left.
4. Push the current element onto the stack.
5. Repeat the process for each element in the array.
3. Code Program
import java.util.Stack;
public class Solution {
// Main method for testing
public static void main(String[] args) {
int[] arr = {4, 5, 2, 10, 8};
printNearestGreaterToLeft(arr);
}
// Method to print nearest greater elements to the left
public static void printNearestGreaterToLeft(int[] arr) {
Stack<Integer> stack = new Stack<>();
StringBuilder result = new StringBuilder();
for (int j : arr) {
while (!stack.isEmpty() && stack.peek() <= j) {
stack.pop();
}
result.append(stack.isEmpty() ? "-1 " : stack.peek() + " ");
stack.push(j);
}
System.out.println(result.toString().trim());
}
}
Output:
-1 5 5 10 10
Explanation:
The printNearestGreaterToLeft method utilizes a stack to find the nearest greater element to the left for each element in the array. The stack keeps elements in a way such that the top element is always the nearest greater element for the next array element.
For example, in the array [4, 5, 2, 10, 8], the nearest greater element to the left of 2 is 5, and for 10 and 8, it's 10. The method efficiently traverses the array and prints the correct nearest greater element for each element.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment