Why my program doesn't completely work

, ,

the link of problem : MAXREM Problem - CodeChef

and here is my code :

#include<stdio.h>
/// the link of explanation :Find Second largest element in an array - GeeksforGeeks
int main(){
int t,a=0,b=0;
scanf(“%i”,&t);
while(t–){
int n;
scanf(“%i”,&n);
if(n>a){
b=a;
a=n;
}
else if(n>b && b!=a) b=n;
}
printf(“%i\n”,b%a);
}

Let’s figure out with an example
T=3
Array=[8,8,7]
Let’s walk through your code

Iteration 0
a=0, b=0

Iteration 1
n=8, a=8, b=0

Iteration 2
n=8, a=8, b=8

Iteration 3
Nothing happens

Output
8%8=0

Expected Output
7

Remedy
I think you can figure out yourself after this example. Let know if you need help

2 Likes

thank you dude … everything is clear now :+1:

1 Like