use a dictionary (available in C#... basically a collection of generic
key-value pairs where the key lookup is O(1) - hashed internally)

since "number that occurred first should be listed first" when
frequencies are the same, u need to record the first occurrence of
each number as well. Hence, the value could be a pair of integers, one
for frequency, one for first occurrence)

time complexity is O(n log n) for sorting.. space, thrice the size of
input (key, freq, first occurrence)

On Dec 14, 11:38 pm, Saurabh Koar <saurabhkoar...@gmail.com> wrote:
> @Bittu:
>
> for(i=size-1;i>=0;i--)
>    {
>                      for(int j=0;j<count[i];j++)
>                      {
>                        array[pos]=i;
>                        pos++;
>                      }
>    }
>
> explain this part.
> You hv compared j<count[i].At first i=size-1(=8).Bt only memory space
> for 5 integers is allocated in count via malloc.That means count[8] is
> undefined.
>
> Instead of code u plz explain the algorithm.It will be more understandable.
>
> On 12/15/10, Ankur Murarka <ankur.murarka....@gmail.com> wrote:
>
>
>
>
>
>
>
> > I think ankur khanna's solution is appropriate. couldn't get what bittu was
> > trying to do completely.. could you just explain it once please!
>
> > On Wed, Dec 15, 2010 at 10:35 AM, Soumya Prasad Ukil
> > <ukil.sou...@gmail.com>wrote:
>
> >> bittu, in stead of writing your code, put your logic here. It is not the
> >> place to show how capable one is to write a program.
>
> >> On 14 December 2010 11:00, bittu <shashank7andr...@gmail.com> wrote:
>
> >>> #include <stdlib.h>
> >>> #include<stdio.h>
> >>> #include<conio.h>
>
> >>> int main()
> >>> {
> >>>    int array[]={1,3,3,1,2,3,5,2,3};
> >>>    int size=sizeof(array)/sizeof(int);
> >>>    int min,max;
> >>>    max=min=array[0];
> >>>    int i=0;
>
> >>>  for(i = 1; i < size; i++)
> >>>  {
> >>>    if (array[i] < min)
> >>>      min = array[i];
> >>>    else if (array[i] > max)
> >>>      max = array[i];
> >>>  }
>
> >>>  int range = max - min + 1;
> >>>  int *count =(int *) malloc(range * sizeof(int));
>
> >>>    for(i = 0; i < size; i++)
> >>>    count[i] = 0;
>
> >>>    for(i = 0; i < size; i++)
> >>>    count[array[i]]++;
>
> >>>    int pos=0;
>
> >>>    for(i=size-1;i>=0;i--)
> >>>    {
> >>>                      for(int j=0;j<count[i];j++)
> >>>                      {
> >>>                        array[pos]=i;
> >>>                        pos++;
> >>>                      }
> >>>    }
> >>>    //free(count);
>
> >>>    for(int i=0;i<size;i++)
> >>>    printf("%d \n" ,array[i]);
>
> >>>    getch();
> >>> }
>
> >>> in case of a tie
> >>> print the number which occurred first  ...i think dis situation never
> >>> occurs...as ..array is sorted..
>
> >>> --
> >>> 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<algogeeks%2bunsubscr...@googlegroups
> >>>  .com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/algogeeks?hl=en.
>
> >> --
> >> regards,
> >> soumya prasad ukil
>
> >> --
> >> 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<algogeeks%2bunsubscr...@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.

-- 
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.

Reply via email to