Question

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 -> Undo in most editorial processing programs such as Microsoft Word. Similarly, most programs have Edit -> Redo allowing you to undo your Undo's. To record backtracking in both directions (undo, redo), a software program uses two separate stacks, one for tracking all of the push operations for Undo and another for tracking all of the pop operations for Redo. To illustrate this, say we label each user event with a unique, sequentially increasing, non-negative number:

Event Type Event Number
Clicking a Mouse button 1
Pressing the Enter key 2
Pressing the “A” key 3
Pressing the “b” key 4

All push operations are sequentially tracked in an events_pushed stack, whereas pop operations are tracked in an events_popped stack.

If the user goes through the above events 1 -> 2 -> 3 -> 4 without undoing them, we have

Event Sequence: push(1) -> push(2) -> push(3) -> push(4):
events_pushed: [1,2,3,4]
events_popped: []

If the user goes through the sequential events 1 -> 2 -> 3 -> 4 and undo the last one:

Event Sequence: push(1) -> push(2) -> push(3) -> push(4) -> pop(4)
events_pushed: [1,2,3,4]
events_popped: [4]

Both events_pushed and events_popped keep track of all of the pushes and pops, respectively and sequentially, recording the user journey from beginning to end. Your will write a solution method to check if two input stacks, events_pushed and events_popped, represent the result of an actual sequence of events happened on an initially empty stack. If so, the solution methods turns true, false otherwise.

import java.util.Stack;
public class HomeworkAssignment2_1 {    
    public static void main(String[] args) {
    // just like any problems, whatever you need here, etc.

}
// YOUR STYLING AND DOCUMENTATION GOES HERE
class Solution {
   // OR, YOUR STYLING AND DOCUMENTATION CAN GO HERE TOO
   public boolean isSameEventSequence(int[] events_pushed, int[] events_popped) { 
      // YOUR CODE HERE 
   }
}

For example, the sequence of events 1 -> 2 -> 3 -> undo 3 -> 4 -> undo 4 -> undo 2 -> undo 1 is captured as follows:

Event Sequence: push(1) -> push(2) -> push(3) -> pop(3) -> push(4) -> pop(4) -> pop(2) -> pop(1)
events_pushed: [1,2,3,4]
events_popped: [3,4,2,1]

Both events_pushed and events_popped listed above represent the actual sequence of events; your solution method returns true in this case.

EXAMPLES

Input: events_pushed = [2,1,3], events_popped = [3,2,1]

Output: false

Explanation: this is not possible, given that 2 happens before 1 so the events_popped is not the result of the actual event sequence represented in events_pushed: push(2) -> push(1) -> push(3)

Input: events_pushed = [1,2,3], events_popped = [1,2,3]

Output: true

Explanation: this is the result this sequence:

push(1) -> pop(1) -> push(2) -> pop(2) -> push(3) -> pop(3)

Input: events_pushed = [1], events_popped = [1]

Output: true

Explanation: this is the result of this sequence: push(1) -> pop(1)

CONSTRAINTS AND ASSUMPTIONS

  • events_pushed.length = events_popped.length; this means that both input stacks have the same number of events.
  • events_pushed is a permutation of events_popped in values; this means that both input stacks have the same event numbers but in different order.
  • Both stacks’ length > 0 but less than 100.
  • No repeated values in either stacks.
  • 1 <= events_pushed[i], events_popped[i] < 100

HINTS

  • There is no need to modify your input stacks; they can be read-only.
  • You may consider using a local stack to keep track of your accounting. Another stack means additional O(n) memory. However if you think you can solve this problem without extra space, I'd love to see it!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Stack;
import java.util.Scanner;
public class HomeworkAssignment2_1
{
   public static void main(String[] args) {
   Scanner S=new Scanner(System.in);
      
       System.out.println("Enter the number do you want to push and pop");
       int n=S.nextInt();
      
       int[] events_pushed=new int[n];
       int[] events_popped=new int[n];
      
       System.out.println("Enter the push values");
       for(int i=0;i<n;i++)
       events_pushed[i]=S.nextInt();
          
       System.out.println("Enter the pop values");
       for(int i=0;i<n;i++)
       events_popped[i]=S.nextInt();
  
       Solution s=new Solution();
       boolean flag=s.isSameEventSequence(events_pushed, events_popped);
       System.out.print(flag);
   }
}


class Solution {

public boolean isSameEventSequence(int[] events_pushed, int[] events_popped) {
Stack<Integer> s=new Stack<Integer>();
int j=0;
for(int i=0;i<events_pushed.length;i++)
{
s.push(events_pushed[i]);
if(events_pushed[i]==events_popped[j])
{s.pop();
j++;
}
}

int i=s.size()-1;
while(i>=0&&j<events_popped.length)
{
if(s.get(i)==events_popped[j])
s.pop();
else
break;
i--;
j++;
}
if(s.isEmpty())
return true;
  
return false;
}
}

Add a comment
Know the answer?
Add Answer to:
Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. 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
  • Stacks and queues 1. Show the status of the stack or return values along with the...

    Stacks and queues 1. Show the status of the stack or return values along with the following sequence of operations. Push(5), push(3), size(), pop(), isEmptyO, pop(), isEmpty, pop, push(7), push(9), top(), push(4), size(), pop(), push(6), push(8), pop(). 2. What's the spatial cost of stack above? What's the temporal cost of push() and pop() operations, respectively?

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in...

    JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x onto stack. • pop() -- Removes the element on top of the stack. • top() -- Get the top element. • getMin() -- Retrieve the minimum element in the stack. Example 1: Input ["MinStack", "push", "push","push", "getMin","pop","top", "getMin"] [1], [-2], [0], [-3],[1,1],[1, [1] Output [null, null, null,null,-3, null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);...

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • Design a stack class by importing the available java.util.Stack to have the following features: push(x) --...

    Design a stack class by importing the available java.util.Stack to have the following features: push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE. pop() -- remove the element on top of the stack. top() -- get the top element. getMax() -- retrieve the max element in the stack in constant time (i.e., O(1)). Your code should have the following shape and form, all in one .java file. Note the styling and documentation API already...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • Define a class DoubleStack which implements two stacks of objects of type Object using a single...

    Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the push and pop operations specify which of the two stacks is involved in the operation (as described below). (a) Specify necessary instance variables and write a constructor “DoubleStack(int n)” that takes integer n and creates an empty DoubleStack object with the shared array with room for n elements (2 pt); b) Write methods "boolean push(int i, Object o)" and...

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