Skip to content

Instantly share code, notes, and snippets.

@kharioki
Created July 6, 2021 08:43
Show Gist options
  • Save kharioki/3d3efce000d1a277d88cd8630adf63b6 to your computer and use it in GitHub Desktop.
Save kharioki/3d3efce000d1a277d88cd8630adf63b6 to your computer and use it in GitHub Desktop.
Calculation hit probability in battleship game in 2d array
function getHitProbability(R, C, G) {
// Write your code here
let empty = 0;
let filled = 0;
let tot = R*C
for(let i = 0; i < R; i++){
for(let j = 0; j < C; j++){
G[i] && G[i][j] === 1 ? filled++ : empty++
}
}
let prob = filled / tot
return prob.toFixed(6);
}
@Vigneshgvs
Copy link

how to do this in java?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment