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

Simple Divide and Conquer Example

Here is my test divide and conquer program, but it gives me an error. I am heading in the right direction? public class getSum { static int sum = 0; public static void main(String[] args) { int[] numbers = {2,2,2,2,2,2,2,2}; …
shinjuo
  • 20,498
  • 23
  • 73
  • 104
0
votes
2 answers

Divide and Conquer Algorithms

We are starting to work with Divide and conquer algorithms in my Data structures class and I am having a lot of trouble completely understanding what I am supposed to do. Below is the which is basically asking for me to write a program that sums an…
shinjuo
  • 20,498
  • 23
  • 73
  • 104
0
votes
2 answers

Divide and conquer: IndexSearch

i solved the following task by myself: Give an algorithm to find an index i such that 1 <= i <= n and A[i] = i provide such an index exists. If there are any such indexes, algorithm can return any of them. I used the divide and conquer approach and…
Patric
  • 57
  • 1
  • 3
  • 7
0
votes
2 answers

sorted matrix search master theorem analysis

So the problem is to find whether x is in one of the elements of a sorted matrix ascending by row and by column. example : 1 2 3 4 5 6 7 8 9 I'm interested to find the time complexity of the divide and conquer solution of this problem. I've googled…
0
votes
1 answer

Calculating runtime in recursive algorithm

Practicing recursion and D&C and a frequent problem seems to be to convert the array: [a1,a2,a3..an,b1,b2,b3...bn] to [a1,b1,a2,b2,a3,b3...an,bn] I solved it as follows (startA is the start of as and startB is the start of bs: private static void…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
0
votes
1 answer

Why is there so much of a speed difference in these factorial functions?

So I recently became interested in computing factorials really fast, and I wrote one and and got a few others from the internet to test. Here is the code (Python 2.7): def prime_sieve(n): """A prime sieve, takes an int n, and returns all the…
Broseph
  • 1,655
  • 1
  • 18
  • 38
0
votes
2 answers

Python translation of C code not working

I want to find the maximum element in an array in O(log N) time using divide and conquer. I found a working code at planet-source-code. I translated it into Python as follows: def largest(arr,i,j): global max1 global max2 if i == j: …
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
0
votes
2 answers

Counting the number of inversions in an array, C++

Inversion in an array a of size n is called a pair (i,j) for which it holds that ia[j]. I'm trying to implement a function in C++ which counts the number of inversions in a given array. I followed the divide and conquer approach, which…
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
0
votes
1 answer

Divide and Conquer algorithm for generating n-bit strings?

Can someone please tell how to generate n-bit strings(all possible combinations) i.e. counting bits form 0 to 2^n-1 using Divide and Conquer Approach. I was able to do this with the following algorithm, but the space complexity as well as time…
user1581106
0
votes
3 answers

Maximum sub array using D&C/Recursion

I want to implement Maximum Sub Array problem with an algorithm that exhibits (n log n): Find the maximum contigous sub array, or maximum sum of contiguous elements in the array. Assumption:not all elements are negative numbers I have somewhat…
cyber101
  • 2,822
  • 14
  • 50
  • 93
0
votes
3 answers

what is the complexity of recursive linear search ( using divide and conquer technique )?

wanted to analyse the complexity of recursive linear search ( using divide and conquer technique ). Is it log(n) or n ? if not log(n) then what is the actually complexity and how ? int linear_search(int *a,int l,int h,int key){ if(h == l){ …
0
votes
3 answers

divide and conque binary search in C

I'm trying to make a divide and conquer version of binary search, but one that divides the array to two subarrays and search similar to merging in merge sort, the reason I want to do that becuase I want to use it in cilk, but I have to make it that…
hakuna matata
  • 3,243
  • 13
  • 56
  • 93
0
votes
3 answers

Mergesort gives garbage value for the first element of the sorted array when executing

I am implementing Mergesort using the algorithm described in "Introduction to Algorithms". However, upon every execution I get a garbage value as the first element of the sorted array. Here is the code for it: #include #include…
user1043884
  • 43
  • 1
  • 6
0
votes
1 answer

Implement simple algorithm using divide and conquer

I am trying to implement the following algorithm using the divide and conquer method in order to get the running time to O(n*logn). Given a sequence of numbers a_1, a_2,…, a_n and a number k, find i and j such that 1<= j – i <= k while maximizing…
Tesla
  • 822
  • 2
  • 13
  • 27
0
votes
1 answer

mergesorting 3 sub-arrays at once

I am making a program which implements the mergesort algorithm but instead of dividing each time in 2 parts it divides them in 3 parts each time and mergesorting them recursively. In case I confused you it is basically a mergesort but instead of…
Aki K
  • 1,222
  • 1
  • 27
  • 49