Re: [algogeeks]

2010-06-05 Thread sharad kumar
@sharad,ya inplace soln is desired... -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For

Re: [algogeeks]

2010-06-05 Thread harit agarwal
if the array contains only numbers 1 to n then it can be done in O(n) inplace otherwise only way is hashing or sorting.which is trivial solution... -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to

Re: [algogeeks] Print the string in reverse order (not revstr)

2010-06-05 Thread Lalit Rajora
Its simple... Reverse the whole string than reverse each word. an dthe work is done complexity O(n) This is a standard question of interview On Sat, Jun 5, 2010 at 9:49 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: Have you posted the same question twice or i am feeling sleepy? --

Re: [algogeeks] Print the string in reverse order (not revstr

2010-06-05 Thread Antony Vincent Pandian.S.
@Shobhit @Rohit Is it done it linear time?? I dont think so... On Sat, Jun 5, 2010 at 9:33 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: Tokenize the string and print it reverse! -- -- Rohit Saraf Second Year Undergraduate, Dept. of

Re: [algogeeks] Print the string in reverse order (not revstr)

2010-06-05 Thread Antony Vincent Pandian.S.
Rohit, you are very well awake On Sat, Jun 5, 2010 at 9:49 AM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: Have you posted the same question twice or i am feeling sleepy? -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer

Re: [algogeeks] Re: partion of array

2010-06-05 Thread Anand
DP approach for solving this problem. Anand On Fri, Jun 4, 2010 at 9:06 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: What precisely did you not understand?? -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and

Re: [algogeeks] Longest Increasing Subsequence in O(nlogn)

2010-06-05 Thread Anand
http://codepad.org/NDAeIpxR Here is code for it On Sat, May 29, 2010 at 7:45 PM, Anurag Sharma anuragvic...@gmail.comwrote: @pawan Will it not take O(n^2) time then. What he is talking about is solving it in O(nlogn) time Anurag Sharma http://anuragsharma-sun.blogspot.com/ On Sat, May

Re: [algogeeks] Print the string in reverse order (not revstr

2010-06-05 Thread Rohit Saraf
Tokenization is done in linear time. Just save the words in an list (And what makes you think of non-linearity in tokenization!) And then iteration over the tokens is trivially linear. -- Rohit Saraf Second Year Undergraduate, Dept. of Computer

[algogeeks] o/p

2010-06-05 Thread sharad
#includestdio.h int main() { short int i=0; for(i=5i=-1;++i;i0) printf(%u\n,i); return 0; } o/p coming is 1...4,294,967,295 short int is of 2 bytes can any one plzz explain the o/p -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to

Re: [algogeeks] Quick short stack space reduction

2010-06-05 Thread Senthilnathan Maadasamy
Hi all, Please correct me if I am wrong. Since quick sort is in-place sort, there is no difference in stack space whichever order you do it. In the worst case the stack depth can be O(n) unless you convert the quick sort of longer sub-array to iteration instead of recursion in which

[algogeeks] Largest even length palindrome in a string!!

2010-06-05 Thread Satya
Hi, How to find largest palindrome, which is even in length in a string. Complexity should be lessthan O(n^2). Ex;- abacbbcababac - Given string. 'abacbbcaba' - is the largest even length palindrome. Thanks, Satya -- You received this message because you are subscribed to the Google Groups

Re: [algogeeks] Quick short stack space reduction

2010-06-05 Thread Rohit Saraf
Actually he means the case when you implement quicksort using stacks. -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you

Re: [algogeeks] o/p

2010-06-05 Thread divya jain
output on my compiler is 165535 as i=0 initialy ++i is true and so for loop condition is true and printf is executed. when i becomes 65535 then ++i is equal to 0 and so for loop condition becomes false. On 5 June 2010 13:44, sharad sharad20073...@gmail.com wrote: #includestdio.h int main()

Re: [algogeeks] Largest even length palindrome in a string!!

2010-06-05 Thread divya jain
1. first find two consecutive letters which are same. 2 point ptr1 to left character and ptr2 to right one 3. now increment ptr2 and decrement ptr1. compare if they are pointing to same characters. if yes repeat this step 4. if no then store in length ptr2-ptr1-2 5. go to step 1 untill all

[algogeeks] Re: Largest even length palindrome in a string!!

2010-06-05 Thread Minotauraus
for(int i=0; iword.length-1; i++) { if(word[i]==word[i+1]) //for an even palindrome, consecutive letters at the middle of the string have to be the same { palindromeFound=true; while(n0mword.length) //once you've found the middle of the palindrome, compare left of

Re: [algogeeks]

2010-06-05 Thread Minotauraus
Can be done in O(n log n) without using aux. memory. Can be done in O(n) if you use something like a hash table. On Jun 5, 12:40 am, Rohit Saraf rohit.kumar.sa...@gmail.com wrote: Inplace in O(n) seems unlikely to me. Well you should still try!

[algogeeks] dynamic vs greedy aproach

2010-06-05 Thread divya jain
how can we make out whether to apply greedy approach or dynamic programming to a problem?? -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send

Re: [algogeeks] o/p

2010-06-05 Thread sharad kumar
@divya,which compiler u r using,i m getting this o/p on gcc compiler @mohit:loop stops at 4,294,967,295 in gcc -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe

Re: [algogeeks] dynamic vs greedy aproach

2010-06-05 Thread Rohit Saraf
Greedy can be used to find one solution which has some special characteristics. It should be like - You are able to say that if for any instance of size k the greedy and the optimal solution match, then for any corresponding instance of size k+1, the greedy solution is atleast as good as optimal.

Re: [algogeeks] o/p

2010-06-05 Thread Rohit Saraf
If you make it unsigned short int.. then it goes to 65535 on g++/gcc -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you are

Re: [algogeeks] Print the string in reverse order (not revstr

2010-06-05 Thread Rohit Saraf
Ok. so you have a list. Iterating over it is linear isn't it? Ahh... you will need a doubly linked list or an arraylist.does it solve ur prob? -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay

Re: [algogeeks] o/p

2010-06-05 Thread divya jain
turbo c++ and my o/p is logical as well On 5 June 2010 17:58, sharad kumar sharad20073...@gmail.com wrote: @divya,which compiler u r using,i m getting this o/p on gcc compiler @mohit:loop stops at 4,294,967,295 in gcc -- You received this message because you are subscribed to the Google

[algogeeks] Find Max Number of Elephants

2010-06-05 Thread amit
Maximum number of elephants alive Hello guyz, Every elephant has a birth_time and a death_time. Given N Elephants with birth times and death times.. How can we find 1) the maximum number of elephants that can be alive at any given point of time. 2) what is the year in which you can have maximum

Re: [algogeeks] Re: Largest even length palindrome in a string!!

2010-06-05 Thread Bharath
Complexity is O(n3) for both above solutions Ex: On Sat, Jun 5, 2010 at 3:27 AM, Minotauraus anike...@gmail.com wrote: for(int i=0; iword.length-1; i++) { if(word[i]==word[i+1]) //for an even palindrome, consecutive letters at the middle of the string have to be the same {

[algogeeks] Re: o/p

2010-06-05 Thread Devendra Pratap Singh
reason for that o/p is ... coz range of short int is -32768 to 32767 and value of i start with i=0 and each time it will increment by 1 so corresponding value of i will be 1 2 3 . . . 32766 32767 -32768 -32767 . . . . -2 -1 but coz in printf u r using %u so it will print... 1 2 3 . . .

Re: [algogeeks] Re: o/p

2010-06-05 Thread Rohit Saraf
oh... why did u put %u. i did not even notice that :P -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you are

Re: [algogeeks] Recursion help!

2010-06-05 Thread Anand
This a good example of dynamic programming. On Fri, Jun 4, 2010 at 10:15 AM, satwik krishna sathi...@gmail.com wrote: i think the best way to trace is to draw a picture of the stack and put the values and acc understand the flow On Fri, Jun 4, 2010 at 7:22 AM, Prashant Kulkarni

Re: [algogeeks] Find Max Number of Elephants

2010-06-05 Thread Anurag Sharma
use interval trees. Anurag Sharma http://anuragsharma-sun.blogspot.com/ On Sat, Jun 5, 2010 at 6:16 PM, amit amitjaspal...@gmail.com wrote: Maximum number of elephants alive Hello guyz, Every elephant has a birth_time and a death_time. Given N Elephants with birth times and death times..

Re: [algogeeks] Find Max Number of Elephants

2010-06-05 Thread Jitendra Kushwaha
@Amit : Query 1 : It can be solved in O(n) by checking which elephants life span contain the given year Query 2 : Picking up the fact that a elephant die only after its birth we can find the second query in O(nlogn) ALGO: 1. sort in increasing order birth year and death

[algogeeks] divisible by 3

2010-06-05 Thread divya
Find if a number is divisible my 3, without using %,/ or *. You can use atoi(). -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to

Re: [algogeeks] Largest even length palindrome in a string!!

2010-06-05 Thread Antony Vincent Pandian.S.
Follow hare and tortoise algorithm. For even length, once the traversal through out the string is done, move the fast pointer to slow pointer +1 location and start iterating for (length/2) times with 2 indices. One indice increasing for fast pointer and the other for slow pointer. Keep checking

[algogeeks] array question

2010-06-05 Thread divya
Given an array with some repeating numbers. Like 12,6,5,12,6 output: 12,12,6,6,5 12 shud come before 6 since it is earlier in list. So cant use a dictionary. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to

Re: [algogeeks] Largest even length palindrome in a string!!

2010-06-05 Thread Antony Vincent Pandian.S.
Apologies. I thought the question was about just checking for palindrome. On 6/6/10, Antony Vincent Pandian.S. sant...@gmail.com wrote: Follow hare and tortoise algorithm. For even length, once the traversal through out the string is done, move the fast pointer to slow pointer +1 location

[algogeeks] sorted 2-d array

2010-06-05 Thread divya
Given an n X n array with rows sorted and cols sorted, find the number of negative elements in most efficient way -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe

[algogeeks] Product Array

2010-06-05 Thread Raj N
Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. Solve it without division operator and in O(n). Can someone explain me the logic. Thanks!! -- You received this message

[algogeeks] One question on Heap sort

2010-06-05 Thread Anand
Give a O(nlogk) time algorithm to merge k sorted list into one sorted list, where n is the total number of elements in all the input list. My approach: 1. take first element from all the k sorted list and build heap for those elements. 2. call heapify on all k elements to sort it. 3. repeat the

Re: [algogeeks] Product Array

2010-06-05 Thread sharad kumar
#includeiostream using namespace std; int main() { int arr[5]={1,2,3,4,5}; int res[5]={1,1,1,1,1}; int left=1,right=1,i=0; for(i=0;i5;++i) {res[i]*=left; res[4-i]*=right; left*=arr[i]; right*=arr[4-i];

Re: [algogeeks] divisible by 3

2010-06-05 Thread sharad kumar
this problem has been discussed i think.. On Sun, Jun 6, 2010 at 12:15 AM, divya sweetdivya@gmail.com wrote: Find if a number is divisible my 3, without using %,/ or *. You can use atoi(). -- You received this message because you are subscribed to the Google Groups Algorithm Geeks

Re: [algogeeks] Re: Largest even length palindrome in a string!!

2010-06-05 Thread Rohit Saraf
What makes you think it is O(n^3). I did not read the code one but divya's solution seems to be O(n^2) for worst case. -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay

[algogeeks] Re: Largest even length palindrome in a string!!

2010-06-05 Thread Minotauraus
Actually complexity is O(n), BUT it won't work for aa. It'll return aa instead of the whole string. I think you can use similar approach but add another check under the first 'if' statement since 'aa' is a special case. But then again the algo will crap out for a string= bb. I

[algogeeks] Re: divisible by 3

2010-06-05 Thread Minotauraus
Subtract 3 from the number until either you get 0 or a negative number. If you get 0, its divisible, else not. You can probably do this by bit shifting too. On Jun 5, 11:45 am, divya sweetdivya@gmail.com wrote: Find if a number is divisible my 3, without using %,/ or *. You can use atoi().

[algogeeks] Re: sorted 2-d array

2010-06-05 Thread Minotauraus
1. check arr[0][0]. If this is non negative, there are 0 negative numbers. (assuming sorting is ascending) 2. for arr[i][j], increment j until you hit a non-negative number. this index will be limitRow 3. increment i, until you hit a non-negative number, this index will be limitColumn, for

Re: [algogeeks] One question on Heap sort

2010-06-05 Thread Anand
My approach: let's say we have two sorted arrays. array1= x1, x2 x3 array2 = y1, y2, y3 array3= z1, z2,z3 all three arrays are sorted. 1. sort x1,y1,z1 using heap sort. 2. do this for rest of the elements of the array to create a final sorted array in O(nlogk) On Sat, Jun 5, 2010 at 8:45 PM,

Re: [algogeeks] Re: Largest even length palindrome in a string!!

2010-06-05 Thread Rohit Saraf
Just read your code. It wont even work. Do you assume only one even length palindrome!? -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this

Re: [algogeeks] One question on Heap sort

2010-06-05 Thread Rohit Saraf
Can u elaborate on the 2nd step. btw, did u understand my soln? -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received this message because you are

Re: [algogeeks] Re: partion of array

2010-06-05 Thread Rohit Saraf
This is almost the most basic dp. Read some of the examples from eva tardos. That would help. -- -- Rohit Saraf Second Year Undergraduate, Dept. of Computer Science and Engineering IIT Bombay http://www.cse.iitb.ac.in/~rohitfeb14 -- You received

Re: [algogeeks] One question on Heap sort

2010-06-05 Thread Anand
@Rohit I could understand your solution. second step is sort the element using heap sort. On Sat, Jun 5, 2010 at 9:04 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote: Can u elaborate on the 2nd step. btw, did u understand my soln? -- --

Re: [algogeeks] Re: sorted 2-d array

2010-06-05 Thread vinayan c
assuimg the array is sorted likeeach number below is less than the one above and more than the one right to it start from the bottom right ..start moving right until u reach anumber less than zerocalcuate the num of negative elements in that row(n-current columng pos)..and then move