├── Problems ├── 01 Reverse an array.md ├── 02 Linear Search.md ├── 03 Transpose matrix.md ├── 04 Reverse a linked list.md ├── 05 Insertion sort.md ├── 06 Merge two sorted LL.md ├── 07 Check element in BST.md ├── 08 Inorder traversal.md ├── 09 Insert a node in Binary Tree.md ├── 10 Print first n fibanacci number.md ├── 12 Inorder Traversal.md └── 13 BFS of Graph.md └── README.md /Problems/01 Reverse an array.md: -------------------------------------------------------------------------------- 1 | #### Problem 01: reverse the array 2 | 3 | - This code defines a function reverseArray that takes an integer array arr and its length n as input and reverses the order of elements in the array. Here's how the code works 4 | 5 | ![image](https://user-images.githubusercontent.com/93985255/233402536-a6060534-d148-4325-8220-72afdd723502.png) 6 | 7 | - The function initializes two variables, s and e, to the first and last index of the array, respectively. These variables represent the two endpoints of the array that will be swapped. 8 | - The function enters a while loop that continues until s is greater than or equal to e. This loop iteratively swaps the elements at the s and e indices of the array, and then increments s and decrements e. 9 | - The swap function is used to exchange the values of the two elements being swapped. 10 | 11 | - After the loop finishes executing, the array is reversed and the function terminates. 12 | -------------------------------------------------------------------------------- /Problems/02 Linear Search.md: -------------------------------------------------------------------------------- 1 | #### Problem 02: Linear Search 2 | 3 | This is linear search algorithm that searches for an integer value X in an integer array arr of size N. The function returns the index of the first occurrence of X in the array arr, or -1 if X is not present in the array. 4 | ![image](https://user-images.githubusercontent.com/93985255/233403880-c69585b1-9e05-400b-9915-3ea66d69c682.png) 5 | 6 | - The function starts by iterating through each element of the array arr using a for loop, with the loop variable i starting from 0 and ending at N-1. For each element of the array arr[i], the code checks if it is equal to the search value X using the == operator. 7 | - If arr[i] is equal to X, then the function immediately returns the index i. This is because we want to find the first occurrence of the search value, and the loop starts from the beginning of the array. 8 | - If the search value is found at index i, then there is no need to search the rest of the array as we are only interested in the first occurrence. 9 | - If the search value is not found in the entire array, then the function returns -1. This indicates that the search value is not present in the array arr. 10 | -------------------------------------------------------------------------------- /Problems/03 Transpose matrix.md: -------------------------------------------------------------------------------- 1 | #### Problem 03: Transpose of Matrix 2 | 3 | 4 | - The function takes two arguments: the matrix (as a reference to a vector of vectors of integers) and the size of the matrix (which is assumed to be n x n). 5 | 6 | ![image](https://user-images.githubusercontent.com/93985255/233677486-d962a082-6c3e-4b47-b7ef-7ee5badcfe49.png) 7 | 8 | - The outer loop iterates over the rows of the matrix (i.e., from 0 to n-1). 9 | The inner loop iterates over the columns of the matrix (i.e., from i+1 to n-1). By starting at i+1, the function only iterates over the upper triangle of the matrix, excluding the diagonal. 10 | 11 | - Inside the inner loop, the swap function is used to exchange the elements at (i, j) and (j, i), which gives the transpose of the matrix. 12 | 13 | -------------------------------------------------------------------------------- /Problems/04 Reverse a linked list.md: -------------------------------------------------------------------------------- 1 | #### Problem 04: Reverse a linked list 2 | 3 | - The function takes a pointer to the head node of a singly linked list as input and returns a pointer to the head node of the reversed list. 4 | ![image](https://user-images.githubusercontent.com/93985255/233793953-fbf13218-d811-4bc8-9da6-a22cb3d8f4fd.png) 5 | 6 | - The function first checks if the input pointer is NULL, which indicates an empty list. In that case, it returns NULL since there's nothing to reverse. 7 | - If the input pointer is not NULL, the function initializes three pointers p, curr, and n. curr initially points to the head node of the list, while p and n are both NULL. 8 | - The function then enters a loop that continues until curr becomes NULL. Within this loop, it first sets the next pointer of the current node curr to p. This effectively reverses the direction of the linked list by pointing the current node to the previous node in the list. 9 | - Next, the function updates p and curr to the current and next nodes, respectively, using the values of n and curr->next. 10 | - Finally, the function checks if n is not NULL, which means there is at least one more node in the list to process. If n is not NULL, the function updates n to point to the next node in the list. 11 | - Once the loop ends, the function returns the pointer p, which now points to the head of the reversed list. 12 | -------------------------------------------------------------------------------- /Problems/05 Insertion sort.md: -------------------------------------------------------------------------------- 1 | 2 | #### Problem 05: Insertion sort 3 | 4 | 5 | - The Insertion Sort algorithm works by iteratively comparing each element of the array with the elements before it and inserting the element at the correct position in the sorted part of the array. 6 | ![image](https://user-images.githubusercontent.com/93985255/235308144-59563289-ef3b-45e0-9574-56426ac7235e.png) 7 | 8 | - The function takes an array arr and its size n as input arguments. 9 | 10 | - The outer loop of the function iterates over each element of the array arr except the last element (i.e., n-1) starting from index 0. The inner loop iterates from the next element (i.e., i+1) to the first element 11 | - Within the inner loop, the if statement checks if the current element at index j is smaller than its previous element at index j-1. If it is true, then it swaps the two elements by using a temporary variable temp. 12 | - This ensures that the smaller element is moved towards the beginning of the array, and the larger element is moved towards the end. 13 | 14 | - After the inner loop completes, the array is sorted up to the i+1th element. The outer loop continues the process until all elements are sorted. 15 | 16 | -------------------------------------------------------------------------------- /Problems/06 Merge two sorted LL.md: -------------------------------------------------------------------------------- 1 | 2 | #### Problem 6: Merge two sorted LL. 3 | 4 | - This is the code of merge function for merging two sorted linked lists, head1 and head2, into a single sorted linked list. The function takes the heads of the two lists as input and returns the head of the merged list. 5 | ![image](https://user-images.githubusercontent.com/93985255/234627896-4e51c9b1-3fcf-4346-b735-795445d0965b.png) 6 | 7 | - The basic idea behind the merge function is to compare the nodes at the head of each list and append the smaller node to the merged list. 8 | We first check if either of the lists is empty, and if so, return the other list as the merged list. 9 | - We create a new linked list head and tail pointer to keep track of the merged list. 10 | We then compare the first node of both lists and append the smaller node to the merged list. 11 | We update the head and tail pointers accordingly. 12 | - We continue comparing the nodes at the heads of each list and append the smaller node to the merged list until we traversed through both the lists. Finally, we append the remaining nodes of either list to the merged list by simply pointing the tail pointer to the remaining list 13 | - Once we have merged the two lists, we return the head of the merged list. 14 | 15 | -------------------------------------------------------------------------------- /Problems/07 Check element in BST.md: -------------------------------------------------------------------------------- 1 | #### Problem 7: To check element is present of not in the Binary search tree. 2 | 3 | - This is a code to find a given integer value x in a BST. The function takes two arguments: a pointer to the root of the BST, and the value x that needs to be searched. 4 | ![image](https://user-images.githubusercontent.com/93985255/235307984-7ee80784-f999-434b-bda0-937f8814bfc3.png) 5 | - The function starts by checking if the root node is not NULL, which means that the BST is not empty. If the root node's data matches the value x, then the function immediately returns true, as the value has been found in the BST. 6 | - If the root node's data is less than x, then the value x can only be present in the right subtree of the root node. 7 | So, the function updates the root pointer to point to the right subtree and continues searching from there. 8 | - If the root node's data is greater than x, then the value x can only be present in the left subtree of the root node. 9 | So, the function updates the root pointer to point to the left subtree and continues searching from there. 10 | - The search continues in this way until either the value x is found in the BST, in which case the function returns true, or the root node becomes NULL, indicating that the value x is not present in the BST. In this case, the function returns false. 11 | 12 | -------------------------------------------------------------------------------- /Problems/08 Inorder traversal.md: -------------------------------------------------------------------------------- 1 | Problem 08 : Inorder Traversal of Binary tree 2 | 3 | - The function takes a pointer to the root node of the binary tree as its input parameter. The root node is a pointer to a Node struct, which typically contains data, left and right child pointers. 4 | ![image](https://user-images.githubusercontent.com/93985255/236871282-6262e779-82c9-41b1-aad6-28bf3f0545f7.png) 5 | 6 | 7 | - The function first creates an empty vector res to store the inorder traversal result. 8 | 9 | - Then it checks if the root node is not NULL. If the root node is not NULL, it recursively traverses the left subtree by calling the inOrder function on the left child of the current node. This will visit all nodes in the left subtree before the current node. 10 | 11 | - Then the function pushes the data of the current node into the vector. This step will add the value of the current node to the vector after visiting all nodes in the left subtree. 12 | 13 | - Next, the function recursively traverses the right subtree by calling the inOrder function on the right child of the current node. This will visit all nodes in the right subtree after the current node. 14 | 15 | - Finally, the function returns the result vector res which contains the inorder traversal of the binary tree. 16 | -------------------------------------------------------------------------------- /Problems/09 Insert a node in Binary Tree.md: -------------------------------------------------------------------------------- 1 | Problem 09: Insert a node in Binary Tree 2 | 3 | - This is the insertion of a node into a binary search tree 4 | 5 | - The insert() function takes two parameters: root, which represents the root node of the BST, and key, which is the value to be inserted into the BST. 6 | ![image](https://github.com/prashantjagtap2909/Coding-Challenge/assets/93985255/8e2bcf37-8ff2-41ea-b903-e4d5c5ca2914) 7 | 8 | - Initially, the function checks if the root is NULL, which indicates an empty tree. If it is NULL, a new node temp is created with the given key value, and it becomes the root of the BST. The function then returns the temp node. 9 | - If the root is not NULL, the function proceeds to check if the key value is already present in the BST. If it is, the function simply returns the root without making any changes to the tree. 10 | - If the key value is not present in the BST, the function determines the correct position to insert the new node based on the property of a BST. If the key is greater than the root->data, it means the new node should be inserted in the right subtree. 11 | - The insert() function is recursively called with the root->right as the new root and the key value. The return value of this recursive call is assigned to root->right, connecting the new node to the right subtree. 12 | - If the key value is less than the root->data, it means the new node should be inserted in the left subtree. The insert() function is recursively called with the root->left as the new root and the key value. 13 | - The return value of this recursive call is assigned to root->left, connecting the new node to the left subtree. 14 | 15 | - Finally, the function returns the root node, which represents the modified BST after the insertion. 16 | -------------------------------------------------------------------------------- /Problems/10 Print first n fibanacci number.md: -------------------------------------------------------------------------------- 1 | problem 10 : Print first n Fibonacci Numbers 2 | 3 | - This code is an implementation of a function named printFibb that takes an integer n as an input parameter and returns a vector of type long long containing the first n numbers in the Fibonacci sequence. 4 | - ![image](https://github.com/prashantjagtap2909/Coding-Challenge/assets/93985255/1c7d6824-b247-46fa-b7ef-0a188c26f280) 5 | 6 | - The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers, starting from 0 and 1. Therefore, the first two numbers in the sequence are always 0 and 1, and subsequent numbers are obtained by adding the previous two. 7 | - The function initializes a vector v of size n with all elements initialized to zero. It then sets the first two elements of the vector to 1, as these are the first two numbers in the Fibonacci sequence. 8 | - The function then enters a loop starting from the third element of the vector (index 2) up to the last element (index n-1). 9 | - In each iteration of the loop, the value of the current element is set to the sum of the previous two elements, which is calculated by adding the value of the element at index i-1 and the value of the element at index i-2. 10 | - Finally, the function returns the vector v containing the first n numbers in the Fibonacci sequence. 11 | -------------------------------------------------------------------------------- /Problems/12 Inorder Traversal.md: -------------------------------------------------------------------------------- 1 | 2 | ### Problem 12: Inorder Traversal 3 | 4 | - This is the in-order traversal of a binary tree which returns a vector of integers containing the values of the tree nodes in the in-order sequence. 5 | - ![image](https://github.com/prashantjagtap2909/Coding-Challenge/assets/93985255/d689c41e-1860-4fb3-9ab8-453c7b7a604e) 6 | 7 | - The function inOrder takes a pointer to a Node object as its parameter. Node seems to be a structure or class representing a node in a binary tree. 8 | 9 | - It declares a vector of integers named res to store the values of the nodes in the in-order sequence. 10 | - The first conditional statement checks if the root is not NULL, which means there is a node in the tree. 11 | 12 | - Inside the conditional statement, the inOrder function is recursively called on the left child of the current root. This step traverses the left subtree of the current node. 13 | - Then, the code prints the value of the current root node using cout. This printing is performed as a step in the in-order traversal, where the values of the nodes are visited in ascending order. 14 | - After printing the current node's value, the inOrder function is recursively called on the right child of the current root. This step traverses the right subtree of the current node. 15 | -------------------------------------------------------------------------------- /Problems/13 BFS of Graph.md: -------------------------------------------------------------------------------- 1 | ### Problem 13 - BFS of Graph 2 | 3 | 4 | - The function bfsOfGraph takes two parameters: V (the number of vertices in the graph) and adj[] (an array of vectors representing the adjacency list of the graph). 5 | ![image](https://github.com/prashantjagtap2909/Coding-Challenge/assets/93985255/db9210de-fa40-4460-9359-38e512e17fa7) 6 | 7 | 8 | - Two empty vectors are initialized: res (which will store the BFS traversal order) and vis (which will keep track of visited vertices). res will be returned as the result. 9 | 10 | - A queue q is created to store the vertices to be processed during the BFS traversal. 11 | 12 | - The first vertex (vertex 0) is added to the queue and marked as visited (vis[0] = 1). 13 | 14 | - The BFS traversal begins with a loop that continues until the queue becomes empty. 15 | 16 | - In each iteration of the loop, the front vertex (node) is removed from the queue using q.front() and q.pop(). 17 | 18 | - The current vertex (node) is added to the res vector to store the BFS traversal order. 19 | 20 | - For each adjacent vertex it of the current vertex (node), the following steps are performed: 21 | 22 | - If the adjacent vertex it has not been visited (!vis[it]), it is added to the queue using q.push(it) and marked as visited (vis[it] = 1). 23 | - After the BFS traversal is complete, the res vector containing the BFS traversal order is returned. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Challenge..... [![Views](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fprashantjagtap2909%2FCoding-Challenge&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=Views&edge_flat=false)](https://hits.seeyoufarm.com) 2 | - It is a coding challenge to stay consistent for 100 days. 3 | - Will post a DSA question everyday with approach to solve as I does on twitter. 4 | - Follow journey 5 | 6 | 7 | ### [Content]() 8 | - [1 - Reverse an array](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/01%20Reverse%20an%20array.md) 9 | - [2 - Linear Search](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/02%20Linear%20Search.md) 10 | - [3 - Transpose of matrix](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/03%20Transpose%20matrix.md) 11 | - [4 - Reverse a linked list](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/04%20Reverse%20a%20linked%20list.md) 12 | - [5 - Insertion sort](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/05%20Insertion%20sort.md) 13 | - [6 - Merge two sorted LL](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/06%20Merge%20two%20sorted%20LL.md) 14 | - [7 - Check element in BST]( https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/07%20Check%20element%20in%20BST.md) 15 | - [8 - Inorder Traversal](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/08%20Inorder%20traversal.md) 16 | - [9 - Insert a node in Binary tree](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/09%20Insert%20a%20node%20in%20Binary%20Tree.md) 17 | - [10 - Print n fibanacci number](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/10%20%20Print%20first%20n%20fibanacci%20number.md) 18 | - [10 - Print n fibanacci number](https://github.com/prashantjagtap2909/Coding-Challenge/blob/main/Problems/10%20%20Print%20first%20n%20fibanacci%20number.md) 19 | 20 | --------------------------------------------------------------------------------