Divide and conquer is a recursive problem solving approach in data structure and algorithms that divides a problem into smaller subproblems, recursively solves subproblems, and combines subproblem solutions to get the solution of the original problem. So, there are three steps of the DC method: divide, conquer and combine.
There are two sorted arrays A and B of size n each, write a program to find the median of these two sorted arrays obtained after merging (new merged array will be an array of length 2n which is even). The median of a sorted array of size n is defined as the middle element when n is odd and the average of the middle two elements when n is even.
Given an array A of n integers, find out the maximum difference between any two elements such that the larger element appears after the smaller element. In other words, we need to find max(A[j] - A[i]), where A[j] > A[i] and j > i. Note: This is an excellent problem to learn problem-solving using divide and conquer and a single loop.
Given an array of n elements, write a program to find the largest contiguous subarray sum. A subarray of array X is a contiguous segment from X[i] through X[j] where 0 <= i <= j <= n. Note: This is an excellent problem to learn problem solving using divide and conquer approach, dynamic programming and single loop (In place O(n) time solution).
Binary search is an efficient algorithm to search a value in the sorted array using divide and conquer approach. It compares target value with value at mid-index and repeatedly reduces search interval by half. Searching continues until value is found or subarray size gets reduced to 0 (unsuccessful search). Time complexity of binary search is O(logn).
Recursion means solving the problem using the solution of smaller sub-problems. This blog will explain these critical concepts: 1) What is recursion? 1) How recursion works in programming? 2) Advantages and disadvantages of recursion 3) Steps to solve problems using recursion? 4) Difference between recursion and iteration? Etc.
Merge sort is one of the fastest comparison based sorting algorithms, which works on the idea of divide and conquer approach. Worst and best case time complexity of merge sort is O(nlogn), and space complexity is O(n). This is also one of the best algorithms for sorting linked lists and learning design and analysis of recursive algorithms.
Quicksort algorithm is often the best choice for sorting because it works efficiently on average O(nlogn) time complexity. It is also one of the best algorithms to learn divide and conquer approach. In this blog, you will learn: 1) How quick sort works? 2) How to choose a good pivot? 3) Best, worst, and average-case analysis 4) Space complexity and properties of quicksort.
Understanding iteration vs recursion is one of the critical ideas in data structures and algorithms. If we compare iterative vs recursive approaches, one thing is common: Repeated execution of instructions until our task is done. But there are many differences in terms of implementation, code execution, time complexity analysis, etc.
You are given an array X of n elements, write a program to find majority element in an array. A majority element is an element that appears more than n/2 times, so there is at most one such element. Assume that array is non-empty and majority element always exists in the array. Note: This is an excellent problem to learn various approaches.
Learning analysis of recursion is critical to understand time complexity analysis of recursive algorithms. We will discuss these concepts of recursion analysis: recurrence relations of recursive algorithms, steps to analyze time complexity of recursion, recursion tree method, master theorem to analyze divide and conquer algorithms, etc.
Given an array X of size n, write a program to find the maximum and minimum element in an array. Our algorithm should make the minimum number of comparisons. Note: This is an excellent question to learn problem-solving using a single loop and divide and conquer approach. In the efficient single loop solution, we increment the loop by two.
Learning divide and conquer vs dynamic programming is one of the critical ideas in DSA. Both use a similar idea to break problems into subproblems and combine their solutions to get the final solution. There are lot of differences between both approaches in terms of the types of problems they solve, implementation, time and space complexity, etc.
In this blog, we will learn how to use segment trees for efficient point and range updates. For point update query, update(idx, val), we need to increment element at index idx from the original array by val, i.e. arr[idx] = arr[idx] + val. For range update query, update(l, r, val), we increment all elements from index l to r from the original array by val.
A segment tree is a binary tree data structure such that each node stores information about a range. We use segment trees to efficiently answer multiple range queries on an array like range minimum, range maximum, range sum, etc. Also, it allows us to modify the array by replacing an element or an entire range of elements in logarithmic time.
Given an array and a positive integer k, write a program to find the kth smallest element in the array. This is an excellent problem to learn problem-solving using the max and min heap data structure. The quick-select approach (divide and conquer) is also worth exploring because it helps to optimize time complexity to O(n) time average.
Subscribe to get weekly content on data structure and algorithms, machine learning, system design and oops.