Given is an array of n non-negative integers. The operation is to insert a number in the array which is strictly greater than current sum of the array. After performing the operation p times, find whether the last element of the array is even or odd.

Examples:

Input : arr[] = {2, 3} P = 3 Output : EVEN For p = 1, Array sum = 2 + 3 = 5. So, we insert 6. For p = 2, Array sum = 5 + 6 = 11. So, we insert 12. For p = 3, Array sum = 11 + 12 = 23. So, we insert 24 (which is even). Input : arr[] = {5, 7, 10} p = 1 Output : ODD For p = 1, Array sum = 5 + 7 + 10 = 22. So, we insert 23 (which is odd).

**Naive Approach:** First find the sum of the given array. This can be done in a single loop. Now make an another array of size P + N. This array will denote the element to be inserted, and last element will be our required answer. At any step, if parity of the sum of the elements of array is “even”, parity of inserted element will be “odd”.

**Efficient Approach:** Let us say that sum of the array is even, next inserted element will be odd. Now sum of array will be odd, so next inserted element will be even, now sum of array becomes odd, so we insert an even number, and so on. We can generalize that if sum of array is even, then for P = 1, last inserted number will be odd, otherwise it will be even.

Now, consider the case in which sum of the array is odd. The next inserted element will be even, now sum of array will become odd, so next inserted element will be even, now sum of array will be odd, add another even number and so on. We can generalize that last inserted number is always even in this case.

Below is the implementation of the above approach:

## C++

// CPP program to check whether the last // element of the array is even or odd // after performing the operation p times. #include <bits/stdc++.h> using namespace std; string check_last(int arr[], int n, int p) { int sum = 0; // sum of the array. for (int i = 0; i < n; i++) sum = sum + arr[i]; if (p == 1) { // if sum is even if (sum % 2 == 0) return "ODD"; else return "EVEN"; } return "EVEN"; } // driver code int main() { int arr[] = { 5, 7, 10 }, p = 1; int n = sizeof(arr) / sizeof(arr[0]); cout << check_last(arr, n, p) << endl; return 0; }

## Python3

# Python3 code to check whether the last # element of the array is even or odd # after performing the operation p times. def check_last (arr, n, p): _sum = 0 # sum of the array. for i in range(n): _sum = _sum + arr[i] if p == 1: # if sum is even if _sum % 2 == 0: return "ODD" else: return "EVEN" return "EVEN" # driver code arr = [5, 7, 10] p = 1 n = len(arr) print(check_last (arr, n, p)) # This code is contributed by "Abhishek Sharma 44"

Output:

ODD

**Time Complexity:** O(n)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.