Questions tagged [divide-and-conquer]

Divide-and-conquer is a top-down technique for designing algorithms that consists of dividing the problem into smaller subproblems hoping that the solutions of the subproblems are easier to find and then composing the partial solutions into the solution of the original problem.

The basic idea of divide-and-conquer technique is

1.Divide: Decompose problems into sub instances.
2.Conquer: Solve sub instances successively and independently.
3.Combine: Combine the sub solutions to obtain the solution to the original problem.

706 questions
215
votes
10 answers

Difference between Divide and Conquer Algo and Dynamic Programming

What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between them. Please take a simple example to explain any difference between the two…
saplingPro
  • 20,769
  • 53
  • 137
  • 195
115
votes
17 answers

How to find the kth smallest element in the union of two sorted arrays?

This is a homework question, binary search has already been introduced: Given two arrays, respectively N and M elements in ascending order, not necessarily unique: What is a time efficient algorithm to find the kth smallest element in the union of…
Michael
  • 41,026
  • 70
  • 193
  • 341
32
votes
17 answers

Why is Binary Search a divide and conquer algorithm?

I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the examinators asked where the conquer part in it was,…
31
votes
23 answers

How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?

How to optimally divide an array into two subarrays so that sum of elements in both subarrays is same, otherwise give an error? Example 1 Given the array 10, 20 , 30 , 5 , 40 , 50 , 40 , 15 It can be divided as 10, 20, 30, 5, 40 and 50, 40,…
Samarth2011
  • 1,343
  • 2
  • 11
  • 13
27
votes
7 answers

Algorithm for Shuffling a Linked List in n log n time

I'm trying to shuffle a linked list using a divide-and-conquer algorithm that randomly shuffles a linked list in linearithmic (n log n) time and logarithmic (log n) extra space. I'm aware that I can do a Knuth shuffle similar to that could be used…
5StringRyan
  • 3,604
  • 5
  • 46
  • 69
23
votes
4 answers

algorithms: how do divide-and-conquer and time complexity O(nlogn) relate?

In my Algorithms and Data Structures class a first divide-and-conquer algorithm namely merge sort was introduced. While implementing an algorithm for an assignment a few questions came to my mind. Does any algorithm that is implemented with the…
Andrew Tobey
  • 915
  • 3
  • 10
  • 27
22
votes
2 answers

Why do divide and conquer algorithms often run faster than brute force?

Why do divide and conquer algorithms often run faster than brute force? For example, to find closest pair of points. I know you can show me the mathematical proof. But intuitively, why does this happen? Magic? Theoretically, is it true that "divide…
user1445654
17
votes
8 answers

divide and conquer and recursion

I wonder if the technique of divide and conquer always divide a problem into subproblems of same type? By same type, I mean one can implement it using a function with recursion. Can divide and conquer always be implemented by recursion? Thanks!
Tim
  • 1
  • 141
  • 372
  • 590
15
votes
3 answers

How to find multiplicative partitions of any integer?

I'm looking for an efficient algorithm for computing the multiplicative partitions for any given integer. For example, the number of such partitions for 12 is 4, which are 12 = 12 x 1 = 4 x 3 = 2 x 2 x 3 = 2 x 6 I've read the wikipedia article for…
TCSGrad
  • 11,898
  • 14
  • 49
  • 70
13
votes
7 answers

Passing an array as an argument in C++

I'm writing a merge sort function, and right now I am just using a test case array (there is no input - this is static, for now). I don't know how to pass an array as an argument. Here is my code right now: //merge sort first attempt #include…
jkeys
  • 3,803
  • 11
  • 39
  • 63
13
votes
8 answers

How can I speed up my 'divide and conquer' XSLT template which replaces certain characters in a string?

UPDATE: I added an answer to this question which incorporates almost all the suggestions which have been given. The original template given in the code below needed 45605ms to finish a real world input document (english text about script…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
11
votes
3 answers

Understanding double recursion

I'm able to comprehend recursion easily if there is just one recursive call within a function. However, I am really getting confused when I see two or more recursive calls within the same function. Example: int MaximumElement(int array[], int index,…
acc_so
  • 175
  • 1
  • 1
  • 12
10
votes
3 answers

A divide-and-conquer algorithm for counting dominating points?

Let's say that a point at coordinate (x1,y1) dominates another point (x2,y2) if x1 ≤ x2 and y1 ≤ y2; I have a set of points (x1,y1) , ....(xn,yn) and I want to find the total number of dominating pairs. I can do this using brute force by comparing…
ERJAN
  • 23,696
  • 23
  • 72
  • 146
9
votes
1 answer

How to throw 2 eggs from a building and find the floor F with ~c*sqrt(F) throws?

I am reading Robert Sedgewick's algorithms 4th edition book, and he has the following task: Suppose that you have an N-story building and 2 eggs. Suppose also that an egg is broken if it is thrown off floor F or higher, and unbroken otherwise. Your…
9
votes
1 answer

Given a set of n points (x,y), is it possible to find the number of pairs of points with negative slopes between them in O(n logn) time?

Given a set of n points on a 2-d plane of the form (x,y), the aim is to find the number of pairs of all points (xi,yi) and (xj, yj) such that the line joining the two points has a negative slope. Assume that, no two xi's have the same value. Assume…
1
2 3
47 48