Open In App

Check if the last element of array is even or odd after performing a operation p times

Improve
Improve
Like Article
Like
Save
Share
Report

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"


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.
?>


Javascript




<script>
    // Javascript 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)
    {
        let sum = 0;
 
        // sum of the array.
        for (let 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";
    }
     
    let arr = [ 5, 7, 10 ], p = 1;
    let n = arr.length;
    document.write(check_last(arr, n, p));
     
    // This code is contributed by divyesh072019.
</script>


Output

ODD

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time.
Auxiliary Space: O(1), as we are not using any extra space.



Last Updated : 27 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads