The post Find Square Root of a Number appeared first on Coding Flare.

]]>For example:

```
Input: 27
Output: 5
```

This problem seems easy to be solved in linear time. We can iterate each number from 1 to X and check if the square of number is less than or equal to X. The number whose square is just less than or equal to X is our answer. This seems easy. Let us stretch ourselves and think of a better solution. O(logn) perhaps.

We can apply binary search to pin point the number whose square will be just less than or equal to X. We can find the middle element of the range and check on which side square of middle element lies. This will provide us our next range. The solution will be more clear after looking at the code.

Since multiplying the 2 middle elements can cause integer overflow, we have used division to find the other component in the multiplication which will give us the number X.

```
public class CodingFlare {
public int sqrt(int a) {
if (a == 0) {
return 0;
}
int lo = 1;
int hi = a;
int res = 0;
while (lo <= hi) {
// finding middle element
int mid = lo + (hi - lo) / 2;
// finding the other component by diving a by middle element
int x = a / mid;
// if both components are equal, we have arrived at our solution
if (mid == x) return mid;
// compare and take decision
else if (x < mid) hi = mid - 1;
else {
res = mid;
lo = mid + 1;
}
}
return res;
}
}
```

The post Find Square Root of a Number appeared first on Coding Flare.

]]>The post Convert Excel Column String to Number appeared first on Coding Flare.

]]>For example

A -> 1 B -> 2 ... Z -> 26 AA -> 27 AB -> 28

We can see the each element of input can be in the range A-Z (26 possibilities). But every element in output can only be in the range 0-9 (10 possibilities). We need a way to somehow represent 26 characters in 10 characters (base conversion). Good thing is we already know how to convert base of a number e.g. converting a decimal number to a binary number. We can apply the same technique here for conversion of base 26 to base 10.

```
public class CodingFlare {
public int titleToNumber(String title) {
if (title == null) {
return 0;
}
long mult = 1;
long num = 0;
for (int i = title.length() - 1; i >= 0; i--) {
num += (title.charAt(i) - 64) * mult;
mult *= 26;
}
return (int) num;
}
}
```

The post Convert Excel Column String to Number appeared first on Coding Flare.

]]>The post Why I am starting this blog? appeared first on Coding Flare.

]]>It won’t be fair if I say I am starting this blog just for sharing knowledge to the world. This blog will be highly beneficial to me also. It will also be used to neatly organize my technical notes ( I have used OneNote till now) and to document frequent issues. We will also be discussing interesting Data Structures and Algorithm problems. I will frequently try to update the blog with the knowledge that I gain at my work. This blog will also help me to better my communication skills.

Well, you can expect a lot of content variation in this blog. You can expect:

- Technical guides of software in Development and DevOps field.
- Data Structures and Algorithm Problems.
- Commonly faced critical issues.
- Best practices in Software Industry.
- Basic programming guide for beginners

The post Why I am starting this blog? appeared first on Coding Flare.

]]>