Re: [algogeeks] How to store largest N values efficiently

2011-07-11 Thread abhijith reddy
Wouldn't a heap be ideal for this ? On Mon, Jul 11, 2011 at 3:35 PM, John Reid j.r...@mail.cryst.bbk.ac.ukwrote: I have a procedure that generates N x M values sequentially. I want to store the N largest ones and discard the others. Obviously I can store all the values in a vector, sort it

Re: [algogeeks] Re: How to store largest N values efficiently

2011-07-11 Thread abhijith reddy
You can use priority_queue in STL. The size needs to be limited to N elements. At any point the the N elements in the heap will give the largest N elements processed so far. On Mon, Jul 11, 2011 at 4:41 PM, John Reid j.r...@mail.cryst.bbk.ac.ukwrote: On 11/07/11 12:07, abhijith reddy wrote

Re: [algogeeks] GATE 2011 C Ques

2011-07-02 Thread abhijith reddy
p[3] = 'E' p[1] = 'A' p[3]-p[1] = 4 ? On Sat, Jul 2, 2011 at 7:10 PM, KK kunalkapadi...@gmail.com wrote: 10. What does the following fragment of C-program print? char c[] = GATE2011; char *p =c; printf(%s, p + p[3] - p[1]); (A) GATE2011 (B) E2011 (C) 2011 (D) 011 Answer: - (C) why is

Re: [algogeeks] Algorithmic Pioneers

2011-06-24 Thread abhijith reddy
+1 On Fri, Jun 24, 2011 at 1:06 PM, rizwan hudda rizwanhu...@gmail.com wrote: Add Donald Knuth, Rajeev Motwani, Manindra agrawal, Richard Karp, Vijay V Vazirani to the list. Peter is no doubt a great algorithm champion, and so is ACRush. But they are definetely not the pioneers of the

Re: [algogeeks] EOF in Python for SPOJ

2011-05-25 Thread abhijith reddy
while(1): try: # code # # except EOFError: break On Wed, May 25, 2011 at 8:41 PM, Vishnutej mylavarapu.vishnu...@gmail.comwrote: Hello everyone, I'm new to python.How can I check the EOF for inputs in SPOJ? Thanks in advance!! -Vishnutej -- You received this

Re: [algogeeks] EOF in Python for SPOJ

2011-05-25 Thread abhijith reddy
, 2011 at 8:42 PM, abhijith reddy abhijith200...@gmail.comwrote: while(1): try: # code # # except EOFError: break On Wed, May 25, 2011 at 8:41 PM, Vishnutej mylavarapu.vishnu...@gmail.com wrote: Hello everyone, I'm new to python.How can I check the EOF for inputs

Re: [algogeeks] Candy_splitting in GCJ

2011-05-09 Thread abhijith reddy
Let *E(n)* be the *expected* number of hits needed sort *'n'* *misplaced*numbers. The optimal strategy is to hold the numbers that are already in place and shuffle the remaining. Now after a shuffle assume that x numbers are still out of place. Hence we get the following recurrence. E(n) = Sum[

Re: [algogeeks]

2011-05-06 Thread abhijith reddy
O(N^3) DP - char str[MAX]; int dp1[MAX][MAX],dp2[MAX][MAX]; int isPalin(int low,int high) { if(low=high) return 1; if(dp1[low][high]!=-1) return dp1[low][high]; return

Re: [algogeeks]

2011-05-06 Thread abhijith reddy
possible positions between i j and take minimum of all. On Fri, May 6, 2011 at 8:23 PM, sourabh jakhar sourabhjak...@gmail.comwrote: can you explain the logic On Fri, May 6, 2011 at 8:02 PM, abhijith reddy abhijith200...@gmail.comwrote: O(N^3) DP

Re: [algogeeks] Reading Huge input from the terminal in least time.

2011-04-19 Thread abhijith reddy
You could read input character by character using getchar_unlocked() untill you hit a space or new line or EOF. Alternatively you read the whole input at once using fread_unlocked() and then process it as per your need. On Wed, Apr 20, 2011 at 7:41 AM, shubham shubh2...@gmail.com wrote: Hello

Re: [algogeeks] Re: prime number using Sieve of Eratosthenes

2011-03-21 Thread abhijith reddy
cont int MAX = 10005; bool isPrime[MAX]; void sieve() { int lim=sqrt(MAX); /* Initially Mark all numbers as Prime */ for(int i=2;iMAX;i++) isPrime[i]=1; for(int i=2;i=lim;i++) if(isPrime[i])/* for each Prime */ for(int j=2*i;jMAX;j+=i)/* Cross

Re: [algogeeks] Another maths problem

2011-03-17 Thread abhijith reddy
17^13 is odd 13*17 is odd so isn't the answer simply 2 ? On Thu, Mar 17, 2011 at 8:25 PM, saurabh singh saurab...@gmail.com wrote: Find the smallest divisor of 17^13+13*17... PS:please dont say 1 -- Saurabh Singh B.Tech (Computer Science) MNNIT ALLAHABAD -- Saurabh Singh

Re: [algogeeks] Link list Problem

2011-03-13 Thread abhijith reddy
copy data from the next node and then delete that next node. Say you need to delete 3 1 - 2 - 3 - 4 - 5 1 - 2 - 4 - 4 - 5 * Now delete node which is next to '*'. On Sun, Mar 13, 2011 at 10:23 PM, UMESH KUMAR kumar.umesh...@gmail.comwrote: hi Given a singly Link list but

Re: [algogeeks] SPOJ DP : SUMITR

2011-03-08 Thread abhijith reddy
algorithm isn't required for C++ 4.0.*. The same code will give Compile Error in C++ 4.3 and above On Tue, Mar 8, 2011 at 10:42 PM, Logic King crazy.logic.k...@gmail.comwrote: Well tried, i have got the correct answer after working on it for almost 2 hours here is my code

Re: [algogeeks] Re: Spoj Problem : Small Factorials

2011-02-04 Thread abhijith reddy
http://zobayer.blogspot.com/2010/03/small-bigint-library.html On Fri, Feb 4, 2011 at 10:24 PM, Rahul Verma rahulverma@gmail.comwrote: @jeeva Can you pls explain me in detail or some link to the tutorial of string manipulation in C++. I googled about it but most of the links are in Python

Re: [algogeeks] first larger element in unsorted array...

2011-01-30 Thread abhijith reddy
simple dp void solve(int *arr,int sz) { int ans[sz]; ans[sz-1]=-1; for(int i=sz-2;i=0;i--) { if(arr[i]arr[i+1]) ans[i]=arr[i+1]; else ans[i]=ans[i+1]; } for(int i=0;isz;i++) printf(%d ,ans[i]); } On Sun, Jan 30, 2011 at 10:00 PM, ritu

Re: [algogeeks] Re: Good Maths Question

2011-01-25 Thread abhijith reddy
I remember solving this @ spoj Here is an O(1) solution #!/usr/bin/python def solve(n): val=1 for i in range(1,9): val*=(n+i) return float((n+9)/9.0-(40320.0/val)) cases=int(raw_input()) while(cases): cases-=1 n=int(raw_input())

Re: [algogeeks] Complexity Help ??

2011-01-22 Thread abhijith reddy
O(2^n) On Sat, Jan 22, 2011 at 8:58 PM, Decipher ankurseth...@gmail.com wrote: fun(n) { if(n=2) return (1); else return ((fun(n-1)*fun(n-2)); } find the order of complexity . -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
Below is code for modular exponentation in general // (a^b)%c int modexp(int a,int b,int c) { int ans=1; while(b) { if(b1) ans=(ans*a)%c; a=(a*a)%c; b=1; } return ans; } On Fri, Jan 21, 2011 at 3:27 PM, juver++ avpostni...@gmail.com wrote: int l = 0, r = ...;

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
not to use log or power. isnt there any approach using bitwise operators On Fri, Jan 21, 2011 at 5:24 PM, Manmeet Singh mans.aus...@gmail.comwrote: this will be O(log(n) * log(n)) solution On Fri, Jan 21, 2011 at 4:29 PM, abhijith reddy abhijith200...@gmail.com wrote: Below is code

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
@snehal .. misread it .. my apologies. On Fri, Jan 21, 2011 at 5:56 PM, abhijith reddy abhijith200...@gmail.comwrote: O(lg(n)*lg(n)) is the complexity of the solution ! Not the solution. M=3^x We binary search on x and then compute 3^x in log(x) time using exponentiation. Hence

[algogeeks] Re: Google Question

2011-01-20 Thread abhijith reddy d
I think its correct. On Jan 19, 9:35 pm, nishaanth nishaant...@gmail.com wrote: How about the following dynamic programming solution. Let dp[i] be the max no of As with i keystrokes. dp[i]=max(dp[i-1]+1,2*dp[i-3]) dp[N] is the required solution. Correct me if i am wrong. On Wed, Jan

Re: [algogeeks] value of n

2010-04-30 Thread abhijith reddy
binary search on n On Fri, Apr 30, 2010 at 10:15 PM, Amit Agarwal lifea...@gmail.com wrote: how do I compute n from this equation. n 8lg(n) -- 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]

2010-03-31 Thread abhijith reddy
I guess she was asking that the per query complexity should be better that O(n). If that is the case then you can use any of these Simple RMQ O(sqrt(n)) Segment/Interval Trees O(lgn) Binary Indexed Trees O(lgn) On Wed, Mar 31, 2010 at 12:58 PM, Rohit Saraf rohit.kumar.sa...@gmail.comwrote:

Re: [algogeeks] Implementation of Algorithms

2010-03-31 Thread abhijith reddy
First learn a language of your choice and then you can start solving over there On Wed, Mar 31, 2010 at 12:05 AM, scanfile rahul08k...@gmail.com wrote: I am new to the world of programming. I have to solve the problems on the spoj.pl , but I have no idea that how I would implement the

Re: [algogeeks]

2010-03-31 Thread abhijith reddy
@All: Doing a sorting for O(n logn) and then binary search for x1 and x2 in O(logn) will be less efficient than the simple solution of O(n). Think on the data structure that can optimize it. Is it possible in time complexity O(n)? If you want to do the operation just once then it cannot

Re: [algogeeks] You have to count the occurances of all words in a document. You are given a method chat * GetNextWord, that returns the next word from the document.

2010-02-27 Thread abhijith reddy
You can use a TRIE .. Structure can be something like this struct trie { int count; // no of occurences char *child[SIZE]; }; when ever u insert ( it will take just O(length) time) .. just increment count by 1 For each query (also O(length) time) the no of occurrences of the word will

Re: [algogeeks] Re: gcd sum function

2010-01-30 Thread abhijith reddy
Yes i have .. and i have the worst time in the rank page :) On Fri, Jan 29, 2010 at 8:51 PM, fundoonick fundoon...@yahoo.co.in wrote: I tried this problem using the method of solving using eulerphi values. I calculate values of eulerphi in O(nlogn) and then use them to calculate gcdsum using

[algogeeks] gcd sum function

2010-01-26 Thread abhijith reddy
Hello Let gcdsum[n] = gcd(1,1)+gcd(1,2)+gcd(1,3)+ ... +gcd(n,n) Also gcdsum[n] can be evaluated using : gcdsum[n] = n*sum(eulerphi(d)/d) for all d | n Now to compute all gcdsum values upto n we first need to precompute all eulerphi and then use a sieve like algorithm to make a table of all

Re: [algogeeks] A MS QUESTION

2009-12-15 Thread abhijith reddy
*TRIE*: Using a trie we would get a linear time solution but i think the memory requirement would be huge .. On Mon, Dec 14, 2009 at 11:02 PM, vicky mehta...@gmail.com wrote: Given a file with a lot of words (10 million) find out the top 10% most frequently occuring words. -- You

[algogeeks] Linear time sieve

2009-11-29 Thread abhijith reddy
I heard that there's a sieve algorithm whose complexity is O(n). Can any give me the pseudo code for that ? Thank u ! -- 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

Re: [algogeeks] Sum of the Sequence

2009-11-06 Thread abhijith reddy
+ 10 + 1 = 16 On Wed, Nov 4, 2009 at 11:23 AM, abhijith reddy abhijith200...@gmail.com wrote: Is there a way to find the sum of the Kth series ( Given below) K=0 S={1,2,3,4,5,6,} K=1 S={1,2,4,7,11,16..} common diff = 1,2,3,4 5 ... K=2 S={1,2,4,8,15,26...} common diff = 1,2,4,7 11

[algogeeks] Sum of the Sequence

2009-11-05 Thread abhijith reddy
Is there a way to find the sum of the Kth series ( Given below) K=0 S={1,2,3,4,5,6,} K=1 S={1,2,4,7,11,16..} common diff = 1,2,3,4 5 ... K=2 S={1,2,4,8,15,26...} common diff = 1,2,4,7 11... (series with K=1) K=3 S={1,2,4,8,16,31...} common diff = 1,2,4,8 15... (series with K=2) Note