Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(inputArr){
- let row = inputArr[0];
- let col = inputArr[1];
- let startRow = inputArr[2];
- let startCol = inputArr[3];
- const sqaredSize = row*col;
- let currSize = 1;
- let currentNumber = 1;
- //ARRAY FIXING
- let arr = new Array(row);
- for(let i = 0; i < row; i++){
- arr[i] = new Array(col);
- }
- arr[startRow].fill(currentNumber, startCol, startCol+1);
- //ARRAY FIXING End
- //initial fixing of rows and cols
- let topRow = startRow - 1;
- if(topRow < 0) {
- topRow = 0;
- }
- let bottomRow = startRow + 1;
- if(bottomRow >= row){
- bottomRow = row-1;
- }
- let leftCol = startCol - 1;
- if(leftCol < 0){
- leftCol = 0;
- }
- let rightCol = startCol + 1;
- if(rightCol >= col){
- rightCol = col-1;
- }
- //
- while(currSize < sqaredSize){
- currentNumber +=1;
- let lengthRow = bottomRow - topRow + 1;
- let lenghtCol = rightCol - leftCol + 1;
- currSize = lengthRow*lenghtCol;
- //top and bottom row (left col to right col)
- for(let i = leftCol; i <= rightCol; i+=1){
- if(arr[topRow][i] === undefined){
- arr[topRow].fill(currentNumber,i,i+1);
- }
- if(arr[bottomRow][i] === undefined){
- arr[bottomRow].fill(currentNumber,i,i+1);
- }
- }
- //left and right col (top row to bottom row)
- for(let j = topRow+1; j < bottomRow; j+=1){
- if(arr[j][rightCol] === undefined){
- arr[j][rightCol] = currentNumber;
- }
- if(arr[j][leftCol] === undefined){
- arr[j][leftCol] = currentNumber;
- }
- }
- leftCol -=1;
- if(leftCol < 0){
- leftCol = 0;
- }
- rightCol +=1;
- if(rightCol >= col){
- rightCol = col-1;
- }
- topRow -=1;
- if(topRow < 0) {
- topRow = 0;
- }
- bottomRow +=1;
- if(bottomRow >= row){
- bottomRow = row-1;
- }
- }
- arr.map((item)=>{
- console.log(item.join(' '));
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment