Programming Challenge - try this (biggest number!)

Hi Guys,

Can you solve this?

Problem: Find the nth biggest number in an array!
Input: array of numbers, n
Output: n biggest in the array.

Example:
Array = { 9, 15, 18, 6, 3, 10 }
n = 4;

Output: 9 is the 4th biggest number!

for Array = { 15, 15, 18, 6, 15, 10 }
& n = 4

Output: 10 is the 4th biggest number. ( it has to take care of duplicates!)
The 1st, 2nd & 3rd biggest are 15.

Constraints: Dont sort and then give the answer. It will corrupt the initial array and if you copy and then sort it will waste space. even complexity will be more.

Best of Luck

Thanks & Regards
Sriram

Replies

  • prakash.athani
    prakash.athani
    Here it is in C

    #include
    int main() {
    int a[20],n=0,size=0,cnt=0,big=0,index=0,i,j;

    printf("Input the size of the array :");
    scanf("%d",&size);
    printf("Input %d numbers into array :",size);
    for(i=0;i scanf("%d",&a);
    }
    printf("Input the value for n :");
    scanf("%d",&n);

    while(cnt
    big=0;
    for(i=0;i if(a>big) {
    big=a;
    index=i;
    }
    }
    cnt=cnt+1;
    for(j=index;j a[j]=a[j+1];
    }
    size=size-1;
    }

    printf("%d th biggest is %d :",n,big);
    return 0;
    }
  • sriramchandrk
    sriramchandrk
    Hey Prakash,

    Its good, it works. Actually you are using the technique used in insertion sort.

    But now i would like you to work on it a bit more.

    You are corrupting the initial array, right?
    Can you implement it without corrupting the initial array?
    Please dont copy into another array! imagine what if there were billion of numbers.
    It will increase the space complexity as well as after each find you are actually shifty one space which is reduce the performance.

    Thanks & Regards
    Sriram
  • prakash.athani
    prakash.athani
    Hey Sriram,

    Check this. It finds without corrupting or copying of the initial array.
    Reply back if it works or has flaws.



    #include
    #include
    #include

    int main() {
    int a[10],size,n,i,cnt=0,big=-999,bigger=-999,foundBiggest=0;
    clrscr();

    printf("\nInput the size of the array :");
    scanf("%d",&size);
    printf("\nInput %d integers into array :",size);
    for(i=0;i scanf("%d",&a);
    }
    printf("Input the value for n: ");
    scanf("%d",&n);

    while(cnt big=0;
    for(i=0;i if(a>big && (a>bigger && foundBiggest==0))) {
    big=a;
    }
    else if(a==bigger) {
    cnt=cnt+1;
    if(cnt==n){
    printf("%d th biggest is : %d",cnt,bigger);
    getch();
    exit(0);
    }//end of if
    }//end of elseif
    }//end of for loop

    bigger=big;
    foundBiggest=1;
    }// end of while loop

    return 0;
    }
  • sriramchandrk
    sriramchandrk
    Congratulations Prakash, you done it!!
    Wasn't it intresting? more than what you thought at the beginning?

    Because once you starting thinking for realtime problems you will have all constraints.

    Thanks for putting effort.


    Regards
    Sriram
  • Kaustubh Katdare
    Kaustubh Katdare
    Great stuff going on here. Can we have lots of programming challenges like this?
  • prakash.athani
    prakash.athani
    Yes The_Big_K. Why not? You put challenges. We will try to come up with solutions.
  • Kaustubh Katdare
    Kaustubh Katdare
    ๐Ÿ˜

    Okay I will. But I can't post problems beyond certain difficulty level. The hardcore CS/IT folks are the right people! ๐Ÿ˜‰
  • sriramchandrk
    sriramchandrk
    Yes, let me post one more. But what happened to other guys, i see only Computer Sc & IT section has more activity, eventhough very few giving try?

    Let me put this way for correct answer i will give prize money!
    Rs 10 for first correct answer. ( I am not serious on this front ๐Ÿ˜‰ ).

    Is it ok?

    Prakash, you have to assign Big to possible small value so that it works on negative numbers as well.
    one way is to assign on 32 bit machine is:
    big = 0x80000000;
    guess what it does?

    Thanks & Regards
    Sriram
  • Kaustubh Katdare
    Kaustubh Katdare
    Guys, please start a new discussion thread for each new topic. ๐Ÿ˜€

You are reading an archived discussion.

Related Posts

Find integers a, b and c such that :- 987654321a + 123456789b + c = (a + b + c)ยณ There are (at least) 3 solutions with a and b...
HELLO to all, ๐Ÿ˜Ž
hai,everyone im jenifer ๐Ÿ˜€
Lot of companies ( I guess IT companies ) allow their employees to work from home. Now, I want to know from you if work from home is cool or...
This is supposed to be a group discussion and I hope to have lots of different views in this discussion. Just to set the ground, I assume most of you...