Re: [algogeeks] output help

2011-08-09 Thread dinesh bansal
On a little-endian machine, bit structure will be represented as:

 0x00 00 00 45

which is bit.bit4 = 0010 (2 in decimal) bit.bit3 = 0010 (2 in decimal)
bit.bit1 = 1 (1 in decimal)

since bit.bit1 exists at the rightmost position, while displaying data as
integer, compiler just repeats the same bit to all the remaining positions
making it 0x (-1). You can confirm it while setting bit.bit1 to 0

-Dinesh Bansal

On Tue, Aug 9, 2011 at 12:24 PM, Rohit Srivastava wrote:

> #include
> #include
>
> int main()
> {
> struct value
> {
> int bit1:1;
> int bit3:4;
> int bit4:4;
> }bit={1,2,2};
> printf("%d %d %d\n",bit.bit1,bit.bit3,bit.bit4);
> getche();
> return 0;
> }
>  the above code gives output : -1 2 2
> any idea why???
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Nagarro Coding Round Ques......

2011-07-25 Thread dinesh bansal
I think you can do it in O(n).

Have two pointers, one a start (char *start = &str[0]) of the string and
another at end (char *end = str[strlen(str)]). Scan the string till midway
with start moving forward and end moving backward. Compare the character at
start and end (*start == *end). If there is a mismatch, output FALSE
otherwise TRUE.
Hope it helps


On Sun, Jul 24, 2011 at 7:55 PM, UMESH KUMAR wrote:

> Using the all characters of a given String how to specify either
> a palindrome or not.
>
> Ex:-
>
> 1) String="teste"
> After arrange all character we can made a palindrome String as "teset"
> So output is TRUE.
>
> 2)String="hello"
> we can not made a palindrome String
> output is FALSE
>
> bool IspalindromePossible (String str)
> {
> }
>
>
> Thanks
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Address of var

2011-07-25 Thread dinesh bansal
User space applications does not have access to physical address. Its just
the Virtual Address gets printed.

On Mon, Jul 25, 2011 at 7:38 PM, Mani Bharathi wrote:

> int i=5;
> printf("%u",&i);
> What it will print:
>
>1. 5
>2. Base address of the memory
>3. Physical address
>4. Logical address
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/algogeeks/-/78m9TSEzaNAJ.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Nagarro Coding Round Ques......

2011-07-25 Thread dinesh bansal
Can you please elaborate on this?

I think what you said is the neccessary condition for palindroms but not the
sufficient. What I mean is with these checks, you can rule out the
non-palindrom strings quickly but to make sure that a particular string is
palindrom, you will need to have some more checks.

On Sun, Jul 24, 2011 at 8:11 PM, sasi kumar  wrote:

> On 24 July 2011 20:04, vaibhav shukla  wrote:
> > generate all possible permutations and check each permutation whether it
> is
> > palindrome or not.
>
>
>  The time complexity will be high. Instead find the length of the
> string . If it is even check the number of occurences
> of every character . If a character occurs for an odd number of time
> then it will not be a palindrome. Check for even number of
> occurences of every character.  If the length of the string is odd .
> One character must occur odd number of time .
>
>  So the problem can be solved just by counting
>
> Regards
> Sasi kumar T
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] reverse a line.

2011-07-21 Thread dinesh bansal
Then is need to be done at the cost of performance.

1. First scan the input string till midway and swap the first character with
last and second character with second last and so on.
2. Second scan, swap the word within string the same way.




On Thu, Jul 21, 2011 at 5:36 PM, Saurabh  wrote:

> ya it will work but if i dont want any extra space,I have to reverse it
> with in that char buffer then how can i it be done..
>
>
> On Thu, Jul 21, 2011 at 5:17 PM,  wrote:
>
>> **Push each word into the stack and pop it out at the end of the line.
>>
>> Sent from BlackBerry® on Airtel
>> --
>> *From: *Saurabh 
>> *Sender: *algogeeks@googlegroups.com
>> *Date: *Thu, 21 Jul 2011 17:15:29 +0530
>> *To: *
>> *ReplyTo: *algogeeks@googlegroups.com
>> *Subject: *[algogeeks] reverse a line.
>>
>> what is the best way to reverse a line word by word in c or c++???
>>
>>
>> ex - my name is john
>> Then after reversing it will be like - john is name my
>>
>>
>>
>>
>>
>>
>>
>> Regards
>> Saurabh
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Regards  n Luv
> Saurabh Badhai
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] output

2011-07-21 Thread dinesh bansal
Value which is changed inside a function will not be reflected after call.
So you are setting b as pointer to class a but this value will not retain
after function call in main().



On Thu, Jul 21, 2011 at 5:40 PM, Saurabh  wrote:

> Can any one explain why the following program not giving the correct
> output.
>
> #include 
> using namespace std;
> class a
> {
>int x;
>
>public:
>void set(int y)
>{
>x=y;
>}
>int get()
>{
>   return x;
>}
>
> };
> void f(a * b)
> {
>  b = new a();
>  b->set(5);
> }
> int main()
> {
>  a *a1;
>  f(a1);
>  cout<<"x = "<get();
> return 0;
> }
>
>
>
> --
> Regards
> Saurabh
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Adding w/o +

2011-07-13 Thread dinesh bansal
never mind...thanks.

On Wed, Jul 13, 2011 at 2:29 PM, dinesh bansal  wrote:

> Can you explain how its working?
>
> On Wed, Jul 13, 2011 at 1:58 PM, Anika Jain 
> wrote:
>  > a better idea is use this:
> >
> > int a=9,b=4;
> > int sum=printf("%*s%*s",a,"",b,"");
> > printf("%d\n",sum);
> >
> >
> > On Wed, Jul 13, 2011 at 1:52 PM, nicks 
> wrote:
> >>
> >> if someone has better idea...then please suggest that.
> >> plz explain how this is calculating sum -->>  sum= (int)&p[b];
> >>
> >> On Wed, Jul 13, 2011 at 1:50 PM, nicks 
> wrote:
> >>>
> >>> I am looking for a code which can add without using + sign
> >>> i searched the net and found the following code..can anyone explain
> >>> me whats happening in this ??
> >>>
> >>> #include
> >>> #include
> >>> int main()
> >>> {
> >>> int a=3000,b=20,sum;
> >>> char *p;
> >>> p=(char *)a;
> >>> sum= (int)&p[b]; //adding a & b
> >>> printf("Answer is :");
> >>> printf("%d",sum);
> >>> return 0;
> >>> }
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to algogeeks@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> algogeeks+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >> http://groups.google.com/group/algogeeks?hl=en.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algogeeks@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> > http://groups.google.com/group/algogeeks?hl=en.
> >
>
>
>
>  --
> Dinesh Bansal
> The Law of Win says, "Let's not do it your way or my way; let's do it
> the best way."
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Reversing the order of words in String

2011-07-13 Thread dinesh bansal
Hi Guys,

I have a O(n) solution for this problem:

/*
* Input: "my name is ram"
* Output: "ram is name my"
* Approach: Divide the string into tokens and store the token pointers on a
* stack. Pull them up to display output.
* */
#include 
#define MAX_WORDS_PER_STRING 100
#define MAX_STRING_LENGTH 512
int stack[MAX_WORDS_PER_STRING]; // maximum 100 words
int top; // stack top pointer
void push(int Item)
{
   stack[top++] = Item;
}

int pop()
{
   return stack[--top];
}

int main()
{
   char InStr[MAX_STRING_LENGTH] = "my name is ram";
   int ii = 0, NewWord = 1, len = 0;
   //printf("Input string");
   //scanf("%s",InStr);
   len = strlen(InStr);
   while(ii < len) {
   if(InStr[ii] == ' ') {
   /* replace black space with '\0' */
   InStr[ii] = '\0';
   if(InStr[ii+1] != ' ') NewWord = 1;
   } else {
   if(NewWord) {
   push(ii);
   NewWord = 0;
   }
   }
   ii++;
   }
   /* Now print the words from stack */
   while(1) {
   ii = pop();
   printf("%s ",(char *)&InStr[ii]);
   if(ii == 0) break;
   }
}

 -Dinesh Bansal

On Wed, Jul 6, 2011 at 9:49 PM, Navneet Gupta  wrote:
> I think somebody on this thread has asked this question but i am not
> able to find that.
>
> Question was if a string is like "my name is ram", then output should
> be "ram is name my".
>
> Wrote the code for same, so sharing.
>
> #include
> #include
> using namespace std;
>
> void SwapStringChars(string &str, int pos1, int pos2)
> {
>char ch = str[pos1];
>str[pos1] = str[pos2];
>str[pos2] = ch;
> }
>
> void reverseString(string &str, int left, int right)
> {
>for(int i = left ; i <= left + (right-left)/2 ; i++)
>SwapStringChars(str, i, right + left -i));
> }
>
> void reverseWordsInString(string &str)
> {
>char space = ' ';
>int len = str.length();
>int startIndex = 0, endIndex = 0;
>while(endIndex < len - 1)
>{
>while(str[endIndex] != space && endIndex < len)endIndex++;
>reverseString(str, startIndex, endIndex-1);
>startIndex = endIndex;
>while(str[startIndex] == space)startIndex++;
>endIndex = startIndex;
>}
> }
>
> int main()
> {
>string str;
>cout<<"\nEnter enter the string :";
>getline(cin,str);
>
>//Reverse whole string at once
>reverseString(str, 0, str.length() - 1);
>
>//Reverse Individual words in string
>reverseWordsInString(str);
>cout<cin.get();
>return 0;
> }
>
> --
> Regards,
> Navneet
>
> --
> You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.
>
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Adding w/o +

2011-07-13 Thread dinesh bansal
Can you explain how its working?

On Wed, Jul 13, 2011 at 1:58 PM, Anika Jain  wrote:
> a better idea is use this:
>
> int a=9,b=4;
> int sum=printf("%*s%*s",a,"",b,"");
> printf("%d\n",sum);
>
>
> On Wed, Jul 13, 2011 at 1:52 PM, nicks  wrote:
>>
>> if someone has better idea...then please suggest that.
>> plz explain how this is calculating sum -->>  sum= (int)&p[b];
>>
>> On Wed, Jul 13, 2011 at 1:50 PM, nicks  wrote:
>>>
>>> I am looking for a code which can add without using + sign
>>> i searched the net and found the following code..can anyone explain
>>> me whats happening in this ??
>>>
>>> #include
>>> #include
>>> int main()
>>> {
>>> int a=3000,b=20,sum;
>>> char *p;
>>> p=(char *)a;
>>> sum= (int)&p[b]; //adding a & b
>>> printf("Answer is :");
>>> printf("%d",sum);
>>> return 0;
>>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Adding w/o +

2011-07-13 Thread dinesh bansal
Its basically addition using pointer arithmatics. check for array indexing.

On Wed, Jul 13, 2011 at 1:50 PM, nicks  wrote:
> I am looking for a code which can add without using + sign
> i searched the net and found the following code..can anyone explain me
> whats happening in this ??
>
> #include
> #include
> int main()
> {
> int a=3000,b=20,sum;
> char *p;
> p=(char *)a;
> sum= (int)&p[b]; //adding a & b
> printf("Answer is :");
> printf("%d",sum);
> return 0;
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] remove duplicate chars in a string without using extra memory

2011-07-11 Thread dinesh bansal
On Sat, May 28, 2011 at 11:32 AM, Rajeev Kumar  wrote:
> Design an algorithm and write code to remove the duplicate characters in a
> string without using any additional buffer.
>  NOTE: One or two additional variables are fine.
>  An extra copy of the array is not.
>
> --
> Thank You
> Rajeev Kumar
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

I have a solution. Please check whether is fits the requirement:

#include
#include
#include

int main()
{
char input[256], ele;
int i, j, size;
printf("Enter input string::");
scanf("%s",input);
size = strlen(input);
for(i = 0; i< size; i++) {
if(input[i])
ele = input[i];
else
continue;
for(j=i+1; j < size; j++) {
input[j] ^= ele;
if(input[j]) {
input[j] ^= ele;
    }
    }
}
for(i=0; i < size;i++)
printf("%c",input[i]);
}


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] priority inversion

2011-06-21 Thread dinesh bansal
Priority inversion happens when a higher priority task is help blocked
by a lesser priority task.

Suppose a system has a low priority task L, a high priority task H and
few medium priority tasks. Now resource R has been locked by L and
goes to sleep due to other medium priority tasks coming in. Since
medium priority tasks does not need R, they can interrupt L. Now if H
also need the same resource R, it has to wait until L is done with it.
So H gets blocked. This is called priority inversion.

-Dinesh Bansal

On Mon, Jun 20, 2011 at 10:35 PM, ricky  wrote:
> In priority inversion the high priority process has to wait for the
> low priority process. why can't it just preempt the low priority one
> instead of waiting?  Is it becoz it will jeopardize system stability
> or something else?
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to 
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/algogeeks?hl=en.
>
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Reverse the bits.

2011-06-11 Thread dinesh bansal
Thanks Guys I got it.

@balaji... you are right.. it will work just fine.

-Dinesh Bansal

On Fri, Jun 10, 2011 at 10:22 PM, Vetri Balaji  wrote:
> int flip(int j,int k,int n)
> {
>   int t1=(1<   int t2=(1<   t1=t2^t1;
> return n^t1;
> }
> correct me if im wrong
>
> On Fri, Jun 10, 2011 at 10:09 PM, Kunal Patil  wrote:
>>
>> How about this???
>>
>> unsigned int flip_j_to_k_bits (unsigned int n,unsigned int j,unsigned int
>> k)
>> {
>>  unsigned int temp;
>>  int num_of_on_bits = k-j+1;
>>
>>  temp = (1<>  temp <<= j;
>>
>>  return (n^temp);
>> }
>>
>> I dont know whether shift operation is O(1) or not !
>> But i think this is the best possible that can be done !!!
>>
>> On Fri, Jun 10, 2011 at 8:40 PM, dinesh bansal 
>> wrote:
>>>
>>> How do you reverse the bits between j to k in a 32 bit integer.
>>>
>>> For e.g.:
>>>
>>> n = 11100011;  j = 2 and k =  5
>>> output: 1101  (bits from 2 to 5 are reversed.)
>>>
>>> n = 11010110; j = 1 and k = 5
>>> output: 11101000
>>>
>>> O(1) method is preferred.
>>> Thanks,
>>> --
>>> Dinesh Bansal
>>> The Law of Win says, "Let's not do it your way or my way; let's do it
>>> the best way."
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Reverse the bits.

2011-06-10 Thread dinesh bansal
How do you reverse the bits between j to k in a 32 bit integer.

For e.g.:

n = 11100011;  j = 2 and k =  5
output: 1101  (bits from 2 to 5 are reversed.)

n = 11010110; j = 1 and k = 5
output: 11101000

O(1) method is preferred.
Thanks,
-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it
the best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Algorithm for dynamic resource allocation

2011-05-05 Thread dinesh bansal
yes, something like that.

My approach would have been

priority = high,  weight = 4 (assume)
priority = medium,  weight = 2
priority = low,  weight = 1

now from you example, lets say number of consumers:

Priority|  No of Consumers
---
High  |   8
Medium |   5
Low   |   2

Calculate total weight = 8*4+5*2+2*1 = 44

Now assign resource based on priority:

Priority = high, Resource = 4 * 100 / 44

Priority = medium, Resource = 2 *  100 / 44

Priority= low, Resource = 1 * 100 / 44

Hope it makes more clear.

-Dinesh Bansal

On Thu, May 5, 2011 at 12:28 PM, LiveShell  wrote:

> Dinesh,
>
> something like 
>
> Weight of
> Low  = 0.2 (High)
> Medium = 0.3 (High)
>
> and calculate the W of High and assign resource to all H, M , L
> accordingly am i on ryt way ???
>
> or is there any other proven method to address this problem ???
>
>
>
>
>
>
>
> On May 5, 11:39 am, dinesh bansal  wrote:
> > You can assign weight to all the consumers based on priority and then
> divide
> > all the resource based on weight.
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > On Wed, May 4, 2011 at 6:13 PM, LiveShell  wrote:
> > > Hi all,
> >
> > > I am working on dynamic resource allocation problem. I have set of
> > > Consumer and resource. Consumers are with priority (High, Medium,
> > > Low). Now I want to assign resource slice to each consumer based on
> > > priority and no of consumers.
> >
> > > ie
> >
> > > Priority|  No of Consumers
> > > ---
> > > High  |   8
> > > Medium   |5
> > > Low   |2
> >
> > > OR
> >
> > > Priority|  No of Consumers
> > > ---
> > > High  |   1
> > > Medium   |2
> > > Low   |10
> >
> > > My assumption is resource is 100 % and how can I assign slice of
> > > resource to each Consumer based on priority.
> >
> > > Can any body give me idea which algorithm I should use.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Algorithm Geeks" group.
> > > To post to this group, send email to algogeeks@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > algogeeks+unsubscr...@googlegroups.com.
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.
> >
> > --
> > Dinesh Bansal
> > The Law of Win says, "Let's not do it your way or my way; let's do it the
> > best way."
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Algorithm for dynamic resource allocation

2011-05-04 Thread dinesh bansal
You can assign weight to all the consumers based on priority and then divide
all the resource based on weight.


On Wed, May 4, 2011 at 6:13 PM, LiveShell  wrote:

> Hi all,
>
> I am working on dynamic resource allocation problem. I have set of
> Consumer and resource. Consumers are with priority (High, Medium,
> Low). Now I want to assign resource slice to each consumer based on
> priority and no of consumers.
>
> ie
>
>
> Priority|  No of Consumers
> ---
> High  |   8
> Medium   |5
> Low   |2
>
> OR
>
>
> Priority|  No of Consumers
> ---
> High  |   1
> Medium   |2
> Low   |10
>
>
> My assumption is resource is 100 % and how can I assign slice of
> resource to each Consumer based on priority.
>
> Can any body give me idea which algorithm I should use.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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

2011-02-15 Thread dinesh bansal
got it clear.. thanks guys.

On Tue, Feb 15, 2011 at 1:46 PM, jagannath prasad das
wrote:

> here start is pointing to string constant which is created in read-only
> memory which can't be changed.so modifying it in the for loop causes
> segmentation fault
> declare it as a array type
>
>
> On Tue, Feb 15, 2011 at 12:36 PM, rahul  wrote:
>
>> char *start is const char *start, pointer to const char, u can't
>> derefernce it and change it.
>> take a char start[256].try this.
>>
>>
>> On Tue, Feb 15, 2011 at 12:31 PM, dinesh bansal wrote:
>>
>>> Hi All,
>>>
>>> Can you please point me to the error in the following function. It gives
>>> segmentation fault.
>>>
>>> int main()
>>> {
>>> char *start = "12.12.12.12.12976665176069214";
>>> char *hex_buf = "65003a0a";
>>> unsigned short i, j = 0;
>>> int new_len2 = strlen(hex_buf);
>>> for (i = 0; ((j+1) < new_len2); i+=2,j+=2)
>>> {
>>> start[i] = hex_buf[j];
>>> start[i+1] = hex_buf[j+1];
>>> }
>>> }
>>>
>>> Thanks,
>>> --
>>> Dinesh Bansal
>>> The Law of Win says, "Let's not do it your way or my way; let's do it the
>>> best way."
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] What is the error in the following function

2011-02-14 Thread dinesh bansal
Hi All,

Can you please point me to the error in the following function. It gives
segmentation fault.

int main()
{
char *start = "12.12.12.12.12976665176069214";
char *hex_buf = "65003a0a";
unsigned short i, j = 0;
int new_len2 = strlen(hex_buf);
for (i = 0; ((j+1) < new_len2); i+=2,j+=2)
{
start[i] = hex_buf[j];
start[i+1] = hex_buf[j+1];
}
}

Thanks,
-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] amazon c questn

2011-02-13 Thread dinesh bansal
On Tue, Jan 11, 2011 at 10:14 PM, snehal jain  wrote:

> what is the wrong in the program?
>
> main()
> {
> char *p,*q;
> p=(char *)malloc(25);
>
Check for p against NULL.

> q=(char *)malloc(25);
>
Check for p against NULL.

> strcpy(p,"amazon");
> strcpy(q,"hyd");
> strcat(p,q);
>
compilation error

> printf("%s"p);
>
Free allocated memory.

> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] ThreeListSum

2011-01-11 Thread dinesh bansal
I think for unsorted lists, it will be O(n^3).


On Tue, Jan 11, 2011 at 1:26 PM, juver++  wrote:

> There is no need to sort first two arrays.
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Single linked list questions.

2011-01-06 Thread dinesh bansal
On Thu, Jan 6, 2011 at 5:25 PM, juver++  wrote:

> 1. Recursive function. Print node's element after processing next link of
> the current node. Also this can be achieved using stack.
> 2. Please clarify the question.
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

Hi juver++,

Ya.. recurrsive function call was a good idea.. thanks.
Regrading second question, there are two SLLs L1 and L2, at some node both
lists nodes point to the same node and create single linked list.

hope I am clear.

-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Single linked list questions.

2011-01-06 Thread dinesh bansal
Hi Guys,

There are some questions asked to me:

1. How do you print the SLL in reverse order. List should not be changed.
2. Two SLLs are merging at one point, how can you find out efficiently.

Thanks
-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] SUN Microsystem Question

2010-12-24 Thread dinesh bansal
On Fri, Dec 24, 2010 at 3:02 PM, bittu  wrote:

> You Have File of Containing 1 Million Integers You need To Find 10
> Maximum Integer Out of Them.How You Will Do That ...what is Time &
> space Complexcity of Algorithm that you will usethen optrmize the
> solution..
>
> Constraints- U can't Store Whole File in memory @ one time e.g. if u
> will do that gigabyt eof memory may be required so that should be
> avoided.
>
>
> Regards
> Shashank Mani Narayan
> Birla Instute of Technology,Mesra
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
Use External sorting.
Divide the file data into smaller chunks. Sort the chunks of data one at a
time and save them back to file.
Then get top values from the chunks and sort them again till you get top 10
values.

-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Adobe Interview Question

2010-12-21 Thread dinesh bansal
If we rewrite question in terms of Probability, call to foo2() depends on
two events:
1. (E1) A > B, probablity 75%.
2. (E2) C < D, again probability 75%.

Probability (E) = Prob(E1) * Prob(E2) = 75/100 * 75/100 * 5000 = 2812.50
times.

Correct me if wrong.
- Dinesh Bansal

On Wed, Dec 15, 2010 at 12:36 AM, bittu  wrote:

>
> void foo1()
> {
>  if(AThen {_/* */}
>  else
>   if(C then foo2()
> }
>
> How many time foo2() would get called given
> A 5000 times
>
> although i had diff...solution..but i wants to confirm wid others..so
> hav a look
>
> Regards
> Shashank Mani
> BIT Mesra
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] help me find a solution

2010-12-09 Thread dinesh bansal
On Thu, Dec 9, 2010 at 1:19 PM, ankit sablok  wrote:

> can anyone suggest me how to go about this problem i m finding it
> quite tough
>
> http://www.codechef.com/problems/TEAMSEL/
>
> thanx in advance
>
> Its is the problem similar to minimum difference subarray problem. This can
be solved using dynamic programming.

Basically you need to divide an array into two subarray keeping their sum's
difference minimum.

> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] plz explain output

2010-10-13 Thread dinesh bansal
check http://en.wikipedia.org/wiki/NaN

On Sat, Oct 9, 2010 at 6:15 PM, ankush  wrote:

> #include
> void main()
> {
>int x;
>float t;
>scanf("%f",&t);
>printf("%f\n",t);
>x=90;
>printf("%f\n",x);
>{
>x=1;
>printf("%f\n",x);
>{
>x=30;
>printf("%d\n",x);
>}
>printf("%f\n",x);
>}
>x=9;
>printf("%f\n",x);
> }
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: find duplicate and missing element

2010-09-01 Thread dinesh bansal
On Wed, Sep 1, 2010 at 11:08 AM, Dhritiman Das wrote:

>
> Given a array, A[1..n], do the following.
> Start from i=1 and try placing each number in its correct position in the
> array.
> If the number is already in its correct position, go ahead. if (A[i] == i)
> , i++
> else if the number was already restored to its correct position, then it is
> a duplicate , so remember it and move ahead if (A[A[i]] == A[i]), dup =
> A[i], i++ ;
> else
>   swap the elements at the current index i and that at A[i]'s correct
> position in the array.
> continue this until all numbers are placed in their correct position in the
> array
> Finally, A[missing] will be dup.
> O(n)
>
>
*...@dharitiman, good solution man. No expensive computation, no extra memory
required. Only problem is it will change the input array to sorted order
which may not be desired. *


> On Wed, Sep 1, 2010 at 7:14 AM, Dave  wrote:
>
>> Suppose that x is duplicated and y is omitted. Then the sum of the
>> numbers would be 5050 + x - y. Similarly, the sums of the squares of
>> the numbers would be 338,350 + x^2 - y^2. Calculate the sum and sum of
>> squares of the numbers and solve the resulting equations for x and y.
>>
>> Dave
>>
>> On Aug 31, 1:57 pm, Raj Jagvanshi  wrote:
>> >   There is an array having distinct 100 elements from 1 to 100
>> >  but by mistake some no is duplicated and a no is missed.
>> >  Find the duplicate no and missing no.
>> >
>> > Thanks
>> > Raj Jagvanshi
>>
>> --
>> 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 more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>  --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: find duplicate and missing element

2010-09-01 Thread dinesh bansal
can you explain on your algorithm. Are you using any extra array.

On Wed, Sep 1, 2010 at 11:08 AM, Dhritiman Das wrote:

>
> Given a array, A[1..n], do the following.
> Start from i=1 and try placing each number in its correct position in the
> array.
> If the number is already in its correct position, go ahead. if (A[i] == i)
> , i++
> else if the number was already restored to its correct position, then it is
> a duplicate , so remember it and move ahead if (A[A[i]] == A[i]), dup =
> A[i], i++ ;
> else
>   swap the elements at the current index i and that at A[i]'s correct
> position in the array.
> continue this until all numbers are placed in their correct position in the
> array
> Finally, A[missing] will be dup.
> O(n)
>
>
>
> On Wed, Sep 1, 2010 at 7:14 AM, Dave  wrote:
>
>> Suppose that x is duplicated and y is omitted. Then the sum of the
>> numbers would be 5050 + x - y. Similarly, the sums of the squares of
>> the numbers would be 338,350 + x^2 - y^2. Calculate the sum and sum of
>> squares of the numbers and solve the resulting equations for x and y.
>>
>> Dave
>>
>> On Aug 31, 1:57 pm, Raj Jagvanshi  wrote:
>> >   There is an array having distinct 100 elements from 1 to 100
>> >  but by mistake some no is duplicated and a no is missed.
>> >  Find the duplicate no and missing no.
>> >
>> > Thanks
>> > Raj Jagvanshi
>>
>> --
>> 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 more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>  --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Find the duplicate

2010-08-05 Thread dinesh bansal
If I understand the question correctly... there is an array of size n which
has n/2 distinct elements and one element is repeated n/2 times.

For e.g.:
   n = 4:   1 2 3 3
   n = 61 2 3 4 4 4
   n = 81 2 3 4 5 5 5 5

So now this problem can be reduced to finding the first duplicate element in
the array because remaining other elements will be unique. I think this can
be done in linear time.


On Thu, Aug 5, 2010 at 7:06 PM, AlgoBoy  wrote:

> an array in which n/2 elements are unique...and the remaning n/2 have
> the same elements but reapeated n/2 times. can anyone suggest a linear
> solution with constant space/...
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] All about references

2010-08-03 Thread dinesh bansal
Yes, both the cases are true because in both the cases memory is allocated
outside the functions internal memory. But just want to know why do you need
to return reference of a static variable.

On Tue, Aug 3, 2010 at 7:38 PM, Raj N  wrote:

> 1) Can we return a reference to an internal static variable in a
> function ?
> 2) Can we return a reference to a dynamic variable variable in a
> function ?
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Sort the data from a big file.

2009-12-21 Thread dinesh bansal
On Mon, Dec 21, 2009 at 6:47 PM, Linus Probert wrote:

> If the numbers are unique you could use a bitmap-sort this way you could
> easily read just parts of the file at a time.
>
> If they aren't unique it gets a bit trickier.
>
> /L
>
> dinesh bansal wrote:
> > Hi All,
> >
> > Suppose I have a big file (~100M) containing integer data. I want to sort
> > this file. The problem is I don't want to load the complete file data
> into
> > main memory in one shot. I mean I can read the file in batches and sort
> the
> > batch and save it in another file but cannot store the entire file
> contents
> > in main memory. Can somebody help me with algorithm or pseudo code?
> >
> > Thanks in advance.
> >
>
> --
>
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
>
Hi Linus,

Thanks for the reply. But yes we cannot guarantee that data value are
unique.

-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




[algogeeks] Sort the data from a big file.

2009-12-21 Thread dinesh bansal
Hi All,

Suppose I have a big file (~100M) containing integer data. I want to sort
this file. The problem is I don't want to load the complete file data into
main memory in one shot. I mean I can read the file in batches and sort the
batch and save it in another file but cannot store the entire file contents
in main memory. Can somebody help me with algorithm or pseudo code?

Thanks in advance.
-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




Re: [algogeeks] Re: Can you guys help me how to approach this problem !!!

2009-12-06 Thread dinesh bansal
I think this problem can be reduced to:

For a particular element in Superblock, find all the subblocks which
contains next element or the NULL. For example, take first element i.e. 7,
we need to find all the blocks which has 7 and has 3 or NULL as its next
element in the subblock.

Now suppose in first iteration, we found n1 subblocks which has either 7 or
7 and 3 (consecutively), If the next element is NULL, we include this
subblock in final count otherwise we keep these subblocks for next iteration
because subblock which are bad for first element are not good for other
elements also.

Thanks,

On Sun, Dec 6, 2009 at 9:11 AM, Aditya Shankar  wrote:

> Hi,
>Consider the sequence 1,2,3,4,5,6...,n. There are n^2 blocks, so the
> question may not be correct.
>
>
> Regards
> Aditya Shankar
>
> --
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




Re: [algogeeks] Print binary tree in spiral

2009-11-19 Thread dinesh bansal
On Wed, Nov 18, 2009 at 8:05 AM, Nayn  wrote:

> Hi guys,
> Recently I came across a problem. We've to display a binary tree in
> spiral.
> 1. We need to print the nodes in BFS manner.
> 2. The nodes should be displayed in alternate direction; in one level
> from left to right and in next level right to left.
> Needless to mention, we need least time complex solution.
> Thanks
> Nayn
>
> --
>
> 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 more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=.
>
>
> If its just about display, you can traverse the tree in BFS manner and
store the nodes in an array at their specific locations. At the end, display
the nodes from the array.

Thanks,
-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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 from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=.




Re: [algogeeks] Re: aai + bj + ck =0

2009-11-02 Thread dinesh bansal
I think this algorithm is O(N^3) because in a worst case when condition
"if(ai + bj + ck > 0)" fail for all the elements in C, the inner loop will
run for N^3 times since we have not incremented j yet.

lets take a very  rough example , A = {1,2,3,4,5}, B = {2,3,4,5,6}, C =
{7,6,5,4,3}, the condition check "if(ai + bj + ck == 0)" will execute for
5^3 times.

Thanks,

On Sun, Nov 1, 2009 at 2:04 PM, daizi sheng  wrote:

> In the inner loop, either ck get decreased, or bj get increased,
> but you know there are at moast n possible ck and n possible bj, so
> the complexity
> of the inner loop is at most O(n), plus the outer loop, the whole
> complexity
> is O(n^2)
>
> On Sun, Nov 1, 2009 at 4:32 PM, Arun  wrote:
> > This is O(n^3) because of the goto statement (effectively you have
> replaced
> > a loop with goto :) I think.
> > Instead of neightbor in C, you can do a binary search.
> > This is what I could think of but it doesnt meet the problem's requiremen
> -
> > O(nlgn):
> > 1) O(n^2logn) with O(1) space:)
> > Sort all 3 arrays. For each pair of a[i],b[j], binary search for
> > (0-(a[i]+b[j])) in C to see if its present.
> > 2) O(n^2) with O(n) space
> > Same as above, but instead of binary search of 0-(a[1]+b[j]) on C, you
> put
> > all elements of C in a hash.
> > There must be some variation of merge sort to do it in nlgn, but Im not
> able
> > to see it :)
> > On Sun, Nov 1, 2009 at 12:39 AM, daizi sheng 
> wrote:
> >>
> >> with all arrays sorted firstly, if you enumerate ai, bj in ascedning
> >> order,
> >> ck will be sure in descending order.
> >>
> >> foreach(ai in A)
> >>  ck = largest element in C
> >>  foreach(bj in B)
> >>AGAIN:
> >>  if(ai + bj + ck == 0) algorithm over
> >>  if(ai + bj + ck > 0) ck change to its neighbor in C and goto AGAIN
> >>  if(ai + bj + ck < 0) continue checking next bj
> >>
> >>
> >> On Sun, Nov 1, 2009 at 3:10 PM, anilkumarmyla 
> >> wrote:
> >> > No matter whatever i could think of, I am unable to do better than N^3
> >> >
> >> > @daizi sheng
> >> > I don't get your algorithm
> >> > "2. enumerate ai, bj both in ascending order,"
> >> > Will that really help ? In what 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.
> >> > For more options, visit this group at
> >> > http://groups.google.com/group/algogeeks?hl=en.
> >> >
> >>
> >> --
> >>
> >> 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.
> >> For more options, visit this group at
> >> http://groups.google.com/group/algogeeks?hl=en.
> >>
> >>
> >
> > --
> >
> > 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.
> > For more options, visit this group at
> > http://groups.google.com/group/algogeeks?hl=en.
> >
>
> --
>
> 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.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best 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.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.




[algogeeks] find minimum number of races

2009-09-11 Thread dinesh bansal
Hi All,

I got a question:
I have 25 horses and I need to find the 1st, 2nd and 3rd fastest among all.
I have a race course of 5 tracks. That means I can run 5 horses at a time.
What are the minimum number of races required for this.

Thanks,

-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~--~~~~--~~--~--~---



[algogeeks] Re: minimum difference.

2009-09-02 Thread dinesh bansal
While sorting itself, algorithm can keep track of minimum difference between
two consecutive elements found so far and the elements. This way time
complexity is same as time complexity of sorting algorithm.

Space complexity also I think its O(1).



On Wed, Sep 2, 2009 at 6:00 PM, Ralph Boland  wrote:

>
>
>
> On Sep 1, 7:05 am, ankur aggarwal  wrote:
> > given  a array of length n. find  2 number such that their differnce is
> > minimum.
>
> As already pointed out sorting and then comparing adjacent elements
> gives you an O(n log n) algorithm.
> If you have a likelyhood of duplicates then heapsort might be better
> since the heap can be built in linear time and the sort phase can be
> aborted if a duplicate element is found.  Alternatively one could
> put your elements into a set (using hashing) and stop immediately
> if you discover a duplicate by adding an element to the set that is
> already there (O(n) expected time to build the set).
>
> Ralph Boland
> >
>


-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~--~~~~--~~--~--~---



[algogeeks] algorithm to find Anagrams

2009-01-21 Thread dinesh bansal
Hi All,

Can anyone provide me a good program/algorithm to find all anagrams for a
given word.
Input string should be of variable length (max 26 char). Just printing is
not enough,
we need to store the output to a file or static memory somewhere.

Thanks in advance.

-- 
Dinesh Bansal
The Law of Win says, "Let's not do it your way or my way; let's do it the
best way."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~--~~~~--~~--~--~---