Problem Given an array of positive integers. Find the n(th) largest element in the array. Solution Sort the array in decreasing order then loop through the sorted array while counting unique elements then stop when the count is equal to (n). In the code below we will assume the array is already sorted. You can
Divide and conquer largest two numbers problem Given an array of integers. Find the first and second largest elements in the array. Use divide and conquer technique. Divide and conquer algorithm largest two elements Simply divide the array into two halves then find the largest two elements in each half. At the end you need
Problem Given an array of positive integers. Find the first and second largest elements in the array. You are not allowed to use more than one loop in your solution. Solution The solution is to use a single loop to find the first and second maximum. The current array element is compared to both first
Find two largest elements array Given an array of positive integers. Find the first and second largest elements in the array. Largest two numbers in an array solution One solution is to use two loops. The first loop finds the first maximum and the second loop finds the second maximum. Note that the second loop
Greedy algorithm interval scheduling problem An event starts at 9AM and finishes at 6PM. Several volunteers have signed to the event each providing a time period during which they can help. We need to cover the entire time of the event (9AM-6PM) with the least number of volunteers. Assume the next volunteer in line can
Optimized version of bubble sort Bubble sort is a slow comparison based sorting algorithm. Can you suggest one way to optimize it. Optimized bubble sort complexity and solution Bubble sort works by repeatedly visiting all elements of the list to be sorted comparing each pair of adjacent elements and swapping them if they are not
Most repeated character string Given a string of characters find the character that is repeated the most. Solution This is almost the same as the First None Repeating Char problem. We are going to borrow the same code however we will slightly modify it. The only code that we need to add is calculating the
Integer division problem definition Given the following function which computes integer division. Debug the code and identify the major bugs. Modify the code to fix those bugs. Assume all integer variables are within range and no overflow is about to happen. Integer division explained (1) If the numerator is equal to the denominator the function
Problem Write a function to compute the sum of integers between two given integer values. Solution The naive solution is to loop by going from the lower integer ending at the higher integer while calculating the sum. The other solution is compute the sum using the following formula Sum (1..n) = n (n + 1)/2
All Keyword in SQL Given the following database table: employee(emp_name, emp_salary) write an SQL statement to print the employee name with the highest salary Solution Using the max aggregate function will give us the maximum salary but it does not give us the name of the employee who has the maximum salary. The solution is