Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
Last Updated :
19 Nov, 2021
Given an M x N matrix mat[][]. The task is to count the number of good cells in the matrix. A cell will be good if the sum of the cell value and the number of the adjacent cells is a Fibonacci number.
Examples:
Input: mat[][] = {
{1, 2},
{3, 4}}
Output: 2
Only the cells mat[0][0] and mat[1][0] are good.
i.e. (1 + 2) = 3 and (3 + 2) = 5 are both Fibonacci numbers.
Input: mat[][] = {
{1, 0, 5, 3},
{2, 17, 5, 6},
{5, 8, 15, 11}};
Output: 7
Approach: Iterate the entire matrix and for each cell find the count of adjacent cells. There can be 3 types of cells, one with 2 adjacent cells, one with 3 adjacent cells and the rest with 4 adjacent cells. Sum this count with the value at the current cell and check whether the result is a Fibonacci number. If yes then increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 3
#define N 4
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
int goodCells( int mat[M][N])
{
int count = 0;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
int sum = mat[i][j];
if ((i == 0 && j == 0)
|| (i == M - 1 && j == 0)
|| (i == 0 && j == N - 1)
|| (i == M - 1 && j == N - 1)) {
sum += 2;
}
else if (i == 0 || j == 0
|| i == M - 1 || j == N - 1) {
sum += 3;
}
else {
sum += 4;
}
if (isFibonacci(sum))
count++;
}
}
return count;
}
int main()
{
int mat[M][N] = { { 1, 0, 5, 3 },
{ 2, 17, 5, 6 },
{ 5, 8, 15, 11 } };
cout << goodCells(mat);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int M = 3 ;
static int N = 4 ;
static boolean isPerfectSquare( long x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static boolean isFibonacci( int n)
{
return isPerfectSquare( 5 * n * n + 4 )
|| isPerfectSquare( 5 * n * n - 4 );
}
static int goodCells( int mat[][])
{
int count = 0 ;
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
int sum = mat[i][j];
if ((i == 0 && j == 0 )
|| (i == M - 1 && j == 0 )
|| (i == 0 && j == N - 1 )
|| (i == M - 1 && j == N - 1 ))
{
sum += 2 ;
}
else if (i == 0 || j == 0
|| i == M - 1 || j == N - 1 )
{
sum += 3 ;
}
else
{
sum += 4 ;
}
if (isFibonacci(sum))
count++;
}
}
return count;
}
public static void main (String[] args)
{
int mat[][] = { { 1 , 0 , 5 , 3 },
{ 2 , 17 , 5 , 6 },
{ 5 , 8 , 15 , 11 } };
System.out.println( goodCells(mat));
}
}
|
Python
from math import ceil,sqrt,floor
M = 3
N = 4
def isPerfectSquare(x):
sr = (sqrt(x))
return ((sr - floor(sr)) = = 0 )
def isFibonacci(n):
return isPerfectSquare( 5 * n * n + 4 ) or isPerfectSquare( 5 * n * n - 4 )
def goodCells(mat):
count = 0
for i in range (M):
for j in range (N):
sum = mat[i][j]
if ((i = = 0 and j = = 0 )
or (i = = M - 1 and j = = 0 )
or (i = = 0 and j = = N - 1 )
or (i = = M - 1 and j = = N - 1 )):
sum + = 2
elif (i = = 0 or j = = 0 or i = = M - 1 or j = = N - 1 ):
sum + = 3
else :
sum + = 4
if (isFibonacci( sum )):
count + = 1
return count
mat = [ [ 1 , 0 , 5 , 3 ],
[ 2 , 17 , 5 , 6 ],
[ 5 , 8 , 15 , 11 ] ]
print (goodCells(mat))
|
C#
using System;
class GFG
{
static int M = 3;
static int N = 4;
static bool isPerfectSquare( long x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
static int goodCells( int [,]mat)
{
int count = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
int sum = mat[i,j];
if ((i == 0 && j == 0)
|| (i == M - 1 && j == 0)
|| (i == 0 && j == N - 1)
|| (i == M - 1 && j == N - 1))
{
sum += 2;
}
else if (i == 0 || j == 0
|| i == M - 1 || j == N - 1)
{
sum += 3;
}
else
{
sum += 4;
}
if (isFibonacci(sum))
count++;
}
}
return count;
}
public static void Main ()
{
int [,]mat = { { 1, 0, 5, 3 },
{ 2, 17, 5, 6 },
{ 5, 8, 15, 11 } };
Console.WriteLine( goodCells(mat));
}
}
|
Javascript
<script>
let M = 3;
let N = 4;
function isPerfectSquare(x)
{
let sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function isFibonacci(n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
function goodCells(mat)
{
let count = 0;
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
{
let sum = mat[i][j];
if ((i == 0 && j == 0)
|| (i == M - 1 && j == 0)
|| (i == 0 && j == N - 1)
|| (i == M - 1 && j == N - 1))
{
sum += 2;
}
else if (i == 0 || j == 0
|| i == M - 1 || j == N - 1)
{
sum += 3;
}
else
{
sum += 4;
}
if (isFibonacci(sum))
count++;
}
}
return count;
}
let mat = [ [ 1, 0, 5, 3 ],
[ 2, 17, 5, 6 ],
[ 5, 8, 15, 11 ] ];
document.write( goodCells(mat));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...