How to Merge Two Sorted Arrays

In this example, we’ll see how to merge two sorted arrays: Algorithm: Create an arr3[] to store all sorted elements Loop through arr1[] and arr2[] and compare each element to both the arrays Copy smaller element from arr1[] and arr2[] to arr3[] and move ahead in arr3[] and in the array whose element was picked. …

Read more

How to Find Leaders in an Array

In this example, we’ll find leaders in an array. An element of an array is a leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. 1. Java Solution with time complexity O(n2) Output: Solution with time complexity O(n) Output: Complete Java …

Read more

How to Find Possible Triangles in an Array

In this example, we’ll find possible triangles in an array: Algorithm: (Approach #1) Run three nested loops, each loop starting from the index of the previous loop to end of the array. Check if sum of two sides is greater than the third. If all three conditions match, (that means it is a possible triangle), then …

Read more

How to Find Missing Number From Integer Array

In this example, we’ll find missing number from Integer array but this method works only with a set of integers that are sorted and are in sequence like below: This is a common question asked in Java Interviews. 1. Java Output: Complete Java Solutions can be found here: Github Link