Knowee
Questions
Features
Study Tools

Given an array 𝐴A of size 𝑁N, find the size of the largest subarray of 𝐴A which has an even sum.For example, for the array [2,1,2][2,1,2] the answer is just 11 as the largest subarray with an even sum is [2][2].Note that a subarray is a contiguous part of an array. For the array [1,3,2][1,3,2], some possible subarrays are [1],[2],[1,3],[1,3,2][1],[2],[1,3],[1,3,2]. However, [1,2][1,2] is not a subarray for this array.Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains a single integer 𝑁N — the size of the array.The next line contains 𝑁N space-separated integer - 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ .Output FormatFor each test case, output on a new line, the size of the largest subarray with even sum.Constraints1≤𝑇≤501≤T≤501≤𝑁≤501≤N≤501≤𝐴𝑖≤1041≤A i​ ≤10 4 Sample 1:InputOutput432 1 231 3 262 2 1 1 1 2111340Explanation:Test case 11: The largest subarray with even sum is the subarray [2][2] having size 11.Test case 22: The largest subarray with even sum is the subarray [1,3,2][1,3,2] having size 33.Test case 33: The largest subarray with even sum is the subarray [2,2,1,1][2,2,1,1] having size 44.Test case 44: No subarray has even sum.

Question

Given an array 𝐴A of size 𝑁N, find the size of the largest subarray of 𝐴A which has an even sum.For example, for the array [2,1,2][2,1,2] the answer is just 11 as the largest subarray with an even sum is [2][2].Note that a subarray is a contiguous part of an array. For the array [1,3,2][1,3,2], some possible subarrays are [1],[2],[1,3],[1,3,2][1],[2],[1,3],[1,3,2]. However, [1,2][1,2] is not a subarray for this array.Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains a single integer 𝑁N — the size of the array.The next line contains 𝑁N space-separated integer - 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ .Output FormatFor each test case, output on a new line, the size of the largest subarray with even sum.Constraints1≤𝑇≤501≤T≤501≤𝑁≤501≤N≤501≤𝐴𝑖≤1041≤A i​ ≤10 4 Sample 1:InputOutput432 1 231 3 262 2 1 1 1 2111340Explanation:Test case 11: The largest subarray with even sum is the subarray [2][2] having size 11.Test case 22: The largest subarray with even sum is the subarray [1,3,2][1,3,2] having size 33.Test case 33: The largest subarray with even sum is the subarray [2,2,1,1][2,2,1,1] having size 44.Test case 44: No subarray has even sum.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Para resolver este problema, sigamos los siguientes pasos:

  1. Leer el número de casos de prueba, T T .
  2. Para cada caso de prueba:
    • Leer el tamaño del array, N N .
    • Leer el array A A de N N elementos.
    • Determinar el tamaño del subarray más grande con suma par.
  3. Imprimir el resultado para cada caso de prueba.

Pasos detallados:

  1. Leer el número de casos de prueba, T T :

    T = int(input())
    
  2. Para cada caso de prueba:

    • Leer el tamaño del array, N N :
      N = int(input())
      
    • Leer el array A A :
      A = list(map(int, input().split()))
      
  3. Determinar el tamaño del subarray más grande con suma par:

    • Si la suma total del array es par, entonces el subarray más grande con suma par es el array completo.
    • Si la suma total del array es impar, entonces necesitamos encontrar el subarray más grande eliminando un elemento impar del inicio o del final.
    • Si no hay elementos pares en el array, el resultado es 0.
  4. Imprimir el resultado para cada caso de prueba:

    for _ in range(T):
        N = int(input())
        A = list(map(int, input().split()))
        
        total_sum = sum(A)
        
        if total_sum % 2 == 0:
            print(N)
        else:
            found = False
            for i in range(N):
                if A[i] % 2 != 0:
                    found = True
                    break
            if found:
                print(N - 1)
            else:
                print(0)
    

Código completo:

T = int(input())

for _ in range(T):
    N = int(input())
    A = list(map(int, input().split()))
    
    total_sum = sum(A)
    
    if total_sum % 2 == 0:
        print(N)
    else:
        found = False
        for i in range(N):
            if A[i] % 2 != 0:
                found = True
                break
        if found:
            print(N - 1)
        else:
            print(0)

Explicación de los ejemplos:

  • Caso de prueba 1: [2, 1, 2]

    • La suma total es 5 (impar), eliminando un elemento impar (1), obtenemos [2, 2] con suma 4 (par). Tamaño del subarray más grande con suma par es 2.
  • Caso de prueba 2: [1, 3, 2]

    • La suma total es 6 (par), el subarray más grande con suma par es el array completo. Tamaño es 3.
  • Caso de prueba 3: [2, 2, 1, 1]

    • La suma total es 6 (par), el subarray más grande con suma par es el array completo. Tamaño es 4.
  • Caso de prueba 4: [1, 1, 1, 1]

    • La suma total es 4 (par), pero no hay subarray con suma par. Tamaño es 0.

This problem has been solved

Similar Questions

Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum.Sample Input:9-2 -1 -3 4 -1 2 1 -5 4Sample Output:6Explanation:The subarray is [4, -1, 2, 1] and the sum is 6

Write an efficient C program to find the sum of contiguous subarray within a one-dimensionalarray of integer which returns the largest sum.Explanation:Lets take the example of array {5,-3, 4}Possible contiguous subarray combinations are{5}, {-3}, {4}, {5,-3}, {-3,4}, {5,-3,4}The contiguous subarray {5,-3,4} has got the largest sum 6

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer. Example 1:Input: arr = [1,15,7,9,2,5,10], k = 3Output: 84Explanation: arr becomes [15,15,15,9,10,10,10]

We define the following:A subarray of an -element array is an array composed from a contiguous block of the original array's elements. For example, if , then the subarrays are , , , , , and . Something like would not be a subarray as it's not a contiguous subsection of the original array.The sum of an array is the total sum of its elements.An array's sum is negative if the total sum of its elements is negative.An array's sum is positive if the total sum of its elements is positive.Given an array of integers, find and print its number of negative subarrays on a new line.Input FormatThe first line contains a single integer, , denoting the length of array .The second line contains space-separated integers describing each respective element, , in array .ConstraintsOutput FormatPrint the number of subarrays of having negative sums.Sample Input51 -2 4 -5 1Sample Output9

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.