Question

In: Computer Science

Problem 1: (10 marks) ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a...

Problem 1: ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a positive numberias an input, where i=a1a2...an,n>=3(numberwith 3 or more digits, every digit can have the value between 0 and 9–both inclusive). The algorithm should find the largest number jsuch that j=b1..bm, 1<=m<=n(numberwith m digits),andevery bk<=bk+1where1<=k<m and bk,bk+1ε{a1, a2, an}.Also, perform the computational complexity (time complexity)analysis of your algorithm and write the algorithm complexity in Big-Oh notation.

Examples:Input: 23720, Output: 237Input: 9871, Output: 9Input: 1789, Output:1789Input: 2372891, Output: 289

Solutions

Expert Solution

Algorithm:

  • take number input, n as string.
  • initialize two variables max_num = 0, and curr_num = 0
  • start iterating through the string from left.
  • if number[i] > number[i-1] then curr_num = curr_num*10 + (number[i]-'0')
  • else curr_num = number[i]-'0'
  • update max_num at each step using max_num = max(max_num,curr_num)
  • return max_num

Code:

#include <iostream>
using namespace std;

int main() {
   string num;
   cin>>num;
   int max_num = 0,curr_num = 0;
   for(int i=0;i<num.size();i++){
   if(i>0&&num[i]-'0'>num[i-1]-'0'){
   curr_num = curr_num*10 + (num[i]-'0');
   }
   else{
   curr_num = (num[i]-'0');
   }
   max_num = max(max_num,curr_num);
   }
   cout<<max_num<<'\n';
   return 0;
}

Code Screenshot:


Related Solutions

Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the...
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the following requirements: Reads in a series of positive integers,  one number at a time;  and Calculate the product (multiplication) of all the integers less than 25,  and Calculate the sum (addition) of all the integers greater than or equal to 25. Use 0 as a sentinel value, which stops the input loop. [ If the input is 0 that means the end of the input list. ]...
1. Perform a Desk check for the following pseudocode algorithm L1: Program AddFirst5Numbers; L2: Data Counter...
1. Perform a Desk check for the following pseudocode algorithm L1: Program AddFirst5Numbers; L2: Data Counter as Integer; L3: Data Sum as Integer; L4: Counter:= 1; L5: Loop until Counter = 5 L6: Sum := Sum + Counter; L7: counter := counter + 1; L8: Next Loop; L9: Output “The sum is”, Sum; L9: End AddFirst5Numbers; 2. Write a pseudocode algorithm to print the following pattern 2 4 6 8 10 3. Using nested loops, write a pseudocode algorithm to...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode...
The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way we've been generating random numbers in the book; however, there appears to be an error. Assume that the random() function is a built-in library function. Correct the pseudocode so that the program works as it should (This has 1 error and is easy to spot) //This program displays...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
1) Using algorithm or pseudocode, describe the implementation of the server side of a public chatroom...
1) Using algorithm or pseudocode, describe the implementation of the server side of a public chatroom application with the following specification: Any client that wishes to join the chatroom, must send the message “Hello” to the server’s IP address at port 2020. After sending the “Hello” message, the client is admitted into the chatroom. While in the chatroom, any message sent by any client must be received by all other clients. Finally, when a client decides to leave the chatroom,...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT