Sorting and Searching

Here are some LeetCode problems that involve sorting as an intermediate step in the problem-solving process:

  1. “973. K Closest Points to Origin”: You need to sort the points by their distance to the origin to find the K closest points.

  2. “179. Largest Number”: The problem involves sorting numbers in a specific way to form the largest possible number.

  3. “56. Merge Intervals”: Intervals need to be sorted to easily identify overlapping intervals.

  4. “15. 3Sum”: The problem becomes simpler to solve if the input array is sorted, as we can use the two-pointer approach.

  5. “75. Sort Colors”: The problem requires sorting an array with only three distinct elements.

  6. “148. Sort List”: The problem involves sorting a linked list.

  7. “215. Kth Largest Element in an Array”: A common approach is to sort the array first, making it trivial to find the Kth largest element.

  8. “454. 4Sum II”: By creating sums of pairs and sorting these sums, the problem becomes a two-sum problem.

  9. “57. Insert Interval”: After inserting the new interval, the array of intervals needs to be sorted.

  10. “350. Intersection of Two Arrays II”: Sorting both arrays first makes it straightforward to find their intersection.

Here are some LeetCode problems that involve searching as an intermediate step in the problem-solving process:

  1. “704. Binary Search”: This problem requires you to implement binary search, a fundamental searching algorithm.

  2. “33. Search in Rotated Sorted Array”: This problem is a variant of binary search where the array has been rotated.

  3. “81. Search in Rotated Sorted Array II”: A similar problem to the one above, but this time the array may contain duplicates.

  4. “34. Find First and Last Position of Element in Sorted Array”: This problem involves using binary search to find the first and last occurrence of a target number in a sorted array.

  5. “74. Search a 2D Matrix”: This problem involves searching in a 2D matrix where the rows and columns are sorted.

  6. “240. Search a 2D Matrix II”: This problem involves searching in a 2D matrix where the rows and columns are sorted, but with a different arrangement than the above problem.

  7. “153. Find Minimum in Rotated Sorted Array”: The problem involves finding the minimum in a sorted and rotated array, which can be done using binary search.

  8. “162. Find Peak Element”: Binary search can be used to find a peak element in an array where nums[i] ≠ nums[i+1].

  9. “35. Search Insert Position”: This problem involves finding the position to insert a target value in a sorted array, using binary search.

  10. “278. First Bad Version”: In this problem, you need to use binary search to minimize the number of calls to a given API to find the first bad version.