Arrays

Left most index of an element

  • Count of occurrences of x in sorted element

  • Count of 1s in binary sorted array

  1. Perform binary search to the left most element

  2. Perform the required operation from that side

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

https://leetcode.com/problems/contains-duplicate/

Maximum Subarray

https://leetcode.com/problems/maximum-subarray/

Two Sum

https://leetcode.com/problems/two-sum/

Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/

Intersection of Two Arrays II

https://leetcode.com/problems/intersection-of-two-arrays-ii/

Best Time to Buy and Sell Stock

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

https://leetcode.com/problems/binary-search/

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

Search Insert Position

https://leetcode.com/problems/search-insert-position/

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Squares of a Sorted Array

https://leetcode.com/problems/squares-of-a-sorted-array/

Rotate Array

https://leetcode.com/problems/rotate-array/

Move Zeroes

https://leetcode.com/problems/move-zeroes/

Two Sum II - Input Array Is Sorted

https://leetcode.com/problems/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

link : https://leetcode.com/problems/rotate-array/

Replace Elements with Greatest Element on Right Side

link : https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

Prefix Sum array

  • input : 10, 4, 16, 20

  • output : 10 14 30 50

Find Pivot Index or Equilibrium Point

https://leetcode.com/problems/find-pivot-index/

Maximum occurred integer in N ranges*

Here L[i] and R[i] present ranges : 1- 15, 4-8, 9-12 etc...

  • mark the starting of the range with 1 and ending with -1 on the next element after the range ends, do this for all ranges

  • find the prefix sum array for the array and find the max element, the index will the be most occur ed integer in N ranges

Strongest Neighbour | Find Peak Element

link : https://leetcode.com/problems/find-peak-element/

First Missing Positive

link : https://leetcode.com/problems/first-missing-positive/

Rearrange array alternatively

Rearrange the array

Given an array arr[] of size N where every element is in the range from 0 to n-1. Rearrange the given array so that arr[i] becomes arr[arr[i]].

Maximum Index

Check if array is sorted and rotated

Trapping Rainwater

  • Step 1 : Find the max of ( tallest bar to the left left[] , itself )

    • This will give the tallest bar on left till which it can hold water

  • Step 2 : Find the max of ( tallest bar to the right , itself )

    • This will give the tallest bar on right till which it can hold water

  • Step 3 : Find the sum : water += min ( left[i] , right[i] ) - arr[i]

    • This will give you the water captured in between the bars

link : https://leetcode.com/problems/trapping-rain-water/

Best Time to Buy and Sell Stock

link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Best Time to Buy and Sell Stock II

link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

Best Time to Buy and Sell Stock III

link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

Sub array Sum Equals K

link : https://leetcode.com/problems/subarray-sum-equals-k/

Minimum Size Sub array Sum

link : https://leetcode.com/problems/minimum-size-subarray-sum/

Sum of Sub array Minimums

link : https://leetcode.com/problems/sum-of-subarray-minimums/

Maximum Sub array | Kadane Algorithm

link : https://leetcode.com/problems/maximum-subarray/

Kadane Algorithm

Sliding Window Maximum

Maximum Gap

link : https://leetcode.com/problems/maximum-gap/

Last updated