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

Tree recursion - Print subsequence of a given number

Problem statement: // m is the number, n is upto-length of subsequences // m = 20125, n =3 should print 201, 202, 205, 212, 215, 225, 012, 015, 125 // m = 20125, n =2 should print 20, 21, 22, 25, 01, 02, 05, 12, 15, 25 // m = 20125, n =1 should…
overexchange
  • 15,768
  • 30
  • 152
  • 347
-1
votes
1 answer

I designed a algorithm and implemented code , but I do not know the time complexity of the code T(n)=O(??)

I used so many for loops to get closet lower and larger elements, so I felt a little bit difficult to analysis the time complexity of this code. I defined a function named FindClosetLagerAndLower, input a array, as results it will return two arrays…
Jay Park
  • 308
  • 1
  • 6
  • 14
-1
votes
2 answers

Why is python list showing slicing abnormality?

I am trying to partition the list element recursively (like in divide and conquer) and consequently printing the sliced elements but suddenly sees unexpected abnormality(at line 6 and onwards in Output). def Mergesort(a, l, r): if(l
Anurag
  • 37
  • 1
  • 6
-1
votes
1 answer

Number execution of recursive function

I have this recursive algorithm and T(n) is the number of times that P(a, b) is executed and n := b - a int foo[] = ... // array of big enough size function P(int a, int b) if (a+1 < b) { int h = floor((a+b)/2) if foo[h] >= 0 then…
skinnyBug
  • 289
  • 1
  • 3
  • 12
-1
votes
4 answers

Python Divide and Conquer Implementation of Nth Power of X

As a new python programmer, I'm working on a Leetcode question and don't know why my code does not work, so I really appreciate your advice: Question: Implement pow(x, n), which calculates x raised to the power n. Example: Input: 2.00000, 10 Output:…
M00000001
  • 309
  • 2
  • 10
-1
votes
1 answer

How can I get all elements in range from a generic List recursively?

Given a Generic type List (List),and two generic type objects: a,b: -Design an algorithm that returns a List that contains every element from the original list that belongs to the range [a,b). -The algorithm must include comparator E. -The solution…
-1
votes
1 answer

Implement different algorithm to find dominant object with several parameter in typescript

I have some student's profile with values on several subjects like physics, chemistry and math. I need find a list of dominant students depending on individual score on subjects. For example: let students = [{name: "A", phy: 70, chem: 80, math:…
-1
votes
1 answer

Count number of 1s in a subarray when array is generated from a single number

I am looking at this coding problem: For a given number n, form a list and insert the following pattern into the list at the same position sequentially: { floor(n/2), n%2, floor(n/2) } ...until every element in the list is either 1 or 0. Now,…
danish
  • 9
  • 4
-1
votes
2 answers

Divide & Conquer algorithm for finding sum of positive numbers in circularly sorted array

I am trying to find the O(N) solution with Divide & Conquer for the next problem: Given a circularly sorted array, I need the sum of the positive numbers on it. i.e: If the array is: {-2, 0, 3, 4, 11, 13, -23, -15, -8} Then the algorithm should…
dev
  • 506
  • 6
  • 16
-1
votes
1 answer

divide and conquer to count the number of good segments

I have been studying divide-and-conquer for a while, and am now looking into the following problem: Given a permutation P of length N, count the number of good segments. Note that a sequence A of N integers is called a permutation of length N if:  …
Dennis
  • 93
  • 11
-1
votes
1 answer

Reverse the elements of a vector with divide et impera method

I tried reversing a vector through a divide et impera algorithm. Here's my code: #include #include using namespace std; //prototype void Reverse (std::vector &v, int left, int right); //helper function void Switch(int &x,…
Lastrevio2
  • 51
  • 6
-1
votes
1 answer

Special case in maximum subarray

I use the divide and conquer to solve maximum subarray problem. It works fine on most common case but fail on special one. I think the problem might happen here : struct subarray maximum_crossing(int A[], int low, int mid ,int high){ int…
Kyle
  • 154
  • 3
  • 9
-1
votes
2 answers

Incorrect Output when applying Divide & Conquer

I have to solve an one-root equation in an interval, using Divide & Couquer. Here is my code: #include double coef[4]; int d=3; double f(double x){ int i; double res; for(i=0;i<=d;i++) res+=coef[i]*pow(x,i); return…
user10499805
-1
votes
1 answer

Ho good is this optimization to heapsort by dividing into 2 parts

I was thinking about quicksort not finding the exact midpoint for pivot. Any effort to find exact midpoint as pivot slows down quicksort & is not worth it. So is it possible to accomplish that using heapsort & is it any worthwhile? I selected…
Winter Melon
  • 97
  • 1
  • 6
-1
votes
1 answer

How to design a divide and conquer algorithm to checks if there is a pair ai,aj such that ai-aj= M in an array?

Given a list of integers a1,a2,...,an, write an algorithm that checks if there is a pair ai,aj such that ai-aj= M. The time complexity of the algorithm should be O(nlogn) or better. As far as I am concerned, when the problem is ai+aj=M, this…