Ruby Array Exercises: Compute the sum of two arrays and return the array which has the largest sum
Ruby Array: Exercise-21 with Solution
Write a Ruby program to compute the sum of two arrays (length 3) and return the array which has the largest sum.

Ruby Code:
def check_array(a, b)
sum = (a[0]+a[1]+a[2])-(b[0]+b[1]+b[2])
if(sum >= 0)
return a
end
return b
end
print check_array([1, 3, 5], [2, 4, 4]),"\n"
print check_array([11, 3, 5], [21, 0, -4]),"\n"
Output:
[2, 4, 4] [11, 3, 5]
Flowchart:

Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to set 5 to 1 if there is a 3 immediately followed by a 4 in a given array of integers (length 3).
Next: Write a Ruby program to create a new array of length 2 containing the middle two elements from an given array of integers of even length 2 or more.
What is the difficulty level of this exercise?
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook