fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*
  5. Constraints: n <= 100000
  6. This program:
  7. 1. Reads an array of n integers.
  8. 2. Calculates the sum of all elements.
  9. 3. Finds the maximum element.
  10. 4. Counts how many even numbers are in the array.
  11. */
  12.  
  13. int main() {
  14. int n;
  15. cin >> n;
  16.  
  17. int a[n];
  18.  
  19. // Input loop
  20. for (int i = 0; i < n; ++i) {
  21. cin >> a[i];
  22. }
  23.  
  24. // First loop: Calculate the sum
  25. long long sum = 0;
  26. for (int i = 0; i < n; ++i) {
  27. sum += a[i];
  28. }
  29.  
  30. // Second loop: Find the maximum element
  31. int maxElement = a[0];
  32. for (int i = 1; i < n; ++i) {
  33. if (a[i] > maxElement) {
  34. maxElement = a[i];
  35. }
  36. }
  37.  
  38. // Third loop: Count even numbers
  39. int evenCount = 0;
  40. for (int i = 0; i < n; ++i) {
  41. if (a[i] % 2 == 0) {
  42. evenCount++;
  43. }
  44. }
  45.  
  46. // Output the results
  47. cout << "Sum = " << sum << "\n";
  48. cout << "Max = " << maxElement << "\n";
  49. cout << "Even Count = " << evenCount << "\n";
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0.01s 5324KB
stdin
5
2 4 6 4 1
stdout
Sum = 17
Max = 6
Even Count = 4