From dd973a39454e7c27b8f669f56687caf43436ecab Mon Sep 17 00:00:00 2001 From: black shadow Date: Wed, 16 Oct 2024 00:19:18 +0530 Subject: [PATCH 1/3] Added the number of ways to kth stare program --- C++/Numbers_Of_Ways_to_Kth_stare.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 C++/Numbers_Of_Ways_to_Kth_stare.py diff --git a/C++/Numbers_Of_Ways_to_Kth_stare.py b/C++/Numbers_Of_Ways_to_Kth_stare.py new file mode 100644 index 0000000..88e57a6 --- /dev/null +++ b/C++/Numbers_Of_Ways_to_Kth_stare.py @@ -0,0 +1,23 @@ +def numberOfWays(k: int) -> int: + # dp[i] will store the number of ways to reach stair i + dp = [0] * (k + 1) + + # Base case: 1 way to reach the first stair + dp[1] = 1 + + # Iterate over each stair up to k + for i in range(1, k): + jump = 0 + stair = i + # Keep jumping upwards as long as within bounds + while stair <= k: + if stair <= k: + dp[stair] += dp[i] + jump += 1 + stair = i + 2**jump + + return dp[k] + +# Test the function +k = 10 # example value +print(numberOfWays(k)) From 6bc6b1a71c742d29def51b72236090dd8186ef5f Mon Sep 17 00:00:00 2001 From: black shadow Date: Wed, 16 Oct 2024 00:20:18 +0530 Subject: [PATCH 2/3] Added the number of ways to kth stare program --- C++/Numbers_Of_Ways_to_Kth_stare.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/C++/Numbers_Of_Ways_to_Kth_stare.py b/C++/Numbers_Of_Ways_to_Kth_stare.py index 88e57a6..1f56495 100644 --- a/C++/Numbers_Of_Ways_to_Kth_stare.py +++ b/C++/Numbers_Of_Ways_to_Kth_stare.py @@ -1,3 +1,9 @@ +#This is a hard Leetcode Problem which uses a concept of memoization in recursion . Here is the question : +# https://leetcode.com/problems/find-number-of-ways-to-reach-the-k-th-stair/description + +# this is the solution to the above leetcode problem + + def numberOfWays(k: int) -> int: # dp[i] will store the number of ways to reach stair i dp = [0] * (k + 1) From ea18943d01f0db6771388c6a240d44e33d5ff058 Mon Sep 17 00:00:00 2001 From: black shadow Date: Wed, 16 Oct 2024 00:26:24 +0530 Subject: [PATCH 3/3] Added the Program for Merge Sort Algorithm --- C++/Merge_sort_Algorithm.cpp | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 C++/Merge_sort_Algorithm.cpp diff --git a/C++/Merge_sort_Algorithm.cpp b/C++/Merge_sort_Algorithm.cpp new file mode 100644 index 0000000..3aaab0e --- /dev/null +++ b/C++/Merge_sort_Algorithm.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +// Function to merge two subarrays of arr[] +void merge(int arr[], int left, int mid, int right) { + // Find sizes of two subarrays to be merged + int n1 = mid - left + 1; + int n2 = right - mid; + + // Create temporary arrays + int L[n1], R[n2]; + + // Copy data to temp arrays L[] and R[] + for (int i = 0; i < n1; i++) + L[i] = arr[left + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[mid + 1 + j]; + + // Merge the temporary arrays back into arr[left..right] + int i = 0; // Initial index of first subarray + int j = 0; // Initial index of second subarray + int k = left; // Initial index of merged subarray + + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + + // Copy the remaining elements of L[], if any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy the remaining elements of R[], if any + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} + +// Function to implement Merge Sort +void mergeSort(int arr[], int left, int right) { + if (left < right) { + // Find the middle point + int mid = left + (right - left) / 2; + + // Recursively sort the two halves + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + + // Merge the sorted halves + merge(arr, left, mid, right); + } +} + +// Utility function to print an array +void printArray(int arr[], int size) { + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +int main() { + int arr[] = {38, 27, 43, 3, 9, 82, 10}; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Original array: \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "Sorted array: \n"; + printArray(arr, arr_size); + + return 0; +}