Introduction: In this quick tutorial, we’ll learn to perform a binary search in a sorted array. We’ll also write our own algorithmic implementation in Java. Later, we’ll discuss the binarySearch() method available in the Java library. Problem Definition: Let’s say we have a sorted array a[]: int[] a = {1, 2, 3, 4, 5, 6}; […]
Introduction: In this quick tutorial, we’ll first start by looking at the brute-force solution to find the middle element in the linked list. Later, we’ll learn how to achieve it in a single iteration through the entire linked list i.e. in just a single pass. Problem Definition: Given a linked list L, our aim is […]
Overview: In this quick tutorial, we’ll learn ways in which we can invert or reverse an array. We’ll first look at the most basic Java implementations and later cover a few third-party options. Problem Definition: Consider we have an array of elements: Integer[] intArr = {12, 30, 1, 7, 4}; Our problem is to be […]
Introduction: A prime number is a natural number greater than 1 having only two factors – 1 and the number itself. For eg: 2, 3, 5, 7, 11 etc are all prime numbers. Numbers that are not prime are known as composite numbers. In this tutorial, we’ll learn how to generate all prime numbers less […]
Introduction: Today we’ll learn ways in which we can generate a random integer in a given range – minValue to maxValue, both inclusive in Java. It is a common requirement we come across while writing some algorithmic solutions. So let’s get started! Generating Random Integer: Using Math.random() : A traditional solution to generate a random […]
Introduction: Prime Number is an integer greater than 1 and only divisible by 1 and itself. For instance – 2, 3, 5, 7, 11, 13 etc are all prime numbers. In other words, any number with only two factors – 1 and the number itself is known as a prime number. Numbers like 4, 8, […]
Introduction: In this tutorial, we’ll learn how to sort arrays as well as collections like List, Set, and Map using Java API. Sorting Arrays: Java exposes java.util.Arrays.sort() API which helps us to sort our arrays. There are two flavors to it: public static void sort(type[] arr) //sorts array in range [fromIndex – toIndex) : toIndex […]