Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java
Last Updated :
04 Dec, 2020
Given an array arr[] of size N, Convert the given array as even numbers in the array occurs at the odd index, odd numbers in the array occur at the even index, and whether the sum of the numbers in an array is divisible by the size of an array. If the array follows all the properties then an array is valid else it is invalid.
An array will be valid if it follows all the given three conditions:
- In the given array arr[], at every even index position, it must contain an odd number.
- In the given array arr[], at every odd index position, it must contain an even number.
- The sum of the given array arr[] must be divisible by the size of the given array.
Note: Array is 0 indexes based.
Examples:
Input : arr = {1, 2, 3, 4, 5, 6, 9, 10}
Output: "VALID"
Explanation:
1.Sum of given array is 40, and size of array is 8
2.At every even index position array containing odd number
3.At every odd position index array containing even number
Input : arr = {11, 4, 9, 3, 13}
Output: "INVALID"
Explanation:
1.Sum of given array is 40, and size of array is 4
2.At index 3 it containing an odd value which does not follow given condition
Hence it is invalid.
Approach:
- Traverse the given array and check for every element whether they satisfy the condition or not.
- Check for every index:
- if the index is odd, then the value at that index must be even.
- Else if the index is even, then the value at that index must be odd.
- Else print array is invalid and return.
- Store the sum of every given element.
- At last check it is divisible from the given array size or not.
- If the sum is divisible then print "VALID" else print "INVALID".
Below is the implementation of the above approach:
Java
// Sum of Array Divisible by Size with Even
// and Odd Numbers at Odd and Even Index
// in Java
public class Main {
// check whether array is valid or not
public static String validate(int[] arr)
{
// Finding size of given array
int N = arr.length;
// Store sum of given array
int s = 0;
// traverse the given array
for (int i = 0; i < N; i++) {
// store sum of elements
s += arr[i];
// if index is even and value is
// odd, then continue
if (i % 2 == 0 && arr[i] % 2 == 1)
continue;
// if index is odd and value is
// even, then continue
else if (i % 2 == 1 && arr[i] % 2 == 0)
continue;
// violeting given condition
else
return "INVALID";
}
// check last condition, sum is
// divisible by size or not
return s % N == 0 ? "VALID" : "INVALID";
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6, 9, 10 };
System.out.println(validate(arr));
int[] barr = { 11, 4, 9, 3, 13 };
System.out.println(validate(barr));
}
}
- Time Complexity: O(N)
- Space Complexity: O(1)
Similar Reads
Java Program to Store Even & Odd Elements of an Array into Separate Arrays Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers i
2 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Java Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
3 min read
Java Program to Find the Frequency of Odd & Even Numbers in the Matrix Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix. The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is
3 min read
Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7,
7 min read