Arrays
Left most index of an element
Find an element in infinite sized sorted array
class Solution {
// Simple binary search algorithm
static int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
// Method takes an infinite size array and a key to be
// searched and returns its position if found else -1.
// We don't know size of arr[] and we can assume size to be
// infinite in this function.
// NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE
// THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING
static int findPos(int arr[], int key) {
int l = 0, h = 1;
int val = arr[0];
// Find h to do binary search
while (val < key) {
l = h; // store previous high
// check that 2*h doesn't exceeds array
// length to prevent ArrayOutOfBoundException
if (2 * h < arr.length - 1)
h = 2 * h;
else
h = arr.length - 1;
val = arr[h]; // update new val
}
// at this point we have updated low
// and high indices, thus use binary
// search between them
return binarySearch(arr, l, h, key);
}
// Driver method to test the above function
public static void main(String[] args) {
int arr[] = new int[] { 3, 5, 7, 9, 10, 90,
100, 130, 140, 160, 170 };
int ans = findPos(arr, 10);
if (ans == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index " + ans);
}
}Find pair in unsorted array which gives sum X
Find pair in sorted array which gives sum X
Find triplet in an array which gives sum X
Contains Duplicate
Maximum Subarray
Two Sum
Merge Sorted Array
Intersection of Two Arrays II
Best Time to Buy and Sell Stock
Binary Search
Search Insert Position
Squares of a Sorted Array
Rotate Array
Move Zeroes
Two Sum II - Input Array Is Sorted
Largest Subarray with Sum Zero
Largest Subarray with Sum X
Longest Subarray with equal 0s and 1s
Find pair with having sum X in unsorted array
Reverse an array
Rotate an array by number by D
Replace Elements with Greatest Element on Right Side
Prefix Sum array
Find Pivot Index or Equilibrium Point
Maximum occurred integer in N ranges*
Strongest Neighbour | Find Peak Element
First Missing Positive
Rearrange array alternatively
Rearrange the array
Maximum Index
Check if array is sorted and rotated
Trapping Rainwater
Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock III
Sub array Sum Equals K
Minimum Size Sub array Sum
Sum of Sub array Minimums
Maximum Sub array | Kadane Algorithm
Kadane Algorithm
Sliding Window Maximum
Maximum Gap
Last updated