Posts

sumOfDiagonals.js (***Coding test***)

Image
  /**Sum of Diagonals Description Given a square matrix, print the sum of all the numbers on both the diagonals. Input format First line contains a positive integer n, denoting the number of rows and columns It is followed by n lines. Each line contains space separated integers denoting elements of that row. Output format One line containing the sum of all the numbers on both the diagonals. Sample input 3 1 2 3 4 5 6 7 8 9 Sample output 30 Explanation First line is 3, i.e. 3 rows and 3 columns. We can see that there are 2 diagonals [1,5,9] and [3,5,7]. When we add all the numbers in both the diagonals, it comes up to 15 + 15 which i s 30 which is our output */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } ///////////////////////////////////////////////////////////////////////////////////////////////////...

matrixDiagonal.js with less dry run

Image
  /*Matrix Diagonal Write a function which takes a 2 dimentional array of size nxn where n > 0 and changes its diagonal according to the following conditions if an element e < 0 replace it with 0 If element e >= 0 replace it with 1 Input The first line contains n, denoting the number of lists. This is followed by n lines. Each line contains n integers separated by a space Output n lines, each line representing a list of numbers separated by a space. Example Input: 4 2 0 1 4 0 -1 1 10 0 0 0 0 1 2 3 4 Output: 1 0 1 4 0 0 1 10 0 0 1 0 1 2 3 1 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// // This part is only for taking inputs const rows = parseInt ( readLine ()); // 4 is the no. of rows ...

matrixDiagonal.js with lots of dry run( *** Please Please Please REVISE ***)

Image
  /*Matrix Diagonal Write a function which takes a 2 dimentional array of size nxn where n > 0 and changes its diagonal according to the following conditions if an element e < 0 replace it with 0 If element e >= 0 replace it with 1 Input The first line contains n, denoting the number of lists. This is followed by n lines. Each line contains n integers separated by a space Output n lines, each line representing a list of numbers separated by a space. Example Input: 4 2 0 1 4 0 -1 1 10 0 0 0 0 1 2 3 4 Output: 1 0 1 4 0 0 1 10 0 0 1 0 1 2 3 1 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// // This part is only for taking inputs const rows = parseInt ( readLine ()); // 4 is the no. of rows ...

maxdial.js with lots of dry run (*** please please please REVISE***)

Image
  /*Max Dial Description Imagine a series of n dials. They are all set to zero initially. An input i arrives. The i-th dial is incremented by 1. An input j arrives. The j-th dial is incremented by 1. You are also given a max value. Given a sequence of such inputs, find which dial reaches the max value first. Input format First line contains n, n >= 0, denoting the number of dials. Second line contains max, max > 0, denoting the max limit for the dial. Third line contains m, m >= 0, denoting the number of inputs. This is followed by m lines each containing a positive integer i, 1 <= i <= n. Output format A positive integer i, denoting that ith dial reaches maximum first. If none reaches maximum limit, then output should be 0. Sample input 3 2 4 1 2 1 3 Sample output 1 Explanation First line is 3, i.e. 3 dials. Second line is 2, i.e. max dial limit is 2. Third line is 4, i.e. 4 inputs arriving. 1,2,1,3 are the inputs. The dial 1 reaches 2 first. So output is 1 */...

maxdial.js with less dry run

Image
  /*Max Dial Description Imagine a series of n dials. They are all set to zero initially. An input i arrives. The i-th dial is incremented by 1. An input j arrives. The j-th dial is incremented by 1. You are also given a max value. Given a sequence of such inputs, find which dial reaches the max value first. Input format First line contains n, n >= 0, denoting the number of dials. Second line contains max, max > 0, denoting the max limit for the dial. Third line contains m, m >= 0, denoting the number of inputs. This is followed by m lines each containing a positive integer i, 1 <= i <= n. Output format A positive integer i, denoting that ith dial reaches maximum first. If none reaches maximum limit, then output should be 0. Sample input 3 2 4 1 2 1 3 Sample output 1 Explanation First line is 3, i.e. 3 dials. Second line is 2, i.e. max dial limit is 2. Third line is 4, i.e. 4 inputs arriving. 1,2,1,3 are the inputs. The dial 1 reaches 2 first. So output is 1 */...

foldArray.js without dry run

  /*Fold Array You are given an array of size n. Imagine the array as a length of rope. You have to fold the rope at the middle. You are also given an input integer numOfFolds that specifies the number of times you should fold your array. To illustrate more, say the given array is 1,4,9. Folding it in the middle results in 10, 4 as 9 and 1 get combined by the fold. Say the given array is 1,10,20,21,2. Folding it would lead to: 3, 31, 20 as 2, 1 have combined and 10, 21 have combined. Based on the numOfFolds repeat folding. Input. The first line contains n, the number of elements in the array. This is followed by n lines, each containing one integer The last line contains m, the number of folds to do. Output The first line contains k, the number of elements in the final array This is followed by k lines, each containing one integer element of the output array Example Odd length array Input: 5 -1 4 2 3 1 1 Output: 3 0 7 2 Even length array Input: 6 3 1 6 7 2 3 1 Output: 3 6 3 13 Exp...

foldArray.js with dry run ( ***REVISE***)

Image
  /*Fold Array You are given an array of size n. Imagine the array as a length of rope. You have to fold the rope at the middle. You are also given an input integer numOfFolds that specifies the number of times you should fold your array. To illustrate more, say the given array is 1,4,9. Folding it in the middle results in 10, 4 as 9 and 1 get combined by the fold. Say the given array is 1,10,20,21,2. Folding it would lead to: 3, 31, 20 as 2, 1 have combined and 10, 21 have combined. Based on the numOfFolds repeat folding. Input. The first line contains n, the number of elements in the array. This is followed by n lines, each containing one integer The last line contains m, the number of folds to do. Output The first line contains k, the number of elements in the final array This is followed by k lines, each containing one integer element of the output array Example Odd length array Input: 5 -1 4 2 3 1 1 Output: 3 0 7 2 Even length array Input: 6 3 1 6 7 2 3 1 Output: 3 6 3 13 Exp...