The following snapshot gives you the required functionality:
Hope it helps, do give your valuable response.
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...
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...
(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...
An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of...
A sequence of n distinct values A[O..n – 1] is said to be downup if there is an index p with 0 < p < n such that the values of A decrease up to Aſp) and then increase for the remainder of the sequence. The index p of value Aſp) is the valley of the sequence. For example sequence 50, 10, 5, 2, 1, 20, 30 is downup with valley 4, since A[5] = 60 and the sequence decreases...
Sequence a1,a2……an consists of nonnegative integers. We want to determine the largest sum of such subsequences that do not contain two consecutive elements of the original sequence (For example if a3 is a member of the subsequence, then a2 and a4 cannot be contained in the subsequence ). Give an O(n) running time algorithms for this problem.
(a) Prove the following loop invariant by induction on the number of loop iterations: Loop Invariant: After the kth iteration of the for loop, total = a1 + a2 + · · · + ak and L contains all elements from a1 , a2 , . . . , ak that are greater than the sum of all previous terms of the sequence. (b) Use the loop invariant to prove that the algorithm is correct, i.e., that it returns a...
: Let a1, a2, a3, . . . be the sequence of integers defined by a1 = 1 and defined for n ≥ 2 by the recurrence relation an = 3an−1 + 1. Using the Principle of Mathematical Induction, prove for all integers n ≥ 1 that an = (3 n − 1) /2 .