Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- given an N x M binary matrix , there are Q queries in each query which is i,j a position in the matrix
- you need to select the row i and column j and flip the values ,
- v1 - thing is the i,j position changes only one time
- v2 - thing is the i,j position changes twice
- find the unchanged cells after all the q queries
- note - here the q queries use 0 based indexing
- */
- /*
- //inputs for array , given you the N x M and then the binary matrix and then q queries
- 3 3
- 0 0 1
- 0 1 0
- 1 0 0
- 2
- 2 0
- 1 1
- expected output -
- The result is 5
- */
- @SuppressWarnings("unused")
- public class A20243012_googleOA {
- static Scanner sc = new Scanner(System.in);
- private static int[] getArray() {
- String[] sArr = sc.nextLine().split(" ");
- int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
- return arr;
- }
- private static char[] getCharArray() {
- String[] sArr = sc.nextLine().split(" ");
- char[] cArr = new char[sArr.length];
- for (int i = 0; i < sArr.length; i++) {
- cArr[i] = sArr[i].charAt(0); // Take the first character of each string
- }
- return cArr;
- }
- private static int getMax(int[] arr) {
- int currMax = Integer.MIN_VALUE;
- for (int curr : arr) {
- currMax = Math.max(currMax, curr);
- }
- return currMax;
- }
- public static void main(String args[]) {
- // prepare the inputs
- int[] arr = getArray();
- int N = arr[0];
- int M = arr[1];
- int[][] binMat = new int[N][M];
- for(int i = 0;i<N;i++){
- binMat[i] = getArray();
- }
- int q = getArray()[0];
- // System.out.println("q is"+q);
- HashMap<Integer,Integer> rowMap = new HashMap<>();
- HashMap<Integer,Integer> colMap = new HashMap<>();
- HashMap<Pair,Integer> pairMap = new HashMap<>();
- for(int i=0;i<q;i++){
- int[] query = getArray();
- Pair pair = new Pair(query[0],query[1]);
- rowMap.put(pair.row,rowMap.getOrDefault(pair.row,0)+1);
- colMap.put(pair.col,colMap.getOrDefault(pair.col,0)+1);
- pairMap.put(pair,pairMap.getOrDefault(pair,0)+1);
- }
- // System.out.println("hii");
- int changedCount=0;
- for(int i=0;i<N;i++){
- for(int j=0;j<M;j++){
- Pair pair = new Pair(i,j);
- int rowFreq = rowMap.getOrDefault(pair.row,0);
- int colFreq = colMap.getOrDefault(pair.col,0);
- int pairFreq = pairMap.getOrDefault(pair,0);
- int currFlipped =(rowFreq)+(colFreq)-pairFreq;//for v2 just remove the pairFreq
- if(currFlipped%2!=0){
- // System.out.println(" pair.row "+pair.row+" pair.col "+pair.col+" is changed ");
- changedCount+=1;
- }
- }
- }
- int res = N*M-changedCount;
- System.out.println("The result is " + res);
- }
- }
- class Pair{
- int row;
- int col;
- public Pair(int i,int j){
- this.row = i;
- this.col = j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement