Sunday, September 29, 2019

C++: Print matrix in counter-clockwise spiral form

We are given a matrix of any dimension and we need to print the matrix elements in counter clockwise direction.


Output: 1 5 9 10 11 12 8 4 3 2 6 7

C++: Find all the unique elements in a matrix

We are given a matrix of any dimension and wee need to print all the elements that are unique i.e appearing only once. So here is the c++ code for the algorithm.

Output:
The unique elements in the matrix are 4 6 9

Explanation:

First, in the 'main' function, we create the matrix 'mat' of size 4 with 16 elements. Then we call the function 'uniqueElement' with the matrix 'mat' as the parameter. Note, at the top of the program, we have defined R(row) as 4 and C(col) as 4.

In the function 'uniqueElement', we first declare a map 'cnt' of integer type. Then we loop through all the elements of the matrix and increment the frequency of all the elements using 'cnt[mat[i][j]++'. After the loop, all the elements in the matrix will be mapped to its corresponding frequency.

After this, we loop through the map 'cnt' and print the elements with frequency 1.

Thursday, September 26, 2019

C++: Turn a matrix by 180 degree.

We are given a matrix of any dimension and we need to turn it by 180 degree. Here is the source code for the algorithm in c++.


Output:  12  11  10  9
               8    7    6   5
               4    3    2    1
             

Wednesday, September 25, 2019

C++: Print a Matrix in Spiral Form

We are given a matrix of any dimension, say 3x3, 4x4 or 4x3, and we are required to print the elements of the matrix in spiral form.


Output: 1 2 3 4 8 12 11 10 9 5 6 7

C++: Rotate Matrix to the right.

Given a square matrix and we need to write a program to rotate the matrix to the right. Here is the code for the program.

Output:
5    1    2    3
9    10   6    4
13   11   7    8
14   15   16   12


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: