Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java Last Updated : 04 Dec, 2020 Comments Improve Suggest changes Like Article Like Report 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)); } } OutputVALID INVALIDTime Complexity: O(N)Space Complexity: O(1) Comment More infoAdvertise with us Next Article Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java ajaykr00kj Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java 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 First element that appears even number of times in an array Given an array, find the first element that appears even number of times in the array. It returns the element if exists otherwise returns 0. Examples: Input : arr[] = {1, 5, 4, 7, 4, 1, 5, 7, 1, 5}; Output : 4 Explanation, 4 is the first element that appears even number of times. Input : arr[] = {2, 7 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 Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E 3 min read Implement Filter Function using Reduce in Java 8 Streams Many times, we need to perform operations where a stream reduces to a single resultant value, for example, maximum, minimum, sum, product, etc. Reducing is the repeated process of combining all elements. reduce operation applies a binary operator to each element in the stream where the first argumen 2 min read Program to print product of even and odd indexed elements in an Array Given an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately. Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero. Examples: Input : arr = {1, 2, 3, 4, 5, 6} Output : 9 min read Sort all even numbers in the Array without changing order of odd elements Given an array arr[] of size N, the task is to sort all the even numbers in the array, without changing the order of odd elementsExamples: Input: arr[] = {4, 7, 2, 11, 15}Output: {2, 7, 4, 11, 15}Explanation: Even numbers are sorted at their corresponding places, without changing the order of odd el 4 min read Like