给定的是n个非负整数的数组。操作是在数组中插入一个严格大于数组当前和的数字。执行p次运算后,找出数组的最后一个元素是偶数还是奇数。 例如:
null
Input : arr[] = {2, 3} P = 3Output : EVENFor 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 = 1Output : ODDFor 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 <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> |
输出:
ODD
时间复杂性: O(n)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END