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

Maximize this equation E[a1]-E[a2]+E[a3]-E[a4]

Can anyone please help me how todo this problem We have to maximize the value of: E[a1]- E[a2]+ E[a3]- E[a4] where E is an array constraints: 1<=E<=100 (size) N>=4 a1>a2>a3>a4 (index) Input format N (no. of integers in array) N value separated by…
-2
votes
1 answer

Number of Inversions in Merge Sort python

I am trying to count the number of inversions inside a Merge Sort algorithm. I have not made many modifications except adding a counter for the condition to flip the values if the value on the right hand is smaller than the left. The code I am using…
Haseeb Khan
  • 127
  • 2
  • 9
-2
votes
2 answers

Divide and conquer algorithm to find two matching elements in a sublist

I'm trying to implement following picture. The logic this illustrates is as follows: compare the first entity and last entity of a block and if they are different, divide it into two blocks. Then, compare the first and last entity of the divided…
z2ouu
  • 41
  • 1
  • 10
-2
votes
1 answer

Maxsubsequence - What are the key insights for this problem?

Below is the problem assignment using tree recursion approach: Maximum Subsequence A subsequence of a number is a series of (not necessarily contiguous) digits of the number. For example, 12345 has subsequences that include 123, 234, 124, 245, etc.…
overexchange
  • 15,768
  • 30
  • 152
  • 347
-2
votes
1 answer

Algorithm for winner trains?

This is a problem from a past exam paper: Let there be $n$ trains X_1,X_2 ... X_n all which run along parallel tracks. Train $i$ starts from position S[i] >= 0 and runs at constant speed V[i]. A train is said to be a winner if there exists a time…
Anon
  • 381
  • 1
  • 2
  • 13
-2
votes
2 answers

Where is the addition from the recurrence relation, in the summation?

On this coursera course, the instructor is showing how to convert from the recurrence relation into the actual summation of the work done. What I don't understand is where is the constant amout of work at each level O(n^d) represented in the…
Danilo Souza Morães
  • 1,481
  • 13
  • 18
-2
votes
1 answer

Determine if there exists a number in the array occurring k times

I want to create a divide and conquer algorithm (O(nlgn) runtime) to determine if there exists a number in an array that occurs k times. A constraint on this problem is that only a equality/inequality comparison method is defined on the objects of…
Aerole
  • 105
  • 2
  • 10
-2
votes
1 answer

python list being interpreted as an integer; counting inversions

Getting error: File "inversions.py", line 26, in merge if left[i] < right[j]: TypeError: 'int' object is not subscriptable My implementation of merge sort is like so; accepts a list and it's length. Base case is when length is 1, where I simply…
-2
votes
1 answer

Need Help Design pseudocode for divide and conquer algorithm

Given a sorted array A, which stores n integers, and a value key. Design an efficient divide and conquer algorithm that returns the index of the value key if it can be found in array A.Otherwise, the algorithm returns 0.
kybookie
  • 11
  • 5
-2
votes
3 answers

Weird behaviour of a local pointer

This code finds the smallest and largest element in an array using divide and conquer: #include #include #define MAX(a, b) ((a>=b)?a:b) #define MIN(a, b) ((a<=b)?a:b) void printarr(int *arr, int base, int height){ int i; …
Devi Prasad Khatua
  • 1,185
  • 3
  • 11
  • 23
-2
votes
1 answer

Partitioning an array into 3 sets

Given an array of integers , Partition the array into 3 sets such that sum of the elements of the 3 sets are as close as possible. My method is as follows: Sort the array in the decreasing order Insert the element into that set whose sum is…
-2
votes
3 answers

Minimization of the maximum element in an array

You have an array of size n of positive integers on which you can do the following operation by at most N times: Choose a sub-array and decrease its elements value by k (k must be smaller than the sub-array's minimum value). Such an operation…
GEP
  • 311
  • 3
  • 12
-3
votes
1 answer

Use the divide-and-conquer approach to write an algorithm that finds the largest item

Use the divide-and-conquer approach to write an algorithm that finds the largest item in a list of n items. Analyze your algorithm, and show the results in order notation
-3
votes
2 answers

C++ Divide and conquer square matrix multiplication problems

I'm trying to do divide and conquer matrix multiplication so i can parallelize it, but I'm getting half random garbage numbers and half 0's in the result, e.g. on a 2x2 matrix "[[15909360,0][15909360,0]]". This is what I have so far based on the…
Sasha
  • 1
  • 1
-3
votes
2 answers

Check if al elements are the same with divide and conquer

I want to ckeck if all elements in an array are the same. I did it recursively, but I want to do it with divide and conquer method. Also I want that the time complexity to be O(n). How can I explain with the master theorem? bool same_elements(int*…