Simple implementation without using data structures

Imagine an M*N matrix where blocks are given consecutive numbers

- for a given row all the numbers are consecutive
- make sure that the last number of a row and the first number of the next row are not consecutive (I’m using row gap 1)
- let ‘T’ be the number of trees
- consider an
**array of size T**now run a loop for taking tree positions as input and simultaneously store the block number of tree block in the array

/* for(i=0;i<trees;i++)

{

scanf("%ld",&r);

scanf("%ld",&c);

block_number[i] = c + ((r-1)*(columns+1)); // row gap 1

} */ - now sort the array block_number[ T ] using
**quick sort** - search(using
**binary seach**) the left,right,up,down block numbers for each tree block in the array block_number[ T ]

if found add 1 to**count ( number of removable fences )** - Maximum number of fences possible = 4*trees
- Number of required fences = 4*trees - count

here’s my implementation using C