1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm:
If x, y are two adjacent elements in a sequence, with x before y, we say that the pair x, y is in order when x <= y and the pair is out of order when x > y.
For example, in the string “BEGGAR” the pair G, A are out of order, but all the other pairs are in order.
out-of-order counting
input: a list L of n comparable elements output: the number of out-of-order pairs in L
Hint: An optimal algorithm for this problem takes O(n) time.
Greedy Algorithm(C++ code) for the above problem is as follows-
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
int n=s.length();
//to count the No. of out-of-order pairs
int ans=0;
//traversing the list
for(int i=0;i<n-1;i++){
//checking if the element before
//is greater then element after it.
if(s[i]>s[i+1])
ans++;
}
//displaying the answer
cout<<"No. of out-of-order pairs-> "<<ans;
return 0;
}
Code-
Input and Output-
Time complexity of the above algorithm is
O(n) because only one loop has been used
to traverse the list only once.
If the answer helped then please upvote, it means a
lot.
And for any queries feel free to comment.
1) Design a greedy algorithm that solves the problem; describe your algorithm with clear pseudocode; and...
9-2. For each of the following problems: design a reduction algorithm a hash table or sorting algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm. duplicate search problem input: a vector V of comparable objects output: an element of V that appears more than once in V, or None if no such element exists 9-2. For each of the following problems: design a reduction algorithm a hash table or...
There is a very simple greedy algorithm(Pseudocode) that maximizes your total reward. Find it. Consider the following scheduling problem. Each task I has a length L[I] and a deadline D[I]. There is a single processor that handles one task at a time. Let E[I] be the ending time of I under a given schedule. You get paid an amount D[I] − E[I] if you finish task I early and you get fined an amount E[I] − D[I] if you finish...
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...
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....
Problem 6. (Weighted Graph Reduction) Your friend has written an algorithm which solves the all pairs shortest path problem for unweighted undirected graphs. The cost of a path in this setting is the number of edges in the path. The algorithm UNWEIGHTEDAPSP takes the following input and output: UNWEİGHTEDA PSP Input: An unweighted undirected graph G Output: The costs of the shortest paths between each pair of vertices fu, v) For example, consider the following graph G. The output of...
Your task is to design algorithms to solve the following problems. For full credit, your algorithm must run in logarithmic time. Given a number n greaterthanorequalto 1 and a (user-specified) error tolerance e, you want to approximate the squareroot of n to within error tolerance e. Specifically, you want to return an x = Squareroot n that satisfies |x^2 - n| greaterthanorequalto e. For example, to compute the squareroot of n = 2 with e = 0.01, an acceptable answer...
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...
There is an old fairy tale where some elves secretly help an old shoemaker finish his work for the day. In this version, the shoemaker and the elves collaborate openly Each day the shoemaker receives n orders for custom-made shoes. In the evening, n elves arrive to help him assemble the shoes. The shoemaker first cuts out the leather pieces for one pair of shoes. Then an elf stitches the pieces together to complete the shoes. For each i, let...
Design a dynamic programming algorithm for the problem. Define the original problem as a function that takes parameters, and return some results. Define the subproblems Write recursive formula that relates a problem's solution to solutions of smaller subproblems. Finally write out pseudocode for the algorithm (using top-down memoization or bottom-up) Suppose a list Р[L..n] gives the daily stock price of a certain company for a duration of n days, we want to find an optimal day di to buy the...
Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...