# Count of moves to escape given Matrix from given position based on given conditions

Given an **N x M** matrix **mat[][]** where initially we are standing at the cell with index **(i, j)**, the task is to find the number of operations required to escape the given matrix where at each operation **mat[x][y]** can be reached from **mat[i][j] **such that **x** represents the count of** 0’s** in the binary representation of **mat[i][j]** and **y** represents the count of **1’s** in the binary representation of **mat[i][j]**.

**Examples**:

Input: mat[][] = {{5, 13, 8, 1}, {7, 9, 2, 15}, {12, 4, 8, 3}}, i = 0, j = 0Output: 4Explanation: Initially, the current position is mat[0][0] = 5. Therefore, the index reachable from 5 is mat[1][2] as count of 0’s in 5 is 1 and count of 1’s in 5 is 2. Hence the first jump is from mat[0][0] => mat[1][2]. Similarly, the jumps are in the following order: mat[0][0] => mat[1][2] => mat[1][1] => mat[2][2] => mat[3][1]. Since, the index (3, 1) does not exist in the given matrix, the number of required operations are 4.

Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, i = 1, j = 2Output: -1Explanation: It is not possible to escape the matrix from the index.

**Approach**: The given problem can be solved with the help of Brian Kernighan’s Algorithm and the inbuilt log function. Below are the steps to follow:

- Create a variable
**Jump**, which stores the required number of jump operations to escape the given 2D array. Initially,**Jump = 0**. - If the initial cell is already out of bounds of the given matrix, return
**0**. - Calculate the count of
**0’s**and**1’s**in the binary representation of**mat[i][j]**. - Jump to the next valid index and set
**mat[i][j]**as**-1**. Also, increment the value of**Jump**by**1**. - Check if the current value of
**mat[i][j] = -1**after the jump. It means that the current index is already visited, hence creating a never-ending loop. Therefore, return**-1**. - Repeat the above process until the current index is already visited or it is out of bounds of the given matrix.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `#define Max 100` `using` `namespace` `std;` `// Function to find total count of` `// set bits in any number` `int` `brianKernighan(` `int` `N)` `{` ` ` `int` `Count = 0;` ` ` `while` `(N) {` ` ` `N = N & (N - 1);` ` ` `Count += 1;` ` ` `}` ` ` `return` `Count;` `}` `// Function to find left most Set bit` `// position in any number` `int` `getMsbIndex(` `int` `N)` `{` ` ` `return` `((` `int` `)log2(N) + 1);` `}` `// Function to find the number of` `// jumps to escape the matrix from` `// the given index [i, j]` `int` `countJumps(` `int` `Mat[][Max],` ` ` `int` `N, ` `int` `M,` ` ` `int` `i, ` `int` `j)` `{` ` ` `// Initialize the variable Jump` ` ` `int` `C0, C1, Jump = 0;` ` ` `while` `(i < N && j < M) {` ` ` `// When the element is already visited` ` ` `// then a closed loop is formed and` ` ` `// it is impossible to escape then` ` ` `// return -1.` ` ` `if` `(Mat[i][j] == -1)` ` ` `return` `-1;` ` ` `// Calculate Count of 1 in Mat[i][j]` ` ` `C1 = brianKernighan(Mat[i][j]);` ` ` `// Calculate Count of 0 in Mat[i][j]` ` ` `C0 = getMsbIndex(Mat[i][j]) - C1;` ` ` `// Set the element Mat[i][j] visited` ` ` `Mat[i][j] = -1;` ` ` `// Set i and j to count if 0 and 1` ` ` `i = C0, j = C1;` ` ` `// Increment Jump by 1` ` ` `Jump++;` ` ` `}` ` ` `// Return number of Jumps to escape` ` ` `// the matrix if it is possible to` ` ` `// escape` ` ` `return` `Jump;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 3, M = 4;` ` ` `int` `i = 0, j = 0;` ` ` `int` `Mat[][Max] = { { 5, 13, 8, 1 },` ` ` `{ 7, 9, 2, 15 },` ` ` `{ 12, 4, 8, 3 } };` ` ` `cout << countJumps(Mat, N, M, i, j);` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `// Function to find total count of` `// set bits in any number` `static` `int` `brianKernighan(` `int` `N)` `{` ` ` `int` `Count = ` `0` `;` ` ` `while` `(N != ` `0` `) {` ` ` `N = N & (N - ` `1` `);` ` ` `Count += ` `1` `;` ` ` `}` ` ` `return` `Count;` `}` `// Function to find left most Set bit` `// position in any number` `static` `int` `getMsbIndex(` `int` `N)` `{` ` ` `return` `((` `int` `)(Math.log(N) / Math.log(` `2` `)) + ` `1` `);` `}` `// Function to find the number of` `// jumps to escape the matrix from` `// the given index [i, j]` `static` `int` `countJumps(` `int` `Mat[][],` ` ` `int` `N, ` `int` `M,` ` ` `int` `i, ` `int` `j)` `{` ` ` ` ` `// Initialize the variable Jump` ` ` `int` `C0, C1, Jump = ` `0` `;` ` ` `while` `(i < N && j < M) {` ` ` `// When the element is already visited` ` ` `// then a closed loop is formed and` ` ` `// it is impossible to escape then` ` ` `// return -1.` ` ` `if` `(Mat[i][j] == -` `1` `)` ` ` `return` `-` `1` `;` ` ` `// Calculate Count of 1 in Mat[i][j]` ` ` `C1 = brianKernighan(Mat[i][j]);` ` ` `// Calculate Count of 0 in Mat[i][j]` ` ` `C0 = getMsbIndex(Mat[i][j]) - C1;` ` ` `// Set the element Mat[i][j] visited` ` ` `Mat[i][j] = -` `1` `;` ` ` `// Set i and j to count if 0 and 1` ` ` `i = C0; j = C1;` ` ` `// Increment Jump by 1` ` ` `Jump++;` ` ` `}` ` ` `// Return number of Jumps to escape` ` ` `// the matrix if it is possible to` ` ` `// escape` ` ` `return` `Jump;` `}` `// Driver Code` `public` `static` `void` `main (String[] args) {` ` ` ` ` `int` `N = ` `3` `, M = ` `4` `;` ` ` `int` `i = ` `0` `, j = ` `0` `;` ` ` `int` `Mat[][] = { { ` `5` `, ` `13` `, ` `8` `, ` `1` `},` ` ` `{ ` `7` `, ` `9` `, ` `2` `, ` `15` `},` ` ` `{ ` `12` `, ` `4` `, ` `8` `, ` `3` `} };` ` ` `System.out.println(countJumps(Mat, N, M, i, j));` `}` `}` `// This code is contributed by target_2.` |

## Python3

`# Python Program to implement` `# the above approach` `import` `math as Math` `Max` `=` `100` `# Function to find total count of` `# set bits in any number` `def` `brianKernighan(N):` ` ` `Count ` `=` `0` ` ` `while` `(N):` ` ` `N ` `=` `N & (N ` `-` `1` `)` ` ` `Count ` `+` `=` `1` ` ` `return` `Count` `# Function to find left most Set bit` `# position in any number` `def` `getMsbIndex(N):` ` ` `return` `Math.floor(Math.log2(N) ` `+` `1` `)` `# Function to find the number of` `# jumps to escape the matrix from` `# the given index [i, j]` `def` `countJumps(Mat, N, M, i, j):` ` ` ` ` `# Initialize the variable Jump` ` ` `Jump ` `=` `0` ` ` `while` `(i < N ` `and` `j < M):` ` ` `# When the element is already visited` ` ` `# then a closed loop is formed and` ` ` `# it is impossible to escape then` ` ` `# return -1.` ` ` `if` `(Mat[i][j] ` `=` `=` `-` `1` `):` ` ` `return` `-` `1` ` ` `# Calculate Count of 1 in Mat[i][j]` ` ` `C1 ` `=` `brianKernighan(Mat[i][j])` ` ` `# Calculate Count of 0 in Mat[i][j]` ` ` `C0 ` `=` `getMsbIndex(Mat[i][j]) ` `-` `C1` ` ` `# Set the element Mat[i][j] visited` ` ` `Mat[i][j] ` `=` `-` `1` ` ` `# Set i and j to count if 0 and 1` ` ` `i ` `=` `C0` ` ` `j ` `=` `C1` ` ` `# Increment Jump by 1` ` ` `Jump ` `+` `=` `1` ` ` `# Return number of Jumps to escape` ` ` `# the matrix if it is possible to` ` ` `# escape` ` ` `return` `Jump` `# Driver Code` `N ` `=` `3` `M ` `=` `4` `i ` `=` `0` `j ` `=` `0` `Mat ` `=` `[[` `5` `, ` `13` `, ` `8` `, ` `1` `],` ` ` `[` `7` `, ` `9` `, ` `2` `, ` `15` `],` ` ` `[` `12` `, ` `4` `, ` `8` `, ` `3` `]]` `print` `(countJumps(Mat, N, M, i, j))` `# This code is contributed by gfgking.` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `// Function to find total count of` `// set bits in any number` `static` `int` `brianKernighan(` `int` `N)` `{` ` ` `int` `Count = 0;` ` ` `while` `(N != 0) {` ` ` `N = N & (N - 1);` ` ` `Count += 1;` ` ` `}` ` ` `return` `Count;` `}` `// Function to find left most Set bit` `// position in any number` `static` `int` `getMsbIndex(` `int` `N)` `{` ` ` `return` `((` `int` `)(Math.Log(N) / Math.Log(2)) + 1);` `}` `// Function to find the number of` `// jumps to escape the matrix from` `// the given index [i, j]` `static` `int` `countJumps(` `int` `[,] Mat,` ` ` `int` `N, ` `int` `M,` ` ` `int` `i, ` `int` `j)` `{` ` ` ` ` `// Initialize the variable Jump` ` ` `int` `C0, C1, Jump = 0;` ` ` `while` `(i < N && j < M) {` ` ` `// When the element is already visited` ` ` `// then a closed loop is formed and` ` ` `// it is impossible to escape then` ` ` `// return -1.` ` ` `if` `(Mat[i, j] == -1)` ` ` `return` `-1;` ` ` `// Calculate Count of 1 in Mat[i][j]` ` ` `C1 = brianKernighan(Mat[i, j]);` ` ` `// Calculate Count of 0 in Mat[i][j]` ` ` `C0 = getMsbIndex(Mat[i, j]) - C1;` ` ` `// Set the element Mat[i][j] visited` ` ` `Mat[i, j] = -1;` ` ` `// Set i and j to count if 0 and 1` ` ` `i = C0; j = C1;` ` ` `// Increment Jump by 1` ` ` `Jump++;` ` ` `}` ` ` `// Return number of Jumps to escape` ` ` `// the matrix if it is possible to` ` ` `// escape` ` ` `return` `Jump;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 3, M = 4;` ` ` `int` `i = 0, j = 0;` ` ` `int` `[,] Mat = {{ 5, 13, 8, 1 },` ` ` `{ 7, 9, 2, 15 },` ` ` `{ 12, 4, 8, 3 } };` ` ` `Console.Write(countJumps(Mat, N, M, i, j));` `}` `}` `// This code is contributed by sanjoy_62.` |

## Javascript

`<script>` ` ` `// JavaScript Program to implement` ` ` `// the above approach ` ` ` `var` `Max = 100` ` ` `// Function to find total count of` ` ` `// set bits in any number` ` ` `function` `brianKernighan(N) {` ` ` `let Count = 0;` ` ` `while` `(N) {` ` ` `N = N & (N - 1);` ` ` `Count += 1;` ` ` `}` ` ` `return` `Count;` ` ` `}` ` ` `// Function to find left most Set bit` ` ` `// position in any number` ` ` `function` `getMsbIndex(N) {` ` ` `return` `Math.floor(Math.log2(N) + 1);` ` ` `}` ` ` `// Function to find the number of` ` ` `// jumps to escape the matrix from` ` ` `// the given index [i, j]` ` ` `function` `countJumps(Mat,` ` ` `N, M,` ` ` `i, j) {` ` ` `// Initialize the variable Jump` ` ` `let C0, C1, Jump = 0;` ` ` `while` `(i < N && j < M) {` ` ` `// When the element is already visited` ` ` `// then a closed loop is formed and` ` ` `// it is impossible to escape then` ` ` `// return -1.` ` ` `if` `(Mat[i][j] == -1)` ` ` `return` `-1;` ` ` `// Calculate Count of 1 in Mat[i][j]` ` ` `C1 = brianKernighan(Mat[i][j]);` ` ` `// Calculate Count of 0 in Mat[i][j]` ` ` `C0 = getMsbIndex(Mat[i][j]) - C1;` ` ` `// Set the element Mat[i][j] visited` ` ` `Mat[i][j] = -1;` ` ` `// Set i and j to count if 0 and 1` ` ` `i = C0, j = C1;` ` ` `// Increment Jump by 1` ` ` `Jump++;` ` ` `}` ` ` `// Return number of Jumps to escape` ` ` `// the matrix if it is possible to` ` ` `// escape` ` ` `return` `Jump;` ` ` `}` ` ` `// Driver Code` ` ` `let N = 3, M = 4;` ` ` `let i = 0, j = 0;` ` ` `let Mat = [[5, 13, 8, 1],` ` ` `[7, 9, 2, 15],` ` ` `[12, 4, 8, 3]];` ` ` `document.write(countJumps(Mat, N, M, i, j));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

4

**Time** **Complexity**: O(N * M * log(M * N))**Auxiliary** **Space**: O(1)