Posts

Showing posts from March, 2023

shuffleArray.js( ***REVISE***)

Image
  /*Shuffle array Description Given an array consisting of 2n elements in the form [x1, x2, ..., xn, y1, y2, ..., yn], suffle the array into [x1, y1, x2, y2, ... , xn, yn]. Assume that n is never 0. Input format First line contains a positive integer, denoting n. It is followed by 2n lines. Each line contains one integer. Output format 2n lines where each line contains one element of the shuffled array. Sample input 3 2 5 1 3 4 7 Sample output 2 3 5 4 1 7 Explanation 1) First line is 3, i.e. Following 6 lines are the elements of the array. 2) The first 3 lines contains the first half of the array and the 2nd set of 3 elements contain the second half of the array. 3) First 2 lines of output will be first element of first half, first element of second half so the first 2 lines of the output are 2 and 3. 4) Next 2 lines of output will be second element of first half and second element of second half, so the next 2 lines of output will be 5 and 4 5) Next 2 lines of output will be th...

divisibilityTest.js

Image
  /*Divisibility Test Description Given an array of positive integers of size N and an integer x. Count the number of elements in the array that are divisible by x. Input format First line contains two space separated positive integers n and x, where n denotes the number of elements in the array. Second line contains n space separated elements of array. Output format For the given array, print the count of numbers that are divisible by x. Sample input-1 5 5 1 5 6 10 9 Sample output-1 2 Sample input-2 4 7 11 15 22 100 Sample output-2 0 */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// const array = readLine (). split ( ' ' ); // [ '5', '5' ] green color //console.log(array...

countoddeven.js

Image
  /*Count odd even numbers Count number of odd and even number in given list. Input First line contains length of the list. Each line contains integer specifying each element in list. Output 2 integers in each line specifying count of odd and even numbers respectively. Example Input: 5 12 14 15 13 18 Output: 2 3 */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// const noOfInputs = parseInt ( readLine ()); // 5 let oddCount = 0 ; let evenCount = 0 ; let i = 0 ; // intialisation of while loop while (i < noOfInputs) { const numbers = parseInt ( readLine ()); if (numbers % 2 != 0 ) { oddCount ++ } else { evenCount ++ } i ++ ; // increment o...

rotateClockwise.js ( ***Please Revise***)

Image
  /*Rotate Array Clockwise Write a function which takes an array arr and a postive integer m and rotates arr clockwise, m times. Input First line contains a sequence of integers separated by spaces, denoting arr Second line contains a positive integer, denoting m Output Elements of the rotated array, one in each line Example Input: 2 1 3 4 5 10 1 Output: 10 2 1 3 4 5 Explanation Given array is [2, 1, 3, 4, 5, 10]. m is 1, which means that the array needs to be rotated clockwise 1 time. One clockwise rotation means all the elements get shifted towards right by 1 position. The last element will get shifted to the first postion. So, the rotated array will be [10, 2, 1, 3, 4, 5]. */ 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 (); } //////////////////////////////////////////////////////////////////////////////////...

minMax.js (*** Revise for math.max and math.min)

Image
  /*Algorithms: Min Max For a given sequence of integers, find the minimum and maximum integers in the sequence. Input First line contains n , the number of integers. Next n lines contain n integers, one integer per line. Output First line integer is the maximum Second line integer is the minimum Example Input: 4 73 344 56 11 Output: 344 11 */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// let lengthofintegers = parseInt ( readLine ()); // 4 let arr = []; // initialising an array for (i = 0 ; i < lengthofintegers; i ++ ) // 1.(i=0), 2.(i=1), 3.(i=2), 4.(i=3), 5.(i=4<4 false exit the loop). { arr[i] = parseInt ( readLine ()); } // 1.(73), 2.(344) 3.(56) 4.(11) //console.log(arr)...

countNegative.js

  /*Count negative numbers Description Given an array of integers of size N, find the number of negative elements in the array. Input format First line contains a positive integer n denoting the number of elements in the array and second line contains n space separated elements of array. Output format For the given array, print the count of negative numbers in the array. Sample input-1 5 1 -5 -4 3 0 Sample output-1 2 Explanation-1 There are two negative numbers in the array, that are -5 and -4. Sample input-2 5 1 5 3 7 11 Sample output-2 0 Explanation-2 There are no negative elements in the array. */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// let noOfInputs = parseInt ( readLine ()); // 5 let s...

If no. of rows and coloumns of the matrix is not given, how to find it?

Image
 

matrixAddition.js ( without dry run)

  /* Matrix Addition Input:- 3 - Rows 4 - Col 1,2,3,4 5,6,7,8 9,10,11,12 20,21,22,23 24,25,26,27 28,29,30,31 Output: [ [ 21, 23, 25, 27 ], [ 29, 31, 33, 35 ], [ 37, 39, 41, 43 ] ] */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// const noOfRows = parseInt ( readLine ()); // 3 const noOfColoumns = parseInt ( readLine ()); // 4 const A = []; // we will push the first matrix A here afterwards for ( let i = 0 ; i < noOfRows; i ++ ) // this will give us the array but not in integer form { let subArr = readLine (). split ( ',' ); for (j = 0 ; j < noOfColoumns; j ++ ) // this will convert each element into an integer { subArr[j] = parseInt (subArr[j]); } ...

matrixAddition.js (***Revise Hard please***)

Image
  /* Matrix Addition Input:- 3 - Rows 4 - Col 1,2,3,4 5,6,7,8 9,10,11,12 20,21,22,23 24,25,26,27 28,29,30,31 Output: [ [ 21, 23, 25, 27 ], [ 29, 31, 33, 35 ], [ 37, 39, 41, 43 ] ] */ 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 (); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// const noOfRows = parseInt ( readLine ()); // 3 const noOfColoumns = parseInt ( readLine ()); // 4 const A = []; // we will push the first matrix here afterwards for ( let i = 0 ; i < noOfRows; i ++ ) // this will give us the array but not in integer form { let subArr = readLine (). split ( ',' ); //console.log(subArr); for ( let j = 0 ; j < noOfColoumns; j ++ ) // this will convert each element into an integer { subArr[...

Matrix Addition

Image
  =>

2D Array

Image
  1)                         0     1     2        3  A[0]   = [1, 2, true, 4] A[0] [2] = "true" 2) Accessing in 2D Array 3) Alteration in 2D Array

sumofArrayElements.js

Image
  /* Sum of Array Elements for odd number of elements Given A =[8,9,7,6,3,10,5] Output:- A= [13,19,10,6] */ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //index 0 1 2 3 4 5 6 let A = [ 8 , 9 , 7 , 6 , 3 , 10 , 5 ] let ans = []; let p = 0 ; let q = A.length - 1 ; // 7-1 = 6 while (p < q) { ans. push (A[p] + A[q]); p ++ ; q -- ; } if (p == q) { ans. push (A[p]); /* or ans.push(A[q]); */ } console. log (ans); T.C = o(n/2) => o(n) S.C = o(1) /////////////////////////////////////////////////////////////////////////////////////////////// TERMINAL: