foldArray.js with dry run ( ***REVISE***)
/*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
Explanation
Odd number n
The first line contains 5 indicating 5 elements in the input array
The next 5 lines contain the array elements: -1, 4, 2, 3, 1
The last line contains 1 indicating one fold to be made
The first and last element merge: -1 + 1 = 0
The second and 4th element merge: 4 + 3 = 7
The middle element is 2. It remains unchanged because of the fold
So the result is 0, 7, 2
Even number n
The first line contains 6 indicating 6 elements in the input array
The next 6 lines contain the array elements: 3, 1, 6, 7, 2, 3
The last line contains 1 indicating one fold to be made
The first and last element merge: 3 + 3 = 6
The second and 5th element merge: 1 + 2 = 3
The third and 4th element merge: 6 + 7 = 13
So the result is 6, 3, 13
*/
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 for taking the input in array format only
const noOfElementsInArray = parseInt(readLine()); // 5
let arr = [] // we will push the elements in this empty array
for (let i = 0; i < noOfElementsInArray; i++) {
// arr[i] = parseInt(readLine());
arr.push(parseInt(readLine()));
// or arr[i] = parseInt(readLine()); both will give the same result
// console.log(arr)
// [ -1 ] when i=0
// [ -1, 4 ] when i=1
// [ -1, 4, 2 ] when i=1
// [ -1, 4, 2, 3 ] when i=2
// [ -1, 4, 2, 3, 1 ]when i=3
// [ -1, 4, 2, 3, 1 ]when i=4 , all yellow color elements
}
const noOfFolds = parseInt(readLine()); // 1
for (let folds = 0; folds < noOfFolds; folds++) // 0) folds =0<1 true go inside the forloop
// 2) folds =1<1 false , exit the forloop
completely and go to the console statements
we came to fols = 1 after breaking and
exiting from the inner while loop
{
// for each fold
let i = 0; // initialization for while loop
while (i < arr.length) // 0) i=0 <5 true, go inside the while loop
// 1) i=1<5 true, go inside the while loop
// 2) i=2<5 true , go inside the while loop
{
if (i == arr.length - 1) // 0) i=0 == 4 (5-1) false don't go inside if condition
// 1) i=1 == 3 (4-1)false don't go inside if condition
// 2) i=2 == 2 (3-1)true , now break from this while loop
completely and go for folds++ i.e., folds = 1 in the
main forloop
{
break;
}
let lastElement = arr[arr.length - 1]; // 0) lastElement = arr[4] = 1
the arr is [-1, 4, 2, 3, 1]
// 1) lastElement = arr[3] = 3
the arr is [0,4,2,3]
arr[i] += lastElement; // 0) arr[0] = arr[0] + lastElement i.e., arr[0] = -1+1 = 0
now the arr = [0, 4, 2, 3, 1]
// 1) arr[1] = arr[1] + lastElement i.e., arr[1] = 4+ 3 =7
now the arr = [0,7,2,]
i++; // 0) i++ now i is 1
// 1) i++ now i is 2
arr.pop(); // 0) 1 will be removed from the back of arr array and now continue with
while loop of i=1
and the arr after pop will be arr =[0,4,2,3]
// 1) 3 will be removed from the back of arr array and now continue with
while loop of i=2
and the arr after pop this time will be arr =[0,7,2]
}
}
//console.log(arr) [ 0, 7, 2 ] yellow color elements
console.log(arr.length); // 3 this statement is executed only after exiting the top main
forloop.
for (let i = 0; i < arr.length; i++) // 0) i=0<3 true ,go inside forloop , this statement is
executed only after exiting the top main forloop
1) i=1<3 true, go inside forloop
2) i=2<3 true, go inside forloop
3) i=3<3 false , exit the forloop completely
{
console.log(arr[i]); // 0) arr[0] = 0 , i++ now i=1
// 1) arr[1] = 7 , i++ now i=2
// 2) arr[2] = 2 , i++ now i=3
}
//////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:

Comments
Post a Comment