# ARRAY

An array is a data structure in Java that allows you to store a collection of elements of the same type, indexed by an integer value. It is a fixed-size container that can store a set of values in contiguous memory locations.

Here's an example of how to create and use an array in Java:

```
```// Create an array of integers

int[] myArray = new int[5];

// Assign values to the array

myArray[0] = 10;

myArray[1] = 20;

myArray[2] = 30;

myArray[3] = 40;

myArray[4] = 50;

// Access elements of the array

System.out.println("The first element of the array is: " +
myArray[0]);

System.out.println("The third element of the array is: " +
myArray[2]);

// Iterate through the array using a for loop

for (int i = 0; i < myArray.length; i++) {

System.out.println("Element " + i + " is: " +
myArray[i]);

}

// Create and initialize an array in one line

int[] anotherArray = {1, 2, 3, 4, 5};

An array is a data structure in Java that allows you to store a collection of elements of the same type, indexed by an integer value. It is a fixed-size container that can store a set of values in contiguous memory locations.

Here's an example of how to create and use an array in Java:

```
```// Create an array of integers

int[] myArray = new int[5];

// Assign values to the array

myArray[0] = 10;

myArray[1] = 20;

myArray[2] = 30;

myArray[3] = 40;

myArray[4] = 50;

// Access elements of the array

System.out.println("The first element of the array is: " +
myArray[0]);

System.out.println("The third element of the array is: " +
myArray[2]);

// Iterate through the array using a for loop

for (int i = 0; i < myArray.length; i++) {

System.out.println("Element " + i + " is: " +
myArray[i]);

}

// Create and initialize an array in one line

int[] anotherArray = {1, 2, 3, 4, 5};

In this example, we create an array of integers called myArray with a length of 5. We assign values to the array using the bracket notation, where myArray[0] represents the first element of the array, myArray[1] represents the second element, and so on. We can access individual elements of the array using the same bracket notation.

We can also iterate through the array using a for loop and the length property of the array. Finally, we create and initialize an array in a single line using curly braces.

Arrays can be very useful for storing and accessing collections of data in Java, and are a fundamental part of the language.

Arrays: dynamic arrays, bit arrays, and sparse arrays.

## DYNAMIC ARRAYS

A dynamic array is a data structure that is similar to a regular array, but with the added ability to resize itself as needed. A dynamic array allocates memory in a continuous block, like a regular array, but it can increase or decrease its size on demand. This makes it a flexible and efficient data structure that can be used to store collections of elements of the same type.

Here's an example implementation of a dynamic array in Java:

```
```public class DynamicArray<E> {

private Object[] data;

private int size;

private int capacity;

public DynamicArray() {

this(10);

}

public DynamicArray(int capacity) {

this.capacity = capacity;

data = new Object[capacity];

}

public int size() {

return size;

}

public boolean isEmpty() {

return size == 0;

}

public E get(int index) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

return (E) data[index];

}

public void set(int index, E element) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

data[index] = element;

}

public void add(E element) {

if (size == capacity) {

resize();

}

data[size] = element;

size++;

}

public void remove(int index) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

for (int i = index; i < size - 1; i++) {

data[i] = data[i + 1];

}

data[size - 1] = null;

size--;

}

private void resize() {

capacity *= 2;

Object[] newData = new Object[capacity];

System.arraycopy(data, 0, newData, 0, size);

data = newData;

}

}

In this implementation, the DynamicArray class is a generic class that can store elements of any type. The class has a data field that stores the elements of the dynamic array, a size field that keeps track of the number of elements currently in the array, and a capacity field that stores the maximum number of elements the array can hold.

The DynamicArray class has a number of methods that allow users to add, remove, and modify elements in the array, including get, set, add, and remove. If the array is full and a user attempts to add another element, the resize method is called to double the capacity of the array.

The resize method creates a new array with double the capacity of the current array, copies the existing elements from the current array to the new array, and then sets the data field to reference the new array.

## BIT ARRAYS

A bit array, also known as a bit set or bit vector, is a data structure that is used to store a sequence of bits, typically represented as ones and zeros. Bit arrays are often used to represent a large set of binary values efficiently in computer memory.

Here's an example implementation of a bit array in Java:

```
```public class BitArray {

private int[] data;

private int size;

public BitArray(int size) {

this.size = size;

int numInts = (size + 31) / 32;

data = new int[numInts];

}

public void set(int index) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

int i = index / 32;

int bit = index % 32;

data[i] |= (1 << bit);

}

public boolean get(int index) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

int i = index / 32;

int bit = index % 32;

return (data[i] & (1 << bit)) != 0;

}

}

In this implementation, the BitArray class has a data field that is an array of integers, and a size field that stores the number of bits in the bit array. The data array is initialized to be large enough to hold all the bits in the bit array, with each integer in the array holding 32 bits.

The set method sets a bit at a given index in the bit array by calculating the index of the integer in the data array and the position of the bit within that integer. The method then uses a bitwise OR operation to set the bit to 1.

The get method retrieves the value of a bit at a given index in the bit array by calculating the index of the integer in the data array and the position of the bit within that integer. The method then uses a bitwise AND operation to retrieve the value of the bit.

## SPARSE ARRAYS

A sparse array is a data structure that is used to represent arrays where most of the elements are zero. A sparse array can be more memory-efficient than a regular array because it only stores the non-zero elements and their indices.

Here's an example implementation of a sparse array in Java:

```
```import java.util.HashMap;

import java.util.Map;

public class SparseArray {

private final Map<Integer, Integer> elements;

private final int size;

public SparseArray(int size) {

if (size <= 0) {

throw new
IllegalArgumentException("Size must be greater than 0");

}

this.size = size;

this.elements = new HashMap<>();

}

public void set(int index, int value) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

if (value != 0) {

elements.put(index, value);

} else {

elements.remove(index);

}

}

public int get(int index) {

if (index < 0 || index >= size) {

throw new
IndexOutOfBoundsException("Index " + index + " is out of bounds");

}

return elements.getOrDefault(index, 0);

}

}

In this implementation, the SparseArray class has a size field that stores the size of the sparse array, and a Map called elements that stores the non-zero elements and their indices.

The set method sets a value at a given index in the sparse array by checking whether the value is zero. If the value is not zero, it is added to the elements map along with its index. If the value is zero, the corresponding key-value pair is removed from the map.

The get method retrieves the value of a given index in the sparse array by looking up the value in the elements map. If the index is not present in the map, the method returns 0.

## SORTING

Array sorting refers to the process of arranging the elements of an array in a specific order, such as ascending or descending order. There are several algorithms for sorting arrays, including bubble sort, insertion sort, selection sort, quicksort, mergesort, heapsort, and others.

Here is an example of how to sort an array of integers in Java using the built-in Arrays class and its sort method:

```
```import java.util.Arrays;

public class Main {

public static void main(String[] args) {

int[] myArray = { 10, 5, 20, 7, 15 };

Arrays.sort(myArray);

System.out.println(Arrays.toString(myArray));

}

}

In this example, we have an array of integers called myArray with the values of 10, 5, 20, 7, and 15. We can sort this array in ascending order using the Arrays.sort method, which sorts the elements in place. The resulting sorted array will have the values of 5, 7, 10, 15, and 20. Finally, we print out the sorted array using the Arrays.toString method.

It's important to note that different sorting algorithms have different time complexities, which can affect the performance of the program when sorting large arrays. It's generally a good practice to choose the most efficient sorting algorithm for the size and type of data you're working with.

### BUBBLE SORT

Bubble sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It is called "bubble" sort because the smaller elements "bubble" to the top of the array, while the larger elements sink to the bottom.

Here's an example of how bubble sort works in Java:

```
```public static void bubbleSort(int[] arr) {

int n = array.length;

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// swap arr[j] and
arr[j+1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

}

In this example, we define a method called bubbleSort that takes an array of integers as its input. The outer loop runs for n - 1 times, where n is the length of the array. The inner loop compares adjacent elements of the array and swaps them if they are in the wrong order. After each pass through the array, the largest element is moved to the end of the array. This process is repeated until the array is completely sorted.

Bubble sort has a time complexity of O(n^2), which makes it inefficient for large arrays. However, it is easy to understand and implement, and is useful for educational purposes or when dealing with small arrays.

### MERGESORT

Divide and conquer is a general problem-solving strategy that involves breaking down a complex problem into smaller, more manageable subproblems, solving each subproblem independently, and then combining the solutions to the subproblems to obtain the solution to the original problem.

The divide-and-conquer approach typically involves three steps:

- Divide: The problem is broken down into smaller, more manageable subproblems. For example, in the case of sorting an array, the problem can be divided into two subarrays of roughly equal size.
- Conquer: The subproblems are solved recursively using the same strategy. In the case of sorting an array, each subarray is sorted independently using the same sorting algorithm.
- Combine: The solutions to the subproblems are combined to obtain the solution to the original problem. In the case of sorting an array, the two sorted subarrays can be merged using a "merge" operation.

The divide-and-conquer strategy is used in a wide variety of algorithms and problem-solving contexts. Some examples include sorting algorithms like merge sort and quicksort.

The divide-and-conquer strategy is particularly useful when the subproblems are similar in nature to the original problem, and when the solutions to the subproblems can be combined in a relatively straightforward way to obtain the solution to the original problem. It is a powerful tool for solving complex problems, and is widely used in computer science and other fields.

Merge sort is a divide-and-conquer algorithm that works by recursively dividing an array into two halves, sorting each half, and then merging the two halves back together. It is a popular sorting algorithm due to its efficiency and stable sort property.

Here's an example of how merge sort works in Java:

```
```public static void mergeSort(int[] arr, int left, int right) {

if (left < right) {

// Find the middle point

int middle = (left + right) / 2;

// Sort the first half

mergeSort(arr, left, middle);

// Sort the second half

mergeSort(arr, middle + 1, right);

// Merge the sorted halves

merge(arr, left, middle, right);

}

}

public static void merge(int[] arr, int left, int middle, int right) {

// Create temporary arrays to hold the two halves

int[] leftArray = new int[middle - left + 1];

int[] rightArray = new int[right - middle];

// Copy data to the temporary arrays

for (int i = 0; i < leftArray.length; i++) {

leftArray[i] = arr[left + i];

}

for (int i = 0; i < rightArray.length; i++) {

rightArray[i] = arr[middle + 1 + i];

}

// Merge the temporary arrays

int i = 0, j = 0, k = left;

while (i < leftArray.length && j < rightArray.length)
{

if (leftArray[i] <= rightArray[j]) {

arr[k] = leftArray[i];

i++;

} else {

arr[k] = rightArray[j];

j++;

}

k++;

}

// Copy remaining elements of leftArray[]

while (i < leftArray.length) {

arr[k] = leftArray[i];

i++;

k++;

}

// Copy remaining elements of rightArray[]

while (j < rightArray.length) {

arr[k] = rightArray[j];

j++;

k++;

}

}

In this example, we define a method called mergeSort that takes an array of integers as its input, along with the left and right indices of the subarray to be sorted. The method first checks if there are at least two elements in the subarray, and if so, it recursively divides the subarray into two halves, sorts each half using the same method, and then merges the two sorted halves together using the merge method.

The merge method takes the original array, the left and right indices of the subarray, and the middle index that separates the two halves. The method creates two temporary arrays to hold the left and right halves, and then merges the two arrays together in sorted order.

The following is the iterative version of the merge sort:

```
```public static void iterativeMergeSort(int[] arr) {

int n = array.length;

int curr_size;

for (curr_size = 1; curr_size < n; curr_size *= 2) {

for (int left_start = 0; left_start < n - 1;
left_start += 2 * curr_size) {

int mid = Math.min(left_start + curr_size -
1, n - 1);

int right_end = Math.min(left_start + 2 *
curr_size - 1, n - 1);

merge(arr, left_start, mid,
right_end);

}

}

}

This version of merge sort uses a bottom-up approach and does not use recursion. Instead, it uses nested loops to divide the array into subarrays of size curr_size and then merges adjacent pairs of subarrays together using the merge method.

When compared with the recursive version of merge sort, the iterative version can be faster and use less memory, especially for very large arrays, since it does not use a call stack for recursion. However, the iterative version can be harder to understand and implement, especially for beginners.

### QUICKSORT

Quicksort is a popular sorting algorithm that uses a divide-and-conquer strategy to sort an array in place. It works by selecting a "pivot" element from the array and partitioning the other elements into two subarrays, according to whether they are less than or greater than the pivot. The algorithm then recursively sorts the subarrays.

Here's an example of how quicksort works in Java:

```
```public static void quickSort(int[] arr, int left, int right) {

if (left < right) {

// Choose a pivot element

int pivot = partition(arr, left, right);

// Recursively sort the subarrays

quickSort(arr, left, pivot - 1);

quickSort(arr, pivot + 1, right);

}

}

public static int partition(int[] arr, int left, int right) {

// Choose the rightmost element as the pivot

int pivot = arr[right];

// Index of the smaller element

int i = left - 1;

for (int j = left; j < right; j++) {

// If current element is smaller than the pivot

if (arr[j] < pivot) {

i++;

// Swap arr[i] and arr[j]

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

// Swap arr[i+1] and arr[right] (the pivot)

int temp = arr[i + 1];

arr[i + 1] = arr[right];

arr[right] = temp;

// Return the index of the pivot

return i + 1;

}

In this example, we define a method called quickSort that takes an array of integers as its input, along with the left and right indices of the subarray to be sorted. The method first checks if there are at least two elements in the subarray, and if so, it chooses a pivot element using the partition method and recursively sorts the subarrays.

The partition method takes the original array, the left and right indices of the subarray, and chooses the pivot element as the rightmost element. It then partitions the other elements into two subarrays, according to whether they are less than or greater than the pivot, and swaps elements as needed to put them in the correct position.

There are ways to improve the quicksort performance such as:

- Randomized pivot selection: In the basic implementation of quicksort, the pivot element is chosen as the rightmost element of the subarray. This can be problematic if the array is already sorted or nearly sorted, as it can result in an unbalanced partition and lead to a worst-case time complexity of O(n^2). To avoid this, we can choose the pivot element randomly from the subarray, which reduces the likelihood of encountering a worst-case scenario.

```
```public static void quickSortRandomized(int[] arr, int left, int right) {

if (left < right) {

// Choose a random pivot element

int pivotIndex = left + (int)(Math.random() * (right -
left + 1));

int pivot = arr[pivotIndex];

// Partition the array using the pivot element

int partitionIndex = partition(arr, left, right,
pivot);

// Recursively sort the subarrays

quickSortRandomized(arr, left, partitionIndex -
1);

quickSortRandomized(arr, partitionIndex, right);

}

}

- Insertion sort for small subarrays: For very small subarrays, quicksort can be less efficient than other sorting algorithms such as insertion sort. To improve performance, we can modify the quicksort algorithm to use insertion sort for subarrays below a certain size threshold.

```
```public static void quickSortInsertion(int[] arr, int left, int right) {

if (left < right) {

if (right - left <= 10) {

insertionSort(arr, left, right);

} else {

int pivot = partition(arr, left,
right);

quickSortInsertion(arr, left, pivot -
1);

quickSortInsertion(arr, pivot + 1,
right);

}

}

}

public static void insertionSort(int[] arr, int left, int right) {

for (int i = left + 1; i <= right; i++) {

int key = arr[i];

int j = i - 1;

while (j >= left && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

}

arr[j + 1] = key;

}

}

- Three-way partitioning: In some cases, quicksort can be inefficient when there are many duplicate elements in the array. One solution to this problem is to use three-way partitioning, where we partition the array into three parts: elements less than the pivot, elements equal to the pivot, and elements greater than the pivot.

```
```public static void quickSortThreeWay(int[] arr, int left, int right) {

if (left < right) {

int[] partitionIndices = partitionThreeWay(arr, left,
right);

quickSortThreeWay(arr, left, partitionIndices[0] -
1);

quickSortThreeWay(arr, partitionIndices[1] + 1,
right);

}

}

public static int[] partitionThreeWay(int[] arr, int left, int right) {

int pivot = arr[left];

int i = left, j = right;

int k = left;

while (k <= j) {

if (arr[k] < pivot) {

swap(arr, i, k);

i++;

k++;

} else if (arr[k] > pivot) {

swap(arr, j, k);

j--;

} else {

k++;

}

}

return new int[]{i, j};

}

public static void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

In this example, we define a method called quickSortThreeWay that takes an array of integers as its input, along with the left and right indices of the subarray to be sorted. The method first checks if there are at least two elements in the subarray, and if so, it partitions the subarray into three parts using the partitionThreeWay method and recursively sorts the subarrays.

The partitionThreeWay method takes the original array, the left and right indices of the subarray, and chooses the pivot element as the leftmost element. It then partitions the other elements into three parts: elements less than the pivot, elements equal to the pivot, and elements greater than the pivot. It uses three pointers: i, j, and k. i and j are used to keep track of the boundaries between the three parts, while k is used to traverse the array.

The method starts with k at the leftmost element, and compares arr[k] to the pivot element. If arr[k] is less than the pivot, it swaps arr[k] with arr[i] and increments i and k. If arr[k] is greater than the pivot, it swaps arr[k] with arr[j] and decrements j. If arr[k] is equal to the pivot, it simply increments k. This process continues until k has traversed the entire subarray.

At the end of the method, i and j define the boundaries between the three parts, and the method returns an array of two integers: the index of the first element of the second part (i.e., the last element of the first part), and the index of the last element of the second part (i.e., the first element of the third part).

### HEAPSORT

Heapsort is a comparison-based sorting algorithm that uses a binary heap data structure to sort elements in an array. The algorithm works by first building a max-heap from the elements in the array, and then repeatedly extracting the maximum element from the heap and adding it to the end of the sorted portion of the array.

Here is an overview of the algorithm:

- Build a max-heap: Starting with the last parent node (i.e., the parent of the last element in the array), we repeatedly "heapify" the subtree rooted at the current node, by swapping the current node with its largest child (if the child is larger than the current node). We move up the tree, from the last parent to the root, until the entire array forms a max-heap.
- Extract the maximum element: We repeatedly extract the maximum element from the heap, which is always the root of the max-heap. We swap the root with the last element in the unsorted portion of the array, and then reduce the size of the heap by one.
- Re-heapify the max-heap: Starting with the new root (which was previously the last element of the array), we "heapify" the subtree rooted at the current node, by swapping the current node with its largest child (if the child is larger than the current node). We move down the tree, from the root to the end of the unsorted portion of the array, until the entire unsorted portion of the array forms a max-heap.
- Repeat steps 2-3: We repeat steps 2-3 until the entire array is sorted.

Here is some sample code for the heapsort algorithm in Java:

```
```public static void heapSort(int[] arr) {

int n = arr.length;

// Build a max-heap

for (int i = n / 2 - 1; i >= 0; i--) {

heapify(arr, n, i);

}

// Extract elements from the heap and re-heapify

for (int i = n - 1; i >= 0; i--) {

swap(arr, 0, i);

heapify(arr, i, 0);

}

}

public static void heapify(int[] arr, int n, int i) {

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {

largest = left;

}

if (right < n && arr[right] > arr[largest]) {

largest = right;

}

if (largest != i) {

swap(arr, i, largest);

heapify(arr, n, largest);

}

}

public static void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

In this example, we define a method called heapSort that takes an array of integers as its input, and first builds a max-heap using the heapify method, and then repeatedly extracts the maximum element from the heap and re-heapifies the remaining elements. The heapify method takes the original array, the size of the heap, and the index of the current node, and recursively re-heapifies the subtree rooted at the current node. The swap method is a utility method that swaps two elements in the array.

The time complexity of heapsort is O(n log n), both in the worst case and average case. However, unlike other O(n log n) sorting algorithms, like merge sort and quicksort, heapsort has a worst-case space complexity of O(1), meaning that it does not use any extra memory beyond the input array. This makes heapsort a good choice when memory usage is a concern.

However, heapsort has a few disadvantages compared to other sorting algorithms. First, it is not stable, meaning that it does not preserve the relative order of equal elements. Second, its cache performance is poor, as it does not take advantage of locality of reference like merge sort does. Finally, its constant factors are larger than those of quicksort, making it slower in practice.

Despite these drawbacks, heapsort is still a useful sorting algorithm in some contexts, particularly when memory usage is a concern and when a stable sort is not necessary.

### RADIXSORT

Radix sort is a non-comparison-based sorting algorithm that sorts elements in an array by processing their digits or characters from least significant to most significant. Radix sort can be used to sort integers, as well as strings and other data types that can be represented as sequences of digits or characters.

Here is an overview of the algorithm:

- Find the maximum element: We first find the maximum element in the array, so that we know the maximum number of digits or characters in any element.
- Sort by least significant digit/character: We sort the elements in the array based on their least significant digit or character. This can be done using a stable sorting algorithm like counting sort or bucket sort.
- Sort by next least significant digit/character: We repeat step 2, sorting the elements based on their next least significant digit or character.
- Repeat until all digits/characters have been sorted: We continue to repeat step 2 and step 3 until all digits or characters have been sorted.

Here is some sample code for the radix sort algorithm in Java:

```
```public static void radixSort(int[] arr) {

int max = getMax(arr);

// Do counting sort for every digit

for (int exp = 1; max / exp > 0; exp *= 10) {

countingSort(arr, exp);

}

}

public static int getMax(int[] arr) {

int max = arr[0];

for (int i = 1; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];

}

}

return max;

}

public static void countingSort(int[] arr, int exp) {

int n = arr.length;

int[] count = new int[10];

int[] output = new int[n];

// Count occurrences of each digit

for (int i = 0; i < n; i++) {

int digit = (arr[i] / exp) % 10;

count[digit]++;

}

// Compute running sums of counts

for (int i = 1; i < 10; i++) {

count[i] += count[i - 1];

}

// Place elements in output array

for (int i = n - 1; i >= 0; i--) {

int digit = (arr[i] / exp) % 10;

output[count[digit] - 1] = arr[i];

count[digit]--;

}

// Copy elements from output array to original array

for (int i = 0; i < n; i++) {

arr[i] = output[i];

}

}

In this example, we define a method called radixSort that takes an array of integers as its input. The method first finds the maximum element in the array using the getMax method. It then performs counting sort on each digit of the elements in the array, from least significant to most significant, using the countingSort method.

The countingSort method takes the original array and an integer exp representing the digit to sort on. It first initializes an array count to count the occurrences of each digit, and then computes the running sums of the counts. It then places each element in the output array in the correct position based on its digit, and finally copies the elements from the output array back into the original array.

The time complexity of radix sort is O(kn), where k is the number of digits or characters in the input elements, and n is the number of elements in the array. The space complexity of radix sort is also O(kn), since it requires a separate count array and output array for each digit.

Radix sort has some advantages over comparison-based sorting algorithms like quicksort and mergesort. First, it is a stable sort, meaning that it preserves the relative order of equal elements. Second, it can be used to sort elements of variable length, such as strings or variable-length integers. Third, it has a linear time complexity for fixed-length elements, making it faster than comparison-based sorting algorithms for large inputs.

However, radix sort also has some disadvantages. First, it has a high constant factor, as it requires a separate counting sort for each digit. Second, it is not an in-place sorting algorithm, as it requires extra space for the count and output arrays. Finally, it is not a universal sorting algorithm, as it requires that the input elements can be represented as sequences of digits or characters.

Despite these drawbacks, radix sort is still a useful sorting algorithm in some contexts, particularly when sorting elements with variable-length keys or when a stable sort is necessary.

### COUNTING SORT

Counting sort is a non-comparison-based sorting algorithm that sorts elements in an array based on their frequency of occurrence. Counting sort assumes that the elements in the array are integers, and that the range of the integers is small compared to the size of the array.

Here is an overview of the algorithm:

- Find the maximum element: We first find the maximum element in the array, so that we know the size of the count array.
- Count the occurrences of each element: We initialize a count array of size max + 1, and iterate through the array, incrementing the count of the element at each index.
- Compute running sums of counts: We iterate through the count array, and at each index, add the count at that index to the count at the previous index.
- Place the elements in the output array: We iterate through the input array from right to left, and place each element in the correct position in the output array based on its count. We decrement the count of the element in the count array after placing it in the output array.

Here is some sample code for the counting sort algorithm in Java:

```
```public static void countingSort(int[] arr) {

int n = arr.length;

int max = getMax(arr);

int[] count = new int[max + 1];

int[] output = new int[n];

// Count occurrences of each element

for (int i = 0; i < n; i++) {

count[arr[i]]++;

}

// Compute running sums of counts

for (int i = 1; i <= max; i++) {

count[i] += count[i - 1];

}

// Place elements in output array

for (int i = n - 1; i >= 0; i--) {

output[count[arr[i]] - 1] = arr[i];

count[arr[i]]--;

}

// Copy elements from output array to original array

for (int i = 0; i < n; i++) {

arr[i] = output[i];

}

}

public static int getMax(int[] arr) {

int max = arr[0];

for (int i = 1; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];

}

}

return max;

}

In this example, we define a method called countingSort that takes an array of integers as its input. The method first finds the maximum element in the array using the getMax method. It then initializes a count array of size max + 1, and iterates through the input array, incrementing the count of the element at each index.

The method then computes the running sums of the counts in the count array, and places each element in the correct position in the output array based on its count. Finally, the method copies the elements from the output array back into the original array.

The time complexity of counting sort is O(n + k), where n is the number of elements in the array, and k is the range of the integers in the array. The space complexity of counting sort is also O(n + k), since it requires a count array of size k+1 and an output array of size n.

Counting sort is a useful sorting algorithm when the range of the integers in the array is small compared to the size of the array. It is a stable sort, meaning that it preserves the relative order of equal elements. However, it is not an in-place sorting algorithm, as it requires extra space for the count and output arrays. Additionally, if the range of the integers in the array is very large, counting sort can become impractical, as it requires a large amount of memory for the count array.

Counting sort is often used as a subroutine in other algorithms, such as radix sort, and can be used as a building block for more complex data structures, such as hash tables. It is particularly useful in applications where the values being sorted represent frequencies or probabilities, such as in speech processing, image processing, or natural language processing.

### BUCKETSORT

Bucket sort is a non-comparison-based sorting algorithm that sorts elements in an array by distributing them into a number of buckets, then sorting the buckets and concatenating them back into the original array.

Here is an overview of the algorithm:

- Create the buckets: We initialize an array of empty buckets, where the number of buckets is equal to the number of elements in the input array.
- Distribute the elements into the buckets: We iterate through the input array, and place each element in the corresponding bucket based on its value.
- Sort each bucket: We sort each bucket using a comparison-based sorting algorithm, such as quicksort or mergesort.
- Concatenate the buckets: We concatenate the sorted buckets back into the original array.

Here is some sample code for the bucket sort algorithm in Java:

```
```public static void bucketSort(int[] arr) {

int n = arr.length;

int max = getMax(arr);

// Create the buckets

List<Integer>[] buckets = new List[n];

for (int i = 0; i < n; i++) {

buckets[i] = new ArrayList<Integer>();

}

// Distribute the elements into the buckets

for (int i = 0; i < n; i++) {

int index = (int) ((double) arr[i] / max * (n -
1));

buckets[index].add(arr[i]);

}

// Sort each bucket

for (int i = 0; i < n; i++) {

Collections.sort(buckets[i]);

}

// Concatenate the buckets

int index = 0;

for (int i = 0; i < n; i++) {

for (int j = 0; j < buckets[i].size(); j++) {

arr[index++] = buckets[i].get(j);

}

}

}

public static int getMax(int[] arr) {

int max = arr[0];

for (int i = 1; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];

}

}

return max;

}

In this example, we define a method called bucketSort that takes an array of integers as its input. The method first finds the maximum element in the array using the getMax method. It then initializes an array of empty buckets, and distributes each element in the input array into the corresponding bucket based on its value.

The method then sorts each bucket using the Collections.sort method, which uses a comparison-based sorting algorithm. Finally, the method concatenates the sorted buckets back into the original array.

The time complexity of bucket sort is O(n+k), where n is the number of elements in the array, and k is the number of buckets. The space complexity of bucket sort is also O(n+k), since it requires an array of size k and a list of size n.

Bucket sort is a useful sorting algorithm when the input elements are uniformly distributed over a range, and the range is not too large. It is particularly useful when the input elements are integers or floating point numbers. However, it can become impractical if the range of the input elements is very large or if the elements are not uniformly distributed. Additionally, bucket sort requires extra memory to store the buckets, which can be a disadvantage when memory is limited.

## SEARCHING

Searching in arrays is the process of finding the location of a particular element in an array. There are several algorithms that can be used to search for an element in an array, including linear search and binary search.

### LINEAR SEARCH

Linear search is a simple algorithm that works by iterating through the elements in the array one by one, until the target element is found or the end of the array is reached. Here is some sample code for linear search in Java:

```
```public static int linearSearch(int[] arr, int target) {

for (int i = 0; i < arr.length; i++) {

if (arr[i] == target) {

return i;

}

}

return -1; // target element not found

}

In this example, we define a method called linearSearch that takes an array of integers and a target element as its inputs. The method iterates through the elements in the array using a for loop, and checks if each element is equal to the target element. If the target element is found, the method returns its index in the array. If the end of the array is reached without finding the target element, the method returns -1.

### BINARY SEARCH

Binary search is a more efficient algorithm that works by dividing the array in half at each step, and comparing the target element to the middle element of the current subarray. If the middle element is equal to the target element, the search is complete. If the target element is less than the middle element, the search continues in the left half of the subarray. If the target element is greater than the middle element, the search continues in the right half of the subarray. Here is some sample code for binary search in Java:

```
```public static int binarySearch(int[] arr, int target) {

int left = 0;

int right = arr.length - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

}

}

return -1; // target element not found

}

In this example, we define a method called binarySearch that takes an array of integers and a target element as its inputs. The method initializes two pointers, left and right, that define the current subarray being searched. The method then enters a while loop that continues until the subarray has been reduced to a single element or the target element is found. At each step, the method computes the middle index of the current subarray using integer division, and compares the middle element to the target element. If the middle element is equal to the target element, the search is complete. If the target element is less than the middle element, the method updates the right pointer to search in the left half of the subarray. If the target element is greater than the middle element, the method updates the left pointer to search in the right half of the subarray. If the target element is not found, the method returns -1.

The time complexity of linear search is O(n), where n is the number of elements in the array. The time complexity of binary search is O(log n), where n is the number of elements in the array. Binary search is faster than linear search for large arrays, but requires the array to be sorted. If the array is not sorted, it must be sorted first, which adds an additional O(n log n) time complexity.

### INTERPOLATION SEARCH

Interpolation search is a search algorithm that works by estimating the position of the target element based on the value of the target element and the values of the elements at the beginning and end of the search range. Interpolation search is particularly effective when the elements in the array are uniformly distributed.

Here is an overview of the algorithm:

- Initialize the search range: We initialize two pointers, left and right, that define the current search range.
- Estimate the position of the target element: We estimate the position of the target element by interpolating its position based on its value and the values of the elements at the beginning and end of the search range. Specifically, we calculate the position of the target element using the following formula: pos = left + (target - arr[left]) * (right - left) / (arr[right] - arr[left])
- Compare the target element to the element at the estimated position: If the target element is equal to the element at the estimated position, the search is complete. If the target element is less than the element at the estimated position, the search continues in the left half of the search range. If the target element is greater than the element at the estimated position, the search continues in the right half of the search range.
- Repeat until the target element is found or the search range is empty.

Here is some sample code for interpolation search in Java:

```
```public static int interpolationSearch(int[] arr, int target) {

int left = 0;

int right = arr.length - 1;

while (left <= right && target >= arr[left] &&
target <= arr[right]) {

int pos = left + (target - arr[left]) * (right - left) /
(arr[right] - arr[left]);

if (arr[pos] == target) {

return pos;

} else if (arr[pos] < target) {

left = pos + 1;

} else {

right = pos - 1;

}

}

return -1; // target element not found

}

In this example, we define a method called interpolationSearch that takes an array of integers and a target element as its inputs. The method initializes two pointers, left and right, that define the current search range. The method then enters a while loop that continues until the target element is found or the search range is empty. At each step, the method estimates the position of the target element using the interpolation formula, and compares the target element to the element at the estimated position. If the target element is equal to the element at the estimated position, the search is complete. If the target element is less than the element at the estimated position, the method updates the right pointer to search in the left half of the search range. If the target element is greater than the element at the estimated position, the method updates the left pointer to search in the right half of the search range. If the target element is not found, the method returns -1.

The time complexity of interpolation search is O(log log n) on average, where n is the number of elements in the array. However, in the worst case, when the elements in the array are not uniformly distributed, interpolation search can have a time complexity of O(n). Additionally, interpolation search requires the array to be sorted, and can produce incorrect results if the array is not sorted.

### EXPONENTIAL SEARCH

Exponential search is a search algorithm that works by exponentially increasing the size of the search range until the target element is found or the end of the array is reached. Exponential search is particularly effective when the array is sorted and unbounded, or when the location of the target element is not known in advance.

Here is an overview of the algorithm:

- Find the range that may contain the target element: We initialize two variables, left and right, to 0 and 1, respectively. We then repeatedly double the value of right until the value at index right is greater than or equal to the target element, or the end of the array is reached.
- Perform a binary search within the range: We perform a binary search within the range defined by left and right, using the same procedure as in binary search. If the target element is found, the search is complete. If the target element is not found, we return -1.

Here is some sample code for exponential search in Java:

```
```public static int exponentialSearch(int[] arr, int target) {

int n = arr.length;

if (arr[0] == target) {

return 0;

}

int i = 1;

while (i < n && arr[i] <= target) {

i *= 2;

}

int left = i / 2;

int right = Math.min(i, n - 1);

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

}

}

return -1; // target element not found

}

In this example, we define a method called exponentialSearch that takes an array of integers and a target element as its inputs. The method first checks if the target element is equal to the first element in the array. If it is, the method returns 0. The method then initializes a variable i to 1, and repeatedly doubles its value until the value at index i is greater than or equal to the target element, or the end of the array is reached.

The method then performs a binary search within the range defined by i/2 and Math.min(i, n-1), using the same procedure as in binary search. If the target element is found, the search is complete. If the target element is not found, the method returns -1.

The time complexity of exponential search is O(log n), where n is the number of elements in the array. The time complexity of the binary search step is also O(log n). However, the time complexity of the first step, which involves finding the range that may contain the target element, can be O(log n) in the worst case if the target element is located at the end of the array. Additionally, exponential search requires the array to be sorted.

## PATTERNS

Arrays are a fundamental data structure in computer science, and many programming problems involve manipulating and processing arrays. Here are some common patterns that you may encounter when solving array problems:

### LINEAR TRAVERSAL

This pattern involves iterating through an array sequentially, often using a for loop, and performing some operation on each element. For example, finding the sum of all elements in an array, or finding the maximum or minimum element.

Linear traversal search is a search algorithm that works by iterating through the elements in an array or list one by one, until the target element is found or the end of the array is reached. Linear traversal search is a simple algorithm that is easy to implement, but can be slow for large arrays.

Here is an example problem where linear traversal search can be applied:

Suppose we have an unsorted array of integers, and we want to find the minimum and maximum values in the array.

Here is some sample code for using linear traversal search to solve this problem in Java:

```
```public static void findMinMax(int[] arr) {

if (arr == null || arr.length == 0) {

System.out.println("Array is empty");

return;

}

int min = arr[0];

int max = arr[0];

for (int i = 1; i < arr.length; i++) {

if (arr[i] < min) {

min = arr[i];

} else if (arr[i] > max) {

max = arr[i];

}

}

System.out.println("Minimum value: " + min);

System.out.println("Maximum value: " + max);

}

In this example, we define a method called findMinMax that takes an array of integers as its input. The method first checks if the array is null or empty, and prints a message if it is. The method then initializes two variables, min and max, to the first element in the array. The method then iterates through the elements in the array using a for loop, and compares each element to the current values of min and max. If an element is smaller than min, the method updates min. If an element is larger than max, the method updates max. Finally, the method prints the values of min and max.

The time complexity of linear traversal search is O(n), where n is the number of elements in the array. In this example, the algorithm has to traverse the entire array to find the minimum and maximum values. However, since the array is unsorted, linear traversal search is the only option. If the array were sorted, other algorithms like binary search or interpolation search could be used to find the minimum and maximum values more efficiently.

### MULTIPLE POINTER TECHNIQUE

This pattern involves using two pointers to traverse the array simultaneously from different ends or in different directions. This technique is often used to solve problems involving searching, sorting, or merging arrays.

The multiple pointer technique is a strategy for solving problems that involves using two or more pointers to traverse an array or list. By using multiple pointers, we can often solve problems more efficiently than by using a single pointer.

The two sum problem is a classic example of a problem that can be solved using the multiple pointer technique. The two sum problem asks us to find two numbers in an array that add up to a given target value. Here is an example of how the multiple pointer technique can be used to solve the two sum problem in Java:

```
```public static int[] twoSum(int[] nums, int target) {

int left = 0;

int right = nums.length - 1;

while (left < right) {

int sum = nums[left] + nums[right];

if (sum == target) {

return new int[] {left, right};

} else if (sum < target) {

left++;

} else {

right--;

}

}

return new int[] {}; // no two numbers add up to target

}

In this example, we define a method called twoSum that takes an array of integers and a target value as its inputs. The method initializes two pointers, left and right, to the beginning and end of the array, respectively. The method then enters a while loop that continues until the pointers meet in the middle of the array. At each step, the method calculates the sum of the values at the left and right pointers, and compares the sum to the target value. If the sum is equal to the target value, the method returns the indices of the two numbers that add up to the target value. If the sum is less than the target value, the method moves the left pointer one position to the right. If the sum is greater than the target value, the method moves the right pointer one position to the left. If no two numbers add up to the target value, the method returns an empty array.

The time complexity of the multiple pointer technique depends on the specific problem being solved, but is often O(n) or O(nlogn) for problems involving sorted arrays. In the two sum problem, the time complexity of the multiple pointer technique is O(n), since the pointers traverse the entire array at most once.

### SLIDING WINDOW TECHNIQUE

This pattern involves maintaining a subarray of a fixed size while iterating through the array. The subarray is moved one element at a time, and some operation is performed on the elements in the subarray. This technique is often used to solve problems involving finding subarrays with certain properties, such as maximum or minimum sum or product.

The sliding window technique is a strategy for solving problems that involve finding a subarray, subsequence, or substring that satisfies a certain condition. The technique works by defining a window, which is a contiguous subarray, subsequence, or substring of the input data, and then sliding the window along the data while updating the condition.

Here is an example problem that can be solved using the sliding window technique:

Suppose we have an array of integers, and we want to find the maximum sum of any subarray of length k.

Here is an example of how the sliding window technique can be used to solve this problem in Java:

```
```public static int maxSumSubarray(int[] nums, int k) {

int maxSum = 0;

int currentSum = 0;

for (int i = 0; i < k; i++) {

currentSum += nums[i];

}

maxSum = currentSum;

for (int i = k; i < nums.length; i++) {

currentSum += nums[i] - nums[i - k];

maxSum = Math.max(maxSum, currentSum);

}

return maxSum;

}

In this example, we define a method called maxSumSubarray that takes an array of integers and an integer k as its inputs. The method initializes two variables, maxSum and currentSum, to 0. The method then calculates the sum of the first k elements in the array and assigns it to currentSum. The method updates maxSum to be equal to currentSum.

The method then enters a for loop that iterates from k to the end of the array. At each step, the method updates currentSum by subtracting the value of the element k positions back from the current index, and adding the value of the current element. The method then updates maxSum to be the maximum value of maxSum and currentSum.

The method returns maxSum, which is the maximum sum of any subarray of length k in the input array.

The time complexity of the sliding window technique is often O(n), where n is the size of the input data. In the example problem, the time complexity of the sliding window technique is O(n), since the algorithm traverses the input array twice at most.

### BINARY SEARCH

This pattern involves using binary search to find a particular element or to solve problems that involve finding the first or last occurrence of a particular element in a sorted array.

The missing number problem asks us to find the missing number in an array of n distinct integers that are randomly ordered between 0 and n. For example, if the input array is [3,0,1], then the missing number is 2.

Binary search is a good approach to solving this problem, as it can help us find the missing number in O(log n) time complexity. Here is how we can use binary search to solve the missing number problem:

- First, we sort the array using a sorting algorithm like quicksort or mergesort. Sorting the array ensures that the elements are in ascending order, and makes it easier to find the missing number using binary search.
- Next, we define a search interval between the first and last elements of the sorted array. We initialize the left and right pointers to the first and last indices of the array, respectively.
- We calculate the midpoint of the search interval, and compare the value at the midpoint to the expected value. The expected value is the sum of the first n natural numbers, which is equal to (n * (n + 1)) / 2. If the value at the midpoint is less than the expected value, then we know that the missing number must be in the right half of the search interval. If the value at the midpoint is greater than the expected value, then we know that the missing number must be in the left half of the search interval.
- We update the search interval by setting the left or right pointer to be the midpoint index, depending on whether the missing number is in the left or right half of the interval.
- We repeat steps 3-4 until the left and right pointers meet, at which point we have found the missing number.

Here is an implementation of the missing number problem using binary search in Java:

```
```public static int missingNumber(int[] nums) {

Arrays.sort(nums);

int n = nums.length;

int left = 0;

int right = n;

while (left < right) {

int mid = left + (right - left) / 2;

if (nums[mid] > mid) {

right = mid;

} else {

left = mid + 1;

}

}

return left;

}

In this implementation, we first sort the input array using the Arrays.sort method. We then initialize the search interval between 0 and the length of the array, and enter a while loop that continues until the left and right pointers meet. At each step, we calculate the midpoint of the interval and compare the value at the midpoint to the expected value. If the value at the midpoint is greater than the expected value, we update the right pointer to be the midpoint. Otherwise, we update the left pointer to be the midpoint plus one. When the left and right pointers meet, the missing number is the value of the left pointer.

The time complexity of this algorithm is O(n log n) due to the sorting step, which dominates the O(log n) time complexity of the binary search. However, there is an O(n) solution to the problem using the Gauss formula for summing the first n natural numbers, which is (n * (n + 1)) / 2. The idea is to subtract the sum of the input array from the expected sum to get the missing number.

### DIVIDE AND CONQUER

The divide and conquer technique is a common approach to solving array problems. This technique involves breaking down a problem into smaller subproblems, solving the subproblems recursively, and then combining the solutions to the subproblems to obtain the solution to the original problem. The divide and conquer technique can be used to solve a wide variety of array problems, such as finding the maximum subarray, sorting an array, or computing the majority element.

One classic example of the divide and conquer technique applied to an array problem is the maximum subarray problem. Given an array of integers, the problem is to find the contiguous subarray with the largest sum. The brute force solution to this problem is to consider every possible subarray and compute its sum, which takes O(n^2) time. However, by applying the divide and conquer technique, we can solve the problem in O(n log n) time.

The basic idea of the divide and conquer approach to the maximum subarray problem is to divide the array into two halves and recursively compute the maximum subarray in each half. The maximum subarray must be one of the following:

- The maximum subarray in the left half.
- The maximum subarray in the right half.
- A subarray that crosses the midpoint.

The first two cases can be solved recursively, while the third case can be computed using a linear scan from the midpoint to the left and right ends of the array.

Here is an example implementation of the divide and conquer approach to the maximum subarray problem in Java:

```
```public static int maxSubarray(int[] nums) {

if (nums.length == 0) {

return 0;

}

return maxSubarray(nums, 0, nums.length - 1);

}

private static int maxSubarray(int[] nums, int lo, int hi) {

if (lo == hi) {

return nums[lo];

}

int mid = lo + (hi - lo) / 2;

int maxLeft =
maxSubarray(nums, lo, mid);

int maxRight =
maxSubarray(nums, mid + 1, hi);

int maxCross =
maxCrossingSubarray(nums, lo, mid, hi);

return Math.max(Math.max(maxLeft, maxRight), maxCross);

}

private static int maxCrossingSubarray(int[] nums, int lo, int mid, int hi) {

int leftMax = Integer.MIN_VALUE;

int sum = 0;

for (int i = mid; i >= lo; i--) {

sum += nums[i];

leftMax = Math.max(leftMax, sum);

}

int rightMax = Integer.MIN_VALUE;

sum = 0;

for (int i = mid + 1; i <= hi; i++) {

sum += nums[i];

rightMax = Math.max(rightMax, sum);

}

return leftMax + rightMax;

}

In this implementation, the maxSubarray method takes an array of integers as input and returns the sum of the maximum subarray. The method first checks if the array is empty and returns 0 if it is. Otherwise, the method calls the recursive maxSubarray method with the start and end indices of the array.

The maxSubarray method recursively computes the maximum subarray in the left and right halves of the array, and the maximum subarray that crosses the midpoint. The method returns the maximum of these three values.

The maxCrossingSubarray method computes the maximum subarray that crosses the midpoint by scanning from the midpoint to the left and right ends of the array. The method maintains the maximum sum of a subarray that ends at or to the left of the midpoint and the maximum sum of a subarray that starts at or to the right of the midpoint. The sum of these two values gives the maximum sum of a subarray that crosses the midpoint.

The time complexity of this implementation is O(n log n), where n is the length of the input array. The space complexity is O(log n), which is used to store the recursive stack.

### PREFIX SUM

This pattern involves precomputing the cumulative sum of the elements in an array and using this information to solve various problems. This technique is often used to solve problems involving finding the sum of elements in a subarray or finding subarrays with a particular sum.

The prefix sum technique is a strategy for solving problems that involve calculating the sum of a contiguous subarray or subsequence multiple times. The technique works by calculating the prefix sums of the input data, which are the cumulative sums of the elements up to a certain index, and using them to calculate the sum of any contiguous subarray or subsequence in constant time.

Here is an example of how the prefix sum technique can be used to solve a problem in Java:

Suppose we have an array of integers, and we want to find the sum of all subarrays of the array.

We can solve this problem using the prefix sum technique as follows:

```
```public static int sumOfAllSubarrays(int[] nums) {

int n = nums.length;

int[] prefixSums = new int[n + 1];

int sum = 0;

for (int i = 0; i < n; i++) {

prefixSums[i + 1] = prefixSums[i] + nums[i];

}

for (int i = 0; i < n; i++) {

sum += (i + 1) * (n - i) * (prefixSums[i + 1] -
prefixSums[i]);

}

return sum;

}

In this example, we define a method called sumOfAllSubarrays that takes an array of integers as its input. The method initializes an array called prefixSums to be the length of the input array plus one, and initializes a variable called sum to 0.

The method then enters a for loop that iterates over the input array, and calculates the prefix sums of the input array by storing the cumulative sum of the elements up to a certain index in the prefixSums array.

After calculating the prefix sums, the method enters a second for loop that iterates over the input array again. At each step, the method uses the prefix sums to calculate the sum of all subarrays that include the current element, and adds that sum to sum. The formula for calculating the sum of all subarrays that include the current element is (i + 1) * (n - i) * (prefixSums[i + 1] - prefixSums[i]).

The method then returns sum, which is the sum of all subarrays of the input array.

The time complexity of the prefix sum technique is O(n), where n is the size of the input data, since the prefix sums can be calculated in linear time and used to calculate the sum of any contiguous subarray or subsequence in constant time.

### HASHING

This pattern involves using a hash table to store and retrieve elements from an array efficiently. This technique is often used to solve problems involving finding duplicate or missing elements in an array or counting the frequency of elements.

These are just a few common patterns that you may encounter when solving array problems. By recognizing these patterns and applying appropriate algorithms and data structures, you can solve many array problems efficiently.

The hashing technique is a strategy for solving problems that involve searching, inserting, and deleting elements in an array or other data structure. The technique works by using a hash function to map elements to array indices, allowing for constant time access to elements that have been hashed.

Here is an example of how the hashing technique can be used to solve a problem in Java:

Suppose we have an array of integers, and we want to find a pair of elements in the array that sum to a given target value.

We can solve this problem using the hashing technique as follows:

```
```public static int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {

int complement = target - nums[i];

if (map.containsKey(complement)) {

return new int[] { map.get(complement), i
};

}

map.put(nums[i], i);

}

throw new IllegalArgumentException("No two elements add up to the
target.");

}

In this example, we define a method called twoSum that takes an array of integers and a target value as its inputs. The method initializes a Map object called map to store the hashed elements of the input array, and enters a for loop that iterates over the input array.

At each step, the method calculates the complement of the current element by subtracting it from the target value, and checks if the complement has already been hashed in the map. If the complement is found in the map, the method returns the indices of the current element and the complement as an array. If the complement is not found in the map, the method stores the current element and its index in the map.

If no pair of elements in the input array add up to the target value, the method throws an exception.

The time complexity of this algorithm is O(n), where n is the size of the input array, since the method iterates over the input array once and uses constant time operations to hash and search elements in the map.

### BACKTRACKING

The Backtracking pattern is a general algorithmic technique for solving problems by incrementally building candidates to the solutions and abandoning a candidate ("backtracking") as soon as it determines that the candidate cannot be completed to a valid solution.

The pattern applies to a wide range of problems, particularly in the domains of combinatorial optimization and constraint satisfaction problems. The essential idea is to generate all possible solutions to a problem, and in the process, eliminate any invalid solutions as soon as possible.

The Backtracking pattern typically uses a recursive function that builds candidate solutions by making choices at each step of the recursion. After making a choice, the function checks if the choice leads to a valid solution, and if it does not, the function backtracks and tries another choice. The pattern is complete when all possible solutions have been explored.

A typical example of a problem that can be solved using the Backtracking pattern is the N-Queens problem, which involves placing N chess queens on an N x N chessboard in such a way that no two queens threaten each other. Other examples include the Sudoku puzzle, the Knight's Tour problem, and the Cryptarithmetic problem.

Java implementation of the Backtracking pattern typically involves a recursive function that takes as input the current state of the solution and returns the final solution when it is found. The function is typically designed to explore the solution space in a depth-first manner, making choices at each step of the recursion and backtracking when necessary.

Here's an example implementation of the "permutations" problem using the backtracking pattern:

```
```public List<List<Integer>> permute(int[] nums) {

List<List<Integer>> result = new
ArrayList<>();

List<Integer> current = new
ArrayList<>();

boolean[] used = new boolean[nums.length];

backtrack(nums, current, used, result);

return result;

}

private void backtrack(int[] nums, List<Integer> current, boolean[] used,
List<List<Integer>> result) {

if (current.size() == nums.length) {

result.add(new
ArrayList<>(current));

return;

}

for (int i = 0; i < nums.length; i++) {

if (!used[i]) {

current.add(nums[i]);

used[i] = true;

backtrack(nums, current,
used, result);

current.remove(current.size()
- 1);

used[i] = false;

}

}

}

In this example, we're trying to find all possible permutations of a given set of numbers. The algorithm works by recursively building up a list of candidates (the current list), and checking if each candidate is a valid solution. If it is, we add it to the result list. If not, we backtrack and try the next candidate.

The backtrack method takes four parameters: the original nums array, the current list of candidates, a used boolean array that keeps track of which numbers have been used, and the result list that will eventually hold all valid permutations.

We start by checking if the current list is a valid solution, which is the case when its size is equal to the length of the original array. If it is, we add a copy of the current list to the result list and return.

If the current list is not a valid solution, we try to add each number from the original array that hasn't already been used to the current list, and recursively call the backtrack method with the updated current and used lists. After each recursive call, we remove the last element from the current list and mark the corresponding number as unused, so that we can try the next candidate in the next iteration.

The permute method simply initializes the necessary data structures and calls the backtrack method with the appropriate parameters. Finally, it returns the result list.

### MERGE INTERVALS

The Merge Intervals pattern involves sorting an array of intervals, then merging overlapping intervals into a single interval. This can be useful for problems where we need to find the overlapping intervals in a set of intervals.

Here is an example implementation in Java:

```
```class Interval {

int flex-start;

int end;

public Interval(int start, int end) {

this.start = flex-start;

this.end = end;

}

}

public static List<Interval> mergeIntervals(List<Interval> intervals)
{

List<Interval> result = new ArrayList<>();

if (intervals == null || intervals.size() == 0) {

return result;

}

// Sort the intervals by their start time

Collections.sort(intervals, (a, b) -> a.start - b.start);

// Merge overlapping intervals

Interval currentInterval = intervals.get(0);

for (int i = 1; i < intervals.size(); i++) {

Interval interval = intervals.get(i);

if (currentInterval.end >= interval.start) {

// Merge the two intervals

currentInterval.end =
Math.max(currentInterval.end, interval.end);

} else {

// Add the current interval to the result
and start a new interval

result.add(currentInterval);

currentInterval = interval;

}

}

// Add the last interval to the result

result.add(currentInterval);

return result;

}

In this implementation, we define an Interval class that has a start and end time. The mergeIntervals method takes a list of intervals as input and returns a list of merged intervals.

First, we sort the list of intervals by their start time. Then, we loop through the list and merge any overlapping intervals. If two intervals overlap, we merge them into a single interval by taking the maximum end time of the two intervals. If two intervals do not overlap, we add the current interval to the result and start a new interval. Finally, we add the last interval to the result and return it.

### SUBSETS

The subsets pattern involves generating all possible subsets of an array. This pattern is useful in solving many problems, including combination-based problems. The idea behind this pattern is to use recursion to generate all possible subsets by including or excluding each element in the array.

To implement this pattern, we can start by creating an empty list to hold the subsets. Then we can create a recursive function that takes the original array, the current index, and the current subset as parameters. In the recursive function, we can add the current subset to the list of subsets and then iterate over the remaining elements in the array, calling the recursive function with the next index and the current subset plus the current element. We can also call the recursive function with the next index and the current subset without the current element.

Here's an example implementation in Java:

```
```public List<List<Integer>> subsets(int[] nums) {

List<List<Integer>> result = new
ArrayList<>();

generateSubsets(nums, 0, new ArrayList<>(), result);

return result;

}

private void generateSubsets(int[] nums, int index, List<Integer> current,
List<List<Integer>> result) {

result.add(new ArrayList<>(current));

for (int i = index; i < nums.length; i++) {

current.add(nums[i]);

generateSubsets(nums, i + 1, current, result);

current.remove(current.size() - 1);

}

}

In this implementation, we start by calling the subsets function with the input array. The generateSubsets function is then called recursively with the input array, starting index of 0, an empty list as the current subset, and the result list to hold all the subsets.

In the generateSubsets function, we first add the current subset to the result list. Then, we iterate over the remaining elements in the array starting from the current index. For each element, we add it to the current subset, call the generateSubsets function recursively with the next index and the updated current subset, and then remove the added element from the current subset. This generates all possible subsets of the input array.

### CYCLIC SORT

The cyclic sort pattern is a popular approach to solve problems involving an array containing n elements in the range [1, n]. The idea is to iterate over the array, and for each element, swap it with the element at its correct position.

Here are the steps to follow for the cyclic sort pattern:

- Start iterating over the array from the first element (i = 0).
- For each element, check if it is at its correct position (element at index i should be i + 1). If not, swap the element with the element at its correct position (index element[i] - 1).
- Repeat step 2 until you reach the end of the array.

The main advantage of the cyclic sort pattern is its time complexity. Since it requires only one iteration over the array and swaps the elements in-place, the time complexity is O(n). However, the cyclic sort pattern can be used only if the range of the elements is limited, as it relies on swapping the elements to their correct positions.

Here's an example implementation of the cyclic sort pattern in Java:

```
```public static void cyclicSort(int[] nums) {

int i = 0;

while (i < nums.length) {

if (nums[i] != i + 1) {

int j = nums[i] - 1;

int temp = nums[i];

nums[i] = nums[j];

nums[j] = temp;

} else {

i++;

}

}

}

This implementation takes an array of integers as input and sorts it using the cyclic sort pattern. It first initializes a pointer i to the first element of the array and then iterates over the array until it reaches the end. For each element, it checks if it is at its correct position. If not, it swaps the element with the element at its correct position. If it is at its correct position, it moves the pointer i to the next element.

Note that the implementation assumes that the elements in the array are in the range [1, n], where n is the length of the array. If the elements are in a different range, the implementation would need to be adjusted accordingly.

### IN-PLACE REVERSAL

The in-place reversal pattern is used to reverse the order of elements in an array in place, meaning without creating a new array. This pattern can be used to solve a variety of problems that require an array to be reversed, such as reversing a string or a sentence, rotating an array or performing similar operations.

The in-place reversal pattern can be implemented using a two-pointer approach, where the two pointers start at the two ends of the array and move towards each other until they meet at the center of the array. During each iteration, the elements at the two pointers are swapped, until the entire array has been reversed.

Here is an example implementation of the in-place reversal pattern in Java for an array of integers:

```
```public static void reverse(int[] arr) {

int start = 0;

int end = arr.length - 1;

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

}

}

In this implementation, start and end are the two pointers that move towards each other until they meet in the middle of the array. During each iteration, the elements at the two pointers are swapped using a temporary variable temp. Once the entire array has been reversed, the method returns the reversed array.

### DUTCH NATIONAL FLAG

The Dutch National Flag problem is a pattern in arrays that requires sorting an array of objects with three distinct values (for example, red, white, and blue), using a single pass through the array. The goal is to group objects of the same value together and order them according to their value.

The algorithm works by using three pointers to divide the array into four sections. The first pointer, called "low," starts at the beginning of the array and moves to the right. The second pointer, called "high," starts at the end of the array and moves to the left. The third pointer, called "mid," starts at the beginning of the array and moves to the right.

At each step, the algorithm checks the value of the object pointed to by the mid pointer. If the value is the first distinct value (e.g., red), the object is swapped with the object pointed to by the low pointer, and both pointers are moved to the right. If the value is the second distinct value (e.g., white), the mid pointer is simply moved to the right. If the value is the third distinct value (e.g., blue), the object is swapped with the object pointed to by the high pointer, and the high pointer is moved to the left.

The algorithm continues until the mid pointer has reached the end of the array. At this point, all objects of the first value will be at the beginning of the array, all objects of the third value will be at the end of the array, and all objects of the second value will be in the middle.

Here is an example implementation in Java:

```
```public void dutchNationalFlag(int[] arr) {

int low = 0;

int mid = 0;

int high = arr.length - 1;

while (mid <= high) {

if (arr[mid] == 0) {

swap(arr, low, mid);

low++;

mid++;

} else if (arr[mid] == 1) {

mid++;

} else { // arr[mid] == 2

swap(arr, mid, high);

high--;

}

}

}

private void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

This implementation sorts an array of integers with values 0, 1, and 2. It uses the three pointers low, mid, and high to divide the array into four sections. The swap method is used to swap the positions of two elements in the array.

### TOP K ELEMENTS

The TOP K ELEMENTS pattern is a problem-solving technique where we need to find the K smallest or largest elements in an unsorted array. This pattern can be very useful in various scenarios like finding the Kth smallest or largest element, finding the Kth largest distinct element, finding the smallest range containing the Kth smallest or largest element, and so on.

The basic approach for this pattern involves sorting the array and then returning the first K elements for the K smallest elements or the last K elements for the K largest elements. However, this approach can be inefficient as we may not need to sort the whole array to find the K smallest or largest elements.

A more efficient approach is to use a min-heap for finding the K largest elements or a max-heap for finding the K smallest elements. We can also use the partitioning approach from quicksort, where we choose a pivot and divide the array into two parts based on the pivot, and then recursively select one of the parts until we find the K smallest or largest elements.

Here is an example implementation of finding the K largest elements using a min-heap in Java:

```
```public static List<Integer> findTopKLargest(int[] nums, int k) {

PriorityQueue<Integer> minHeap = new
PriorityQueue<>();

for (int i = 0; i < nums.length; i++) {

if (i < k) {

minHeap.offer(nums[i]);

} else if (nums[i] > minHeap.peek())
{

minHeap.poll();

minHeap.offer(nums[i]);

}

}

return new ArrayList<>(minHeap);

}

In this example, we use a min-heap to keep track of the K largest elements. We iterate over the array and add the first K elements to the heap. Then, for the remaining elements, if an element is greater than the smallest element in the heap, we remove the smallest element and add the new element to the heap. Finally, we return the elements in the heap as the K largest elements.

### BIT MANIPULATION

The Bit Manipulation pattern involves solving problems using bitwise operators. This pattern is particularly useful in problems that involve binary representations of data and in cases where bitwise operations can be used to optimize solutions.

Some common bitwise operators are:

- AND (&): returns a 1 in each bit position where both operands have a 1.
- OR (|): returns a 1 in each bit position where at least one operand has a 1.
- XOR (^): returns a 1 in each bit position where only one of the operands has a 1.
- NOT (~): inverts the bits of its operand.

Some common use cases for this pattern include:

- Counting the number of 1 bits in an integer.
- Setting, clearing, or toggling specific bits in an integer.
- Detecting if a number is a power of 2.
- Implementing bit vectors to represent sets efficiently.

Here is an example implementation of a problem that uses the Bit Manipulation pattern to count the number of 1 bits in an integer in Java:

```
```public int countOnes(int n) {

int count = 0;

while (n != 0) {

count += n & 1;

n >>>= 1;

}

return count;

}

In this implementation, we use the & operator to check if the least significant bit of the number is 1. If it is, we increment our count. We then shift the number one bit to the right using the >>> operator, which fills the leftmost bits with 0. We repeat this process until the number becomes 0. This solution has a time complexity of O(log n), where n is the input number, because we only need to check each bit once.

## PROBLEMS

### SEARCHING PROBLEMS

Easy

Medium

- Search in Rotated Sorted Array
- Search a 2D Matrix
- First Bad Version
- Find Peak Element
- Find Minimum in Rotated Sorted Array
- Find Kth Smallest Pair Distance

Hard

### SORTING PROBLEMS

Easy

Medium

- Merge Intervals
- Top K Frequent Elements
- Sort Colors
- Kth Largest Element in an Array
- Longest Increasing Subsequence
- Wiggle Sort

Hard

### ARRAYS

Easy

Medium

- Three Sum
- Product of Array Except Self
- Next Permutation
- Spiral Matrix
- Rotate Image
- Merge k Sorted Lists

Hard