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

Write a function that removes all even numbers from an array. The function should take the...
Write a function that removes all even numbers from an array. The function should take the array, length of the array, and a pointer for number of odd numbers found as arguments and return a pointer to the new array. If there are no odd numbers found in the array, your code should print "No odds found." and return NULL. Use the function header: int *removeEvens(int *a, int length, int *oddsFound); Example: Input array a[ ] = {3, 4, 5,...
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
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT