Given an ArrayList , write a function that returns the k th minimum element from the arraylist. Note: 0 th minimum element is the smallest element in the ArrayList
^ plz find a recursive method, psudocode is fine
Please find my complete progrm to find kth minimum number:
import java.util.ArrayList;
public class MinHeapArrayList
{
public static int kthMinimum(ArrayList<Integer> arr, int k)
{
int n = arr.size();
BuildMinHeap(arr, n);
int min = Integer.MIN_VALUE;
// One by one extract an element from heap
for (int i=1; i <=k; i++)
{
// Move current root to end
int temp = arr.get(0);
arr.set(0, arr.get(n-1));
arr.set(n-1, temp);
n = n-1;
min = temp;
// call max heapify on the reduced heap
MinHeapify(arr, n, 0);
}
return min;
}
static void BuildMinHeap(ArrayList<Integer> arr, int n){
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
MinHeapify(arr, n, i);
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
static void MinHeapify(ArrayList<Integer> arr, int n, int i)
{
int smallest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr.get(l) < arr.get(smallest))
smallest = l;
// If right child is larger than largest so far
if (r < n && arr.get(r) < arr.get(smallest))
smallest = r;
// If largest is not root
if (smallest != i)
{
int swap = arr.get(i);
arr.set(i, arr.get(smallest));
arr.set(smallest, swap);
// Recursively heapify the affected sub-tree
MinHeapify(arr, n, smallest);
}
}
/* A utility function to print array of size n */
public static void display(ArrayList<Integer> arr)
{
int n = arr.size();
for (int i=0; i<n; ++i)
System.out.print(arr.get(i)+" ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = {15,13,9,5,12,8,7,4,0,6,2,1};
ArrayList<Integer> list = new ArrayList<>();
for(int x : arr)
list.add(x);
System.out.println("4th min: "+kthMinimum(list, 4));
}
}
/*
Sample run:
4th min: 4
*/
Given an ArrayList , write a function that returns the k th minimum element from the...
Given a (Java) LinkedList , write a function that returns the k th minimum element from the arraylist ^recursive method plz, psudocode is fine
(Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0. public static Integer max(ArrayList<Integer> list) Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach.
Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...
Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!
Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character objects of the same size. The returned elements of the ArrayList are assigned letter grade corresponding to integer grade of the same index element of the ArrayList parameter (A if 90 or above, F if less than 60). Include code to test your method. [For other letter grades: 80 ?? 89 −> ?, 70 ?? 79 −> ?, 60 ?? 69 −> ?]
[python] Write a method that given a heap array returns the second smallest element in a heap.
Write a function that returns the index of the smallest element in an array of integers. If there are more such elements than one, return the smallest index. C programming
(Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0. public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...
Write a static recursive method that removes all occurrences of the element from an array list. public static void removeAllOccurrences(ArrayList<Integer> list, Integer element)
Write a recursive function that compares two given binary trees. It returns 1 if the two trees are different, and it returns 0 otherwise. The function signature is: int treeDiff (node "a, node *b); Write a recursive function that counts how many nodes in a tree have a single child. The function signature is: int countoneChild(node "root);