Question
Find sum greater than zero
You are given an array arr of length n. In one operation, you can choose any index i and either increment arr[i] by 1 or decrement arr[i] by 1. Your goal is to find the minimum number of operations required to ensure that the sum of all elements in the array is even but not zero, i. e., the sum should be divisible by 2 and greater than 0.
Input
The first line contains an integer n, the length of the array.
The second line contains n integers separated by spaces, representing the elements of the array.

Constraints
1 <= n <= 105
0 <= arr[i] <= 106, 0<=i<=n-1.
Output
Print an integer, the minimum number of operations required to ensure an even sum greater than of all elements in the array.
Example
Input:
4
5 7 3 2

Output
1

Explanation:
Choose index i=0, a[i]=5 and
Increment it. This will result into sum = (6+7+3+2) = 18 (Even)
OR
Decrement it, This will result into sum = (4+7+3+2) = 16 (Even)

Similarly, we can choose any of the number from the array and operate on it. And in 1 operation we can meet the requirements. Hence, the answer is 1.

Online Ruby Compiler

Learning Ruby Programming online with our free online Ruby editor

Our Ruby programming editor provides a convenient platform to write, run, and test Ruby code directly in your browser. Whether you're a Ruby programming beginner or an experienced developer, our interactive Ruby editor is a powerful tool to practice coding, experiment with Ruby features, and enhance your Ruby programming skills.

Features

  • Real-time Code Execution: Write Ruby code and instantly see the output without any installations or setup.
  • Error Detection: Easily spot and correct syntax errors with our helpful error messages.

How to use?

  • Start Writing Code: Begin typing your Ruby code in the editor window.
  • Run Your Code: Click the "Run" button to execute your Ruby program and view the results.
  • Debugging: Review the editor's error messages to troubleshoot and refine your Ruby code.

Tips for learning Ruby

  • Start with Simple Programs: Begin with basic Ruby programs to grasp the language's fundamental concepts, such as variables, loops, and functions.
  • Utilize Online Resources: Explore tutorials, Ruby documentation, and Ruby community forums to deepen your understanding and find answers to your questions.
  • Practice Regularly: Consistent coding practice is key to mastering Ruby and becoming a proficient Ruby programmer.
  • Experiment with Ruby Features: Dive into Ruby's features, such as arrays, hashes, and object-oriented programming, to gain hands-on experience.

Why choose Newton School's Ruby editor?

  • User-Friendly Interface: Our Ruby editor is designed to be user-friendly, suitable for both beginners and experienced developers.
  • Instant Feedback: Get immediate results as you code, facilitating faster learning and iterative development.
  • No Installation Required: Enjoy the convenience of coding Ruby right in your browser without any installations or downloads.