Skip to content

Commit d52cdb1

Browse files
committed
Add some folders
1 parent 0bcacbe commit d52cdb1

File tree

21 files changed

+676
-0
lines changed

21 files changed

+676
-0
lines changed

C programming/a.out

15.7 KB
Binary file not shown.

C programming/cube.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include<stdio.h>
2+
3+
//Function to calculate the cube of a number that the user provided.
4+
5+
int main() {
6+
int n;
7+
8+
printf("Give n: \n");
9+
scanf("%d", &n);
10+
11+
printf("The cube of n is: %d\n", (n*n*n));
12+
return 0;
13+
}

C programming/hello_world.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# include<stdio.h>
2+
3+
int main() {
4+
int a,b;
5+
6+
printf("Enter a: \n");
7+
scanf("%d", &a);
8+
9+
printf("Enter b: \n");
10+
scanf("%d", &b);
11+
12+
int sum = a + b;
13+
printf("Sum is: %d\n", sum);
14+
return 0;
15+
}

C programming/index.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
int main() {
5+
int a;
6+
scanf("%d", &a);
7+
printf("%d \n", a%2==0);
8+
return 0;
9+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# include<stdio.h>
2+
3+
// function to calculate the perimeter of rectangle after taking sides from user.
4+
int main() {
5+
int a,b;
6+
7+
printf("Give the side a: \n");
8+
scanf("%d", &a);
9+
10+
printf("Give the side b: \n");
11+
scanf("%d", &b);
12+
13+
printf("The perimeter is: %d \n", 2*(a+b));
14+
15+
return 0;
16+
}

DSA /course-questions/q1.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Q) You are given an array of Integers in which each subsequent value is not less than the previous value.
2+
// Write a function that takes this array as an input and returns a new array with the squares of each number sorted in ascending order.
3+
4+
5+
6+
// Brute force method of solving this problem, the time complexity in this case is O(nlogn) and space is O(n).
7+
const arr = [-5, 0, 1, 1, 3];
8+
// const squaredArray = (inputArray) => {
9+
// let finalArray = new Array(inputArray.length).fill(0); //This way of creating array is more optimised that let arr = []; as it takes fix memory.
10+
// for(let i=0; i<inputArray.length; i++) {
11+
// let squared = (inputArray[i] * inputArray[i]);
12+
// finalArray[i] = (squared);
13+
// }
14+
// finalArray.sort((a,b) => a-b)
15+
16+
// return finalArray;
17+
// }
18+
19+
// const output = squaredArray(arr);
20+
// console.log(output);
21+
22+
23+
24+
// The most optimized solution that has Time = O(n) Space = O(n)
25+
const squaredArray = (inputArray) => {
26+
let finalArray = new Array(inputArray.length).fill(0);
27+
28+
let leftPointer = 0;
29+
let rightPointer = inputArray.length - 1;
30+
31+
for (let i=finalArray.length-1; i>=0; i--) {
32+
33+
let leftSquaredElement = inputArray[leftPointer] * inputArray[leftPointer] ;
34+
const rightSquaredElement = inputArray[rightPointer] * inputArray[rightPointer];
35+
36+
if(leftSquaredElement < rightSquaredElement) {
37+
finalArray[i] = rightSquaredElement;
38+
rightPointer--;
39+
}else {
40+
finalArray[i] = leftSquaredElement;
41+
leftPointer++;
42+
}
43+
44+
}
45+
46+
return finalArray;
47+
}
48+
49+
const output = squaredArray(arr);
50+
console.log(output);

DSA /course-questions/q2.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Q) An array is monotonic if it is either monotone increasing or monotone decreasing.
2+
// An array is monotone increasing if all its elements from left to right are non-decreasing.
3+
// An array is monotone decreasing if all its elements from left to right are non-increasing.
4+
// Given an integer array return true if the given array is monotonic, or false otherwise.
5+
6+
const arr1 = [1, 2, 2, 5]; //true
7+
const arr2 = [2, 2, 5, 6]; //true
8+
const arr3 = [1, 1, 1]; // true
9+
10+
11+
const ifMonotonic = (inputArray) => {
12+
13+
if(inputArray.length<=1) return true;
14+
15+
let first = inputArray[0];
16+
let last = inputArray[inputArray.length-1];
17+
18+
if(first === last) {
19+
for(let i=0; i<inputArray.length - 1; i++) {
20+
if (inputArray[i] != inputArray[i + 1]) {
21+
return false;
22+
}
23+
}
24+
25+
26+
}else if(first > last) {
27+
for(let i=0; i<inputArray.length - 1; i++) {
28+
if (inputArray[i] < inputArray[i + 1]) {
29+
return false;
30+
}
31+
}
32+
33+
}else {
34+
for(let i=0; i<inputArray.length - 1; i++) {
35+
if (inputArray[i] > inputArray[i + 1]) {
36+
return false;
37+
}
38+
}
39+
}
40+
return true;
41+
}
42+
43+
const output = ifMonotonic(arr3);
44+
console.log(output)

DSA /course-questions/q3.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Q) Coding Exercise (k-th symbol in Grammar)
2+
// We build a table of n rows (1-indexed). We start by writing 0 in the 1st row.
3+
// Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01,
4+
// and each occurrence of 1 with 10. For example, for n = 3, the 1st row is 0,
5+
// the 2nd row is 01, and the 3rd row is 0110.
6+
// Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows.
7+
8+
const kthGrammar = (n, k) => {
9+
// base case
10+
if (n===1) return 0;
11+
12+
//recursive case
13+
let length = Math.pow(2, (n-1));
14+
let midPoint = length / 2;
15+
16+
if(k<=midPoint) {
17+
return kthGrammar(n-1, k);
18+
}else {
19+
return 1 - kthGrammar(n-1, k-midPoint);
20+
}
21+
}
22+
23+
console.log(kthGrammar(3, 3))

DSA /course-questions/q4.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Q) There are n friends that are playing a game.
2+
// The friends are sitting in a circle and are numbered from 1 to n in clockwise order.
3+
// More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n,
4+
// and moving clockwise from the nth friend brings you to the 1st friend.
5+
// The rules of the game are as follows: Start at the 1st friend.
6+
// Count the next k friends in the clockwise direction including the friend you started at.
7+
// The counting wraps around the circle and may count some friends more than once.
8+
// The last friend you counted leaves the circle and loses the game.
9+
// If there is still more than one friend in the circle, go back to step 2
10+
// starting from the friend immediately clockwise of the friend who just lost and repeat.
11+
// Else, the last friend in the circle wins the game.
12+
// Given the number of friends, n, and an integer k, return the winner of the game.
13+
14+
15+
//APPROACH 1 Time=O(n*n) Space=O(n)
16+
// const josephus = (n, k) => {
17+
// //creating an array with n
18+
// let arr = Array.from({length:n}, (_,i)=>i+1);
19+
20+
// //Function to remove the element
21+
// function helper(arr, start) {
22+
// //base case
23+
// if (arr.length===1) return arr[0];
24+
25+
// //recursive case
26+
// let indexToRemove = (start + k -1) % arr.length;
27+
28+
// arr.splice(indexToRemove, 1);
29+
30+
// return helper(arr, indexToRemove)
31+
// }
32+
33+
// return helper(arr, 0)
34+
// }
35+
36+
// console.log(josephus(41, 2));
37+
38+
39+
//APPROACH 2 Time=O(n) Space=O(n)
40+
41+
// const josephus = (n, k) => {
42+
43+
// function helper(n) {
44+
// //base case
45+
// if (n===1) return 0;
46+
47+
// //recursive case
48+
// return ((helper(n-1, k) + k) % n)
49+
// }
50+
51+
// return helper(n) + 1;
52+
53+
// }
54+
55+
// console.log(josephus(41,2))
56+
57+
58+
//APPROACH 3 Time=O(n) Space=O(1)
59+
60+
const josephus = (n, k) => {
61+
let safeIndex = 0;
62+
for (let i=1; i<=n; i++) {
63+
safeIndex = ((safeIndex + k) % i)
64+
}
65+
return (safeIndex + 1);
66+
}
67+
68+
console.log(josephus(41, 2));

DSA /course-questions/q5.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Q) The tower of Hanoi is a famous puzzle where we have three rods and N disks. The objective of the puzzle is to move the entire stack to another rod. You are given the number of discs N. Initially, these discs are in the rod 1. You need to print all the steps of discs movement so that all the discs reach the 3rd rod. Also, you need to find the total moves.
2+
// Note: The discs are arranged such that the top disc is numbered 1 and the bottom-most disc is numbered N. Also, all the discs have different sizes and a bigger disc cannot be put on the top of a smaller disc. Refer the provided link to get a better clarity about the puzzle.
3+
4+
5+
6+
function toh(n, fromRod, toRod, auxiliaryRod) {
7+
8+
let count = 0;
9+
10+
function helper(n, fromRod, toRod, auxiliaryRod) {
11+
if (n===1) {
12+
count++;
13+
console.log(`move disk ${n} from rod ${fromRod} to ${toRod}`);
14+
return;
15+
}
16+
17+
helper(n-1, fromRod, auxiliaryRod, toRod);
18+
count++;
19+
console.log(`move disk ${n} from rod ${fromRod} to ${auxiliaryRod}`);
20+
helper(n-1, auxiliaryRod, toRod, fromRod)
21+
22+
}
23+
24+
helper(n, fromRod, toRod, auxiliaryRod);
25+
return count;
26+
}

DSA /course-questions/sample.js

Whitespace-only changes.

DSA /self-practice/index.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Q) Write a recursive function called sumToN that takes a positive integer n as input and returns the sum of all integers from 1 to n.
2+
3+
// function sumToN(n) {
4+
// //base case
5+
// if(n===1) return 1;
6+
// //recursive case
7+
// return (sumToN(n-1) + n)
8+
// }
9+
10+
// console.log(sumToN(3));
11+
12+
// Q) Write a recursive function called countDown that takes a positive integer n and returns an array counting down from n to 1.
13+
14+
// function countDown(n) {
15+
// //base case
16+
// if(n===1) return [1];
17+
// //recursive case
18+
// return [n, ...countDown(n-1)];
19+
// }
20+
21+
// console.log(countDown(5));
22+
23+
24+
// Q) Write a recursive function factorial that calculates the factorial of a number n.
25+
26+
function factorial(n) {
27+
//base case
28+
if(n===1) return 1;
29+
//recursive case
30+
return (factorial(n-1) * n)
31+
}
32+
33+
console.log(factorial(5));
34+
35+
// T = O(n) as the number of operations * number of nodes is equal to n and S = O(n) as the depth of the tree is equal to n as well
36+
37+
// Q) Write a recursive function fibonacci that returns the nth number in the Fibonacci sequence.
38+
39+
function fibonacci(n) {
40+
// base case
41+
if(n<=2) return 1;
42+
//recursive case
43+
return (fibonacci(n-1) + fibonacci(n-2))
44+
}
45+
46+
console.log(fibonacci(9));
47+
48+
49+
// Q) Write a recursive function reverseString that reverses a string.
50+
51+
function reverseString(str) {
52+
//base case
53+
if(str.length<=1) return str;
54+
//recursive case
55+
return reverseString(str.slice(1)) + str[0];
56+
}
57+
58+
console.log(reverseString("apple"))
59+
60+
//"apple" => "elppa"
61+
62+
// Q) Write a recursive function arraySum that adds up all numbers in an array.
63+
64+
function arraySum(arr) {
65+
//base case
66+
if(arr.length===0) return 0;
67+
//recursive case
68+
return (arr[0] + arraySum(arr.slice(1)))
69+
}
70+
71+
console.log(arraySum([1,2, 3, 4]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// SCOPE + LEXICAL SCOPE

0 commit comments

Comments
 (0)