Problem Write a recursive algorithm to compute the average of an array of numbers. Solution The average of an array of numbers is the sum of all numbers divided by the total number of elements. It is the same as dividing each number by the total number of elements then adding each division result to
Problem Given a positive integer. Find the number of trailing zeros in the integer’s factorial. For example the factorial of 10 is 3628800 so the algorithm should print 2 Solution Let us start by a simple example. Factorial of 5 = 5 x 4 x 3 x 2 = 5 x 2 x 2 x
Problem Given an array of positive integers. Write an iterative version of binary search to find an element in the array Solution Binary search can be easily implemented using recursion due to the divide and conquer nature of the algorithm however we can also do it using iteration. Please refer to the code below for
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
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
Find non repeating character string Develop an algorithm to print the first none repeating character in a string for example the first none repeating character in the string “baby” is “a” Algorithm A brute force method requires us to use two nested loops for character comparison. The moment we find a match we break the