sriramchandrk

Branch Unspecified

16 Oct 2008

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

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

prakash.athani

Branch Unspecified

10 years ago

Here it is in C

#include <stdio.h>

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<size;i++) {

scanf("%d",&a

#include <stdio.h>

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<size;i++) {

scanf("%d",&a

*);*

}

printf("Input the value for n :");

scanf("%d",&n);

while(cnt<n) {

big=0;

for(i=0;i<size;i++) {

if(a}

printf("Input the value for n :");

scanf("%d",&n);

while(cnt<n) {

big=0;

for(i=0;i<size;i++) {

if(a

*>big) {*

big=abig=a

*;*

index=i;

}

}

cnt=cnt+1;

for(j=index;j<size;j++) {

a[j]=a[j+1];

}

size=size-1;

}

printf("%d th biggest is %d :",n,big);

return 0;

}index=i;

}

}

cnt=cnt+1;

for(j=index;j<size;j++) {

a[j]=a[j+1];

}

size=size-1;

}

printf("%d th biggest is %d :",n,big);

return 0;

}

sriramchandrk

Branch Unspecified

10 years ago

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

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

Branch Unspecified

10 years ago

Hey Sriram,

Check this. It finds without corrupting or copying of the initial array.

Reply back if it works or has flaws.

#include <stdio.h>

#include <stdlib.h>

#include<conio.h>

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<size;i++) {

scanf("%d",&a

Check this. It finds without corrupting or copying of the initial array.

Reply back if it works or has flaws.

#include <stdio.h>

#include <stdlib.h>

#include<conio.h>

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<size;i++) {

scanf("%d",&a

*);*

}

printf("Input the value for n: ");

scanf("%d",&n);

while(cnt<n) {

big=0;

for(i=0;i<size;i++) {

if(a}

printf("Input the value for n: ");

scanf("%d",&n);

while(cnt<n) {

big=0;

for(i=0;i<size;i++) {

if(a

*>big && (a**<bigger || (a**>bigger && foundBiggest==0))) {*

big=abig=a

*;*

}

else if(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;

}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

Branch Unspecified

10 years ago

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

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

Electrical

10 years ago

Great stuff going on here. Can we have lots of programming challenges like this?

prakash.athani

Branch Unspecified

10 years ago

Yes The_Big_K. Why not? You put challenges. We will try to come up with solutions.

Kaustubh Katdare

Electrical

10 years ago

😁

Okay I will. But I can't post problems beyond certain difficulty level. The hardcore CS/IT folks are the right people! 😉

Okay I will. But I can't post problems beyond certain difficulty level. The hardcore CS/IT folks are the right people! 😉

sriramchandrk

Branch Unspecified

10 years ago

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:

Thanks & Regards

Sriram

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

Electrical

10 years ago

Guys, please start a new discussion thread for each new topic. 😀

Only logged in users can reply.