Advertisement
Guest User

Untitled

a guest
May 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #define N 100
  11. int main()
  12. {
  13. int my_array[N];
  14. int length_a;
  15. int curr_sum=0, max_sum=0, curr_counter=0, max_counter=0;
  16. int i=0;
  17. int low_index=0, high_index=0;
  18. scanf("%d", &length_a);
  19. for(i=0; i<length_a; i++)
  20. {
  21. scanf("%d", &my_array[i]);
  22.  
  23. }
  24. for(i=0; i<length_a; i++)
  25. {
  26. if(my_array[i]>=0)
  27. {
  28. curr_sum += my_array[i];
  29. curr_counter++;
  30. }
  31. else
  32. {
  33.  
  34.  
  35. if(curr_sum > max_sum)
  36. {
  37. max_sum = curr_sum;
  38. max_counter = curr_counter;
  39. low_index = i- curr_counter ;
  40. if(i>0) high_index=i-1;
  41. }
  42. else if(curr_sum == max_sum)
  43. {
  44.  
  45. if(curr_counter > max_counter)
  46. {
  47. max_sum = curr_sum;
  48. max_counter = curr_counter;
  49. low_index = i- curr_counter -1;
  50. if(i>0) high_index=i-1;
  51. }
  52. }
  53. else
  54. {
  55. curr_sum=0;
  56. curr_counter=0;
  57. }
  58.  
  59. }
  60.  
  61. }
  62. printf("%d %d %d %d",low_index, high_index, max_counter, max_sum);
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement