Coffee Room

Discuss anything here - everything that you wish to discuss with fellow engineers.

12829 Members

Join this group to post and comment.

# 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

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

#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

*);*

}

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

scanf("%d",&n);

while(cnt

big=0;

for(i=0;i
if(a

}

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

scanf("%d",&n);

while(cnt

big=0;

for(i=0;i

*>big) {*

big=a

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;

} index=i;

}

}

cnt=cnt+1;

for(j=index;j

}

size=size-1;

}

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

return 0;

}

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

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

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

*);*

}

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

scanf("%d",&n);

while(cnt
big=0;

for(i=0;i
if(a

}

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

scanf("%d",&n);

while(cnt

for(i=0;i

*>big && (a*

>bigger && foundBiggest==0))) {

big=a big=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;

}

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

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

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

😁

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! 😉

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

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