Consider the following problem. Given an array ?[1...?] of ? distinct numbers. Output the largest number, or the ???, and the second largest number, or ???2, of ?. Design an algorithm such that the number of comparisons is as small as possible.
Pseudo Code(Algorithm):
Array[1..N]
Initialize max=Array[1],max2=Array[2];
for( i = 3:n){
if(Array[i]>max){
max2=max;
max=Array[i];
}
else if(Array[i]>max2){
max2=Array[i];
}
}
print(max,max2)
C Programm:
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <stdio.h>
int main()
{
int array[5] = {19, 10, 8, 17, 9};
int max,max2,n,i;
n=5;
max=array[0];
max2=array[1];
for(i=2;i<n;i++){
if(array[i]>max){
max2=max;
max=array[i];
}
else if(array[i]>max2){
max2=array[i];
}
}
printf("Max : %d Max2 : %d",max,max2);
}
Output:
*Feel free to ask(comment) if you have any doubts or if you face any difficulties while executing the program, I will be happy to help you, please upvote if you like the solution
Consider the following problem. Given an array ?[1...?] of ? distinct numbers. Output the largest number,...
Modify the algorithm to solve the problem of finding the k-th largest number in array A, 1≤k≤n, without sorting the entire array. Partsof the algorithm are given below. Fill in the blanks. Select-k-th-largest(A: Array [1..n] of numbers) 1 for _____________________ 2 ________________ 3 for _____________________ 4 if _______________ then ___________ 5 if position ≠ i then 6 temp=A[i] 7 A[i]=A[position] 8 A[position]=temp
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...
1. Consider an array of size eight with the numbers in the following order 40, 20, 80, 60, 30, 10, 70, 50. (a) What is the array after heap creation? Make sure to form the heap bottom up as done in class. How many comparisons does the algorithm use? (b) Show the array after each element sifts down during the remainder of heapsort, and state how many comparisons each sift takes. What is the total number of comparisons for the...
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...
Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
Write an algorithm that takes an array B and a number N as inputs. Suppose that the array B contains n distinct numbers. Compute the sum of the N largest numbers in the array B. Example: if the array B= [4, 5, 8, 11, 3] and N = 3, then the algorithm should return 24 (11+8+5).
You are given a sequence of positive real numbers a[1..n]. You can now add ‘+’ and ’×’ signs between these numbers, and your goal is to generate an expression that has the largest value. As an example, if a = {2, 3, 0.5, 2}, then you should output the expression 2 × 3 + 0.5 + 2 = 8.5. This is larger than any other expression (e.g. 2 × 3 × 0.5 × 2 = 6, 2 + 3 +...
need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...