📌  相关文章
📜  执行p次运算后,检查数组的最后一个元素是偶数还是奇数

📅  最后修改于: 2021-06-26 22:24:09             🧑  作者: Mango

给定的是n个非负整数的数组。该操作是在数组中插入一个严格大于数组当前和的数字。在执行了p次运算之后,找到数组的最后一个元素是偶数还是奇数。
例子:

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

天真的方法:首先找到给定数组的总和。这可以在一个循环中完成。现在制作另一个大小为P + N的数组。该数组将表示要插入的元素,最后一个元素将是我们所需的答案。在任何步骤中,如果数组元素之和的奇偶校验为“偶数”,则插入元素的奇偶校验将为“奇数”。
高效方法:让我们说数组的总和是偶数,下一个插入的元素将是奇数。现在数组的总和将是奇数,因此下一个插入的元素将是偶数,现在数组的总和将变为奇数,因此我们插入一个偶数,依此类推。我们可以概括一下,如果数组的总和为偶数,则对于P = 1,最后插入的数字为奇数,否则为偶数。
现在,考虑数组之和为奇数的情况。下一个插入的元素将是偶数,现在数组的总和将变成奇数,因此下一个插入的元素将是偶数,现在数组的总和将是奇数,再加上另一个偶数,依此类推。我们可以得出结论,即使在这种情况下,最后插入的数字也始终是。
下面是上述方法的实现:

C++
// CPP program to check whether the last
// element of the array is even or odd
// after performing the operation p times.
#include 
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


Javascript


输出:

ODD

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。