You are given a set of n numbers. Give an O(n^2) algorithm (NOT O(n^3), O(n^2)) to decide if there exist three numbers a, b and c are in the set such that a + b = c
(Hint: sort the numbers first).
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Efficient approach : The idea is similar to Find a triplet that sum to a given value.
Below is the algorithm in coding style
sort(a); // non-descending
for (i = 0; i < n; i++) {
j = i; k = i + 1;
while (j < n && k < n) {
if (a[i] + a[j] == a[k])
return true;
else if (a[i] + a[k] < a[j])
k++;
else
j++;
}
}
return false;
Since there are 2 nested loops involved. So, it is O(n^2)
Time complexity: O(N^2)
Kindly revert for any queries
Thanks.
You are given a set of n numbers. Give an O(n^2) algorithm (NOT O(n^3), O(n^2)) to...
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.
1. Given a set of numbers: A = {1, 3, 5, 2, 8, 7, 9, 4 }. Add each digit of your N-Number (e.g. N0 0 3 2 2 0 1 8) to each element of A separately to get your own final array B. ( e.g. B={1,3,8,4,10,7,10,12} = {B1,B2,B3,B4,B5,B6,B7,B8}. ) a) Your set B = ? b) Use merge-sort to sort the array B. Show each step. c) How many “comparisons” for each algorithm above? 2. Use binary-search for...
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.
Subject: Algorithm
need this urgent please thank you.
4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...
Given a list of numbers in random order, write an algorithm that works in O( n log(n) ) to find the kth smallest number in the list.
Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1. (Hint: represent a value x as (i, j, k) where x = i · (n^2) + j · n + k.)
Let L be a set of n lines in the plane. Give an O(nlogn) time
algorithm to compute an axis-parallel rectangle that contains all
the intersection points of those n lines in the plane.
Given an array of numbers, you want to find out the three greatest numbers. Do you have to sort the array, or is there a faster way than applying a sorting algorithm? Explain why you use or not use sorting in terms of the worst-case running time. Give an example.
Computer Algorithm question
8) Give an algorithm for building a heap in O(n)
9) Prove the algorithm given in 8) runs in O(n) time.
10) What is the asymptotic runtime of an algorithm represented
by the following recurrence equation?
11) Suppose you have the following priority queue implemented as a (max) heap. What will the heap look like when the max node is removed and the heap is readjusted? Assume on each heapify operation the largest child node is selected...
2) Given an array of n nonzero real numbers a[0]…a[n-1], write a function to partition the array (not sort) so that all its negative elements come before all its positive elements. Your algorithm should have O(n) time complexity. The function prototype is void negpospartition(float a[], int n) Please use C language