- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java

Understanding how arrays work is fundamental for any developer, and Java is no exception. Arrays in Java are objects that store multiple variables of the same type. However, arrays can often be used in more complex ways. One such instance is when you need to figure out if the sum of an array, considering only even numbers at odd indices and odd numbers at even indices, is divisible by the size of the array. In this article, we will guide you step-by-step on how to solve this problem.

## Problem Statement

Given an array of integers, write a function in Java to determine if the sum of even numbers at odd indices and odd numbers at even indices is divisible by the size of the array.

## Approach

The solution involves looping through the array and selectively adding numbers to a sum. We'll iterate over each index. For even indices, we'll check if the number is odd, and if it is, we add it to our sum. For odd indices, we'll check if the number is even, and if so, add it to our sum. Finally, we'll check if the sum is divisible by the size of the array.

## Example

Here is a simple implementation of the above approach in Java −

public class Main { public static boolean isSumDivisible(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { if (i % 2 == 0 && array[i] % 2 != 0) { sum += array[i]; } else if (i % 2 != 0 && array[i] % 2 == 0) { sum += array[i]; } } return sum % array.length == 0; } public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6}; System.out.println(isSumDivisible(array)); } }

## Output

false

## Explanation

Let's go through the example array {1, 2, 3, 4, 5, 6}. In this case, we have −

At index 0 (an even index), we have the number 1, which is odd.

At index 1 (an odd index), we have the number 2, which is even.

At index 2 (an even index), we have the number 3, which is odd.

At index 3 (an odd index), we have the number 4, which is even.

At index 4 (an even index), we have the number 5, which is odd.

At index 5 (an odd index), we have the number 6, which is even.

So, we add these numbers to the sum, and we get 1 + 2 + 3 + 4 + 5 + 6 = 21. The size of the array is 6. Since 21 is not divisible by 6, the output of the function isSumDivisible(array) will be "false".

This problem demonstrates a good understanding of arrays, iteration, and conditional logical

## Conclusion

Understanding how to manipulate arrays and use conditional logic in Java is crucial for solving many problems in programming. This specific problem of checking if the sum of even numbers at odd indices and odd numbers at even indices is divisible by the size of the array, is a good demonstration of how these concepts can be applied. Practice solving such problems to enhance your understanding of Java and improve your problem-solving skills.