comparisons are made in while loop. This is an example of insertion sort. number of comparisons for j=2 is 1 number of comparisons for j=3 is 2 number of comparisons for j=4 is 3 ... number of comparisons for j=n is n-1 total number of comparisons = 1+2+3+...+(n-1)
Discrete math question 2. Consider to the following two algorithms procedure SortA(a1,a2, ..., an: a list...
28. Show that there are 12 pairs of numbers (a1,az) with 0<aj < 4,0 <a2 <6 so that x=a1 (mod 4) x = 22 (mod 6) has a solution.
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...
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.
float useless(A){ n = A.length; if (n==1) { return A[@]; let A1,A2 be arrays of size n/2 for (i=0; i <= (n/2)-1; i++){ A1[i] = A[i]; A2[i] = A[n/2 + i]; for (i=0; i<=(n/2)-1; i++){ for (j=i+1; j<= (n/2)-1; j++){ if (A1[i] == A2[j]) A2[j] = 0; b1 = useless(A1); b2 = useless (A2); return max(b1,b2); What is the asymptotic upper bound of the code above?
2 Double summation Let a1, A2, A3, ... be a sequence of real numbers, and let n > 1 be an integer. Which of the following are always equal? пі пп nn nn « ££« Žia. E£« L« i=1 j=1 i=1 j=1 i= 1 i=1 j=1 j=1 i=j
(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...
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...
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.
Question 2 7 pts Theorem If A1, A2, .., A, are sets for n > 2, then (A, UA, U... A.) = (A) n(A)n... n(A) Upload Choose a File Question 3 6 pts o el DLL