fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n;
  5. scanf("%d",&n);// n>=3
  6. int min1,min2,min3;
  7. scanf("%d%d%d",&min1,&min2,&min3);
  8. n -= 3 ;
  9. if (min1>min2) {
  10. int a;
  11. a = min1; // a = min1 and not the other way round
  12. min1 = min2;
  13. min2 = a;
  14. }
  15. if(min3 < min2) {
  16. if(min3 > min1) {
  17. int b = min3;
  18. min3 = min2;
  19. min2 = b;
  20. } else {
  21. int c = min1; // case where 3 is lesser than 1 and 2 both
  22. min1 = min3; // shift 3 to 1
  23. min3 = min2; // shift 2 to 3
  24. min2 = c; // shift 1 to 2
  25. }
  26. }
  27.  
  28. // printf("%d %d %d",min1,min2,min3);
  29. int i = 0;//i is as counter
  30.  
  31. while(i < n ) {
  32. int inp;
  33. scanf("%d",&inp);
  34.  
  35. if(min1>=inp){ // you missed this condition
  36. int d = min1;
  37. min1 = inp;
  38. min3 = min2;
  39. min2 = d;
  40. }
  41. else if(min2 >= inp) {
  42. //int d = min3; this is not needed
  43. min3 = min2;
  44. min2 = inp; // this statement was incorrect in your code you had written d instead of inp
  45. }
  46. else if(min3>=inp){
  47. min3 = inp;
  48. }
  49.  
  50.  
  51.  
  52. i ++;
  53. }
  54.  
  55. printf("min3:%d",min3);
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 2296KB
stdin
7
-2 3 1 2 -3 0 -1
stdout
min3:-1