Problem Write a C++ recursive function to convert a character string into capital letters. Solution Instead of looping in the string character by character we can recursively call a function while advancing an index each time. Once the index reaches …

# Tag Archives: Cpp - Page 2

## Recursive Multiplication C++

Problem Write a recursive function to compute the product of two positive integers. Solution The product of two positive integers (A*B) is nothing but the sum of the first integer (A), (B) times Code Here is the code in C++ …

## Recursive Average

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 …

## Factorial Trailing Zeros

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 …

## Iterative Binary Search Function

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 …

## kth Largest Element in an Array

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 …

## Divide and conquer largest two elements

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 …