divide-and-conquer

How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?

喜欢而已 提交于 2019-11-27 17:52:31
How to optimally divide an array into two subarrays so that sum of elements in both subarrays is same, otherwise give an error? Example 1 Given the array 10, 20 , 30 , 5 , 40 , 50 , 40 , 15 It can be divided as 10, 20, 30, 5, 40 and 50, 40, 15 Each subarray sums up to 105. Example 2 10, 20, 30, 5, 40, 50, 40, 10 The array cannot be divided into 2 arrays of an equal sum. Gal There exists a solution, which involves dynamic programming, that runs in O(n*TotalSum) , where n is the number of elements in the array and TotalSum is their total sum. The first part consists in calculating the set of all

Why is Binary Search a divide and conquer algorithm?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 13:00:38
问题 I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the examinators asked where the conquer part in it was, which I was unable to answer. They also disapproved that it actually was a divide and conquer algorithm. But everywhere I go on the web, it says that it is, so I would like to know why, and where the conquer part of it is? 回答1: The book Data Structures

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

好久不见. 提交于 2019-11-27 07:50:38
问题 For an array a: a 1 , a 2 , … a k , … a n , a k is a peak if and only if a k-1 ≤ a k ≥ a k+1 when 1 < k and k < n. a 1 is a peak if a 1 ≥ a 2 , and a n is a peak if a n-1 ≤ a n . The goal is to find one peak from the array. A divide and conquer algorithm is given as follows: find_peak(a,low,high): mid = (low+high)/2 if a[mid-1] <= a[mid] >= a[mid+1] return mid // this is a peak; if a[mid] < a[mid-1] return find_peak(a,low,mid-1) // a peak must exist in A[low..mid-1] if a[mid] < a[mid+1]

How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?

假如想象 提交于 2019-11-26 19:15:01
问题 How to optimally divide an array into two subarrays so that sum of elements in both subarrays is same, otherwise give an error? Example 1 Given the array 10, 20 , 30 , 5 , 40 , 50 , 40 , 15 It can be divided as 10, 20, 30, 5, 40 and 50, 40, 15 Each subarray sums up to 105. Example 2 10, 20, 30, 5, 40, 50, 40, 10 The array cannot be divided into 2 arrays of an equal sum. 回答1: There exists a solution, which involves dynamic programming, that runs in O(n*TotalSum) , where n is the number of

Passing an array as an argument in C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 16:05:19
问题 I'm writing a merge sort function, and right now I am just using a test case array (there is no input - this is static, for now). I don't know how to pass an array as an argument. Here is my code right now: //merge sort first attempt #include <iostream> #include <algorithm> #include <vector> int mergeSort(int[]); int main() { int originalarray[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 }; mergeSort(originalarray[]); } int mergeSort(int[] originalarray) { int num = (sizeof(originalarray) / sizeof(int