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

Problem with Divide and Conquer Maximum Consecutive Subarray (MCS)

I want to find a nonempty, contiguous subarray for a given input array of integers, that can have duplicate values. I tried the divide and conquer method to find the maximum consecutive subarray of an array, which returns a different result as…
sij
  • 296
  • 3
  • 13
2
votes
1 answer

Generalizing the median of medians algorithm

I am asked to find the run time of the general form of the median of medians algorithm for groups of size g. It seems from common examples g=3,5,7 with T(n)=T(n/5)+T(2n/3)+cn, T(n)=T(n/5)+T(7n/10)+cn, and T(n)=T(n/7)+T(5n/7)+cn, respectively, that…
2
votes
3 answers

Algorithms question: Largest contiguous subarray selection

Q. Given two arrays, A and B, of equal length, find the largest possible contiguous subarray of indices [i,j] such that max(A[i: j]) < min(B[i: j]). Example: A = [10, 21, 5, 1, 3], B = [3, 1, 4, 23, 56] Explanation: A[4] = 1, A[5] = 3, B[4] = 23,…
2
votes
1 answer

Problem with Inversion Count Algorithm Implementation

Problem Statement I am attempting to solve this problem. Short summary of the problem: Given an unsorted array, find the total number of inversion pairs. Definition of inversion: index i and j forms a valid inversion pair if i < j && arr[i] >…
2
votes
2 answers

Greatest common diviser of an arrray using divide conquer technique

I am trying to find the GCD/HCF of an array, I know to write the function that finds the GCD of two numbers using Euclid's algorithm. so to find the GCD of the array I thought to use this Euclid algorithm as a divide and conquer technique for GCD…
def __init__
  • 1,092
  • 6
  • 17
2
votes
1 answer

optimising count of inversion(i.e for ia[j]) for a large array

I am trying to count inversion(for eg.,for array [2,5,4,1] the inversion count is=4 namely (2,1),(5,4),(5,1),(4,1) using divide and conquer for a large set of arrays, I am getting a recursive count of value when a function executes each merge sort.…
def __init__
  • 1,092
  • 6
  • 17
2
votes
1 answer

How to improve divide-and-conquer runtimes?

When a divide-and-conquer recursive function doesn't yield runtimes low enough, which other improvements could be done? Let's say, for example, this power function taken from here: def power(x, y): if (y == 0): return 1 elif (int(y % 2) ==…
loko
  • 111
  • 7
2
votes
0 answers

How to solve ouput error(Maximum-Subarray Problem)?

I am new to Python. Now, I need to implement a Maximum-Subarray Problem, using divide-and-conquer. However, Sum is correct, but low and high is wrong. For example: Input 16 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7 Output should be: Low =…
Kias
  • 47
  • 5
2
votes
3 answers

Find an element in list that appears at least 60% of the time using Divide and Conquer approach?

Input is an array that has at most one element that appears at least 60% a time. The goal is to find if this array has such an element and if yes, find that element. I came up with a divide and conquer function that finds such an element. from…
2
votes
1 answer

Maximum number of subsets of overlapping intervals

Given a set of intervals S what is the efficient way to find the number of subsets assigned to each interval from the set S. say for example S = (11,75), (14,62), (17,32), (24,48), (31,71), (34,74), (40,97), (41,58) as for output (11, 75) => 6 ->…
High On Math
  • 87
  • 2
  • 9
2
votes
2 answers

Smallest subset with sum greater or equal to k

I am trying to find a divide and conquer algorithm to solve this problem in O(n) but I didn't find anything. given an array A and given value k. find the smallest subset with a sum greater or equal to k. Can someone give me an idea to start solving…
hermi
  • 51
  • 5
2
votes
1 answer

What is the Time and Space Complexity of LeetCode 241. Different Ways to Add Parentheses?

I am trying to understand what is time complexity of leetcode 241. Different ways to add parentheses. I have used memoization technique. My friend was asked in Google coding round, he couldn't give correct time and space complexity. I found same…
2
votes
1 answer

is the implementation of the Recursive Task below correct?

I am beginning to understand the implementation of the Recursive Task and Recursive Actions. Based on my understanding and some java documentation, I came up with the below code to add up all the numbers in an array. I need help in correcting this…
2
votes
1 answer

Maximum weight independent set with divide and conquer

I was reading about the Maximum Weight Independent Set problem which is: Input: An undirected graph G = (V, E)and a non-negative weight Wv for each vertex v ∈ V Output: An independent set S ∈ V of G with the maximum-possible sum ∑Vw of vertex…
2
votes
3 answers

Fill dynamic size vector recursively

Maybe let me state my situation in pseudo-C++-code first: std:vector sample(someFunctor f, double lower, double upper) { double t = (lower + upper)/2; double newval = f(t); if (f(upper) - newval > epsilon) subsample1 =…
Thilo
  • 8,827
  • 2
  • 35
  • 56