Monday, May 16, 2011

Time Complexity of the Solution

Could you suggest me time complexity of the following solution:

02.// import java.math.*;
03.class Solution {
04. 
05.int sum = 0;
06.int sumTillCurrentIndex = 0;
07.int sumFromBack = 0;
08.int equi ( int[] A ) {
09.for(int i : A){
10.sum += i;
11.}
12. 
13.if(sum - A[0] == 0 ){
14.return 0;
15.}
16.if((sum - A[A.length -1]) == 0){
17.return(A.length -1);
18.}
19.sumFromBack = sum - A[0];
20.for(int index = 1; index < A.length - 1; index++){
21.sumTillCurrentIndex += A[index - 1];
22.sumFromBack -= A[index];
23.System.out.println("" + sum + ":"
24.+ sumTillCurrentIndex + ":" + sumFromBack );
25.if(sumFromBack == sumTillCurrentIndex){
26.return(index);
27.}
28.}
29.return -1;
30.}
31.}

Saturday, May 7, 2011

Abstraction v/s Encapsulation

Java is an Object Oriented Programming language. Each object oriented programming language is standing on the following pillars.

A. Polymorphism
B. Abstraction
C. Inheritance
D. Encapsulation

Now everyone who deals with Java code, surely understands inheritance and they have  a fair idea about polymorphism. Everyone knows inheritance because Java has forced developers to use inheritance. Each class inherits Object class internally. That is the reason we all understand Inheritance.

Polymorphism, is little more complex and less used concept. There are possibilities that developer may code thousand lines without encountering necessity of polymorphism in his code.

Abstraction and encapsulation are least used principle in Java programming language. Usually a beginner could not understand the analogy behind abstraction whereas  a beginner uses encapsulation logic by default. So we never try to explore these concepts until one fine day an interviewer asks the difference between Abstraction and Encapsulation. Seriously we should take interviews to increase our knowledge about technology.

If we need to define these principle's in two words then we can say Abstraction is implementation hiding where as encapsulation is Data Hiding.

What is Abstraction?
Class's definition is not complete and undefined definition is completed by child classes.
Weird, is not it? When there is no scope of creating a class then why having such class?

Database drivers are best example to illustrate abstraction. JDBC has defined a set of abstract interfaces/ abstract classes. Database providers have to complete the implementation for the following interfaces and classes. Now to interact with one database, developer need to register respective driver without changing the code. Developer does not need to mug up multiple databases to code for each of them. So using abstraction, problem of tackling different databases has been handled brilliantly. So without disclosing the actual implementation, desired functionality has been achieved.

What is Encapsulation?
Data is not directly accessible to user. It is much safer and recommended to make all fields as private and provide field accessors ( getter and setter methods ). Private is used so that no one could destroy the data intentionally and unintentionally and accessors methods provide more flexibility and control over the field.

Let's try to understand this with Walmart stores located in different countries. Let's say Walmart is trying to sell one product across countries. So for each country it will be inappropriate to have different instance of same product. To save the price of product they need to convert the price while storing and displaying, so having getter and setter methods give the control on data.

Here is the little difference between the use of abstraction and encapsulation. Hope I am able to clarify a little about complex and important concept of core Java.

Saturday, February 5, 2011

Resizing/Increasing Root space at Ubuntu

I have been using Ubuntu for past couple of months and my experience has been great. Till now I have not done much of the experiment with it but I have encountered few problems and resolved few as well.

As I am a Novice in Linux I understand the severity of the problem faced by us. They might seem simple to people who are already in business for years but for people like us those issues are pain in A**.

Today I am gonna discuss one problem which is faced by people who start Linux as experiment and later start loving it.

Problem
My current laptop was equipped with Windows Vista. I wanted to experiment with some flavor of Linux but I was not sure of removing Windows, so windows survived primarily as a backup plan. At the time of installation my HDD dint have much space, But as I was experimenting so I did not think of it as an Issue. I installed Ubuntu on my system and as per the availability of the resources It assigned 6.1 GB of disk space to it. With the time my data increased in Home directory and suddenly I found myself without even I MB space to do my day to day activities. Horrible, is not it? But I solved issue by removing few files here and there and got it working for next couple more days. What next? Hmmm, Now I have to solve this problem.

So problem is to either add size in my Ubuntu partition or reinstall it again with little extra space?

Solution
Oops, second option is scary. I asked one kid(I am still a kid :) :) ) and he said, Uncle gone mad, resize it :). So did I. First of all I removed some movies from my collection.

As I was still having windows, so I created raw disk using windows.
- RAW Disk is something which is not even recognized to windows.

Create EXT4 partition using Unix GUI utility.
Now resizing Ubuntu there were two option. Increase my bootable partition or assign new partition to one folder. Increasing bootable partition need live CD and I wanted some fast recovery. So I used another option.

Steps to be followed:

1. Create a EXT4 partition and LABEL it, e.g. SAMPLE.

2. Go to Root using terminal.
cd /

3. Create a New Directory, e.g home_new
mkdir home_new

4. Modify fstab( /etc/fstab) such that new partition will point to /home_new. Add following entry.
sudo vi /etc/fstab
Pass the root password and add the entry in the file.
LABEL=SAMPLE  /home_new ext4 defaults,nodev,nosuid,relatime 0 2
If you want to read more about fstab entry, then click here .

5. Move whole data from /home to /home_new. It is better to cross verify if the data has been moved or not. If the data is copied to /home_new and still exist in /home then remove this data.
sudo mv /home/* /home_new



5. After coping and removing data exist in /home, Modify fstab( /etc/fstab) such that new partition will point to /home. Modify previous entry to look like this
sudo vi /etc/fstab
Pass the root password and add the entry in the file.
LABEL=SAMPLE  /home ext4 defaults,nodev,nosuid,relatime 0 2



Now you can restart your computer. Now your home is pointing to a different disk and you have more space in your system.

In between I have asked to recheck if data is removed or not. I have specified that because if you are not deleting the data in old home directory then after making all changes this data will not free required space and you will not be able to delete it later. As I did not delete the data, so currently I am not able to use 1.5GB of my hard disk.

I have to figure out a way to retrieve or delete this data and currently it is an open problem for me. Hope this article will help you to add size to your ubuntu version without making much efforts.