fork download
  1. #include <stdio.h>
  2. #define MAX 1000000000
  3.  
  4. int main() {
  5. int n;
  6. scanf("%d",&n);// n>=3
  7. int a[n],i=0,j=0;
  8.  
  9. for(;i<n;i++)
  10. scanf("%d",&a[i]);
  11.  
  12. int min_index=-1,current_min=-1;
  13.  
  14. for(j=1;j<=3;j++){ // find minimum 3 times
  15. current_min = a[0]; // stores minimum
  16. min_index=0; // stores index of the minimum
  17.  
  18. for(i=0;i<n;i++){ // iterate through the entire array and find the minimum
  19. if(a[i]<current_min)
  20. {
  21. current_min=a[i];
  22. min_index = i;
  23. }
  24. }
  25. a[min_index]=MAX; // replace the current minimum with some high value so that it doesnt interfere while finding the minimum in the next iteration
  26. }
  27.  
  28. printf("Value = %d\nPosition = %d\n", current_min,min_index+1); // added 1 due to 0 incexing in array
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 2252KB
stdin
7
-2 3 1 2 -3 0 -1
stdout
Value = -1
Position = 7