You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.
Input :
A={a1,a2,...,an} where 1<= ai<= m and k<=n*m
Output:
Determine whether k can be represented as sum of some numbers from A(numbers can be included only once)
Solution:
Brute force approach would be to generate all subsets and check if the subset sums to the right number. The running time will be exponential i.e.,O((2^n)*k).
Optimal solution :
This problem can be solved by dynamic programming. For each element from array there are two possibilities:
1) include current element in subset and recurse for other elements with remaining sum
2) excluede cureent element and recurse for other elements
Finally, we return true if we can obtain sum by including or excluding current element else return false. Base case will be when no elements are left or k becomes negative . True is returned when k becomes 0.
Since the problem has optimal substructure and overlapping subproblems , it can be solved through dynammic programming(Memorizing recursive solutions).
Time complexity is O(N*k) where N is number of elements in array
Code:
------------------------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
// create a map to store solutions of subproblems
unordered_map<string, int> m;
bool check(int a[], int n, int k)
{
// return true if k becomes 0 (answer found)
if (k == 0)
return true;
// base case: no elements left or k becomes negative
if (n < 0 || k < 0)
return false;
// construct a unique map key from dynamic element of the input
string key_unique = to_string(n) + "|" + to_string(k);
// if sub-problem is seen for the first time, solve it and
// store its result in a map
if (m.find(key_unique) == m.end())
{
// Case 1. include current element in the subset (a[n]) and recurse
// for remaining elements (n - 1) with decreased k (k - arr[n])
bool inc = check(a, n - 1, k - a[n]);
// Case 2. exclude current element n from subset and recurse for
// remaining elements (n - 1)
bool exc = check(a, n - 1, k);
// assign true if we get subset by including or excluding current element
m[key_unique] = inc|| exc;
}
return m[key_unique]; //return solution if already solved
}
int main()
{
int a[] = { 10, 3, 2, 5, 8 };
int k = 17;
// number of elements
int N = sizeof(a) / sizeof(a[0]);
if (check(a, N - 1, k))
cout << "yes";
else
cout << "no";
cout<<endl;
return 0;
}
You are given a set of integer numbers A = {a1, a2, ..., an}, where 1...
Let n be a positive integer. We sample n numbers a1, a2,..., an from the set {1,...,n} uniformly at random, with replacement. We say that picks i and j with are a match if ai = aj, i < j. What is the expected total number of matches? Use indicators.
1. a) Describe an O(m)-time algorithm that, given a set of S of n distinct numbers and a positive integer k c n, determines the top k numbers in s b) Describe an O(n)-time algorithm that, given a set of S of n distinct numbers and a positive integer k < n, determines the smallest k numbers in S.
The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...
(C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...
Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm? Algorithnm Input: a1, a2,...,an, a sequence of numbers...
13. Consider the sequence of numbers ao, ai, a2, a3, given by ao-2, ai-3, and for any positive integer k 2, a3ak 2ak-1. (a) Evaluate a2,a3, a4,as. Show your work. (b) Prove that for all positive integers n, an 2 +1
(a) Let R be a commutative ring. Given a finite subset {ai, a2, , an} of R, con- sider the set {rial + r202 + . . . + rnan I ri, r2, . . . , rn є R), which we denote by 〈a1, a2 , . . . , Prove that 〈a1, a2, . . . , an〉 įs an ideal of R. (If an ideal 1 = 〈a1, аг, . . . , an) for some a,...
1) Consider the clique problem: given a graph G (V, E) and a positive integer k, determine whether the graph contains a clique of size k, i.e., a set of k vertices S of V such that each pair of vertices of S are neighbours to each other. Design an exhaustive-search algorithm for this problem. Compute also the time complexity of your algorithm.
Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2, …, an: integer) i := 1 while (i < n and ai ≤ ai+1) i := i + 1 if i == n then print “Yes!” else print “No!” What property of the input sequence {an} does this algorithm test? What is the computational complexity of this algorithm, i.e., the number of comparisons being computed as a function of the input size n? Provide...