MERGE SORT IN JAVA

By | June 10, 2024

Explanation of Merge Sort Merge Sort is a sorting algorithm that follows the divide and conquer principle. Here’s a simple breakdown of how it works: Steps of Merge Sort Example in Java Here’s a simple example of how to implement Merge Sort in Java. .

TWO POINTERS IN JAVA

By | June 10, 2024

The two pointers algorithm is a technique that uses two pointers to solve various types of problems in an array or list. These problems usually involve searching for pairs or subarrays with certain properties, such as finding a pair of numbers that sum to a specific value, or removing duplicates from a sorted array. The basic idea is… Read More »

BUBBLE SORT PROGRAM IN JAVA

By | June 9, 2024

BUBBLE SORT PROGRAM Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Here is a simple implementation of Bubble Sort in Java: In this code, bubbleSort(int arr[]) is the… Read More »

REVERSE INTEGER PROGRAM IN JAVA

By | March 28, 2024

Example 1:Input: x = 120 Output: 21 Explanation of the overflow check: If the input x is 0, the loop will not run, and 0 will be returned, which is the correct result. In the provided code, digit > 7 and digit < -8 are used as conditions to check for overflow. Let’s break down why these conditions… Read More »

PIVOT INDEX JAVA PROGRAM

By | March 27, 2024

Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11 To find the pivot index in an array, we need to find an index such that the sum… Read More »

FIND THE HIGHEST ALTITUDE PROGRAM IN JAVA

By | March 27, 2024

Example 1:Input: gain = [-5,1,5,0,-7] Output: 1 Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1. To find the highest altitude of a point during a road trip, we can iterate through the gain array and calculate the cumulative altitude at each point. Then, we track the maximum altitude reached during the trip. Here’s the Java program implementing… Read More »

LONGEST COMMON PREFIX IN JAVA

By | March 26, 2024

Example 1: Input: strs = [“flower”,”flow”,”flight”] Output: “fl” To find the longest common prefix among an array of strings, we can use the following approach: Here’s the Java program implementing the above approach: Explanation: