Re: [algogeeks] Doubt in removing loop from linked list

2012-03-09 Thread Terence
@ rahul sharma: the linked list is a combination of a list a-b-...-p-q and a cycle q-r-...-z-q. (z != p). noting that the start of cycle q is the only node with 2 predecessor: p and z. if 2 pointers meet at some node x, different from q, in last step they must have met at x', the predecessor

Re: [algogeeks] Re: How many ways n points can be connected

2012-02-09 Thread Terence
, rspr wrote: @Terence: The DP approach is nice. if we have constraint likewhile choosing the first 3 edges if it makes a triangle so we will require n edges to connect the graph completely...in the same fashion...after selecting 2 more edgesagain we have to check that is it making more

Re: [algogeeks] How many ways n points can be connected

2012-02-08 Thread Terence
Here is an DP solution: (consider only simple graph, with at most 1 line between any 2 distinct points, and no point connect to itself) Suppose f(n,m) is the number of ways m lines can connect n points. Then f(n,m) = 0 when m n-1 or m n(n-1)/2; For graph with n vertices and m edges

Re: [algogeeks]

2012-01-18 Thread Terence
1. the computing of d is incorrect. d = n-1; while((d1)==0) d=1; 2. the accuracy of pow is far from enough. you should implement your own pow-modulo-n method. 3. for big n, (exceed 32bit), x=(x*x)%n can be overflow also. you may need to implement your own multiply method in this

Re: [algogeeks]

2012-01-18 Thread Terence
; } main() {for(int k=1;k20;k++) printf(%d is %d\n,k,is_prime(k)); getch();} On 1/18/12, Sourabh Singhsinghsourab...@gmail.com wrote: @ sunny agrawal you are right. but i have used check an also . no improvement . i think algo is wrong in later part.. somewhere.. @ Terence 1) pow may not work

Re: [algogeeks]

2012-01-18 Thread Terence
error in pow. as long as n 2^31, x*x fits into int64_t, since xn. On 2012-1-18 20:51, Sourabh Singh wrote: @ Terence I belive nw its giving wrong answer for n41 onwards due to error's in pow and x*x over flow as u already stated... i guess algo is right nw.. ;-) thanx again.. On 1/18/12

Re: [algogeeks]

2012-01-18 Thread Terence
%n for each then multiply to get result.. but my question is what if power is prime and big... On 1/18/12, Terencetechnic@gmail.com wrote: error in pow. as long as n 2^31, x*x fits into int64_t, since xn. On 2012-1-18 20:51, Sourabh Singh wrote: @ Terence I belive nw its giving wrong

Re: [algogeeks] Re: Return index of first mismatch bracket ( or )

2011-12-22 Thread Terence
and popped out. If we could not find an unmatched close bracket, and the stack is not empty, we examine the stack and return the bottom index (the left most unmatched), or the top index (the inner most unmatch) as needed. On 2011-12-22 13:27, Terence wrote: Then what's your output for test case

Re: [algogeeks] Re: Return index of first mismatch bracket ( or )

2011-12-22 Thread Terence
Then what's your output for test case ()(()? On 2011-12-22 12:45, atul anand wrote: i guess there is no need of stack , we can take a variable say top; increment top when open bracket occur ( and decrement when close bracket ) occurs. keep track of first close bracket mismatch i.e when top

Re: [algogeeks] Re: Building a max heap takes O(n) time irrespective of the array being sorted / unsorted.

2011-09-15 Thread Terence
http://en.wikipedia.org/wiki/Binary_heap#Building_a_heap The algorithm of heap-building presented in most books is O(n). On 2011-9-16 12:52, Ankuj Gupta wrote: is talks of more tighter bound of O(nlogn) On Sep 15, 11:24 pm, sunny agrawalsunny816.i...@gmail.com wrote: Read CLRS On Thu,

Re: [algogeeks] Math Puzzle

2011-09-14 Thread Terence
P^2/QR+Q^2/PR+R^2/PQ = (P^3+Q^3+R^3)/PQR = ((P+Q+R)(P^2+Q^2+R^2-PQ-QR-PR)+3PQR)/PQR = 3 On 2011-9-15 13:29, rahul sharma wrote: hw? On Thu, Sep 15, 2011 at 10:57 AM, Tamanna Afroze afroze...@gmail.com mailto:afroze...@gmail.com wrote: yah the ans is 3 -- You received

Re: [algogeeks] Re: How to use asm in spoj

2011-06-18 Thread Terence
CONT1D and D1ONE already defined When compile with -O2, gcc 4.3.2 tries to inline short functions like gcd(). Thus the labels CONT1D and D1ONE get redefined. 1) Try gcc 4.0.3. or 2) use local labels instead. eg. __asm__ __volatile__ ( movl %1, %%eax; 0: cmpl

Re: [algogeeks] Re: Find shortest substring that is only occurring once. in Given String

2011-06-14 Thread Terence
Traversal the suffix tree is enough. All substrings from root to leaf (including at least 1 character of leaf node) occur only once. Choose the shortest among them. On 2011-6-15 5:28, T3rminal wrote: @bittu How can u find the shortest substring from the tree in 0(n). Can u please elaborate a

Re: [algogeeks] Please explain this problem

2011-06-11 Thread Terence
a,b,c,d,e,f do not need to be distinct. (It is possible that a=b=c=d=e=f, see Example 1) On 2011-6-10 12:01, saurabh singh wrote: Problem link- ABCDEF https://www.spoj.pl/problems/ABCDEF/ Can someone please explain this problem.The problem says a,b,c,d,e,f belongs to S.But what if size of S

Re: [algogeeks] [brain teaser] Salman age puzzle 7 june

2011-06-07 Thread Terence
84 years. The well known Diophantus Riddle: http://en.wikipedia.org/wiki/Diophantus#Biography On 2011-6-7 16:03, Lavesh Rawat wrote: ***Salman age puzzle * ** *** *** ** ***salman's youth lasted one sixth of his life. He grew a beard after one twelfth

Re: [algogeeks] Array problem

2011-05-20 Thread Terence
Try this case: 1000 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 On 2011-5-18 0:27, Kunal Patil wrote: Ohh..If it is so...Sorry !! I understood it the different way... But if the question is as mentioned in your 2nd case then also I believe there is O(n) solution.

Re: [algogeeks] Inversion Count

2011-05-13 Thread Terence
Guard value error. L[n1]=; R[n2]=999; R[n2] may be merged and counted, if L[1..n1-1] has 999. On 2011-5-12 19:18, Akshata Sharma wrote: I tried to solve this problem using merge sort logic, the algorithm is same as merge sort, only change is in the merge step where i am

Re: [algogeeks] Re: Print Hello

2011-03-26 Thread Terence
@Manikanta: What compiler do you use? $ gcc test.c test.c:2: error: initializer element is not constant $ g++ test.c $ ./a.out Hello $ gcc --version gcc (GCC) 4.1.1 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the

Re: [algogeeks] RR Scheduling

2011-03-26 Thread Terence
In this problem, sum can be as large as 5*10^9. Try breaking the whole interval into several stages (no more than 2*N), each with a fixed amount of tasks. Then in each stage, the schedule is a simple loop: A B C D E A B C D E A B ... Process each stage in O(1) time, then the total time

Re: [algogeeks] [brain teaser ] 22march

2011-03-26 Thread Terence
4 * 4 - 7 = 9 Start both sandglasses at the same time. When the 4-minute sandglass is over, flip it to restart timing. Repeat this 3 times. From the moment when the 7-minute sandglass is over, to the moment when the 4-minute sandglass is over for the 4th time, is an interval of 9 minutes. On

Re: [algogeeks] [brain teaser ] 7march

2011-03-08 Thread Terence
I will get the gold coin or nothing. On 2011-3-7 16:11, Lavesh Rawat wrote: ***Get Gold Coin **Problem Solution* * *Imagine there are 3 coins on the table: gold, silver, and copper. If you make a truthful statement, you will get one coin. If you make a false statement, you will get nothing.

Re: [algogeeks] Print Hello infinite..................

2011-03-08 Thread Terence
system(yes Hello); (on Linux) On 2011-3-7 2:09, sudheer kumar wrote: use GOTO On Sun, Mar 6, 2011 at 10:49 PM, UMESH KUMAR kumar.umesh...@gmail.com mailto:kumar.umesh...@gmail.com wrote: How to print Hello Infinite times without using Loop and Recursion function ? -- You

Re: [algogeeks] Re: A doubt...

2011-03-04 Thread Terence
On 2011-3-2 6:22, rgap wrote: Hi, does anybody know when/where to use typedef long long int64; When 64-bit integer is needed. and const long double EPS = 1E-9; When dealing with floating number comparison. -- You received this message because you are subscribed to the Google Groups

Re: [algogeeks] brain teaser

2011-03-04 Thread Terence
SeeTen-Hat Variant (http://en.wikipedia.org/wiki/Prisoners_and_hats_puzzle#Ten-Hat_Solution). On 2011-3-3 18:02, rajul jain wrote: @naveen but in this question don't know numbers of black and red hats like prison and hat puzzle On Thu, Mar 3, 2011 at 3:25 PM, Naveen Kumar

Re: [algogeeks] Re: String of Max Length Which Repeats More Then Onep

2011-02-26 Thread Terence
@Dave: I think it is to find the longest substring which appears more than once in the given string. @bittu: You could use suffix tree: http://en.wikipedia.org/wiki/Suffix_tree, and find the deepest branch node. or use suffix array: http://en.wikipedia.org/wiki/Suffix_array, and find the

Re: [algogeeks] How to identify what is stored in union in c language

2011-02-22 Thread Terence
Then it should be: struct UserData { uType m_dataType; union { int m_intValue; float m_floatValue; char m_charValue; }; } data; On 2011-2-22 15:29, Rajiv Podar wrote: Hi Shiva, There is no as such way to know what type of value is stored in the

Re: [algogeeks] How to identify what is stored in union in c language

2011-02-22 Thread Terence
Nope. The union defines an memory region of 4 bytes (on 32-bit OS). Access age/grade only specifies which part is retrieved / modified. You may assign value to age, then retrieve grade, or vice versa. On 2011-2-22 14:45, shiva wrote: I have an union with following members union data { int age;

Re: [algogeeks] What is the error in the following function

2011-02-22 Thread Terence
Variable start and hex_buf point to string constant, which can not be modified. Change: char *start = 12.12.12.12.12976665176069214; char *hex_buf = 65003a0a; to: char start[] = 12.12.12.12.12976665176069214; char hex_buf[] = 65003a0a; On 2011-2-15 15:01, dinesh bansal

Re: [algogeeks] Re: SPOJ problem code:MAJOR

2011-02-14 Thread Terence
Try scanf/printf instead of cin/cout. For huge data set, the IO time matters. On 2011-2-14 20:09, Akshata Sharma wrote: link to problem: http://www.spoj.pl/problems/MAJOR/ On Feb 14, 5:03 pm, Akshata Sharmaakshatasharm...@gmail.com wrote: I am trying to submit my solution but its giving TLE.

Re: [algogeeks] Re: Bit Manipulation

2011-01-18 Thread Terence
No. It can be applied to any positive number N. The solution above splits the numbers by the least significant bit, choose the group with missing number, and then drop the last bit and continue. In this way the set [0,N] is partitioned (almost) evenly into 2 groups: {2k | k=0,1,...,N/2} and

Re: [algogeeks] Re: [Athena 2011]

2011-01-13 Thread Terence
No need for calculating individual magic(x), just count the number of ordered list L with max(L)=11. Partition those ordered list by its gcd, and check each part, then you can see the trick. On 2011-1-13 11:06, Pratik Bhadkoliya wrote: For example, for 1: divisor is only 1

Re: [algogeeks] SPOJ PROBLEM-pour1

2011-01-13 Thread Terence
No BFS, only mathematics. Get the better one out of the 2 choices. (Maybe simulating each of the 2 choices also fits into the time limit, given the input range in the problem page.). On 2011-1-12 9:28, Bharath 2009503507 CSE wrote: i tried solving this prob...

Re: [algogeeks] Google Interview Question

2010-12-28 Thread Terence
node , return 0 if the leaf has the value needed else return INFINITY.Also if at any internal node if it is not possible return INFINITY. On Tue, Dec 28, 2010 at 10:06 AM, Anand anandut2...@gmail.com mailto:anandut2...@gmail.com wrote: @Terence. Could please elaborate your answer

Re: [algogeeks] Re: Google Interview Question

2010-12-28 Thread Terence
return INFINITY. On Tue, Dec 28, 2010 at 10:06 AM, Anandanandut2...@gmail.com wrote: @Terence. Could please elaborate your answer. Bottom up level order traversal helps to get the final root value but how to use it to find minimum flips needed to Obtain the desired root value. On Fri, Dec 24, 2010

Re: [algogeeks] Re: Google Interview Question

2010-12-28 Thread Terence
' be the index of the minimum element(x[h] is minimum) Then, dp[i][j]=v+sigma(f!=h)[min(x[f],y[f])] The other cases are similar to this! I can write a code and send if you have doubts. On Dec 28, 9:36 am, Anandanandut2...@gmail.com wrote: @Terence. Could please elaborate your answer. Bottom up level

Re: [algogeeks] Re: convert binary matrix to zero matrix

2010-12-27 Thread Terence
when you are talking abt starting from 1 that means that array is 1 based , right ? Right. 111 000 000 Up-left element: 1 Choice 3: 0 (number of 0's on the first row) + 1 (number of 1's on the first column) = 1 Choice 4: 3 (number of 1's on the first row) + 2 (number of 0's on the first

Re: [algogeeks] Google Interview Question

2010-12-24 Thread Terence
Using the same level order traversal (bottom up), calculating the minimum flips to turn each internal node to given value (0/1). On 2010-12-24 17:19, bittu wrote: Boolean tree problem: Each leaf node has a boolean value associated with it, 1 or 0. In addition, each interior node has either

Re: [algogeeks] Re: Array Ranking Problem

2010-12-24 Thread Terence
My method is using DP, as Snehal have pointed out. Suppose S[0..n-1] and T[0..n-1] denotes the score and time for the n questions respectively. C[k][s] denotes the maximum total time when choosing from the first k questions such that the total score do not exceed s. Then C[0][s] = 0

Re: [algogeeks] Re: Array Ranking Problem

2010-12-23 Thread Terence
@Ankur: It is just 0/1 knapsack problem: Choose a subset of the questions with sum of scores not exceeding (Total Score - Pass Score), while maximize the sum of time of the subset. So I do not think O(nlogn) greedy algorithm will solve this problem. On 2010-12-23 23:38, Ankur Khurana

Re: [algogeeks] bits groups

2010-12-22 Thread Terence
@Saurabh: You are right. I was supposed there were infinite 0's on the left. For 32-bit number, the MSB should also be checked in addition to LSB. Change the first line to: c=1-(N1)-((N31)1); will fix this case. On 2010-12-22 14:44, Saurabh Koar wrote: @Terence: I think the above fails

Re: [algogeeks] Re: spy in the city

2010-12-22 Thread Terence
I second Bhavesh. If A knows B, then B is not a spy; else A is not a spy. Thus we can hold an elimination races: for each pair A,B in a match, ask if A knows B, and eliminate as above. In total N-1 matches are needed to decide the final winner. If there is a spy, then the spy is the

Re: [algogeeks] bits groups

2010-12-21 Thread Terence
zero_group(N) { c=1-(N1); // For even N, zero groups is one more than 1 groups. while(N) { d = (N(-N)); // Get the least significiant bit. N = N+d; // Clear the last 1-group bits c++; // inc counter. } return c; } For more bit manipulations, refer to

Re: [algogeeks] Answer This

2010-12-17 Thread Terence
I think nobody dies on the first 19 days and everyone dies on the 20th day. (I have no difference with other green-eyes. Why I have to suicide ealier? : ) Given: a) there was at least one green-eyed among them, we could get the following conclusions: 1) If there is exactly 1 green-eye, he

Re: [algogeeks] Re: What would be the output of the following program..?

2010-12-15 Thread Terence
Just the opposite. All the operands are evaluated from left to right, and the right most operand is returned as the value of whole comma expression. But remember the operator precedence. Comma is lower than assignment. So c=--a, b++ - c is equivalent to c=--a; b++ -c; While c=(--a, b++ -c) is

Re: [algogeeks] linkd list

2010-12-15 Thread Terence
Here is an O(N^2) algorithm: 1. Sort both A and B. (O(NlogN) or O(N^2), either will work) 2. For each element C[k] in C, find if there is such (i,j) pair that A[i] + B[j] = -C[k]: Start with i = 0 and j = N-1, and loop through the following: a. if A[i] + B[j] -C[k], then j = j-1;

Re: [algogeeks] just confirming my answer

2010-12-12 Thread Terence
I think the answer is (2^m)^(2^n) (or 2^(m*2^n)) For m=1, the answer is 2^2^n. We can express the function using a truth table with 2^n entries, one entry for each possible input set. And each entry has (n+m) fields, represent the n inputs and m outputs. The m*2^n output fields can be filled

Re: [algogeeks] Valid Array

2010-12-12 Thread Terence
No. You made the same mistake as I. Try this case: {1, 2, 2, 5, 5}. Actually, this case defeats the solution of Manmeet's, yours, and mine. (same min/max, same sum, same xor result) I think the key point is that the N variable cannot be determined by 1 or 2 equation constraint. On 2010-12-10

Re: [algogeeks] Re: convert binary matrix to zero matrix

2010-12-08 Thread Terence
As Amir pointed out: convert the first row and first column to all zeros In details: 1. choose operations on first row and first column to make up-left element 0. * There are 2 cases, 2 choices for each case: 1. If the up-left element is 0, then

Re: [algogeeks] Re: Brain Teaser

2010-11-12 Thread Terence
Good point. Here is one step further: 3. Consider the center 4 numbers(regardless of order): {1,8,a,b}, containing 2 even and 2 odd. and {a,b} can be chosen from {3,4,5,6}. Then there is only one case: {1,8,3,6}. The rest is trivial. On 2010-11-12 13:26, Amod wrote: Let me try to

Re: [algogeeks] Learn

2010-11-12 Thread Terence
(((x 0x)1) | ((x 0x)1)) 5 bit operations in total. On 2010-11-12 16:12, sudhir mishra wrote: Write a program to swap odd and even bits in an unsigned integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc).

Re: [algogeeks] Re: modified divide and conquer

2010-11-08 Thread Terence
No. It is possible. This problem focuses on comparisons of each element. The overall time complexity of merge operation can be as high as O(nlogn), while the normal merge operation has time complexity O(n). But the normal merge operation does not meet the requirement, since in the worst case,

Re: [algogeeks] Re: Frequent values spoj

2010-10-22 Thread Terence
http://www.informatik.uni-ulm.de/acm/Locals/2007/output/ On 2010-10-21 18:59, ANUJ KUMAR wrote: please give me its output file also so that i can check mine On 10/21/10, ANUJ KUMARkumar.anuj...@gmail.com wrote: thanks On 10/21/10, juver++avpostni...@gmail.com wrote: Please have a look at

Re: [algogeeks] Re: plz explain output

2010-10-11 Thread Terence
Here is the modified code. Maybe we can find something new. Code: #includestdio.h int main() { union { struct { int x; float t; }; double u; } a; a.x=0; scanf(%f,a.t); printf(%f\n,a.t); printf(%f\n,a.u); a.x=90; printf(%d\n,a.x); printf(%f\n,a.x);

Re: [algogeeks] double tower of hanoi

2010-10-08 Thread Terence
On 2010-10-8 15:14, snehal jain wrote: A Double Tower of Hanoi contains 2n disks of n different sizes, two of each size. As usual, we’re required to move only one disk at a time, without putting a larger one over a smaller one. a. How many moves does it take to transfer a double tower from one

Re: [algogeeks] double tower of hanoi

2010-10-08 Thread Terence
order of all disks is preserved after 1)-7). Denote the number of moves as f(n) in case a), and g(n) in case b). We have: f(n) = 2^(n+1) - 2 and g(n) = 4f(n-1)+3. So g(n) = 4*2^n-5 On 2010-10-9 1:56, ravindra patel wrote: @Terence Solution for first one is trivial. Just double the number

Re: [algogeeks] Bitwise operator - Adobe

2010-09-25 Thread Terence
If +,-,*,/ is completely forbidden, I think at least one loop is needed. Here is my solution: int next8mult (int n) { n = (n ~7);// clear the least 3 bit to make multiple of 8 int r = 8; while(r) { // add 8 to n int r1 = (nr) 1; // get

Re: [algogeeks] Re: Amazon Question make confused !!!!!!!

2010-09-16 Thread Terence
It is true that there are infinitely many solutions (or zero solutions) (x, y, z) in any case. But what we are interested here is S=x+y+z. Apply y = S-x-z, you get: S/Vp+x(1/Vd-1/Vp)+z/(1/Vu-1/Vp) = T1 S/Vp+x(1/Vu-1/Vp)+z/(1/Vd-1/Vp) = T2 Adding the two, you get 2S/Vp +

Re: [algogeeks] Re: Amazon Question make confused !!!!!!!

2010-09-15 Thread Terence
You could also get a unique solution if the car has speed of 72 63 56 in downhill, plain and uphill respectively. I think the speed Vd, Vp, Vu was chosen so that 2Vp = Vd + Vu. But for unique solution, it ought to be 2/Vp = 1/Vd + 1/Vu. Under this condition, we can get the unique S=x+y+z:

Re: [algogeeks] Re: Amazon Question make confused !!!!!!!

2010-09-15 Thread Terence
it will take 3 hrs to climb while travelling from B to A and plain road distance = 5/3 * 64 = 106.67 km dist = 168 + 106.67 On Wed, Sep 15, 2010 at 8:21 AM, Terence technic@gmail.com mailto:technic@gmail.com wrote: You could also get a unique solution if the car has speed of 72 63 56

Re: [algogeeks] Re: Amazon Question

2010-09-15 Thread Terence
The following algorithm traversals both lists twice to find the intersection point, without modification to the original nodes. The only assumptions: 1) Head pointer of two list: La, Lb 2) .next point to the next node. 3) .next of the tail node is NULL intersect(La,Lb) { // Find the length

Re: [algogeeks] Yahoo!!!! Puzzle

2010-09-14 Thread Terence
No coding at all. 1) Take out another A pill from the bottle; 2) Split each of the 4 pills into two equal halves, and take one half of echo pill. 3) Collect the other half of the 4 pills for next time. On 2010-9-14 17:22, bittu wrote: You are on a strict medical regimen that requires you to

Re: [algogeeks] Re: ternary numbers

2010-09-01 Thread Terence
Or directly get the last digits from (-1, 0, 1) 100 = 33 * 3 + 1 33 = 11 * 3 + 0 11 = 4 * 3 + (-1) 4 = 1 * 3 + 1 1 = 0 * 3 + 1 Collect those digits together, we get 11X01_3 On 2010-8-31 23:40, Dave wrote: 352/9 = 39-1/9 = 27 + 9 + 3 + 1/9 = 1*3^3 + 1*3^2 + 1*3^1 + 0*3^0 +

Re: [algogeeks] Could anyone explain how this code works?????????!!!!

2010-09-01 Thread Terence
Simple run-length encoding of a ascii picture. On 2010-9-1 3:49, Albert wrote: #includestdio.h main() { int a,b,c; int count = 1; for (b=c=10;a=- FIGURE?, UMKC,XYZHello Folks,\ TFy!QJu ROo TNn(ROo)SLq SLq ULo+\ UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\ NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\

Re: [algogeeks] Will miracle ever print ?

2010-08-31 Thread Terence
at 10:36 PM, Yan Wang wangyanadam1...@gmail.com mailto:wangyanadam1...@gmail.com wrote: It's miracle can you explain? On Sun, Aug 29, 2010 at 8:07 PM, Terence technic@gmail.com mailto:technic@gmail.com wrote: Try this: int main() { int data = (int

Re: [algogeeks] ternary numbers

2010-08-31 Thread Terence
First convert 352. 352 = 117 * 3 + 1 = (39 * 3 + 0) * 3 + 1 = ((13 * 3 + 0) * 3 + 0) * 3 + 1 = (((4 * 3 + 1) * 3 + 0) * 3 + 0) * 3 + 1 = 1*3+1) * 3 + 1) * 3 + 0) * 3 + 0) * 3 + 1 So 352 = 111001. Noting 9 = 3^2, thus 352 / 9 = 1110.01 On 2010-8-31 7:46, Raj N

Re: [algogeeks] Re: Converting a decimal number to base -2

2010-08-30 Thread Terence
No need to handle -1 specially. 6) -2 |-1| 1 | 1| Binary : 11 On 2010-8-30 20:37, ANKIT AGGARWAL wrote: divide each number by -2 until you get -1, or 1 (Remembering that remainder is always +ve) Ex 1) -2 | 2 | 0 | -1 | Interpret -1 as 11 so binary is 110 2) -2 | 3 | 1

Re: [algogeeks] National Instruments: linked list subtraction

2010-08-26 Thread Terence
On 2010-8-25 20:25, Terence wrote: 1. Calculate the length of both list (A, B). 2. Find the position in the longer list corresponding to the head of shorter list, and copy the previous digits to output list C. (Or for simplicity, appending 0 to the shorter list so as to get the same

Re: [algogeeks] National Instruments: linked list subtraction

2010-08-26 Thread Terence
. else, keep all those digits untouched. Advance px to x' and x to the next of x'. 5. Remove the initial 0s until C begins with non-zero digits. On 2010-8-25 23:45, Raj N wrote: @Terence: It is subtraction of 2 lists and not addition. N for your logic of addition for x9 you add

Re: [algogeeks] National Instruments: linked list subtraction

2010-08-25 Thread Terence
1. Calculate the length of both list (A, B). 2. Find the position in the longer list corresponding to the head of shorter list, and copy the previous digits to output list C. (Or for simplicity, appending 0 to the shorter list so as to get the same length as the longer one) 3. Add the two

Re: [algogeeks] Re: sorting range of numbers in O(n)

2010-08-03 Thread Terence
I think count sort is not acceptable (see Ankur's post for explanation), while radix sort fits the requirement. Take the array as an array of 2-digit base n numbers. First sort by the least significant digit (x%n), then the most significant digit (x//n), both using the stable counting sort.

Re: [algogeeks] Re: Bitwise Ternary operator

2010-07-26 Thread Terence
On 2010-7-26 1:46, Tech Id wrote: int cond (int x, int y, int z) { return (x==0)*z + (x!=0)*y; } Or int cond (int x, int y, int z) { return (!x) * z + (!!x)*y; } if comparing (==/!=) is not permitted. On Jul 21, 10:29 pm, BALARUKESHsbalarukesh1...@gmail.com wrote: Ternary

Re: [algogeeks] Re: SPOJ:RRSCHED

2010-06-21 Thread Terence
Don't try to simulate the scheduler on every second. The total execution time can reach 5 * 10^9 sec. Play with small examples, get the task list in execution order, and find the cyclic property of the list. You can solve this problem in O(N) time complexity. On 2010-6-20 18:15, Shravan

Re: [algogeeks] level order traversal

2010-06-12 Thread Terence
vertical_traversal(v) { stack_init(vstack); stack_init(tstack); while(v) stack_push(vstack, v); if(v.right) stack_push(tstack, v.right); v = v.left; while(! is_empty(vstack)) visit(stack_pop(vstack)); while(! is_empty(tstack))

Re: [algogeeks] Re: Puzzle:

2010-06-11 Thread Terence
No need to enumerate all possible states. In the final state (2,8,5), each jug is neither full nor empty, while every valid operation has to fill or empty one jug. So it is not possible to get this state from any other state by one valid operation. (As others said, the state before the final

Re: [algogeeks] Re: infix

2010-06-11 Thread Terence
On 2010-6-11 13:39, BALARUKESH wrote: The increment and decrement method wont work correctly for all cases Ex:- ))a+b(( It works. In this example, the first ')' lead to -1 which indicate the expression is not well formed. This is not a well formed parenthesis... But satisfies the

Re: [algogeeks] Removing extra parentheses in an infix string

2010-06-03 Thread Terence
You can restore the infix expression from the postfix expression calculated, only add parenthesis when necessary in each step. On 2010-6-3 15:35, divya jain wrote: 1.calculte the postfix of given expression. 2.now remove a particular parenthesis from expression and check if the postfix of

Re: [algogeeks] Valid permutation of the brackets

2010-06-03 Thread Terence
Discard illegal prefix as soon as possible, and only proceed with legal one. * For some position, if the number of '(' and ')' are equal, then do not try ')' on that position. * If the number of '(' reaches n, fill the rest positions with ')' to get a valid permutation. On 2010-6-3 16:15,

Re: [algogeeks] Find the number with maximum frequency in an array. Better than O(nlogn + n) time complexity

2010-06-01 Thread Terence
This is a different problem, where the requested number with maximum frequency is not necessary majority. On 2010-6-1 13:19, harit agarwal wrote: see this .vote majority algorithm.. http://userweb.cs.utexas.edu/~moore/best-ideas/mjrty/index.html

Re: [algogeeks] Why is inorder traversal necessary to reconstruct a binary tree?

2010-04-09 Thread Terence
The simplest case is: A A / and \ BB both with preorder(and level-order) AB and postorder BA On 2010-4-9 1:23, Himanshu Aggarwal wrote: For a binary tree , if we are given an inorder traversal and a preorder/postorder/level-order

Re: [algogeeks] Divide and Conquer problem

2010-04-08 Thread Terence
Suppose n=2^k, a(0), a(1), ..., a(n-1) are the n teams 1. If there are 1 team, then no matches, 0 days needed; If there are 2 teams, arrange 1 match for them, 1day needed. 2. Split the n teams into 2 groups of equal size, ie. a(0),a(1),...,a(n/2-1) and a(n/2),a(n/2+1),...,a(n-1). 3.

Re: [algogeeks] Interview question.Provide the solution as per the constraints.

2010-03-08 Thread Terence
What is the storage structure of your BST? If each node contains pointers to its left child, right child and parent, then we can traverse through the tree without recursive or stack. in_order_traverse() { p = root; last = root-parent = NULL; while(p) { if (last == p-parent) { //

Re: [algogeeks] Re: missing integers in an unsorted array

2010-02-04 Thread Terence
No. This works for all cases. Xor is commutative. On 2010-2-5 13:37, srinivas chintagunta wrote: I think this works if elements are sorted . Is it correct? On Mon, Feb 1, 2010 at 5:07 PM, sachin sachin_mi...@yahoo.co.in mailto:sachin_mi...@yahoo.co.in wrote: A way to solve this problem

Re: [algogeeks] Merge two BST in O(n) time with O(1)

2010-01-29 Thread Terence
On 2010-1-28 21:03, Nirmal wrote: Given two binary search trees, how to merge them in O(n) time and O(1) space? It can be done using O(n) space as below, 1. covert BST #1 into linked list or sorted array 2. covert BST #2 into linked list or sorted array 3. merge them... but how to do this in

[algogeeks] Re: multiply by 2 and subtract by 1

2009-08-26 Thread Terence
The problem can break down to simple case A: A. convert a row of size c elements to all zero using only operations a, b which can further break down to substep B: B. for any two different value x, y in a row, use only operations a, b to convert to same value. Suppose x = y, perform operation a