Advertisement
jsprieto10

The Length of the longest subarray with c zeros

May 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. // OJO: Este archivo no pertenece a ningún paquete (package)
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7. /**
  8.  * Calcula la cantidad y la suma de numeros pares en una lista de numeros
  9.  * @author Rodrigo Cardoso
  10.  */
  11. public class ProblemaA {
  12.    
  13.  
  14.     public static void main(String[] args) throws IOException
  15.     {
  16.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.         String lineain;
  18.         String[] data;
  19.         int n;
  20.         int c;
  21.         boolean primero = false;
  22.         boolean segundo = false;
  23.         int mayor=0;
  24.         int actual=0;
  25.         int p1 = 0;
  26.         int p2;
  27.         int cuenta=0;
  28.         ArrayList<Integer> posiciones;
  29.         while (true)
  30.         {
  31.            
  32.             lineain = br.readLine();
  33.             data = lineain.split(" ");
  34.             n = Integer.parseInt(data[0]);
  35.             c = Integer.parseInt(data[1]);
  36.             p2 = c;
  37.             posiciones = new ArrayList<Integer>();
  38.             for (int i = 2; i < n+2; i++)
  39.             {
  40.               if (data[i].equals("0"))
  41.               {
  42.                 posiciones.add(i-2);
  43.               }
  44.             }
  45.             posiciones.add(n);
  46.             mayor=posiciones.get(p2);
  47.            
  48.             p2++;
  49.             for (int i = 0; i < posiciones.size(); i++)
  50.             {
  51.               int adelante = posiciones.get(p2);
  52.               int atras = posiciones.get(i)+1;
  53.               if ( adelante-atras > mayor)
  54.                 mayor = adelante-atras;
  55.              p2++;
  56.              if (p2 == posiciones.size())
  57.              {
  58.                  break;
  59.              }
  60.  
  61.             }
  62.             System.out.println(mayor);
  63.       }
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement