Question

In: Computer Science

** * Write a recursive function that removes the first k even numbers * from the...

**

* Write a recursive function that removes the first k even numbers

* from the stack. If there are less than k even elements in the stack,

* just remove all even elements. Do not use any loops or data structures

* other than the stack passed in as a parameter.

* @param stack

* @param k

* @return Returns the number of elements removed from the stack.

*/

public static int removeEvenNumbers(Stack<Integer> stack, int k) {

return 0;

}

Solutions

Expert Solution

import java.util.Stack;

public class RemoveEven {

    public static int removeEvenNumbers(Stack<Integer> stack, int k) {
        if (!stack.empty()) {
            int num = stack.pop(), count = 0;
            boolean removed = false;
            if (num % 2 == 0 && k > 0) {
                k--;
                count++;
                removed = true;
            }
            count += removeEvenNumbers(stack, k);
            if (!removed) {
                stack.push(num);
            }
            return count;
        }
        return 0;
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(3);
        stack.push(2);
        stack.push(7);
        stack.push(4);
        stack.push(1);
        System.out.println("Original stack: " + stack);
        int n = removeEvenNumbers(stack, 2);
        System.out.println("Modified stack: " + stack);
        System.out.println("Number of even numbers removed is " + n);
    }
}


Related Solutions

Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Write a C program for the recursive algorithm that removes all occurrences of a specific character...
Write a C program for the recursive algorithm that removes all occurrences of a specific character from a string. (please comment the code)
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
17. Write a function in Python which removes a set of common words from a long...
17. Write a function in Python which removes a set of common words from a long piece of text. Be sure to strip out any punctuation. The common words to be removed are: ​​a, an, as, at, by, for, in, is, it, of, that, this, to, was, will, the These are typical words which are considered to have low semantic value. Process each paragraph provided below individually. Your end result for each paragraph should be a string or list containing...
Use the following two loop strategies to write a function that sums all even numbers before...
Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT