priyansh_max's blog

By priyansh_max, history, 4 days ago, In English

long t = sc.nextLong();

while (t-- > 0) {
        long n = sc.nextLong();
        int[][] grid = new int[2][(int) (n + 3)];

        long ans = 0;
        for(int i = 0 ; i < n ; i++){
            int v1 = sc.nextInt();
            int v2 = sc.nextInt();

            grid[v2][v1]++;
        }

        for(int i = 0 ; i <= n ; i++){
            if(grid[0][i] > 0 && grid[1][i] > 0){
                ans += (n-2);
            }
        }

        for(int i = 0 ; i <= n ; i++){
            if(grid[0][i] > 0 && i > 0 && i < n-1){
                if(grid[1][i-1] > 0 && grid[1][i+1] > 0){
                    ans += 1;
                }
            }
        }


        for(int i = 0 ; i <= n ; i++){
            if(grid[1][i] != 0 && i > 0 && i < n-1){
                if(grid[0][i-1] != 0 && grid[0][i+1] != 0){
                    ans += 1;
                }
            }
        }

        System.out.println(ans);

    }
}

This is my logic I cant figure out which case am i missing

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
4 days ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

you should write i<n in the second and the therd for

  • »
    »
    4 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    not i<n-1

»
4 days ago, # |
  Vote: I like it +3 Vote: I do not like it

Thank you