Saturday, September 21, 2019

C++: Find common elements in three sorted arrays.

We are given three sorted arrays and we need to find the elements that are common to the three arrays.

Output: 20 80

Here we use binary search to check if the element is present in the array or not. We iterate only the first array and check if each element in the first array is present in other two arrays. The function 'binarySearch()' returns 'true' if the element is found in any of the array and at last if all the three arrays does contain the element, then that element is printed.

Thursday, September 12, 2019

C++: Find the second largest element in an array

In this tutorial, we will see how to find the second largest element in an array. There are different ways to do it- like sorting the array first in descending order and then printing the second element or by double iteration- first for finding the largest element and second for finding the number just less than the largest. But there is one way to find the second largest element just in single iteration. Here is the code for the program.


Output: The second largest element is 8.

Saturday, September 7, 2019

C++: Rearrange and array in maximum, minimum form

We are given a sorted array and we want to rearrange the array in the form of {max, min, max-1, min+1, max-2, min+2.........}. So here is the code:


Output: {7,1,6,2,5,3,4}

C++: Make the largest number from the array of elements.

Here is a C++ program where you are given an array of numbers. Append each element one after another in a way that the resulting number is the largest possible number you can make out of all the given array elements.


Output: 969660548546

Tuesday, September 3, 2019

C++: Reverse an array.

In this tutorial, we will see how we can reverse an array in C++. So here goes the code:



Here when the 'reverseArray' function is called, it takes in the array and its size as the parameter. After that we set 'l' as left index and 'r' as right index. The while loop starts swapping the array with given indices, where the 'l' increases and 'r' decreases after each iteration. Thus when they reach the middle index two things happen:

 1. If n is odd, the array has a mid point which is (n/2+1). In that case l=r and the loop stops.
 2. If n is even, the array does not have a middle point, so we give the condition 'l<r' inside the while loop, so that the 'l' index does not increment after 'r'.


Output:

Original Array: 1,2,3,4,5,6,7,8,9.
Reversed Array: 9,8,7,6,5,4,3,2,1.


Monday, September 2, 2019

C++: Finding a pair with given sum in an array.

In this programming tutorial, we will read an unsorted array, sort it, and then will find the pair with the given sum.

We maintain a search space in the while loop with 'l' for left index and 'r' for right index. 'l' and 'r' will increment or decrements respectively depending on the sum of the pairs being tested inside the 'while' loop.

Given below is the source code for the algorithm:


Thursday, August 22, 2019

Kotlin: Write a program to find the factorial of a number (Using function recursion)

In this blog, I will show you how to write a Kotlin program to find the factorial of a number.

First, we are going to write a function called 'fact' which accepts an integer as a parameter and returns an integer. So here goes the function 'fact' definition.

So how does this work? Well, in the main function we call the function 'fact' along with the integer as an argument. The function accepts it and checks if it's 1 at first, because 1! is 1 itself. If it's not one, it goes to the 'else' part where it multiplies the integer with the fact(num-1). What really happens here? When 'fact(num-1)' is called, the 'fact' function is called again, but this time, the argument is one less than the previous number. So it again works as an independent function call with (num-1) argument. This is called function recursion. This recursion occurs again and again and returns the value each time until the argument reaches 1 where it return the value 1.

For example, consider this:



Here, we give 4 as the argument for the function 'fact'. The value 4 goes inside the function and is taken by 'num'. In the 'if' condition, the 'num' is checked it it is equal to one, which it is not. So it goes to the else part and returns num*fact(num-1). Here the function calls itself, but with an argument which is 1 less than the current one. So a completely independent 'fact' function is made and multiplied with it. The same thing happens again inside the new 'fact' function with 3 as the argument. A new 'fact' function is called inside it with 2 as argument and is multiplied with it. Again a new 'fact' function with 1 as argument is made inside it, but this time it does not get multiplied with the 'fact(2)' but just returns 1. So the recursion ends there and the last returned value which got multiplied with 1 will be the final answer, which is 4*3*2*1, which equals to 24.