Tuesday, December 20, 2016

Corners of a square matrix remain corners no matter how many times you invert them

This is a hint That can be used in many programming contests which I noticed during solving the Flipping the matrix problem in Hackerrank.com and thought that it is something that I should write down somewhere. :)
Here is the problem description from Hackerrank.
Sean invented a game involving a  matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times, and the goal of the game is to maximize the sum of the elements in the  submatrix located in the upper-left corner of the  matrix (i.e., its upper-left quadrant).
Given the initial configurations for  matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal. For each matrix, print the maximized sum on a new line.
Input Format
The first line contains an integer, , denoting the number of queries. The subsequent lines describe each of the  queries in the following format:
  1. The first line of each query contains an integer, .
  2. Each line  the  subsequent lines contains  space-separated integers describing the respective values of row  in the matrix.
So first I tried varios thing to reverse the rows and columns in various ways. But later realizd that there should be a simple hint that solve this poblem and there was a one. That is
Corners of a square matrix remain corners no matter how many times you invert them
Using that hint to find the max,

In a 4X4 matrix


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














Sunday, December 11, 2016

3 different ways to loop through every char in a string in c++

When programming sometimes we need to loop through a string and deal with every character in that string. In c++ there are several ways to do that. In this post lets see 3 different ways to loop through a string.
  1. Using a range-based for loop (This feature is available from c++ 11)


  2. Using ierators


  3. Using normal for loop



If you have a null terminated character array this method also can be used.

Those are the 4 different ways that can be used to loop through strings. If you have suggestions or new methods pleas leave a comment below.

Wednesday, December 7, 2016

Find running time of an algorithm in C++

Some times we need to find how long would an algorithm take to complete a task. This is needed very much when we comparing two or more algorithms. So I looked for that in several forums and most of them have suggested to use clock() function which comes with ctime header. That didn't work properly because It gives the time from the start of the program (which I will explain latter part of the post) and not suitable for measuring intervals in nanosecond level.

So here is the solution for that. We can use functions in class high_resolution_clock. Here IsPrime is the function that I want to find the running time of.
As you can see in line 30, we can cast the time difference int any unit we want (ns,ms). That gives us a great advantage of finding very small time intervals. But if we use clock() function instead we wan't be able to achieve that much of accuracy.

Here is a code example of using clock() function (which I don't recommend in this scenario)


clock() function will give the total number of clock ticks until the execution of function from the start of the program (If we want to convert it into seconds, have to divide by CLOCKS_PER_SEC).
But this gives a good result only if we are doing a considerable calculation between the time interval. other wise we will get a zero as result.

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...