(SPOJ) Guerra por Território - Solution

Link to the problem: http://br.spoj.com/problems/GUERRA12/


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        String line = br.readLine();
        int numNumbers = Integer.parseInt(line);
       
        line = br.readLine();
        String[] s = line.split(" ");
        int[] numbers = new int[numNumbers];
        int total = 0;
        for (int i = 0; i < numNumbers; i++) {
            numbers[i] = Integer.parseInt(s[i]);
            total += numbers[i];
        }
        br.close();
        
        int half = total/2;
        int sum = 0;
        for (int i = 0; i < numNumbers; i++) {
            sum += numbers[i];
            if (sum == half) {
                bw.write((i+1)+"\n");
                break;
            }
        }
           
        bw.flush();
        bw.close();
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução