Author: akshay

Insertion Sort is a simple and intuitive sorting algorithm that builds the final sorted array one element at a time. It works by taking each element from the input and inserting it into its correct position in the already sorted part of the array. Starting from the second element, it compares the current element with the previous ones, shifting larger elements one position ahead to make space for the current element. This process continues until all elements are sorted. Insertion Sort is efficient for small or nearly sorted datasets and operates in-place without requiring extra memory. Approach Start from the…

Read More

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the array is sorted. After each pass, the largest unsorted element “bubbles up” to its correct position at the end of the array. It’s called “Bubble Sort” because smaller elements slowly “bubble” to the top of the list. Approach: Iterate through the array multiple times. In each pass, compare adjacent elements: If the current element is greater than the next one, swap them. After each pass, the largest unsorted…

Read More

Selection Sort is a simple comparison-based sorting algorithm. It divides the array into two parts: a sorted subarray and an unsorted subarray. Initially, the sorted part is empty, and the unsorted part is the entire array. In each iteration, it finds the minimum element from the unsorted part and moves it to the end of the sorted part. Example: For input [4, 5, 1, 3, 9], the sorted output will be [1, 3, 4, 5, 9]. Approach: Iterate over the array from index 0 to n-2. For each index i, assume the element at i is the minimum in the…

Read More

Binary Search is an efficient algorithm used to find the position of a target value within a sorted array. Unlike linear search, it repeatedly divides the search interval in half, significantly reducing the number of comparisons. Approach Set left = 0, right = nums.length – 1. While left <= right: Calculate middle = Math.floor((left + right) / 2). If nums[middle] === target, return middle. If target < nums[middle], discard the right half: right = middle – 1. Else, discard the left half: left = middle + 1. If the target is not found, return -1. Example: Given array: [1, 3,…

Read More

Linear Search is a simple search algorithm used to find a specific element in an array. It checks each element of the array one by one until the target value is found or the end of the array is reached. Examples Example 1: Input: arr = [2, 4, 7, 10], target = 10 Output: 3 Explanation: 10 is found at index 3 Example 2: Input: arr = [6, 8, 0, 3], target = 5 Output: -1 Explanation: 5 is not present in the array Approach: Start from the first element of the array. Compare the current element with the target…

Read More

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. Consider the number of unique elements of nums to be k. To get accepted, you need to do the following things: Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important, as well as the size…

Read More

Given an integer array nums and an integer val, remove all occurrences of val in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val. Consider the number of elements in nums which are not equal to val be k. To get accepted, you need to: Modify nums such that the first k elements contain elements not equal to val. The remaining elements beyond k do not matter. Return k. Examples: Example 1: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: The first…

Read More

Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Examples Example 1: Input: s = [“h”,”e”,”l”,”l”,”o”] Output: [“o”,”l”,”l”,”e”,”h”] Example 2: Input: s = [“H”,”a”,”n”,”n”,”a”,”h”] Output: [“h”,”a”,”n”,”n”,”a”,”H”] Constraints: 1

Read More

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Examples Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6 – 1 = 5. Example 2: Input: prices…

Read More

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length…

Read More