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
5
votes
2 answers

Closest pair algorithm for comparing points from 2 different arrays

I would like to compare the points from one array to the points from another array and find the closest pair. Till now whatever I have come across is with a single array. I do not want to compare the points from the same array. The brute force…
Lawrence
  • 427
  • 5
  • 15
5
votes
5 answers

Closest pair of points across a line

I have two sets of 2D points, separated from each other by a line in the plane. I'd like to efficiently find the pair of points, consisting of one point from each set, with the minimum distance between them. There's a really convenient looking paper…
5
votes
1 answer

Divide and conquer algorithm applied in finding a peak in an array.

For an array a: a1, a2, … ak, … an, ak is a peak if and only if ak-1 ≤ ak ≥ ak+1 when 1 < k and k < n. a1 is a peak if a1 ≥ a2, and an is a peak if an-1 ≤ an. The goal is to find one peak from the array. A divide and conquer algorithm is given as…
roland luo
  • 1,561
  • 4
  • 19
  • 24
5
votes
3 answers

Divide and conquer - why does it work?

I know that algorithms like mergesort and quicksort use the divide-and-conquer paradigm, but I'm wondering why does it work in lowering the time complexity... why does usually a "divide and conquer" algorithm work better than a…
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
5
votes
2 answers

Merge skylines, divide and conquer

I'm trying to solve the famous skyline problem (see gif): Input (1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28) Should return, the points that are behind other buildings should be gone and the coordinates of…
Oxymoron
  • 1,382
  • 4
  • 20
  • 40
5
votes
2 answers

a "divide and conquer" algorithm assignment

Now I have N different intergers, I need to find an interval that has the most numbers whose value is between the endpoints of the interval in O(NlogN) time. I call it a "divide and conquer" problem because it is in my final exam's "divide and…
Amos
  • 3,238
  • 4
  • 19
  • 41
4
votes
1 answer

Binary Search on Sorted List with Duplicates

To learn divide-and-conquer algorithms, I am implementing a function in Python called binary_search that will get the index of the first occurrence of a number in a non-empty, sorted list (elements of the list are non-decreasing, positive integers).…
4
votes
1 answer

Given a number N and a sorted array A, check if there are two numbers in A whose product is N

Given a number N and a sorted array A, design an algorithm - using the Divide and Conquer approach - to check if there exist index i and index j such that A[i]*A[j] == N (return 1 if present, 0 if not). I'm having a hard time proceeding in the…
devspyr0
  • 43
  • 5
4
votes
2 answers

Reasoning behind using a divide and conquer approach

I am trying to solve this question on LeetCode: A string s is nice if, for every letter of the alphabet that s contains, it appears both in uppercase and lowercase. For example, "abABB" is nice because 'A' and 'a' appear, and 'B' and 'b' appear.…
P.K.
  • 379
  • 1
  • 4
  • 16
4
votes
1 answer

When I run this code it return that "exit status 143" in java

When I run this code it return that "exit status 143" in java, I don't know what's going wrong there, hope someone can help me fix this problem. class Main { static double diff(double y, double x, double d){ if((y*y*y)+d>x) return…
TIGUZI
  • 231
  • 1
  • 3
  • 12
4
votes
1 answer

What is the implication of recursive calls modeling a perfect binary tree?

I'm learning data-structures and algorithms. The book I refer to(Sedgwick) uses 'finding the maximum element' to illustrate the divide-and-conquer strategy. This algorithm divides an array midway into two parts, finds the maximum elements in the two…
4
votes
1 answer

How many rectangles contain exactly k ones on a grid N*M

You are given a grid of 0's and 1's and it's dimension 1 ≤ N,M ≤ 2500 and a number 0 ≤ K ≤ 6. The task is to count the number of rectangles in the grid that have exactly K ones inside it. It has to be quicker that O(N^2*M), something like…
Dru01
  • 95
  • 7
4
votes
3 answers

How to efficiently find out the maximum value below a threshold?

I have two lists. List L1 contains all positive integers, list L2 contains positive numbers (e.g. 0.01,0.1,0.5,3,5,10,100....). Given a small positive number M (e.g. 0.10948472), find a,b from L1 and cfrom L2 s.t. (b/a)*c is maximized but still…
4
votes
2 answers

How to compute time and space complexity of this solution?

I am solving this problem on leetcode. Can't figure out the time and space complexity for my solution. Particularly, I can't understand how to apply Master Theorem here in case when we have FOR loop. What is a and b here? Since input divided…
4
votes
2 answers

Is every recursive algorithm a divide and conquer algorithm?

I have a problem for homework and I need to solve this problem with a divide and conquer algorithm. I solved this algorithm by using recursion. Did I use divide and conquer automatically by using recursion? For example, is this below approach a…