Consider the following problem:
Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list.
Output: find the missing integer
Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs to return 5.
Let the input array be [6, 3, 4, 5, 1]. Elements in this list are in the range of 1 to 6. There are no duplicates, and 2 is missing. Your algorithm needs to return 2.
Design an algorithm that solves this problem.
Note: You need to consider all the possibilities including overflow.
Note: There is NO requirement of “in-place” algorithms. You can use extra memory/data structure in the algorithm.
(i) describe the idea behind your algorithm in English;
(ii) provide pseudocode;
We can solve this using xor concept
code:
missingNO(int a[], int n)
{
int i;
int xor1 = a[0]; /* For xor of all the elements in array */
int xor2 = 1; /* For xor of all the elements from 1 to n+1 */
for (i = 1; i< n; i++)
x1 = x1^a[i];
for ( i = 2; i <= n+1; i++)
x2 = x2^i;
return (x1^x2);
}
If you are satisfied please upvote or if you have any doubts mention them in comments
Consider the following problem: Input: a list of n-1 integers and these integers are in the...
The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.
Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...
urgent L. Consider the following pseudocode for finding binomial coefficients: Binom(n, r) Input: integers n and r Output: n choose r if r-0 or r-n thern return 1 end else return Binom(n-1, r-1) Binom(n-1, r); end running time of this algorithm. Prove your bound for the upper bound. (5 points) Rewrite the above algorithm so that it is efficient. (You have 2 choices!) Analyze the worst case time of your new algorithm. (5 points) Find the edit distance between "SPOKE...
a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...
Sum of A1l Integers in a List (SUM) Input A list of integers Ala.. b] Output : s-Σ-, A[i] Let S(Ala...b]) represent the output of the SUM problem on input Ala..b 4 points) 5. Statetwo different elf-reductions for the SUM problem. Usethe self-reduction examples from lecture as a guide. (4 points) 6. Give recursive algorithms based on your divide and conquer self-reductions to solve the SUM problem. (2 points) 7. What are the worst case runtimes of the solutions you...
Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...
In python Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n),...
Q1. Write an algorithm (pseudocode or flowchart) for the following problem. Input: Eight integers Output: the sum of even numbers only. Example: Let the eight integers are 2, 9, 3, 20,5,17, 10, 6 then S = 2+20+10+6 = 38
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...