Question

General Instructions In this task, answer all the following questions and complement each answer with a detailed explanation

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1.) Algorithm for the first question :

Sum_in_set(S[],n,x)

1. Sort the array in increasing order. (sorting method should be either merge sort or heap sort - for n.logn time complexity)

2. Initialize two variables with the leftmost and rightmost index.

Suppose l and r are two variables. Then l=0 and r=n-1.

3.While l<r :

a) If (S[l] + S[r] < x ) then l++.

b) else If (S[l] + S[r] ==x) return True.

c) else r--.

4. If no such pair found ,return False.

example : let set be S={2,9,4,5,3}. Sorted set={2,3,4,5,9} and sum to be find be 9.

initialize l=0, r=4

S[l]+S[r] i.e. 2+9 = 11 > 9 Therefore decrement r. Now r=3.

S[l]+S[r] i.e. 2+5 = 7 <9 Therefore increment l .Now l=1.

S[l]+S[r] i.e. 3+5 = 8<9 Therefore increment l. Now l=2.

S[l]+S[r] i.e. 4+5=9 .Found...returns True.

-------------------------------------------------------------------------------------------------------------------------------------------------

Q.2) To do the stack operations (push, pop, min) in a constant O(1) time, we have to implement it using two stacks - actual and auxiliary stack.

The top element in the auxiliary stack will always be the minimum element.

Push (int x) :

1. If x is the first element, push it in actual as well as auxiliary stack.

2. Else, push x to the actual stack.

3. Compare x with the top element of the auxiliary stack. Let the top element in auxiliary stack be y. If x < = y, then push x to auxiliary stack. // the = sign will duplicate the value in auxiliary as it will be helpful during pop operation in case two elements repeat.

Pop () :

1. Pop the top element of the actual stack

2. If the top element of the actual stack is equal to the top element of the auxiliary stack, pop the top element of the auxiliary stack. // to maintain auxiliary stack so that it always has top as minimum element of actual stack

int Min() :

1. Return the top element of the auxiliary stack.

Example :

When we push 9,   Actual : 9 <-top Auxiliary: 9 <-top

When we push 10, Actual : 9 10 <-top Auxiliary: 9 <-top

When we push 2, Actual : 9 10 2 <-top Auxiliary: 9 2 <-top

Hence, top in the auxiliary always point to the minimum element.

------------------------------------------------------------------------------------------------------------------------------------------------------------

If you find anything wrong or have any doubt, please ask in the comment section.

Add a comment
Know the answer?
Add Answer to:
General Instructions In this task, answer all the following questions and complement each answer with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A...

    Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A user event may be “pressing the Enter key on keyboard” or “clicking a mouse button”. Stack is a data structure with the Last-In-First-Out property (LIFO). If we push the aforesaid user events into a stack, a computer program using that data structure can “rewind” user events by popping them out of stack one at a time. This backtracking feature is available as Edit ->...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A...

    Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of size n. For example, given the array |3, 2, 4, 3], the function should return the value 3. For the array 11 2,3 5,5,5,66,81, the function should return an array (or list) 5,6 In this task, you will develop two alternative solutions for the FINDDUPLICATES(Ao..n 1 func- tion. In your implementations, you may call any of the algorithms introduced in...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • Problem 3 Suppose that you have a set of n large, orderable, objects, each of size...

    Problem 3 Suppose that you have a set of n large, orderable, objects, each of size q, so that it requires time e(a) to time to compute a hash function h(a) for any object and requires time e(g) to compare any two objects. Describe a compound data structure, built out of a heap and a hash table, that supports the following operations with the specified run times.. elt (x) Is x an element of the set? Expected run time O(g)....

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • pls answer all questions 1) A step-by-step solution to a problem is called a. hardware b....

    pls answer all questions 1) A step-by-step solution to a problem is called a. hardware b. an operating system c. a computer language d. an algorithm 2) separated the programming task from the computer operation tasks. a. Algorithms b. Data processors c. High-level programming languages d. Operating systems 3) is a 16-bit code that can represent symbols in languages other than English. a. ASCII b. Extended ASCII c. EBCDIC d. Unicode 4) When you want to download music to a...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT