Found 7492 Articles for Java

Length of longest balanced parentheses prefix using Java

Sourav Sarkar
Updated on 07-Nov-2024 11:17:12

145 Views

In this article we will explore how we can find the length of longest balanced parentheses prefix using java , first we will understand the problem with some examples then we learn two different approaches to find it. Problem Statement Here, we will be given a string with parentheses and we need to find the length of balanced parentheses set from the string i.e. for every open parentheses "(" if there is a close parentheses ")", then we call it balanced. Prefix defines the first balanced set from the string, let the parenthesis set '(())()' we consider only '(())' Input ... Read More

Delete middle element of a stack in Java

Sourav Sarkar
Updated on 01-Nov-2024 13:14:09

284 Views

In order to delete the middle element of a stack, then first we need to remove the elements above the middle element. After removing the middle element, we restore the elements above it to maintain the original sequence. We can achieve this using a recursive approach, as explained step-by-step below. We cannot delete the middle element of the stack directly because, according to the stack property, we can perform only push and pop operations on the top of the stack. Steps to Delete the Middle Element of a Stack Step 1: Determine the Position of the Middle Element, First ... Read More

Java program to sort the elements of the stack in descending order

AYUSH MISHRA
Updated on 30-Oct-2024 18:36:32

5K+ Views

In this article, we will learn to sort the elements of the stack in descending order. A stack is a data structure that works on the LIFO (Last In First Out) principle which means that the last added item is removed first. One real-life example of a stack is browser history where the last used website appears first. In this article, we are going to discuss, how we can sort the element of stack in descending order in Java.  Problem Statement In the given problem, we have a stack of unsorted integer elements, which we have to sort in descending ... Read More

Minimum number of jumps to reach end using Java

Pushpa kumari
Updated on 01-Nov-2024 13:13:39

136 Views

In this article, we will learn how to solve the "Minimum Number of Jumps to Reach the End" problem using Java. Let's break it down step-by-step. The idea is to find the smallest number of jumps needed to get from the start to the end of an array. Each element in the array represents the maximum number of steps you can take from that position. Problem StatementGiven an array arr[], where each element represents the maximum number of steps you can move forward from that position, the goal is to start from the beginning of the array and ... Read More

Java program to sort the elements of a given stack in ascending order

Mallikarjun Reddy Ganapuram
Updated on 23-Oct-2024 10:24:03

174 Views

In this tutorial, you'll learn how to sort the elements of a stack in ascending order using Java. Stacks are fundamental data structures in computer science, operating on a Last-In-First-Out (LIFO) principle. We break down a simple yet effective method using an additional temporary stack, provide a detailed step-by-step explanation, and include complete code examples. This tutorial is perfect for those looking to enhance their understanding of stack operations and improve their Java programming skills. Sorting a Stack in Ascending Order Using Java Stacks are like a pile of books where you can only take the top one off. i.e., ... Read More

How do I get a platform-independent new line character?

guru
Updated on 23-Oct-2024 12:01:48

344 Views

When working with text in Java we often need to include new line characters to the format output properly. Different operating systems have different conventions for new line characters: Windows: This uses \r (Carriage Return + Line Feed). Unix/Linux: This uses (Line Feed). Mac (pre-OS X): This uses \r (Carriage Return). To write code that works seamlessly across all platforms and we need to use a platform-independent way of handling new line characters. This article will guide us through the different methods available in the Java to achieve this. Using Platform-Independent New Line Characters The recommended way ... Read More

Returning even numbers from a stack in java

DrOfEng
Updated on 25-Oct-2024 10:28:34

136 Views

Stacks in Java A stack is a Last-In-First-Out (LIFO) data structure. As shown below, the last book inserted onto the stack is the first to be removed, and the first book inserted into the stack is the last to be removed. In Java, a stack of integers can be created by importing the java.util.Stack package and invoking the Stack() constructor. Integer objects can be pushed onto the stack using the push() method. An example is provided in the following code snippet. Example Following is an example program − import java.util.Stack; public class Testing { public static ... Read More

Java Program to insert an element at the Bottom of a Stack

Aishwarya Naglot
Updated on 04-Oct-2024 12:24:27

338 Views

A stack is a data structure that follows the LIFO (Last In, First Out) principle. In other words, The last element we add to a stack is the first one to be removed. When we add (or push) elements to a stack, they are placed on top; i.e. above all the previously-added elements. There may be certain scenarios where we need to add an element at the bottom of the stack. There are multiple ways to add an element to the bottom of the stack. they are − Using Auxiliary Stack ... Read More

Delete all even elements from a stack in Java

Shubham B Vora
Updated on 23-Sep-2024 19:09:09

3K+ Views

In this tutorial, we will learn to remove all even numbers from the given stack containing integer values. In Java, the stack is a data structure that works based on the Last-In-First-Out (LIFO) principle. It means, whatever elements you have added in the last, you can get it first. Removing even elements from the stack is useful in many real-world scenarios. For example, you can use this technique to filter the data from the stack. However, filtering logic can differ according to the scenario. In our case, we will remove even numbers and filter odd numbers. In other cases, you ... Read More

Initializing multiple variables to the same value in Java

divya srivastava
Updated on 23-Sep-2024 19:09:57

668 Views

In this article, we will learn to initialize multiple variables to the same value in Java. What is a variable? A variable is a name given to a space reserved in the memory. Every variable has a type which specifies what kind of data it holds. Initializing multiple variables to the same value Multiple variables can be initialized to the same value in one statement as: variable1 = variable2 = variable3 = value This means that the value is assigned to variable3, then variable3 is assigned to variable2, and finally, variable2 is assigned to variable1. Input 1 a ... Read More

Advertisements