█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 4358

Problema 0x25 - Suma de un subarreglo grande

  • Autor: saris123
  • Fecha: 2021-06-12 17:48:10 UTC (Hace casi 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.119 s 13 KBi
#2
Correcto
0.196 s 14 KBi
#3
Correcto
0.165 s 13 KBi
#4
Correcto
0.141 s 17 KBi
#5
Correcto
0.21 s 15 KBi
#6
Correcto
0.168 s 24 KBi
#7
Correcto
0.184 s 13 KBi
#8
Correcto
0.942 s 43 KBi
#9
Tiempo límite excedido
1.093 s 89 KBi
#10
Tiempo límite excedido
1.083 s 62 KBi
#11
Tiempo límite excedido
1.078 s 61 KBi
#12
Tiempo límite excedido
1.093 s 45 KBi
#13
Tiempo límite excedido
1.035 s 64 KBi
#14
Tiempo límite excedido
1.07 s 78 KBi
Puntos totales: 58 / 100

Código

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
    	Scanner sc= new Scanner(System.in);
        int N = sc.nextInt();
        int numberArray[] = new int[N];
        int totalSum = 0;
        for(int i = 0; i < N; i++)
        {
            numberArray[i] = sc.nextInt();
            totalSum += numberArray[i];
        }
        
        int C = sc.nextInt();
        int resultArray[] = new int[C];
        for(int i = 0; i < C; i++)
        {
        	resultArray[i] = SumArray(numberArray, sc.nextInt(), sc.nextInt(), N, totalSum);
        }
        sc.close();
        
        Arrays.stream(resultArray).forEach(System.out::println);
    }

     private static int SumArray(int[] array, int p, int q, int N, int totalSum) {
    	int result = 0;
    	int size = p - q;
    	
    	if (size == N) {
    		result = totalSum;
    	}
    	
    	if (size < N/2) {
    		for (int i = p; i <= q; i++) {
                result += array[i] ;
            }
    	} else {
        	int inicialTail = 0;
        	int finalTail = 0;
    		for (int i = 0; i < p; i++) {
                inicialTail += array[i] ;
            }
    		for (int i = q +1 ; i < N; i++) {
                finalTail += array[i] ;
            }
    		result = totalSum - inicialTail - finalTail;
    	}
        
        return result;
    }
}