Check if the last element of array is even or odd after performing a operation p times
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 = 1n = len(arr) print(check_last (arr, n, p)) # This code is contributed by "Abhishek Sharma 44" |
Java
// Java program to check whether the last // element of the array is even or odd // after performing the operation p times. import java.util.*; class Even_odd{ public static 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"; } public static void main(String[] args) { int arr[] = { 5, 7, 10 }, p = 1; int n = 3; System.out.print(check_last(arr, n, p)); } } //This code is contributed by rishabh_jain |
C#
// C# program to check whether the last // element of the array is even or odd // after performing the operation p times. using System; class GFG { public static 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 public static void Main() { int []arr = { 5, 7, 10 }; int p = 1; int n = arr.Length; Console.WriteLine(check_last(arr, n, p)); } } //This code is contributed by vt_m. |
PHP
<?php // PHP program to check whether the last // element of the array is even or odd // after performing the operation p times. function check_last($arr, $n, $p) { $sum = 0; // sum of the array. for ( $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 $arr = array(5, 7, 10); $p = 1; $n = count($arr); echo check_last($arr, $n, $p); // This code is contributed by anuj_67. ?> |
Output:
ODD
Time Complexity: O(n)
Recommended Posts:
- Minimum possible sum of array elements after performing the given operation
- Find the number of different numbers in the array after applying the given operation q times
- Sub-string that contains all lowercase alphabets after performing the given operation
- Find the index which is the last to be reduced to zero after performing a given operation
- Check if at least half array is reducible to zero by performing some operations
- Reduce the array to a single element with the given operation
- First element occurring k times in an array
- Largest element in the array that is repeated exactly k times
- Sum of array elements after every element x is XORed itself x times
- First element that appears even number of times in an array
- Smallest element in an array that is repeated exactly 'k' times.
- Unique element in an array where all elements occur k times except one
- Find the K-th minimum element from an array concatenated M times
- Check for an array element that is co-prime with all others
- Check if an array has a majority element
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m



