Thursday, October 28, 2010

Binary Addition and Multiplication

void into2() // x 2
{
for(int i=len;i>0;i--) s[i]=s[i-1];
s[0]=0;
len++;
}

bool plus1() // + 1 : returns true on overflow

{
int i=0;
while(i
if(i==len) return true;
for(i=0;i
{
if(s[i]==0){ s[i]=1; break; }
s[i]=0;
}
return false;
}

Wednesday, October 27, 2010

next_permutation in Java

A few TopCoder SRM problems could be solved by using the next permutation function. Since the next_permutation() function is already available in the C++ library it becomes a sort of a disadvantage for the Java coders. Therefore most of the Java coders have this function in their templates. Here is the implementation in java

public static int[] nextperm(int[] vals) {
int i = vals.length-1;
while (true) {
int ii = i;
i--;
if (vals[i] <>
int j = vals.length;
while (vals[i] >= vals[--j]);
int temp = vals[i]; //Swap
vals[i] = vals[j];
vals[j] = temp;
int begin = ii, end = vals.length-1;
while (end>begin) {
int stemp = vals[end]; //Swap
vals[end] = vals[begin];
vals[begin] = stemp;
end--; begin++;
}
return vals;
} else if (vals[i] == vals[0]) {
int begin = 0, end = vals.length-1;
while (end>begin) {
int stemp = vals[end]; //Swap
vals[end] = vals[begin];
vals[begin] = stemp;
end--; begin++;
}
return vals;
}
}
}

Thursday, October 21, 2010

When I was in Bench!

I was in the bench at L&T Infotech during my first year there. And there were times when I felt really desperate and irritated, and that is when beautiful lines came out from my mind. Following is the transcript of a chat that I had with one of my friends -

Gopinath Mani/LNTINFOTECH (3:23 PM) life is running
Aathif Mohammed/LNTINFOTECH (3:24 PM) LIFE IS PAUSED
Gopinath Mani/LNTINFOTECH (3:24 PM) life is stopped n i wanna rewind
Aathif Mohammed/LNTINFOTECH (3:25 PM) life is stopped nd i wanna fast forward!
Gopinath Mani/LNTINFOTECH (3:25 PM) loose stopped then how u'll FF da... baekku
Aathif Mohammed/LNTINFOTECH (3:26 PM) start and fast forward
Gopinath Mani/LNTINFOTECH (3:26 PM) for that u can break that player itself..... lol
Aathif Mohammed/LNTINFOTECH (3:27 PM) the player is already broken..
Aathif Mohammed/LNTINFOTECH (3:27 PM) they're trying to fix it
Gopinath Mani/LNTINFOTECH (3:27 PM) he he he..........
Gopinath Mani/LNTINFOTECH (3:27 PM) life is funny finally
Gopinath Mani/LNTINFOTECH (3:27 PM) :P
Aathif Mohammed/LNTINFOTECH (3:28 PM) life is not funny ..
Gopinath Mani/LNTINFOTECH (3:28 PM) ur life is funny for me da..... so isnt it funny
Aathif Mohammed/LNTINFOTECH (3:28 PM) it is serious but with lots of fun
Gopinath Mani/LNTINFOTECH (3:28 PM) he he he... jus kidding
Aathif Mohammed/LNTINFOTECH (3:28 PM) ya thats not kidding ..
Aathif Mohammed/LNTINFOTECH (3:29 PM) it is true
Gopinath Mani/LNTINFOTECH (3:29 PM) he he he
Aathif Mohammed/LNTINFOTECH (3:29 PM) life is funny..
Gopinath Mani/LNTINFOTECH (3:29 PM) my life for u?
Aathif Mohammed/LNTINFOTECH (3:29 PM) if try not only to make money
Aathif Mohammed/LNTINFOTECH (3:29 PM) and become a coding bunny
Aathif Mohammed/LNTINFOTECH (3:30 PM) life is'nt only coding
Gopinath Mani/LNTINFOTECH (3:30 PM) dai bhai rapper aagitiya da
Aathif Mohammed/LNTINFOTECH (3:30 PM) maybe
Gopinath Mani/LNTINFOTECH (3:31 PM) funny money bunny kalakkara po :)
Aathif Mohammed/LNTINFOTECH (3:31 PM) :)
Aathif Mohammed/LNTINFOTECH (3:31 PM) FAST FORWARD MAMA
Aathif Mohammed/LNTINFOTECH (3:31 PM) FAST FORWARD
Gopinath Mani/LNTINFOTECH (3:31 PM) peelings ah da mama :P
Aathif Mohammed/LNTINFOTECH (3:31 PM) naa
Gopinath Mani/LNTINFOTECH (3:31 PM) :)
Aathif Mohammed/LNTINFOTECH (3:31 PM) i wont feel
Gopinath Mani/LNTINFOTECH (3:32 PM) but im not able to see thae josh in ur messages da :P
Aathif Mohammed/LNTINFOTECH (3:32 PM) okies then here it is
Aathif Mohammed/LNTINFOTECH (3:32 PM) wat is the value of (1 << n)??
Aathif Mohammed/LNTINFOTECH (3:33 PM) if u feel ur life is boring..
Aathif Mohammed/LNTINFOTECH (3:33 PM) then better start coding
Gopinath Mani/LNTINFOTECH (3:33 PM) sorry da bhai... im jus a tester n a small tym coder not a researcher da :P'
Aathif Mohammed/LNTINFOTECH (3:33 PM) cos it'll make ur life interesting
Aathif Mohammed/LNTINFOTECH (3:34 PM) and ull start contesting
Gopinath Mani/LNTINFOTECH (3:34 PM) bhai i think u r angry on some one
Gopinath Mani/LNTINFOTECH (3:34 PM) who is it
Aathif Mohammed/LNTINFOTECH (3:34 PM) naa
Aathif Mohammed/LNTINFOTECH (3:34 PM) if not for coding
Gopinath Mani/LNTINFOTECH (3:34 PM) u mouth is tellin not ur heart
Aathif Mohammed/LNTINFOTECH (3:34 PM) even if iam bad in it
Aathif Mohammed/LNTINFOTECH (3:34 PM) i wud've run away
Aathif Mohammed/LNTINFOTECH (3:34 PM) mama it is time to show-off
Gopinath Mani/LNTINFOTECH (3:35 PM) baga paandi
Gopinath Mani/LNTINFOTECH (3:35 PM) :P
Aathif Mohammed/LNTINFOTECH (3:35 PM) before the lay-off
Gopinath Mani/LNTINFOTECH (3:35 PM) show off wat
Gopinath Mani/LNTINFOTECH (3:35 PM) :)?
Gopinath Mani/LNTINFOTECH (3:35 PM) he he he
Aathif Mohammed/LNTINFOTECH (3:35 PM) from today i've decided
Aathif Mohammed/LNTINFOTECH (3:35 PM) that show-off does'nt hurt
Gopinath Mani/LNTINFOTECH (3:35 PM) is it
Gopinath Mani/LNTINFOTECH (3:36 PM) lets c
Gopinath Mani/LNTINFOTECH (3:36 PM) wat made u to take this decision
Aathif Mohammed/LNTINFOTECH (3:36 PM) so.....
Gopinath Mani/LNTINFOTECH (3:36 PM) wat hap today
Aathif Mohammed/LNTINFOTECH (3:36 PM) nothing
Gopinath Mani/LNTINFOTECH (3:36 PM) who showed off
Aathif Mohammed/LNTINFOTECH (3:36 PM) nobody
Aathif Mohammed/LNTINFOTECH (3:36 PM) nobody show-off
Gopinath Mani/LNTINFOTECH (3:36 PM) then wy this sudden change in u ?
Gopinath Mani/LNTINFOTECH (3:36 PM) :)'
Aathif Mohammed/LNTINFOTECH (3:36 PM) just to show ppl
Aathif Mohammed/LNTINFOTECH (3:36 PM) mama
Gopinath Mani/LNTINFOTECH (3:36 PM) show ppl wat
Gopinath Mani/LNTINFOTECH (3:37 PM) ur talent
Aathif Mohammed/LNTINFOTECH (3:37 PM) mmmm
Gopinath Mani/LNTINFOTECH (3:37 PM) ?
Gopinath Mani/LNTINFOTECH (3:37 PM) wy
Aathif Mohammed/LNTINFOTECH (3:37 PM) broken talent
Gopinath Mani/LNTINFOTECH (3:37 PM) u wanna show ur talent
Gopinath Mani/LNTINFOTECH (3:37 PM) ?
Aathif Mohammed/LNTINFOTECH (3:37 PM) i know my talent=(otherstalent) * .01
Gopinath Mani/LNTINFOTECH (3:37 PM) wy u wanna show ur talent
Gopinath Mani/LNTINFOTECH (3:38 PM) u missed *10000000 this
Aathif Mohammed/LNTINFOTECH (3:38 PM) i want to mak sure
Gopinath Mani/LNTINFOTECH (3:38 PM) make sure wat?
Aathif Mohammed/LNTINFOTECH (3:38 PM) that even if i die
Gopinath Mani/LNTINFOTECH (3:38 PM) die?
Gopinath Mani/LNTINFOTECH (3:38 PM) wat
Gopinath Mani/LNTINFOTECH (3:38 PM) ?
Gopinath Mani/LNTINFOTECH (3:38 PM) wen? die
Gopinath Mani/LNTINFOTECH (3:38 PM) ?
Aathif Mohammed/LNTINFOTECH (3:38 PM) my broken talent remains
Aathif Mohammed/LNTINFOTECH (3:38 PM) mama if u chkd my orkut profile
Gopinath Mani/LNTINFOTECH (3:38 PM) baga:P
Aathif Mohammed/LNTINFOTECH (3:38 PM) u wud've noticed
Gopinath Mani/LNTINFOTECH (3:39 PM) wat
Aathif Mohammed/LNTINFOTECH (3:39 PM) for(int age=0;age<(life.length);++age);
Aathif Mohammed/LNTINFOTECH (3:39 PM) sumtimes the loop may break
Gopinath Mani/LNTINFOTECH (3:39 PM) ya noticed
Aathif Mohammed/LNTINFOTECH (3:39 PM) if an exeption happens
Gopinath Mani/LNTINFOTECH (3:39 PM) bhai
Gopinath Mani/LNTINFOTECH (3:39 PM) baga bhai da
Gopinath Mani/LNTINFOTECH (3:40 PM) nee
Gopinath Mani/LNTINFOTECH (3:40 PM) put try catch n njoi the exceptions da
Aathif Mohammed/LNTINFOTECH (3:40 PM) if another running thread is interrupted
Aathif Mohammed/LNTINFOTECH (3:40 PM) if a running thread is interrupted
Aathif Mohammed/LNTINFOTECH (3:40 PM) the thread dies
Aathif Mohammed/LNTINFOTECH (3:40 PM) giving only a InterruptedException
Aathif Mohammed/LNTINFOTECH (3:41 PM) u can only know y it was interrupted
Aathif Mohammed/LNTINFOTECH (3:41 PM) u cannot restart the thread again
Gopinath Mani/LNTINFOTECH (3:41 PM) u can start a new action with that thrown exception da... :)
Aathif Mohammed/LNTINFOTECH (3:41 PM) bcos the running thread was LIFE
Gopinath Mani/LNTINFOTECH (3:41 PM) there uis something callled second lucky life :)
Aathif Mohammed/LNTINFOTECH (3:42 PM) that is only wen the Exception is anything other than InterruptedException
Aathif Mohammed/LNTINFOTECH (3:43 PM) Another Thread then waits for the dead Thread to recover...
Aathif Mohammed/LNTINFOTECH (3:43 PM) so that it cud continue
Aathif Mohammed/LNTINFOTECH (3:43 PM) but unfortunately it'll never happen
Aathif Mohammed/LNTINFOTECH (3:43 PM) And finally it is killed by some other process
Gopinath Mani/LNTINFOTECH (3:44 PM) bhai...... catching that interupted exception we can start the waiting thread la
Aathif Mohammed/LNTINFOTECH (3:44 PM) no no
Gopinath Mani/LNTINFOTECH (3:44 PM) wy is it so
Aathif Mohammed/LNTINFOTECH (3:44 PM) catch blocks (atleast in India) is used only for debugging
Aathif Mohammed/LNTINFOTECH (3:44 PM) only to see the stacktrace
Aathif Mohammed/LNTINFOTECH (3:44 PM) wat happened
Gopinath Mani/LNTINFOTECH (3:45 PM) u are a developer da researcher da... use it for this purpose :)
Aathif Mohammed/LNTINFOTECH (3:45 PM) why did the Thread of Life Die
Aathif Mohammed/LNTINFOTECH (3:45 PM) But debugging unfortunately is not possible for the Thread of Life
Gopinath Mani/LNTINFOTECH (3:45 PM) going to sleep mode...... willl come active wen awake
Gopinath Mani/LNTINFOTECH (3:45 PM) :)
Aathif Mohammed/LNTINFOTECH (3:45 PM) as it is run only Once
Gopinath Mani/LNTINFOTECH (3:45 PM) bye
Aathif Mohammed/LNTINFOTECH (3:45 PM) heh
Aathif Mohammed/LNTINFOTECH (3:45 PM) copy paste