GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Need java based solution to solve parking lot search problem. Please read description.

Find a single black colored car in a lot full of white colored cars in a parking lot that has 10 floors A-J, 6 rows 1-6 & 9 cars 1-9 in each row.

Please provide efficient approach. Already tried with brute-force approach which gives complexity O(m*n*o).

asked Mar 6, 2016 by anonymous

2 Answers

Can you elaborate the question give question link etc ?

answered Mar 8, 2016 by shuboy2014

Making some assumptions.

Solution: Lets denote white cars as 0 and the black cars as 1. 

So the array parking[floor][row] gives the bits of the cars located in the row [1-9] (int).

for( Floor f: floors){

for(int row: rows){

int cars = parking[f,row];
   int colors = cars && 0xffff; // Bitwise operation to find the black color car.
   if(colors > 0){
     
      //We know the floor and row of the black car
      // And process car slot to find the slot of the car
      
   }
}
}

So the solution is O( n2) +1 ie., O(n2) 

answered Jul 18, 2016 by Sudeep
...