├── BinaryExponentiation.java ├── C++ ├── 3Sums.cpp ├── Activity_selection_greedy.cpp ├── Add digits of a number until single digit is found.cpp ├── Bipartite_Graph.cpp ├── BucketSort.cpp ├── CoinChange.cpp ├── Coin_Change.cpp ├── FirstSearch.cpp ├── Heap sort.cpp ├── Huffman_Coding.cpp ├── Kadane's_Algorithm.cpp ├── LongestCommonSubsequence.cpp ├── LongestIncreasingSubsequence.cpp ├── Longest_Common_Subsequence.cpp ├── MSTgraph.cpp ├── Max_profit.cpp ├── Merge Sorted Array.cpp ├── Merge_two_sorted_linkedlists.cpp ├── Min_Stack.cpp ├── Min_heap_to_Max_heap.cpp ├── N meetings in room.cpp ├── N-Queens.cpp ├── Pascal's_Triangle.cpp ├── Prims_MST.cpp ├── Reverse_Doubly_Linked_List.cpp ├── Reverse_linkedlist.cpp ├── Rotate_Matrix_Except_Diagonals.cpp ├── Segment_Tree.cpp ├── Selection_Sort_Visualised.cpp ├── ShortestCommonSupersequence.cpp ├── SubsetSum.cpp ├── addTwoMatrices.cpp ├── beautiful_string.cpp ├── bestTime ToBuyAndSellStock.cpp ├── binarytreeVariant.cpp ├── catalan_numbers.cpp ├── celebrity problem.cpp ├── check_circular_linkedlist.cpp ├── cloneGraph.cpp ├── connected_components.cpp ├── dedup_with_set.cpp ├── dijkstra.cpp ├── divide_two_integers_bit_manipulation.cpp ├── duplicate_element_in_array.cpp ├── dynamic_linear-search.cpp ├── egg_dropping_problem.cpp ├── element_deletion_from_begining_singly_linked_list.cpp ├── flip_bits.cpp ├── graph_ds.cpp ├── hello.cpp ├── infinite_sum_array.cpp ├── minOperations.cpp ├── patternSearching.cpp ├── powerOfNum.cpp ├── product_of_array_except_Self.cpp ├── radix_sort.cpp ├── rod_cutting.cpp ├── rotateAlgorithm.cpp ├── search_element_in_row_column_sorted_matrix.cpp ├── shellsort.cpp ├── sieve_of_eratosthenes.cpp ├── spiralprint.cpp ├── stockSpan.cpp ├── sudoku.cpp ├── sudoku_solve.cpp ├── three-pointers.cpp ├── tilingProblem.cpp ├── tower_of_hanoi.cpp ├── tree traversal │ ├── Queuecpp.h │ └── treecpp.cpp └── waveSort.cpp ├── C ├── BinarySearchTraversal.c ├── Circular_Queue.c ├── Doubly_linked_list_all_operation.c ├── Linkedlist.c ├── NQueenProblem.c ├── Queue.c ├── Typing Tutor Project in C │ ├── Typing Tutor Coding │ │ ├── ASD.DAT │ │ ├── ATOZ.TXT │ │ ├── HOOKS.TXT │ │ ├── JACK.DAT │ │ ├── LESSONLI.DAT │ │ ├── PANKAJ.DAT │ │ ├── PUSHKAR.DAT │ │ ├── RADIO.TXT │ │ ├── REGAN.DAT │ │ ├── STAT.DAT │ │ ├── SUJAN.DAT │ │ ├── SUYOGYA.DAT │ │ ├── TT.C │ │ ├── TT.EXE │ │ ├── TT.H │ │ ├── TT.OBJ │ │ ├── TT.PIF │ │ ├── USERLIST.DAT │ │ └── WELCOME.C │ └── readme.md ├── countSort.c ├── hello.c ├── mergeSort.c ├── queue_implement.c ├── quickSort.c └── randomizedQuickSort.c ├── HackerRank-C-main ├── 1D Arrays in C.c ├── Array Reversal.c ├── Bitwise Operators.c ├── Boxes through a Tunnel.c ├── Calculate the Nth term.c ├── Conditional Statements in C.c ├── Digit Frequency.c ├── Dynamic Array in C.c ├── For Loop in C.c ├── Functions in C.c ├── Playing With Characters.c ├── Pointers in C.c ├── Printing Pattern Using Loops.c ├── Printing Tokens.c ├── README.md ├── Small Triangles, Large Triangles.c ├── Students Marks Sum.c ├── Sum and Difference of Two Numbers.c ├── Sum of Digits of a Five Digit Number.c ├── Variadic functions in C.c └── _Hello World!_ in C.c ├── HackerRank-Python-main ├── Alphabet Rangoli.py ├── Arithmetic Operators.py ├── Calendar Module.py ├── Capitalize!.py ├── Designer Door Mat.py ├── Find a String.py ├── Find the Runner-Up Score!.py ├── Finding the percentage.py ├── Incorrect Regex.py ├── Input().py ├── Integers Come In All Sizes.py ├── List Comprehensions.py ├── Lists.py ├── Loops.py ├── Mod Divmod.py ├── Mutations.py ├── Nested Lists.py ├── Polar Coordinates.py ├── Power - Mod Power.py ├── Print Function.py ├── Python If-Else.py ├── Python_ Division.py ├── README.md ├── Say _Hello, World!_ With Python.py ├── Set .difference() Operation.py ├── Set .discard(), .remove() & .pop().py ├── Set .intersection() Operation.py ├── Set .union() Operation.py ├── String Formatting.py ├── String Split and Join.py ├── String Validators.py ├── Text Alignment.py ├── Text Wrap.py ├── Triangle Quest.py ├── Tuples.py ├── What's Your Name_.py ├── Write a function.py ├── hourglass_sum.py ├── python.png └── sWAP cASE.py ├── HackerRank-SQL-main ├── Binary Tree Nodes.sql ├── Employee Names.sql ├── Employee Salaries.sql ├── Higher Than 75 Marks.sql ├── Japanese Cities' Attributes.sql ├── Japanese Cities' Names.sql ├── README.md ├── Revising the Select Query I.sql ├── Revising the Select Query II.sql ├── Select All.sql ├── Select By ID.sql ├── Tables │ └── regexp_sql.png ├── The PADS.sql ├── Type of Triangle.sql ├── Weather Observation Station 1.sql ├── Weather Observation Station 10.sql ├── Weather Observation Station 11.sql ├── Weather Observation Station 12.sql ├── Weather Observation Station 3.sql ├── Weather Observation Station 4.sql ├── Weather Observation Station 5.sql ├── Weather Observation Station 6.sql ├── Weather Observation Station 7.sql ├── Weather Observation Station 8.sql └── Weather Observation Station 9.sql ├── Java ├── Anagrams.java ├── ArrayToSet.java ├── B_Shifting_Sort.java ├── BinarySearch.java ├── Calculator1.java ├── CocktailSort.java ├── Currency Converter in Java ├── Duplicate_in_Array_O(N)_solution.java ├── EMICal.java ├── Factorial.java ├── GUIComponnts.rar ├── HeapSort.java ├── PalindromeNumber.java ├── RemoveDuplicatesInSortedArray.java ├── ReturnPreviousDate ├── Reverse_string.java ├── SelectionSort.java ├── Sieve_Of_Eratosthenes.java ├── calculator.java ├── graphs │ ├── DPQ.java │ ├── LinkedGraph.java │ └── MyGraph.java ├── hello.java ├── lockerPuzzle.java ├── pi.java └── random_password_generator.java ├── LinearPower.java ├── Python ├── AddingNumbers.py ├── AreaOrPerimeter.py ├── BinaryNumbers.py ├── CarGame.py ├── FactorialOfNumbers.py ├── File_handling.py ├── FizzBuzz.py ├── HeapSort.py ├── Linear Regression │ ├── Linear REgression.ipynb │ ├── titanic_test.csv │ ├── titanic_test_prediction.csv │ └── titanic_train.csv ├── Monte_Carlo_Algorithm.py ├── Pet Race.py ├── Remote Procedure Call │ ├── readme.md │ ├── rpc_client.py │ └── rpc_server.py ├── Shell_Sort.py ├── binary_search.py ├── binary_to_decimal.py ├── bubble_sort.py ├── decimal_to_hexadecimal.py ├── diffie_hellman.py ├── el_gamal_encryption.py ├── hello.py ├── insertion_sort.py ├── linear_search.py ├── mongean.py ├── open-cv │ └── face-recoginition.py ├── pareto_optimal_vectors_search.py ├── pushupsCounter.py ├── rsa_encryption_algorithm.py ├── screenshot.py └── selection_sort.py ├── README.md ├── Radix - sort ├── TIC TAC TOE PROJECT.cpp ├── Web_scraping.py ├── inversion_count.cpp ├── javascript ├── 7.Variables │ ├── README.md │ ├── alert.png │ ├── const.png │ ├── op.png │ ├── output.png │ ├── word-image-109.png │ └── word-image-110.png ├── CanvasImages.html ├── Celciuc_to_fahrenheit.js ├── bubble_sort.js ├── clock.js ├── hello.js ├── js-snake-game.html └── kadanes_algorithm.js └── php ├── GET.php ├── POST.php ├── String.php ├── array.php ├── encrypt.php ├── hello.php ├── phpajax.html └── validation.php /BinaryExponentiation.java: -------------------------------------------------------------------------------- 1 | package com.saikat; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BinaryExponentiation { 6 | public static void main(String[] args) { 7 | System.out.println("Enter base number"); 8 | Scanner in = new Scanner(System.in); 9 | double base = in.nextDouble(); 10 | System.out.println("Enter exponent"); 11 | int power = in.nextInt(); 12 | System.out.println(binaryExponentiation(base, power)); 13 | } 14 | static double binaryExponentiation(double x, int n) 15 | { 16 | if(n<0) 17 | return negPow(x,n); 18 | if(n==0) 19 | return 1; //base case 20 | double res = binaryExponentiation(x,n/2); 21 | if(n%2==1) 22 | return x*(res*res); // if n is odd 23 | return res*res; 24 | } 25 | static double negPow(double x, int n) 26 | { 27 | if(n<0) 28 | return 1/x*negPow(1/x,-(n+1)); 29 | return binaryExponentiation(x,n); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /C++/Activity_selection_greedy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1 3 | 4 | There is one meeting room in a firm. There are N meetings in the form of (start[i], end[i]) where start[i] is start time of meeting i and end[i] is finish time of meeting i. 5 | What is the maximum number of meetings that can be accommodated in the meeting room when only one meeting can be held in the meeting room at a particular time? 6 | 7 | Note: Start time of one chosen meeting can't be equal to the end time of the other chosen meeting. 8 | 9 | 10 | Input: 11 | N = 6 12 | start[] = {1,3,0,5,8,5} 13 | end[] = {2,4,6,7,9,9} 14 | Output: 15 | 4 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | 21 | // } Driver Code Ends 22 | class Solution 23 | { 24 | static bool comp(paira,pairb){ 25 | return a.secondarr[n]; 34 | for(int i=0;icurr=arr[0]; 41 | for(int i=1;i> t; 62 | while (t--) { 63 | int n; 64 | cin >> n; 65 | int start[n], end[n]; 66 | for (int i = 0; i < n; i++) cin >> start[i]; 67 | 68 | for (int i = 0; i < n; i++) cin >> end[i]; 69 | 70 | Solution ob; 71 | int ans = ob.maxMeetings(start, end, n); 72 | cout << ans << endl; 73 | } 74 | return 0; 75 | } // } Driver Code Ends 76 | -------------------------------------------------------------------------------- /C++/Add digits of a number until single digit is found.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int singledigit(int); 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int result= singledigit(n); 8 | cout<0){ 15 | 16 | b=n%10; 17 | n=n/10; 18 | sum=sum+b; 19 | } 20 | if(sum%10==sum){ 21 | return sum; 22 | } 23 | else{ 24 | singledigit(sum); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /C++/Bipartite_Graph.cpp: -------------------------------------------------------------------------------- 1 | // Bipartite Graph 2 | // --------------- 3 | // You can divide all the vertices of graph in 2 sets 4 | // such that all edges of the graph are from set1 to set2 5 | 6 | // Two Coloring 7 | 8 | 9 | /// How to flip colors between 1 and 2 ? 10 | // Use 3-color 11 | // 3-2 = 1 and 3-1 = 2 12 | 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | bool dfs_helper(vector graph[], int node, int visited[], int parent, int color){ 18 | // come to node and assign the color 19 | visited[node] = color; // 1 or 2 -- both means visited 20 | for(auto nbr: graph[node]){ 21 | // Many possibilities 22 | if(visited[nbr] == 0){ 23 | int subprob = dfs_helper(graph, nbr, visited, node, 3-color); 24 | if(subprob == false){ 25 | return false; 26 | } 27 | } 28 | else if(nbr != parent and color == visited[nbr]){ 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | // Yes or No, whether graph is Bipartite or Not 36 | bool dfs(vector graph[], int N){ 37 | // Visited array states : 38 | // 0 -> Not visited 39 | // 1 -> Color 1 40 | // 2 -> Color 2 41 | int visited[N]; 42 | for(int i=0; i<=N; i++){ 43 | visited[i] = 0; 44 | } 45 | 46 | int color = 1; 47 | int ans = dfs_helper(graph,0,visited,-1, color); 48 | 49 | for(int i=0; i" << visited[i] << "\n"; 51 | } 52 | 53 | return ans; 54 | } 55 | 56 | int main(){ 57 | 58 | int N, M; 59 | cin >> N >> M; 60 | 61 | vector graph[N]; 62 | 63 | while(M--){ 64 | int x, y; 65 | cin >> x >> y; 66 | graph[x].push_back(y); 67 | graph[y].push_back(x); 68 | } 69 | 70 | // By DFS, coloring the nodes at each step 71 | // Current node -> color 1 72 | // Neighbouring nodes -> color 2 73 | if(dfs(graph, N)){ 74 | cout << "Yes, its bipartite..\n"; 75 | } 76 | else{ 77 | cout << "No, its not bipartite..\n"; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /C++/BucketSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void bucketSort(float arr[], int n) 6 | { 7 | 8 | vector b[n]; 9 | 10 | for (int i = 0; i < n; i++) { 11 | int bi = n * arr[i]; 12 | b[bi].push_back(arr[i]); 13 | } 14 | 15 | for (int i = 0; i < n; i++) 16 | sort(b[i].begin(), b[i].end()); 17 | 18 | int index = 0; 19 | for (int i = 0; i < n; i++) 20 | for (int j = 0; j < b[i].size(); j++) 21 | arr[index++] = b[i][j]; 22 | } 23 | 24 | int main() 25 | { 26 | float arr[] 27 | = { 0.897, 0.565, 0.656, 0.234, 0.665, 0.484 }; 28 | int n = sizeof(arr) / sizeof(arr[0]); 29 | bucketSort(arr, n); 30 | 31 | cout << "Sorted array is \n"; 32 | for (int i = 0; i < n; i++) 33 | cout << arr[i] << " "; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /C++/CoinChange.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coin Change Problem 3 | 4 | Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. 5 | 6 | Examples: 7 | 8 | For N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. 9 | 10 | For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5. 11 | */ 12 | 13 | #include 14 | 15 | using namespace std; 16 | 17 | int count( int S[], int m, int n ) 18 | { 19 | int i, j, x, y; 20 | 21 | // We need n+1 rows as the table 22 | // is constructed in bottom up 23 | // manner using the base case 0 24 | // value case (n = 0) 25 | int table[n + 1][m]; 26 | 27 | // Fill the entries for 0 28 | // value case (n = 0) 29 | for (i = 0; i < m; i++) 30 | table[0][i] = 1; 31 | 32 | // Fill rest of the table entries 33 | // in bottom up manner 34 | for (i = 1; i < n + 1; i++) 35 | { 36 | for (j = 0; j < m; j++) 37 | { 38 | // Count of solutions including S[j] 39 | x = (i-S[j] >= 0) ? table[i - S[j]][j] : 0; 40 | 41 | // Count of solutions excluding S[j] 42 | y = (j >= 1) ? table[i][j - 1] : 0; 43 | 44 | // total count 45 | table[i][j] = x + y; 46 | } 47 | } 48 | return table[n][m - 1]; 49 | } 50 | 51 | // Driver Code 52 | int main() 53 | { 54 | int arr[] = {1, 2, 3}; 55 | int m = sizeof(arr)/sizeof(arr[0]); 56 | int n = 4; 57 | cout << count(arr, m, n); 58 | return 0; 59 | } 60 | 61 | // Output: 4 62 | 63 | // Time Complexity: O(mn) -------------------------------------------------------------------------------- /C++/Coin_Change.cpp: -------------------------------------------------------------------------------- 1 | // ---------------- OUTPUT ----------------------- 2 | // Enter the number of coins 3 | // 3 4 | // Enter the type of coins 5 | // 1 2 5 6 | // Enter the amount to be made 7 | // 11 8 | 9 | // Minimum number of coins required are : 3 10 | // ------------------------------------------------ 11 | 12 | #include 13 | using namespace std; 14 | 15 | int coinChange(vector &coins, int amount) 16 | { 17 | int n = coins.size(); 18 | int dp[n + 1][amount + 1]; 19 | 20 | for (int i = 0; i <= n; ++i) 21 | { 22 | for (int j = 0; j <= amount; ++j) 23 | { 24 | if (j == 0) 25 | dp[i][j] = 0; 26 | else if (i == 0) 27 | dp[i][j] = 1e5; 28 | else if (coins[i - 1] > j) // when coin is 2 and amount is 1 29 | dp[i][j] = dp[i - 1][j]; // exclude 30 | else 31 | dp[i][j] = min(1 + dp[i][j - coins[i - 1]], dp[i - 1][j]); 32 | } 33 | } 34 | return dp[n][amount] > 1e4 ? -1 : dp[n][amount]; 35 | } 36 | 37 | int main() 38 | { 39 | cout << "Enter the number of coins" << endl; 40 | int n; 41 | cin >> n; 42 | cout << "Enter the type of coins" << endl; 43 | vector coins(n, 0); 44 | for (int i = 0; i < n; i++) 45 | { 46 | cin >> coins[i]; 47 | } 48 | cout << "Enter the amount to be made" << endl; 49 | int amt; 50 | cin >> amt; 51 | 52 | cout << endl; 53 | cout << "Minimum number of coins required are : " << coinChange(coins, amt) << endl; 54 | cout << endl; 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /C++/FirstSearch.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to implement Best First Search using priority 2 | // queue 3 | #include 4 | using namespace std; 5 | typedef pair pi; 6 | 7 | vector > graph; 8 | 9 | // Function for adding edges to graph 10 | void addedge(int x, int y, int cost) 11 | { 12 | graph[x].push_back(make_pair(cost, y)); 13 | graph[y].push_back(make_pair(cost, x)); 14 | } 15 | 16 | // Function For Implementing Best First Search 17 | // Gives output path having lowest cost 18 | void best_first_search(int source, int target, int n) 19 | { 20 | vector visited(n, false); 21 | // MIN HEAP priority queue 22 | priority_queue, greater > pq; 23 | // sorting in pq gets done by first value of pair 24 | pq.push(make_pair(0, source)); 25 | int s = source; 26 | visited[s] = true; 27 | while (!pq.empty()) { 28 | int x = pq.top().second; 29 | // Displaying the path having lowest cost 30 | cout << x << " "; 31 | pq.pop(); 32 | if (x == target) 33 | break; 34 | 35 | for (int i = 0; i < graph[x].size(); i++) { 36 | if (!visited[graph[x][i].second]) { 37 | visited[graph[x][i].second] = true; 38 | pq.push(make_pair(graph[x][i].first,graph[x][i].second)); 39 | } 40 | } 41 | } 42 | } 43 | 44 | // Driver code to test above methods 45 | int main() 46 | { 47 | // No. of Nodes 48 | int v = 14; 49 | graph.resize(v); 50 | 51 | // The nodes shown in above example(by alphabets) are 52 | // implemented using integers addedge(x,y,cost); 53 | addedge(0, 1, 3); 54 | addedge(0, 2, 6); 55 | addedge(0, 3, 5); 56 | addedge(1, 4, 9); 57 | addedge(1, 5, 8); 58 | addedge(2, 6, 12); 59 | addedge(2, 7, 14); 60 | addedge(3, 8, 7); 61 | addedge(8, 9, 5); 62 | addedge(8, 10, 6); 63 | addedge(9, 11, 1); 64 | addedge(9, 12, 10); 65 | addedge(9, 13, 2); 66 | 67 | int source = 0; 68 | int target = 9; 69 | 70 | // Function call 71 | best_first_search(source, target, v); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /C++/Heap sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // To heapify a subtree rooted with node i which is 6 | // an index in arr[]. n is size of heap 7 | void heapify(int arr[], int n, int i) 8 | { 9 | int largest = i; // Initialize largest as root 10 | int l = 2 * i + 1; // left = 2*i + 1 11 | int r = 2 * i + 2; // right = 2*i + 2 12 | 13 | // If left child is larger than root 14 | if (l < n && arr[l] > arr[largest]) 15 | largest = l; 16 | 17 | // If right child is larger than largest so far 18 | if (r < n && arr[r] > arr[largest]) 19 | largest = r; 20 | 21 | // If largest is not root 22 | if (largest != i) { 23 | swap(arr[i], arr[largest]); 24 | 25 | // Recursively heapify the affected sub-tree 26 | heapify(arr, n, largest); 27 | } 28 | } 29 | 30 | // main function to do heap sort 31 | void heapSort(int arr[], int n) 32 | { 33 | // Build heap (rearrange array) 34 | for (int i = n / 2 - 1; i >= 0; i--) 35 | heapify(arr, n, i); 36 | 37 | // One by one extract an element from heap 38 | for (int i = n - 1; i > 0; i--) { 39 | // Move current root to end 40 | swap(arr[0], arr[i]); 41 | 42 | // call max heapify on the reduced heap 43 | heapify(arr, i, 0); 44 | } 45 | } 46 | 47 | /* A utility function to print array of size n */ 48 | void printArray(int arr[], int n) 49 | { 50 | for (int i = 0; i < n; ++i) 51 | cout << arr[i] << " "; 52 | cout << "\n"; 53 | } 54 | 55 | // Driver code 56 | int main() 57 | { 58 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 59 | int n = sizeof(arr) / sizeof(arr[0]); 60 | 61 | heapSort(arr, n); 62 | 63 | cout << "Sorted array is \n"; 64 | printArray(arr, n); 65 | } 66 | -------------------------------------------------------------------------------- /C++/Huffman_Coding.cpp: -------------------------------------------------------------------------------- 1 | // ---------------- OUTPUT ----------------------- 2 | // Enter the number of elements 3 | // 5 4 | // Enter the characters 5 | // A B C D F 6 | // Enter the frequencies of characters 7 | // 5 1 2 4 10 8 | 9 | // Huffman Codes are : 10 | // F : 0 11 | // A : 10 12 | // B : 1100 13 | // C : 1101 14 | // D : 111 15 | // ------------------------------------------------ 16 | 17 | #include 18 | using namespace std; 19 | 20 | struct Node { 21 | 22 | char data; 23 | int freq; 24 | Node *left, *right; 25 | 26 | Node(char data, int freq) 27 | { 28 | left = right = NULL; 29 | this->data = data; 30 | this->freq = freq; 31 | } 32 | }; 33 | 34 | struct compare{ 35 | 36 | bool operator()(Node* l, Node* r) 37 | { 38 | return (l->freq > r->freq); 39 | } 40 | }; 41 | 42 | void print(struct Node* root, string str) 43 | { 44 | if (!root) 45 | return; 46 | 47 | if (root->data != '$') 48 | cout <data<<" : "<left,str+"0"); 51 | print(root->right,str+"1"); 52 | } 53 | 54 | void HuffmanCoding(char data[], int freq[], int n) 55 | { 56 | Node *left, *right, *top; 57 | priority_queue, compare> minHeap; 58 | 59 | for (int i = 0; i < n; ++i) 60 | minHeap.push(new Node(data[i], freq[i])); 61 | 62 | while (minHeap.size() != 1) { 63 | 64 | left = minHeap.top(); 65 | minHeap.pop(); 66 | right = minHeap.top(); 67 | minHeap.pop(); 68 | top = new Node('$', left->freq + right->freq); 69 | top->left = left; 70 | top->right = right; 71 | minHeap.push(top); 72 | 73 | } 74 | cout<>n; 84 | 85 | cout<<"Enter the characters"<>arr[i]; 89 | 90 | cout<<"Enter the frequencies of characters"<>freq[i]; 94 | 95 | HuffmanCoding(arr, freq, n); 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /C++/Kadane's_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | /* Write an efficient program to find the sum of contiguous subarray 2 | within a one-dimensional array of numbers that has the largest sum. */ 3 | //if arr[]={-2,-3,4,-1,-2,1,5,-3} so the max contiguous sum possible will be 7 4 | 5 | #include 6 | using namespace std; 7 | 8 | class Solution{ 9 | public: 10 | // arr: input array 11 | // n: size of array 12 | //Function to find the sum of contiguous subarray with maximum sum. 13 | long long maxSubarraySum(int arr[], int n){ 14 | 15 | long long int min1 = INT_MIN; 16 | long long int sum =0; 17 | for(int i=0;i=min1) 21 | { 22 | min1 = sum; 23 | } 24 | if(sum<0) 25 | { 26 | sum=0; 27 | } 28 | } 29 | return min1; 30 | } 31 | }; 32 | //Complexity is O(n) 33 | int main() 34 | { 35 | int t,n; 36 | 37 | cin>>t; //input testcases 38 | while(t--) //while testcases exist 39 | { 40 | 41 | cin>>n; //input size of array 42 | 43 | int a[n]; 44 | 45 | for(int i=0;i>a[i]; //inputting elements of array 47 | 48 | Solution ob; 49 | 50 | cout << ob.maxSubarraySum(a, n) << endl; 51 | } 52 | } -------------------------------------------------------------------------------- /C++/LongestCommonSubsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, 3 | but not necessarily contiguous. 4 | Time complexity : O(N*M) where N and M are size of strings. 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | /* Returns length of LCS for X, Y */ 10 | int lcs(string X, string Y, int m, int n ) 11 | { 12 | if (m == 0 || n == 0) 13 | return 0; 14 | if (X[m-1] == Y[n-1]) 15 | return 1 + lcs(X, Y, m-1, n-1); 16 | else 17 | return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); 18 | } 19 | 20 | /* Driver program to test */ 21 | int main() 22 | { 23 | string X = "OCTOBER"; 24 | string Y = "HACKTOBER"; 25 | int m = X.size(); 26 | int n = Y.size(); 27 | cout<<"Length of LCS is "< 8 | using namespace std; 9 | 10 | /* lis() returns the LIS */ 11 | int lis(int arr[], int n) 12 | { 13 | int lis[n]; 14 | lis[0] = 1; 15 | // bottom-up manner dp 16 | for (int i = 1; i < n; i++) { 17 | lis[i] = 1; 18 | for (int j = 0; j < i; j++) 19 | if (arr[i] > arr[j] && lis[i] < lis[j] + 1) 20 | lis[i] = lis[j] + 1; 21 | } 22 | return *max_element(lis, lis + n); 23 | } 24 | 25 | /* Driver program */ 26 | int main() 27 | { 28 | int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; 29 | int n = sizeof(arr) / sizeof(arr[0]); 30 | cout<<"Length of lis is "< 11 | using namespace std; 12 | 13 | int max(int a, int b) 14 | { 15 | return (a > b) ? a : b; 16 | } 17 | 18 | int LCS(char *X, char *Y, int m, int n) 19 | { 20 | int L[m + 1][n + 1]; 21 | int i, j; 22 | 23 | for (i = 0; i <= m; i++) 24 | { 25 | for (j = 0; j <= n; j++) 26 | { 27 | if (i == 0 || j == 0) 28 | L[i][j] = 0; 29 | 30 | else if (X[i - 1] == Y[j - 1]) 31 | L[i][j] = L[i - 1][j - 1] + 1; 32 | 33 | else 34 | L[i][j] = max(L[i - 1][j], L[i][j - 1]); 35 | } 36 | } 37 | 38 | int index = L[m][n]; 39 | char lcs[index + 1]; 40 | lcs[index] = '\0'; 41 | 42 | i = m, j = n; 43 | while (i > 0 && j > 0) 44 | { 45 | if (X[i - 1] == Y[j - 1]) 46 | { 47 | lcs[index - 1] = X[i - 1]; 48 | i--; 49 | j--; 50 | index--; 51 | } 52 | 53 | else if (L[i - 1][j] > L[i][j - 1]) 54 | i--; 55 | else 56 | j--; 57 | } 58 | 59 | cout << lcs << "\n"; 60 | 61 | return L[m][n]; 62 | } 63 | 64 | int main() 65 | { 66 | 67 | char X[100]; 68 | char Y[100]; 69 | 70 | cout << "Enter the 1st string" << endl; 71 | gets(X); 72 | cout << "Enter the 2nd string" << endl; 73 | gets(Y); 74 | 75 | int m = strlen(X); 76 | int n = strlen(Y); 77 | 78 | cout << "The longest common subsequence is : "; 79 | int length = LCS(X, Y, m, n); 80 | cout << "Length of LCS is " 81 | << length; 82 | 83 | return 0; 84 | } -------------------------------------------------------------------------------- /C++/MSTgraph.cpp: -------------------------------------------------------------------------------- 1 | // DFS , Start time / End time , SubTree 2 | 3 | #include 4 | using namespace std; 5 | 6 | int timer=1; 7 | 8 | vector start; 9 | vector endt; 10 | 11 | void addedge(vector A[],int u,int v) 12 | { 13 | A[u].push_back(v); 14 | A[v].push_back(u); 15 | } 16 | 17 | void DFSatu(int j,vector d[],vector &vis) 18 | { 19 | 20 | start[j]=timer++; 21 | vis[j]=true; 22 | cout< c[] , int s) 36 | { 37 | 38 | vector visited(s,false); 39 | 40 | for(int i=1;istart[y])&&(endt[x]endt[y]) ) 57 | { 58 | cout<<" \n True"; 59 | } 60 | else 61 | { 62 | cout<<" \n False"; 63 | } 64 | 65 | } 66 | 67 | 68 | 69 | int main() 70 | { 71 | int v=6; 72 | vector a; 73 | vector b[v]; 74 | 75 | start.resize(v+1,0); 76 | endt.resize(v+1,0); 77 | 78 | addedge(b,1,2); 79 | addedge(b,2,3); 80 | addedge(b,3,4); 81 | //addedge(b,5,3); 82 | //addedge(b,5,4); 83 | //addedge(b,5,2); 84 | addedge(b,1,5); 85 | 86 | DFS(b,v); 87 | cout< 2 | using namespace std; 3 | 4 | stacks; 5 | stackt; 6 | 7 | void push(int val) { 8 | s.push(val); 9 | if(t.empty()) 10 | t.push(val); 11 | else{ 12 | if(val>n; 40 | 41 | for(int i=0;i>num; 44 | push(num); 45 | } 46 | 47 | cout<<"The current minimum is : "< 2 | using namespace std; 3 | 4 | void MaxHeapify(int arr[], int i, int n) 5 | { 6 | int l = 2*i + 1; 7 | int r = 2*i + 2; 8 | int largest = i; 9 | if (l < n && arr[l] > arr[i]) 10 | largest = l; 11 | if (r < n && arr[r] > arr[largest]) 12 | largest = r; 13 | if (largest != i) 14 | { 15 | swap(arr[i], arr[largest]); 16 | MaxHeapify(arr, largest, n); 17 | } 18 | } 19 | 20 | void convertMaxHeap(int arr[], int n) 21 | { 22 | for (int i = (n-2)/2; i >= 0; --i) 23 | MaxHeapify(arr, i, n); 24 | } 25 | 26 | 27 | void printArray(int* arr, int size) 28 | { 29 | for (int i = 0; i < size; ++i) 30 | printf("%d ", arr[i]); 31 | } 32 | 33 | int main() 34 | { 35 | int arr[] = {-1, 5, 4, 3, 8, -9, 12, 45}; 36 | int n = sizeof(arr)/sizeof(arr[0]); 37 | 38 | printf("Min Heap array : "); 39 | printArray(arr, n); 40 | 41 | convertMaxHeap(arr, n); 42 | 43 | printf("\nMax Heap array : "); 44 | printArray(arr, n); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C++/N meetings in room.cpp: -------------------------------------------------------------------------------- 1 | //@soumyaagr427 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | 10 | //Function to find the maximum number of meetings that can 11 | //be performed in a meeting room. 12 | 13 | int maxMeetings(int start[], int end[], int n) 14 | { 15 | int curr=1; 16 | 17 | vector>ans; 18 | 19 | //pushing the pairs into vectors 20 | 21 | for(int i=0;i> t; 46 | while (t--) { 47 | int n; 48 | cin >> n; 49 | int start[n], end[n]; 50 | for (int i = 0; i < n; i++) cin >> start[i]; 51 | 52 | for (int i = 0; i < n; i++) cin >> end[i]; 53 | 54 | Solution ob; 55 | int ans = ob.maxMeetings(start, end, n); 56 | cout << ans << endl; 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /C++/N-Queens.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool isSafe(int row, int col, vector Board, int n) 8 | { 9 | int rowcopy = row; 10 | int colcopy = col; 11 | 12 | while (row >= 0 && col >= 0) 13 | { 14 | if (Board[row][col] == 'Q') 15 | return false; 16 | row--; 17 | col--; 18 | } 19 | row = rowcopy; 20 | col = colcopy; 21 | while (col >= 0) 22 | { 23 | if (Board[row][col] == 'Q') 24 | return false; 25 | col--; 26 | } 27 | row = rowcopy; 28 | col = colcopy; 29 | while (row < n && col >= 0) 30 | { 31 | if (Board[row][col] == 'Q') 32 | return false; 33 | row++; 34 | col--; 35 | } 36 | return true; 37 | } 38 | void solve(int col, int n, vector &Board, vector> &ans) 39 | { 40 | if (col == n) 41 | { 42 | ans.push_back(Board); 43 | return; 44 | } 45 | for (int row = 0; row < n; row++) 46 | { 47 | if (isSafe(row, col, Board, n)) 48 | { 49 | Board[row][col] = 'Q'; 50 | solve(col + 1, n, Board, ans); 51 | Board[row][col] = '.'; 52 | } 53 | } 54 | } 55 | vector> solveNQueens(int n) 56 | { 57 | vector> ans; 58 | string s(n, '.'); 59 | vector Board(n); 60 | for (int i = 0; i < n; i++) 61 | Board[i] = s; 62 | solve(0, n, Board, ans); 63 | return ans; 64 | } 65 | }; 66 | 67 | int main() 68 | { 69 | int n; 70 | cin >> n; 71 | Solution ob; 72 | vector> ans = ob.solveNQueens(n); 73 | for (int i = 0; i < n; i++) 74 | { 75 | for (int j = 0; j < n; j++) 76 | { 77 | cout << ans[i][j] << "\n"; 78 | } 79 | cout << "\n"; 80 | } 81 | } -------------------------------------------------------------------------------- /C++/Pascal's_Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int rows, coef = 1; // coef is a counter variable to perform the pascal's triangle function 7 | 8 | cout << "Enter number of rows: "; 9 | cin >> rows; // No. of rows the user wants to input 10 | 11 | for(int i = 0; i < rows; i++) 12 | { 13 | for(int space = 1; space <= rows-i; space++) 14 | cout <<" "; // Adding spaces till columns are less than or equal to the (no. of rows - i) 15 | 16 | for(int j = 0; j <= i; j++) 17 | { 18 | if (j == 0 || i == 0) // Base case 19 | coef = 1; 20 | else 21 | coef = coef*(i-j+1)/j; 22 | 23 | cout << coef << " "; 24 | } 25 | cout << endl; // To skip to the other row 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C++/Reverse_Doubly_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Problem Statement: Write a C++ code to reverse a double linked list and demonstrate its working by giving an example 4 | 5 | Time complexity achieved: O(N) where N is the number of nodes in the initial doubly linked list which is to be reversed. 6 | 7 | Explanation for time complexity: We visit each node and swap their next and previous pointers. So, it is accomplished in linar time. 8 | 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | typedef struct node node; 15 | 16 | struct node{ 17 | node* prev; 18 | int data; 19 | node* next; 20 | }; 21 | 22 | node* insert_doubly(node* head, int val){ 23 | node* new = malloc(sizeof(node)); 24 | new->data=val; 25 | new->next=NULL; 26 | 27 | if (head==NULL){ 28 | new->prev=NULL; 29 | return new; 30 | } 31 | 32 | else { 33 | while(head->next!=NULL){ 34 | head = head->next; 35 | } 36 | 37 | head->next = new; 38 | new->prev=head; 39 | } 40 | } 41 | 42 | node* reverse_doubly(node* head){ 43 | node* curr = head; 44 | node* temp; 45 | 46 | while(curr->next!=NULL){ 47 | temp = curr->next; 48 | curr->next = curr->prev; 49 | curr->prev=temp; 50 | curr = temp; 51 | } 52 | 53 | curr->next = curr->prev; 54 | curr->prev=NULL; 55 | return curr; 56 | } 57 | 58 | node* delete_doubly(node* head, int val){ 59 | 60 | node* curr = head; 61 | 62 | while(curr->data!=val){ 63 | curr = curr->next; 64 | } 65 | 66 | node* temp = curr->prev; 67 | 68 | temp->next = curr->next; 69 | 70 | free(curr); 71 | 72 | return head; 73 | } 74 | 75 | void print_doubly(node* head){ 76 | while(head!=NULL){ 77 | printf("%d\t", head->data); 78 | head = head->next; 79 | } 80 | printf("\n"); 81 | } 82 | 83 | int main(void){ 84 | 85 | node* head = NULL; 86 | 87 | head = insert_doubly(head, 8); 88 | insert_doubly(head, 13); 89 | insert_doubly(head, 34); 90 | insert_doubly(head, 76); 91 | insert_doubly(head, 1); 92 | insert_doubly(head, 98); 93 | 94 | print_doubly(head); 95 | 96 | head = reverse_doubly(head); 97 | 98 | print_doubly(head); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /C++/Reverse_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Problem statement : Given pointer to the head node of a linked list, the task is to reverse the linked list. 4 | 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | /* Link list node */ 11 | struct Node { 12 | int data; 13 | struct Node* next; 14 | Node(int data) 15 | { 16 | this->data = data; 17 | next = NULL; 18 | } 19 | }; 20 | 21 | struct LinkedList { 22 | Node* head; 23 | LinkedList() { head = NULL; } 24 | 25 | /* Function to reverse the linked list */ 26 | void reverse() 27 | { 28 | // Initialize current, previous and 29 | // next pointers 30 | Node* current = head; 31 | Node *prev = NULL, *next = NULL; 32 | 33 | while (current != NULL) { 34 | // Store next 35 | next = current->next; 36 | 37 | // Reverse current node's pointer 38 | current->next = prev; 39 | 40 | // Move pointers one position ahead. 41 | prev = current; 42 | current = next; 43 | } 44 | head = prev; 45 | } 46 | 47 | /* Function to print linked list */ 48 | void print() 49 | { 50 | struct Node* temp = head; 51 | while (temp != NULL) { 52 | cout << temp->data << " "; 53 | temp = temp->next; 54 | } 55 | } 56 | 57 | void push(int data) 58 | { 59 | Node* temp = new Node(data); 60 | temp->next = head; 61 | head = temp; 62 | } 63 | }; 64 | 65 | /* Driver code*/ 66 | int main() 67 | { 68 | /* Start with the empty list */ 69 | LinkedList ll; 70 | ll.push(5); 71 | ll.push(4); 72 | ll.push(15); 73 | ll.push(12); 74 | 75 | cout << "Given linked list\n"; 76 | ll.print(); 77 | 78 | ll.reverse(); 79 | 80 | cout << "\nReversed Linked list \n"; 81 | ll.print(); 82 | return 0; 83 | } -------------------------------------------------------------------------------- /C++/Selection_Sort_Visualised.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Siddhant Pandy 3 | Program: Graphical representation of Sorting in command line 4 | Topic: Selection Sort 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | void delay(int number_of_seconds) 13 | { 14 | int milli_seconds = 1000 * number_of_seconds; 15 | clock_t start_time = clock(); 16 | while (clock() < start_time + milli_seconds) 17 | ; 18 | } 19 | 20 | 21 | void display(vector arr) 22 | { 23 | for(int x=0;x selectionSort(vector arr) 45 | { 46 | for(int x=0;xarr[y]) 52 | { 53 | min=y; 54 | } 55 | } 56 | swap(arr[min],arr[x]); 57 | display(arr); 58 | } 59 | 60 | return arr; 61 | } 62 | int main() 63 | { 64 | //int arr[] = {10,3,6,9,1,5,2,4,8,7}; 65 | int arr[] = {54, 89, 62, 26, 78,51}; 66 | 67 | vector vec(arr, arr + sizeof(arr) / sizeof(arr[0])); 68 | vec=selectionSort(vec); 69 | display(vec); 70 | } 71 | -------------------------------------------------------------------------------- /C++/ShortestCommonSupersequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int shortestCommonSupersequence(string X, string Y, int m, int n) 6 | { 7 | 8 | int dp[m+1][n+1]; 9 | 10 | for(int i = 0 ; i <= m ; i++){ 11 | for(int j = 0 ; j <= n ; j++){ 12 | if(i == 0){ 13 | dp[i][j]=0; 14 | } 15 | if(j==0){ 16 | dp[i][j]=0; 17 | } 18 | 19 | if(i>0 and j>0){ 20 | if(X[i-1]==Y[j-1]){ 21 | dp[i][j]= dp[i-1][j-1]+1; 22 | }else{ 23 | dp[i][j]= max(dp[i-1][j],dp[i][j-1]); 24 | } 25 | } 26 | } 27 | } 28 | 29 | 30 | int x = (m+n)-(2*dp[m][n]); 31 | 32 | return x+dp[m][n]; 33 | } 34 | 35 | int main() 36 | { 37 | char X[] = "AGGTAB"; 38 | char Y[] = "GXTXAYB"; 39 | 40 | cout << "Length of the shortest supersequence is " 41 | << shortestSuperSequence(X, Y) << endl; 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /C++/SubsetSum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum. 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 6 9 | arr[] = {3, 34, 4, 12, 5, 2} 10 | sum = 9 11 | Output: 1 12 | Explanation: Here there exists a subset with 13 | sum = 9, 4+3+2 = 9. 14 | */ 15 | 16 | 17 | 18 | // this is a classic knapsack problem 19 | 20 | #include 21 | using namespace std; 22 | 23 | bool isSubsetSum(int N, int arr[], int sum){ 24 | int dp[N+1][sum+1]; 25 | 26 | for(int i = 0 ; i <= N ; i++){ 27 | for(int j = 0 ; j <= sum ; j++){ 28 | if(i==0){ 29 | dp[i][j]= false; 30 | } 31 | if(j==0){ 32 | dp[i][j]= true; 33 | } 34 | 35 | if(i>0 and j >0){ 36 | 37 | if(arr[i-1]<=j){ 38 | // we use or because can't find max between two bool values 39 | dp[i][j]= dp[i-1][j-arr[i-1]] or dp[i-1][j]; 40 | }else{ 41 | dp[i][j]=dp[i-1][j]; 42 | } 43 | } 44 | } 45 | } 46 | 47 | return dp[N][sum]; 48 | } 49 | 50 | int main() 51 | { 52 | int set[] = { 3, 34, 4, 12, 5, 2 }; 53 | int sum = 9; 54 | int n = sizeof(set) / sizeof(set[0]); 55 | if (isSubsetSum( n,set, sum) == true) 56 | cout <<"Found a subset with given sum"; 57 | else 58 | cout <<"No subset with given sum"; 59 | return 0; 60 | } 61 | 62 | // time complexity : O(sum*N) -------------------------------------------------------------------------------- /C++/addTwoMatrices.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int r, c, a[100][100], b[100][100], sum[100][100], i, j; 7 | 8 | cout << "Enter the number of rows: "; 9 | cin >> r; 10 | 11 | cout << "Enter the number of columns: "; 12 | cin >> c; 13 | 14 | cout << endl << "Enter the elements of 1st matrix: " << endl; 15 | 16 | // Get elements of first matrix entered by user. 17 | for (i = 0; i < r; ++i) 18 | { 19 | for (j = 0; j < c; ++j) 20 | { 21 | cin >> a[i][j]; 22 | } 23 | } 24 | 25 | // Get elements of second matrix entered by user. 26 | cout << endl << "Enter the elements of 2nd matrix: " << endl; 27 | for (i = 0; i < r; ++i) 28 | { 29 | for (j = 0; j < c; ++j) 30 | { 31 | cin >> b[i][j]; 32 | } 33 | } 34 | 35 | // Adding the two matrices 36 | for (i = 0; i < r; ++i) 37 | { 38 | for (j = 0; j < c; ++j) 39 | { 40 | sum[i][j] = a[i][j] + b[i][j]; 41 | } 42 | } 43 | 44 | // Display the result. 45 | cout << endl << "Sum of the two matrix is: " << endl; 46 | for (i = 0; i < r; ++i) 47 | { 48 | for (j = 0; j < c; ++j) 49 | { 50 | cout << sum[i][j] << " "; 51 | if (j == c - 1) { 52 | cout << endl; 53 | } 54 | } 55 | } 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /C++/beautiful_string.cpp: -------------------------------------------------------------------------------- 1 | /* Problem Statement: Ninja has been given a binary string ‘STR’ containing either ‘0’ or ‘1’. A binary string is called beautiful if it contains alternating 0s and 1s. 2 | For Example:‘0101’, ‘1010’, ‘101’, ‘010’ are beautiful strings. 3 | He wants to make ‘STR’ beautiful by performing some operations on it. In one operation, Ninja can convert ‘0’ into ‘1’ or vice versa. 4 | Your task is to determine the minimum number of operations Ninja should perform to make ‘STR’ beautiful. 5 | 6 | Time Complexity: O(n) 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int makeBeautiful(string str) 13 | { 14 | int n = str.length(); 15 | int t1 = 0, t2 = 0; 16 | char s1 = '0', s2 = '1'; 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | if (str[i] == '1') 21 | { 22 | if (s1 == '0') 23 | { 24 | t1++; 25 | } 26 | else { 27 | t2++; 28 | } 29 | } 30 | else 31 | { 32 | if (s1 == '1') 33 | { 34 | t1++; 35 | } 36 | else 37 | { 38 | t2++; 39 | } 40 | } 41 | if (s1 == '1') 42 | { 43 | s1 = '0'; 44 | s2 = '1'; 45 | } 46 | else 47 | { 48 | s1 = '1'; 49 | s2 = '0'; 50 | } 51 | } 52 | return min(t1, t2); 53 | } 54 | 55 | int main() 56 | { 57 | int test; 58 | cout<<"Enter total testcases: "<>test; 60 | while(test--) 61 | { 62 | string str; 63 | cin>>str; 64 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int maxProfit(vector& prices) { 6 | int least_left=prices[0],profit=0; 7 | int dpl[prices.size()]; 8 | dpl[0]=0; 9 | for(int i=1;iprofit){ 13 | dpl[i]=profit=prices[i]-least_left; 14 | } 15 | else 16 | dpl[i]=dpl[i-1]; 17 | } 18 | profit=0; 19 | int dpr[prices.size()]; 20 | int max_right=prices[prices.size()-1]; 21 | dpr[prices.size()-1]=0; 22 | for(int i=prices.size()-2;i>=0;i--){ 23 | if(prices[i]>max_right) 24 | max_right=prices[i]; 25 | if(max_right-prices[i]>profit){ 26 | profit=dpr[i]=max_right-prices[i]; 27 | } 28 | else 29 | dpr[i]=dpr[i+1]; 30 | } 31 | int max_profit=dpl[0]+dpr[0]; 32 | for(int i=1;imax_profit) 34 | max_profit=dpl[i]+dpr[i]; 35 | } 36 | return max_profit; 37 | } 38 | 39 | int main(){ 40 | long long n; 41 | cout<<"Enter the no of days"; 42 | cin>>n; 43 | vector prices; 44 | cout<<"Enter price of the days"; 45 | for(long long i=0;i>x; 48 | prices.push_back(x); 49 | } 50 | int ans=maxProfit( prices); 51 | cout<<"Maximum Profit is:"< 3 | using namespace std; 4 | 5 | // Returns value of Binomial Coefficient C(n, k) 6 | unsigned long int binomialCoeff(unsigned int n,unsigned int k) 7 | { 8 | unsigned long int res = 1; 9 | 10 | if (k > n - k) 11 | k = n - k; 12 | 13 | for (int i = 0; i < k; ++i) { 14 | res *= (n - i); 15 | res /= (i + 1); 16 | } 17 | 18 | return res; 19 | } 20 | 21 | // A Binomial coefficient based function to find nth catalan 22 | // number in O(n) time 23 | unsigned long int catalan(unsigned int n) 24 | { 25 | unsigned long int c = binomialCoeff(2 * n, n); 26 | 27 | return c / (n + 1); 28 | } 29 | 30 | // Driver code 31 | int main() 32 | { 33 | for (int i = 0; i < 10; i++) 34 | cout << catalan(i) << " "; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /C++/celebrity problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define N 8 5 | 6 | bool MATRIX[N][N] = {{0, 0, 1, 0}, 7 | {0, 0, 1, 0}, 8 | {0, 0, 0, 0}, 9 | {0, 0, 1, 0} 10 | }; 11 | 12 | bool knows(int a, int b) 13 | { 14 | return MATRIX[a][b]; 15 | } 16 | 17 | int findCelebrity(int n) 18 | { 19 | 20 | int a = 0; 21 | int b = n - 1; 22 | 23 | while (a < b) 24 | { 25 | if (knows(a, b)) 26 | a++; 27 | else 28 | b--; 29 | } 30 | 31 | 32 | for (int i = 0; i < n; i++) 33 | { 34 | 35 | if ( (i != a) && 36 | (knows(a, i) || 37 | !knows(i, a)) ) 38 | return -1; 39 | } 40 | 41 | return a; 42 | } 43 | 44 | int main() 45 | { 46 | int n = 4; 47 | int id = findCelebrity(n); 48 | id == -1 ? cout << "No celebrity" : 49 | cout << "Celebrity ID " 50 | << id; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /C++/check_circular_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | //Ishita Karandikar 2 | // C++ program to check if linked list is circular 3 | #include 4 | using namespace std; 5 | 6 | /* Link list Node */ 7 | struct Node 8 | { 9 | int data; 10 | struct Node* next; 11 | }; 12 | 13 | /* This function returns true if given linked 14 | list is circular, else false. */ 15 | bool isCircular(struct Node *head) 16 | { 17 | // An empty linked list is circular 18 | if (head == NULL) 19 | return true; 20 | 21 | // Next of head 22 | struct Node *node = head->next; 23 | 24 | // This loop would stop in both cases (1) If 25 | // Circular (2) Not circular 26 | while (node != NULL && node != head) 27 | node = node->next; 28 | 29 | // If loop stopped because of circular 30 | // condition 31 | return (node == head); 32 | } 33 | 34 | // Utility function to create a new node. 35 | Node *newNode(int data) 36 | { 37 | struct Node *temp = new Node; 38 | temp->data = data; 39 | temp->next = NULL; 40 | return temp; 41 | } 42 | 43 | /* Driver program to test above function*/ 44 | int main() 45 | { 46 | /* Start with the empty list */ 47 | struct Node* head = newNode(1); 48 | head->next = newNode(2); 49 | head->next->next = newNode(3); 50 | head->next->next->next = newNode(4); 51 | 52 | isCircular(head)? cout << "Yes\n" : 53 | cout << "No\n" ; 54 | 55 | // Making linked list circular 56 | head->next->next->next->next = head; 57 | 58 | isCircular(head)? cout << "Yes\n" : 59 | cout << "No\n" ; 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /C++/cloneGraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef vector vs; 5 | typedef long long int ll; 6 | typedef vector vi; 7 | const int inf = INT_MAX; 8 | typedef UndirectedGraphNode Node; 9 | /* 10 | This code makes a deep copy of a given graph 11 | */ 12 | 13 | 14 | struct UndirectedGraphNode { 15 | int label; 16 | vector neighbors; 17 | UndirectedGraphNode(int x) : label(x) {}; 18 | }; 19 | UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { 20 | UndirectedGraphNode *A = node; 21 | queue q; 22 | q.push(node); 23 | unordered_map m1; 24 | m1[node] = new Node(node->label); 25 | while(!q.empty()){ 26 | Node *s = q.front(); 27 | q.pop(); 28 | for(UndirectedGraphNode *i : s->neighbors){ 29 | if(m1.find(i) == m1.end()) { 30 | UndirectedGraphNode *tmp = new UndirectedGraphNode(i->label); 31 | m1[i] = tmp; 32 | q.push(i); 33 | } 34 | m1[s]->neighbors.push_back(m1[i]); 35 | } 36 | } 37 | return m1[node]; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /C++/connected_components.cpp: -------------------------------------------------------------------------------- 1 | // Dfs using adj as vector of vector 2 | 3 | #include 4 | 5 | #define ll long long 6 | #define vi vector 7 | #define vb vector 8 | #define vil vector 9 | #define mapii map 10 | #define mapci map 11 | #define mapcc map 12 | #define mod 1e9+7 13 | #define INF 1e18 14 | 15 | using namespace std; 16 | vector> adj; 17 | vector vis; 18 | void cc(int node) 19 | { 20 | vis[node]=true; 21 | for(int child:adj[node]) 22 | { 23 | if(!vis[child]) 24 | cc(child); 25 | } 26 | } 27 | int main(void) 28 | { 29 | 30 | ll a,b,n,m; //n=nodes m=edges 31 | int count=0; 32 | cin>>n>>m; //input number of nodes and edges 33 | adj=vector>(n,vector()); //initializing adjacency list 34 | vis=vector(n,false); 35 | for(int i=1;i<=m;i++) 36 | { 37 | cin>>a>>b; 38 | adj[a].push_back(b); 39 | adj[b].push_back(a); 40 | } 41 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | std::set b; 8 | std::vector a{1, 2, 3, 5, 5}; 9 | cout << "data in vector:"; 10 | for(auto i: a) 11 | { 12 | cout << i << ","; 13 | b.emplace(i); 14 | } 15 | cout << std::endl; 16 | 17 | cout << "data in set:"; 18 | for(auto i: b) 19 | { 20 | cout << i << ","; 21 | } 22 | cout << std::endl; 23 | } 24 | -------------------------------------------------------------------------------- /C++/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef vector vs; 5 | typedef long long int ll; 6 | 7 | class Edge{ 8 | public: 9 | int v, cost; 10 | Edge(int v1, int c){ 11 | cost = c; 12 | v = v1; 13 | } 14 | }; 15 | bool operator< (const Edge &e1, const Edge &e2){ 16 | return e1.cost > e2.cost; 17 | } 18 | 19 | 20 | int main(){ 21 | int V, E; 22 | cin>>V>>E; 23 | unordered_map > m1; 24 | for(int i=0;i>x>>y>>wt; 27 | m1[x].push_back({y,wt}); 28 | m1[y].push_back({x,wt}); 29 | } 30 | vector visited(V+1,0); 31 | priority_queue pq; 32 | pq.push({0,0}); 33 | while(!pq.empty()){ 34 | Edge e1 = pq.top(); 35 | pq.pop(); 36 | if(visited[e1.v]){ 37 | continue; 38 | } 39 | else{ 40 | visited[e1.v] = 1; 41 | cout< 11 | using namespace std; 12 | 13 | 14 | // WE WILL USE BIT MANIPULATION HERE TO SOLVE THIS PROBLEM 15 | int get_truncated_quotient_using_bit_magic(int dividend, int divisor) 16 | { 17 | if (dividend == INT_MIN && divisor == -1) {// base case to keep track of overflows 18 | return INT_MAX; 19 | } 20 | long dvd = labs(dividend), dvs = labs(divisor), ans = 0; 21 | int sign = dividend > 0 ^ divisor > 0 ? -1 : 1; 22 | // sign variable to keep track if quotient is negative or not 23 | 24 | //we right shift the divisor till it is less than or equal to dividend 25 | // and maintain a count of how many times the divisor can be subtracted from dividend 26 | // we add that count to ans, and change dividend accordingly 27 | 28 | while (dvd >= dvs) { 29 | long temp = dvs, m = 1; 30 | 31 | while ((temp << 1) <= dvd) { 32 | temp <<= 1;// temp is right shifted by 1, or doubled 33 | m <<= 1;// we right shift m by 1, to to keep track of quotient 34 | } 35 | dvd -= temp;//subtract the largest factor of divisor obtained by right shifting such that conditions are satisfied 36 | ans += m;//m keeps track how may times 37 | } 38 | return sign * ans;// sign is to make sure that quotient follows sign convention 39 | } 40 | // time complexity: O(log(dividend)) 41 | 42 | int main() 43 | { 44 | int divisor, dividend; 45 | // taking input for divisor 46 | cout<<"enter divisor: "; 47 | cin>>divisor; 48 | // taking input for dividend 49 | cout<<"enter dividend: "; 50 | cin>>dividend; 51 | // printing the truncated quotient or the integer part of quotient 52 | 53 | cout<<"integer quotient using bit manipulation: "; 54 | cout< 7 | using namespace std; 8 | 9 | //using bitwise operator xor 10 | //x^x = 0 11 | //0^x = x 12 | int findRepeating(vector v,int n) //Time Complexity : O(n) , Space Complexity: O(1) 13 | { 14 | int res = 0; 15 | for(int i=0;i>test; 28 | while(test--) 29 | { 30 | int n; 31 | cout<<"Enter total: "<>n; 33 | vector v; 34 | for(int i=0;i>temp; 38 | v.push_back(temp); 39 | } 40 | cout<<"The duplicate element is:"< LINEAR SEARCH USING C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | // Creatig a dynamic array 9 | int n,*arr,key,i; 10 | 11 | //Entering the size of array 12 | cout<<"Enter the size of array: "; 13 | cin>>n; 14 | arr = new int [n]; 15 | 16 | //Inserting the values in the array 17 | cout<<"Enter the values in the array: "; 18 | for(int i=0;i>*(arr+i); 20 | } 21 | 22 | //Entering the search value 23 | cout<<"Enter the value to seacrh: "; 24 | cin>>key; 25 | 26 | //Traversing the array to search the value which was entered by the user 27 | for(i=0;i NEXT 11 | Step 4: FREE PTR 12 | Step 5: EXIT 13 | */ 14 | #include 15 | #include 16 | void create(int); 17 | void begdelete(); 18 | struct node 19 | { 20 | int data; 21 | struct node *next; 22 | }; 23 | struct node *head; 24 | void main () 25 | { 26 | int choice,item; 27 | do 28 | { 29 | printf("\n1.Append List\n2.Delete node\n3.Exit\n4.Enter your choice?"); 30 | scanf("%d",&choice); 31 | switch(choice) 32 | { 33 | case 1: 34 | printf("\nEnter the item\n"); 35 | scanf("%d",&item); 36 | create(item); 37 | break; 38 | case 2: 39 | begdelete(); 40 | break; 41 | case 3: 42 | exit(0); 43 | break; 44 | default: 45 | printf("\nPlease enter valid choice\n"); 46 | } 47 | }while(choice != 3); 48 | } 49 | void create(int item) 50 | { 51 | struct node *ptr = (struct node *)malloc(sizeof(struct node *)); 52 | if(ptr == NULL) 53 | { 54 | printf("\nOVERFLOW\n"); 55 | } 56 | else 57 | { 58 | ptr->data = item; 59 | ptr->next = head; 60 | head = ptr; 61 | printf("\nNode inserted\n"); 62 | } 63 | 64 | } 65 | void begdelete() 66 | { 67 | struct node *ptr; 68 | if(head == NULL) 69 | { 70 | printf("\nList is empty"); 71 | } 72 | else 73 | { 74 | ptr = head; 75 | head = ptr->next; 76 | free(ptr); 77 | printf("\n Node deleted from the begining ..."); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /C++/flip_bits.cpp: -------------------------------------------------------------------------------- 1 | /*Problem Statement: You are given an array of integers ARR[] of size N consisting of zeros and ones. You have to select a subset and flip bits of that subset. 2 | You have to return the count of maximum one’s that you can obtain by flipping chosen sub-array at most once. 3 | A flip operation is one in which you turn 1 into 0 and 0 into 1. 4 | Time Complexity : O(n) 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | int flipBits(vector v, int n) 10 | { 11 | int ones=0; 12 | for(int i=0;i>test; 48 | while(test--) 49 | { 50 | int n; 51 | cout<<"Enter total: "<>n; 53 | vector v; 54 | for(int i=0;i>t; 58 | v.push_back(t); 59 | } 60 | cout< 2 | using namespace std; 3 | bool A[10][10]; 4 | void initialize () 5 | { 6 | for(int i = 0; i <10; ++i) 7 | for (int j = 0; j < 10; ++j) 8 | A[i][j] = false; 9 | } 10 | int main () 11 | { 12 | int x, y, nodes, edges; 13 | initialize(); //since there is no edge initially 14 | cout << "enter node" << endl; 15 | cin >> nodes; //Number of nodes 16 | cout << "enter edges" << endl; 17 | cin >> edges; // Number of edges 18 | for (int i = 0; i < edges; ++i) 19 | { 20 | cout << "enter X and Y" << endl; 21 | cin >> x >> y; //Mark the edges from vertex x to vertex y 22 | A[x][y] = true; 23 | } 24 | if (A[3][4] == true) 25 | cout << "There is an edge between 3 and 4" << endl; 26 | else 27 | cout << "There is no edge between 3 and 4" << endl; 28 | if (A[2][3] == true) 29 | cout << "There is an edge between 2 and 3" << endl; 30 | else 31 | cout << "There is no edge between 2 and 3" << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C++/hello.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | cout<<"Hello coders!"; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /C++/infinite_sum_array.cpp: -------------------------------------------------------------------------------- 1 | /* Problem Statement: Given an array “A” of N integers and you have also defined the new array “B” as a concatenation of array “A” for an infinite number of times. 2 | For example, if the given array “A” is [1,2,3] then, infinite array “B” is [1,2,3,1,2,3,1,2,3,.......]. 3 | Now you are given Q queries, 4 | each query consists of two integers “L“ and “R”. Your task is to find the sum of the subarray from index “L” to “R” (both inclusive) in the infinite array “B” for each query. 5 | Time Complexity: O(nlogn) 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | int test; 13 | cin>>test; 14 | while(test--) 15 | { 16 | int n; 17 | cout<<"Enter total: "<>n; 19 | vector v; 20 | for(int i=0;i>t; 24 | v.push_back(t); 25 | } 26 | int q; 27 | cout<<"Enter total queries: "<>q; 29 | int l,r; 30 | while(q--) 31 | { 32 | cin>>l>>r; 33 | int sum=0; 34 | for(int i=l-1;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int minDistance(string word1, string word2) 8 | { 9 | int n = word1.size(); 10 | int m = word2.size(); 11 | vector> dp(n + 1, vector(m + 1, 0)); 12 | for (int i = 0; i <= n; i++) 13 | { 14 | for (int j = 0; j <= m; j++) 15 | { 16 | if (i == 0) 17 | dp[i][j] = j; 18 | else if (j == 0) 19 | dp[i][j] = i; 20 | else if (word1[i - 1] == word2[j - 1]) 21 | dp[i][j] = dp[i - 1][j - 1]; 22 | else 23 | dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])); 24 | } 25 | } 26 | return dp[n][m]; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | string w1, w2; 33 | cin >> w1 >> w2; 34 | Solution ob; 35 | int ans = ob.minDistance(w1, w2); 36 | cout << ans << "\n"; 37 | } -------------------------------------------------------------------------------- /C++/patternSearching.cpp: -------------------------------------------------------------------------------- 1 | // Given a string str and a pattern pat. 2 | //You need to check whether pattern is present or not in the given string. 3 | // Input Format: The input line contains T, denoting the number of testcases. 4 | //Each testcase contains two lines. First line contains string str. Second line contains patter text. 5 | // Output format: For each testcase in new line you have to print the "Present" 6 | //if found otherwise "Not present". 7 | /* 8 | Sample Input: 9 | 2 10 | abcdefh 11 | bcd 12 | axzy 13 | xy 14 | 15 | Sample Output: 16 | Present 17 | Not present 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | 24 | bool searchPattern(string str, string pat) 25 | { 26 | return str.find(pat)!=-1; 27 | } 28 | 29 | 30 | int main() 31 | { 32 | int t; cin>>t; 33 | 34 | while(t--) 35 | { 36 | string str, pat; 37 | cin>>str; 38 | cin>>pat; 39 | 40 | if(searchPattern(str, pat) == true) 41 | cout << "Present" < 9 | using namespace std; 10 | 11 | //using recursion 12 | int powerNum(int n,int p){ 13 | //base case 14 | if(p<=1){ 15 | return n; 16 | } 17 | else{ 18 | return n*powerNum(n,p-1); 19 | } 20 | } 21 | 22 | int main(){ 23 | //enter the two numbers 24 | int num,npow; 25 | cin>>num>>npow; 26 | 27 | //Call the recursive function 28 | cout<<"Recursive ans is "< 18 | using namespace std; 19 | int main() 20 | { 21 | int a[100],b[100],n,mult=1; 22 | cout<<"Enter the number of elements in the array: "; 23 | cin>>n; 24 | cout<<"Enter the elements of the array: "; 25 | for(int i=0;i>a[i]; 28 | mult = mult * a[i]; 29 | } 30 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | #define ll long long int 9 | #define rep(a, b, c) for (ll a = b; a < c; a++) 10 | //time complexity Od(n+k) 11 | //space complexity O(n+k) 12 | //k is range of input 13 | //d is the number of digits in largest number 14 | int getmax(int a[],int n){ 15 | int maxi=a[0]; 16 | for(int i=1;i=0;i--){ 31 | output[count[(a[i]/div)%10]-1]=a[i]; 32 | count[(a[i]/div)%10]--; 33 | } 34 | for(int i=0;i0;div*=10){ 43 | countingsort(a,n,div); 44 | } 45 | 46 | 47 | } 48 | int main(){ 49 | 50 | int n; 51 | cin>>n; 52 | int a[n]; 53 | rep(i,0,n){ 54 | cin>>a[i]; 55 | } 56 | radixsort(a,n); 57 | rep(i,0,n){ 58 | cout< 2 | using namespace std; 3 | int rod(int l[],int p[],int n, int len) 4 | { 5 | if(n==0||len==0) 6 | { 7 | return 0; 8 | } 9 | else if(l[n-1]<=len) 10 | { 11 | return max(p[n-1]+rod(l,p,n,len-l[n-1]),rod(l,p,n-1,len)); 12 | } 13 | else if(l[n-1]>len) 14 | { 15 | return rod(l,p,n-1,len); 16 | } 17 | } 18 | int main() 19 | { 20 | int length[]={1,2,3,4,5,6,7,8}; 21 | int price[]={1,5,8,9,10,20}; 22 | int len=8; 23 | cout< // std::cout 3 | #include // std::rotate 4 | #include // std::vector 5 | 6 | int main () { 7 | std::vector myvector; 8 | 9 | // set some values: 10 | for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 11 | 12 | std::rotate(myvector.begin(),myvector.begin()+3,myvector.end()); 13 | // 4 5 6 7 8 9 1 2 3 14 | // print out content: 15 | std::cout << "myvector contains:"; 16 | for (std::vector::iterator it=myvector.begin(); it!=myvector.end(); ++it) 17 | std::cout << ' ' << *it; 18 | std::cout << '\n'; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C++/search_element_in_row_column_sorted_matrix.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to search an element in row-wise 2 | // and column-wise sorted matrix 3 | #include 4 | 5 | using namespace std; 6 | 7 | int search(int mat[4][4], int n, int x) 8 | { 9 | if (n == 0) 10 | return -1; 11 | 12 | int smallest = mat[0][0], largest = mat[n - 1][n - 1]; 13 | if (x < smallest || x > largest) 14 | return -1; 15 | 16 | int i = 0, j = n - 1; 17 | while (i < n && j >= 0) 18 | { 19 | if (mat[i][j] == x) 20 | { 21 | cout << "n Found at " 22 | << i << ", " << j; 23 | return 1; 24 | } 25 | if (mat[i][j] > x) 26 | j--; 27 | 28 | else 29 | i++; 30 | } 31 | 32 | cout << "n Element not found"; 33 | return 0; 34 | } 35 | 36 | // Driver code 37 | int main() 38 | { 39 | int mat[4][4] = { { 10, 20, 30, 40 }, 40 | { 15, 25, 35, 45 }, 41 | { 27, 29, 37, 48 }, 42 | { 32, 33, 39, 50 } }; 43 | search(mat, 4, 29); 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /C++/shellsort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Shell Sort 2 | #include 3 | using namespace std; 4 | 5 | /* function to sort arr using shellSort */ 6 | int shellSort(int arr[], int n) 7 | { 8 | // Start with a big gap, then reduce the gap 9 | for (int gap = n/2; gap > 0; gap /= 2) 10 | { 11 | // Do a gapped insertion sort for this gap size. 12 | // The first gap elements a[0..gap-1] are already in gapped order 13 | // keep adding one more element until the entire array is 14 | // gap sorted 15 | for (int i = gap; i < n; i += 1) 16 | { 17 | // add a[i] to the elements that have been gap sorted 18 | // save a[i] in temp and make a hole at position i 19 | int temp = arr[i]; 20 | 21 | // shift earlier gap-sorted elements up until the correct 22 | // location for a[i] is found 23 | int j; 24 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 25 | arr[j] = arr[j - gap]; 26 | 27 | // put temp (the original a[i]) in its correct location 28 | arr[j] = temp; 29 | } 30 | } 31 | return 0; 32 | } 33 | 34 | void printArray(int arr[], int n) 35 | { 36 | for (int i=0; i 7 | #include 8 | using namespace std; 9 | 10 | void sieve(long long n) //gunction to calcualte prime numbers 11 | { 12 | vector m(n + 1, 0); //Set all numbers as prime numbers 0->Prime Number, 1->Composite Number 13 | m[0] = m[1] = 1; //set 0 and 1 as composite as they are not prime 14 | for (int i = 2; i * i <= n; i++) /* Start loop from i=2 and go till i is smaller than root of n 15 | because for two pairs of numbers (a,b) if a*b=N then one of (a or b) lies 16 | below the sqrt of n and the other above sqrt of n*/ 17 | { 18 | if (m[i] == 0) /*check if no is prime */ 19 | { 20 | for (int j = i * i; j <= n; j += i) /* Start j from i*i because all numbers before it will already be marked 21 | by other prime numbers */ 22 | m[j] = 1; /* Mark all the multiples of this number as composite*/ 23 | } 24 | } 25 | 26 | for (int i = 0; i <= n; i++) //print prime numbers in the range 1...N 27 | { 28 | if (m[i] == 0) 29 | cout << i << "\n"; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | int n; 36 | cout << "\nEnter range : "; 37 | cin >> n; 38 | sieve(n); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /C++/spiralprint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void spiralPrint(int input[100][100], int nRows, int nCols) 6 | { 7 | int cs=0; 8 | int rs=0; 9 | int re=nRows-1; 10 | int ce=nCols-1; 11 | while(rs<=re && cs<=ce){ 12 | for(int i=cs;i<=ce;i++){ 13 | cout<=cs;i1--){ 24 | cout<=rs;j1--){ 31 | cout<>nRows; 45 | cin>>nCols; 46 | cout<<"Enter the matrix :-"<>input[i][j]; 50 | } 51 | } 52 | 53 | cout<<"Matrix is :-"< 26 | #include 27 | using namespace std; 28 | 29 | void calculateSpan(int price[], int size, int span[]) { 30 | 31 | stack st; // Create a stack and push index of first element to it 32 | st.push(0); 33 | 34 | span[0] = 1; // Span value of first element is always 1 35 | 36 | for (int idx = 1; idx < size; idx++) { 37 | 38 | // Keep popping till you find the nearest greater element to the left. 39 | while (!st.empty() && price[st.top()] <= price[idx]) 40 | st.pop(); 41 | 42 | if(st.empty()) 43 | span[idx] = idx + 1; 44 | else 45 | span[idx] = idx - st.top(); 46 | 47 | st.push(idx); // Push this element to stack 48 | } 49 | } 50 | 51 | //Funtion to print the span values. 52 | void printSpan(int arr[], int size) { 53 | for (int idx = 0; idx < size; idx++) 54 | cout << arr[idx] << " "; 55 | cout << endl; 56 | } 57 | 58 | //The driver function 59 | int main() { 60 | int price[] = {100, 80, 60, 70, 60, 75, 85}; 61 | int size = sizeof(price) / sizeof(price[0]); 62 | int span[size]; // Stores the span values. 63 | 64 | calculateSpan(price, size, span); 65 | printSpan(span, size); // Print the calculated span values 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /C++/sudoku_solve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int n = 9; 5 | /* 6 | given a valid input for sudoku problem like one in the newspapers, it solves and returns a valid answer 7 | */ 8 | bool check(vector> &board, int row, int col, int key){ 9 | //checking in row 10 | for(int j1=0;j1> &board){ 30 | for(int i = 0; i<9;i++){ 31 | for(int j =0;j> &board){ 38 | for(int i=0;i<9;i++){ 39 | for(int j=0;j<9;j++){ 40 | if(board[i][j]==0) return 0; 41 | } 42 | } 43 | return 1; 44 | } 45 | void sudoku(vector> &board, int row, int col){ 46 | if(row == 9){ 47 | printt(board); 48 | 49 | return; 50 | } 51 | int nr,nc; 52 | if(col==8){ 53 | nr=row+1; 54 | nc=0; 55 | } 56 | else{ 57 | nr=row; nc = col+1; 58 | } 59 | 60 | if(board[row][col]!=0){ 61 | sudoku(board,nr,nc); 62 | } 63 | else{ 64 | for(int i = 1;i<=9;i++){ 65 | if(board[row][col]==0 && check(board,row,col,i)==1){ 66 | board[row][col] = i; 67 | sudoku(board,nr,nc); 68 | board[row][col]=0; 69 | } 70 | } 71 | 72 | } 73 | 74 | 75 | 76 | 77 | } 78 | 79 | 80 | 81 | 82 | int main() { 83 | int n, m; 84 | n=9,m=9; 85 | vector < vector < int >> arr(n, vector < int > (m)); 86 | vector < vector < int >> visited(n, vector < int > (m)); 87 | for (int i = 0; i < n; i++){ 88 | for (int j = 0; j < m; j++){ 89 | cin >> arr[i][j]; 90 | if(arr[i][j]) visited[i][j]=1; 91 | } 92 | } 93 | sudoku(arr,0,0); 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /C++/three-pointers.cpp: -------------------------------------------------------------------------------- 1 | /*Count triplets with sum smaller than X 2 | 3 | Given an array arr[] of distinct integers of size N and a value sum, 4 | the task is to find the count of triplets (i, j, k), having (i 9 | using namespace std; 10 | 11 | 12 | 13 | long long countTriplets(long long arr[], int n, long long sum) 14 | { 15 | sort(arr,arr+n); 16 | long long count=0; 17 | long long i=0,j,k; 18 | while(i 4 | using namespace std; 5 | 6 | void towerOfHanoi(int n, char A, char B, char C) 7 | { 8 | if(n==1) 9 | { 10 | cout<<"Move 1 from " << A <<" to " << C < 2 | #ifndef Queuecpp_h 3 | #define Queuecpp_h 4 | 5 | class Node{ 6 | public: 7 | Node *lchild,*rchild; 8 | int data; 9 | }; 10 | 11 | class Queue{ 12 | private: 13 | int size; 14 | int front; 15 | int rear; 16 | Node **Q; 17 | public: 18 | Queue(){front=rear=-1;size=10; Q=new Node *[size];} 19 | 20 | Queue(int size){front=rear=-1;this->size=size; Q=new Node *[this->size];} 21 | 22 | void enqueue(Node *x); 23 | Node* dequeue(); 24 | int isEmpty(){return front==rear;} 25 | void display(); 26 | }; 27 | 28 | 29 | void Queue::enqueue(Node *x){ 30 | if (rear==size-1) 31 | printf("Queue is Full..."); 32 | else{ 33 | rear++; 34 | Q[rear]=x; 35 | } 36 | } 37 | 38 | Node* Queue::dequeue(){ 39 | Node* x=NULL; 40 | if(front==rear) 41 | printf("Queue is Empty..."); 42 | else{ 43 | front++; 44 | x=Q[front]; 45 | } 46 | return x; 47 | } 48 | 49 | void Queue::display(){ 50 | for (int i = front+1; i <= rear; i++) 51 | printf("%d ",Q[i]); 52 | printf("\n"); 53 | } 54 | 55 | 56 | #endif -------------------------------------------------------------------------------- /C++/waveSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void waveSort(int arr[], int n) 5 | { 6 | for (int i = 1; i < n; i += 2) 7 | { 8 | if (arr[i] > arr[i - 1]) 9 | swap(arr[i], arr[i - 1]); 10 | if (arr[i] > arr[i + 1] && (i <= (n - 2))) 11 | swap(arr[i], arr[i + 1]); 12 | } 13 | } 14 | 15 | int main() 16 | { 17 | int arr[] = {3, 2, 1, 4, 5, 6, 0}; 18 | int n = 7; 19 | waveSort(arr, n); 20 | for (int i = 0; i < n; i++) 21 | { 22 | cout << arr[i] << " "; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /C/BinarySearchTraversal.c: -------------------------------------------------------------------------------- 1 | // C program for different tree traversals 2 | #include 3 | #include 4 | 5 | /* A binary tree node has data, pointer to left child 6 | and a pointer to right child */ 7 | struct node { 8 | int data; 9 | struct node* left; 10 | struct node* right; 11 | }; 12 | 13 | /* Helper function that allocates a new node with the 14 | given data and NULL left and right pointers. */ 15 | struct node* newNode(int data) 16 | { 17 | struct node* node 18 | = (struct node*)malloc(sizeof(struct node)); 19 | node->data = data; 20 | node->left = NULL; 21 | node->right = NULL; 22 | 23 | return (node); 24 | } 25 | 26 | /* Given a binary tree, print its nodes according to the 27 | "bottom-up" postorder traversal. */ 28 | void printPostorder(struct node* node) 29 | { 30 | if (node == NULL) 31 | return; 32 | 33 | // first recur on left subtree 34 | printPostorder(node->left); 35 | 36 | // then recur on right subtree 37 | printPostorder(node->right); 38 | 39 | // now deal with the node 40 | printf("%d ", node->data); 41 | } 42 | 43 | /* Given a binary tree, print its nodes in inorder*/ 44 | void printInorder(struct node* node) 45 | { 46 | if (node == NULL) 47 | return; 48 | 49 | /* first recur on left child */ 50 | printInorder(node->left); 51 | 52 | /* then print the data of node */ 53 | printf("%d ", node->data); 54 | 55 | /* now recur on right child */ 56 | printInorder(node->right); 57 | } 58 | 59 | /* Given a binary tree, print its nodes in preorder*/ 60 | void printPreorder(struct node* node) 61 | { 62 | if (node == NULL) 63 | return; 64 | 65 | /* first print data of node */ 66 | printf("%d ", node->data); 67 | 68 | /* then recur on left sutree */ 69 | printPreorder(node->left); 70 | 71 | /* now recur on right subtree */ 72 | printPreorder(node->right); 73 | } 74 | 75 | /* Driver program to test above functions*/ 76 | int main() 77 | { 78 | struct node* root = newNode(10); 79 | 80 | 81 | 82 | 83 | printf("\nPreorder traversal of binary tree is \n"); 84 | printPreorder(root); 85 | 86 | printf("\nInorder traversal of binary tree is \n"); 87 | printInorder(root); 88 | 89 | printf("\nPostorder traversal of binary tree is \n"); 90 | printPostorder(root); 91 | 92 | getchar(); 93 | return 0; 94 | } -------------------------------------------------------------------------------- /C/Circular_Queue.c: -------------------------------------------------------------------------------- 1 | //Write a program to implement the following operations using a circular queue in an array. 2 | //1. Insert an element. 2. Delete an element. 3. Display the elements 4. Exit. 3 | #include 4 | #define size 5 5 | 6 | void insertq(int[], int); 7 | void deleteq(int[]); 8 | void display(int[]); 9 | 10 | int front = - 1; 11 | int rear = - 1; 12 | 13 | int main() 14 | { 15 | int n, ch; 16 | int queue[size]; 17 | do 18 | { 19 | printf("\n\n Circular Queue:\n1. Insert \n2. Delete\n3. Display\n0. Exit"); 20 | printf("\nEnter Choice 0-3? : "); 21 | scanf("%d", &ch); 22 | switch (ch) 23 | { 24 | case 1: 25 | printf("\nEnter number: "); 26 | scanf("%d", &n); 27 | insertq(queue, n); 28 | break; 29 | case 2: 30 | deleteq(queue); 31 | break; 32 | case 3: 33 | display(queue); 34 | break; 35 | } 36 | }while (ch != 0); 37 | } 38 | 39 | 40 | void insertq(int queue[], int item) 41 | { 42 | if ((front == 0 && rear == size - 1) || (front == rear + 1)) 43 | { 44 | printf("queue is full"); 45 | return; 46 | } 47 | else if (rear == - 1) 48 | { 49 | rear++; 50 | front++; 51 | } 52 | else if (rear == size - 1 && front > 0) 53 | { 54 | rear = 0; 55 | } 56 | else 57 | { 58 | rear++; 59 | } 60 | queue[rear] = item; 61 | } 62 | 63 | void display(int queue[]) 64 | { 65 | int i; 66 | printf("\n"); 67 | if (front > rear) 68 | { 69 | for (i = front; i < size; i++) 70 | { 71 | printf("%d ", queue[i]); 72 | } 73 | for (i = 0; i <= rear; i++) 74 | printf("%d ", queue[i]); 75 | } 76 | else 77 | { 78 | for (i = front; i <= rear; i++) 79 | printf("%d ", queue[i]); 80 | } 81 | } 82 | 83 | void deleteq(int queue[]) 84 | { 85 | if (front == - 1) 86 | { 87 | printf("Queue is empty "); 88 | } 89 | else if (front == rear) 90 | { 91 | printf("\n %d deleted", queue[front]); 92 | front = - 1; 93 | rear = - 1; 94 | } 95 | else 96 | { 97 | printf("\n %d deleted", queue[front]); 98 | front++; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /C/Linkedlist.c: -------------------------------------------------------------------------------- 1 | //Problem: Create a singly linked list and traverse in it. 2 | #include 3 | #include 4 | 5 | //Create node of linked list 6 | struct Node { 7 | int data; 8 | struct Node* next; 9 | }; 10 | //linked list traversal 11 | //Complexity: O(n) where n = number of nodes 12 | void LLTraversal(struct Node* ptr){ 13 | while(ptr!=NULL){ 14 | printf("%d\n", ptr->data); 15 | ptr = ptr->next; 16 | } 17 | } 18 | int main() 19 | { 20 | struct Node* head; 21 | struct Node* second; 22 | struct Node* third; 23 | struct Node* fourth; 24 | head = (struct Node*)malloc(sizeof(struct Node)); 25 | second = (struct Node*)malloc(sizeof(struct Node)); 26 | third = (struct Node*)malloc(sizeof(struct Node)); 27 | fourth = (struct Node*)malloc(sizeof(struct Node)); 28 | 29 | head->data = 7; 30 | head->next = second; 31 | 32 | second->data = 11; 33 | second->next = third; 34 | 35 | third->data = 66; 36 | third->next = fourth; 37 | 38 | fourth->data = 2; 39 | fourth->next = NULL; 40 | 41 | LLTraversal(head); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /C/NQueenProblem.c: -------------------------------------------------------------------------------- 1 | // N-Queen Problem 2 | // Program 3 | #include 4 | #include 5 | #include 6 | int x[20],count=0; 7 | bool place(int k,int i) 8 | { 9 | int j; 10 | for(j=1;j<=k-1;j++) 11 | if((x[j]==i)||(abs(x[j]-i)== abs(j-k))) 12 | return false; 13 | return true; 14 | } 15 | void NQueen(int k,int n) 16 | { 17 | int i; 18 | for(i=1;i<=n;i++) 19 | { 20 | if(place(k,i)==true) 21 | { 22 | x[k]=i; 23 | if(k==n) 24 | { 25 | for(i=1;i<=n;i++) 26 | printf("%d\t",x[i]); 27 | printf("\n"); 28 | count++; 29 | } 30 | else NQueen(k+1,n); 31 | } 32 | } 33 | } 34 | int main() 35 | { 36 | int n; 37 | printf("\nEnter the size of the ChessBoard: "); 38 | scanf("%d",&n); 39 | NQueen(1,n); 40 | printf("\nThe total possible arrangements of %d Queens is: %d",n,count); 41 | } 42 | /* 43 | Output: 44 | Enter the size of the ChessBoard: 4 45 | 2 4 1 3 46 | 3 1 4 2 47 | 48 | The total possible arrangements of 4 Queens is: 2 49 | */ 50 | -------------------------------------------------------------------------------- /C/Queue.c: -------------------------------------------------------------------------------- 1 | //Problem statement: Create queue and demonstrate various operations performed in it. 2 | #include 3 | #include 4 | //Create structure for queue 5 | struct queue{ 6 | int size; 7 | int f; 8 | int r; 9 | int *arr; 10 | }; 11 | //Check whether queue is full 12 | int isFull(struct queue* q){ 13 | if(q->r==q->size-1){ 14 | return 1; 15 | } 16 | else{ return 0; } 17 | } 18 | //Check whether queue is empty 19 | int isEmpty(struct queue* q){ 20 | if(q->r==q->f){ 21 | return 1; 22 | } 23 | else{ return 0; } 24 | } 25 | //Insertion in queue 26 | void enqueue(struct queue* q, int val){ 27 | if(isFull(q)){ 28 | printf("Queue is full"); 29 | } 30 | else{ 31 | q->r++; 32 | q->arr[q->r]=val; 33 | } 34 | } 35 | //Deletion in queue 36 | int dequeue(struct queue* q){ 37 | int a=-1; 38 | if(isEmpty(q)){ 39 | printf("Queue is empty"); 40 | return -1; 41 | } 42 | else{ 43 | q->f++; 44 | a = q->arr[q->f]; 45 | return a; 46 | } 47 | } 48 | //Queue traversal 49 | void queueTraverse(struct queue* q){ 50 | for(int i=q->r; i>q->f; i--){ 51 | printf("Element: %d\n", q->arr[i]); 52 | } 53 | } 54 | int main(){ 55 | struct queue q; 56 | q.size = 100; 57 | q.f = q.r = -1; 58 | q.arr = (int *)malloc(q.size*sizeof(int)); 59 | enqueue(&q, 12); 60 | enqueue(&q, 23); 61 | enqueue(&q, 34); 62 | enqueue(&q, 45); 63 | enqueue(&q, 56); 64 | queueTraverse(&q); 65 | printf("Removed element: %d\n", dequeue(&q)); 66 | queueTraverse(&q); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/ASD.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/ASD.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/ATOZ.TXT: -------------------------------------------------------------------------------- 1 | the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/HOOKS.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/HOOKS.TXT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/JACK.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/JACK.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/LESSONLI.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/LESSONLI.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/PANKAJ.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/PANKAJ.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/PUSHKAR.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/PUSHKAR.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/RADIO.TXT: -------------------------------------------------------------------------------- 1 | Maxwell's theory applied primarily to light waves. About 15 years later the German physicist Heinrich Hertz actually generated such waves electrically. He supplied an electric charge to a capacitor, and then short-circuited the capacitor through a spark gap. In the resulting electric discharge the current surged past the neutral point, building up an opposite charge on the capacitor, and then continued to surge back and forth, creating an oscillating electric discharge in the form of a spark. Some of the energy of this oscillation was radiated from the spark gap in the form of electromagnetic waves. Hertz measured several of the properties of these so-called Hertzian waves, including their wavelength and velocity. The concept of using electromagnetic waves for the transmission of messages from one point to another was not new; the heliograph, for example, successfully transmitted messages via a beam of light rays, which could be modulated by means of a shutter to carry signals in the form of the dots and dashes of the Morse code. Radio has many advantages over light for this purpose, but these advantages were not immediately apparent. Radio waves, for example, can travel enormous distances; but microwaves (which Hertz used) cannot. Radio waves can be enormously attenuated and still be received, amplified, and detected; but good amplifiers were not available until the development of electron tubes. Although considerable progress was made in radiotelegraphy (for example, transatlantic communication was established in 1901), radiotelephony might never have become practical without the development of electronics. Historically, developments in radio and in electronics have been interdependent. -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/REGAN.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/REGAN.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/STAT.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/STAT.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/SUJAN.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/SUJAN.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/SUYOGYA.DAT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/SUYOGYA.DAT -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/TT.EXE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/TT.EXE -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/TT.H: -------------------------------------------------------------------------------- 1 | #define NLINES 5 2 | #define PARAWIDTH 60 3 | #define SCRWIDTH 80 4 | #define SCRHEIGHT 24 5 | #define PAGE PARAWIDTH*NLINES 6 | #define ESC 27 7 | #define HB 205 8 | #define VB 186 9 | #define TRC 187 10 | #define BRC 188 11 | #define BLC 200 12 | #define TLC 201 13 | #define TBR 185 14 | #define TBL 204 15 | #define LF 10 16 | #define CR 13 17 | 18 | #define RUNNING 1 19 | /*Main Menu*/ 20 | #define SELECTUSER 1 21 | #define STAT 2 22 | #define VIEWRECORDS 3 23 | #define ABOUT 4 24 | #define QUIT 5 25 | /*User Select Menu*/ 26 | #define USERLIST 1 27 | #define NEWUSER 2 28 | #define BACK 3 29 | /*Lesson Select Menu*/ 30 | #define LESSONLIST 1 31 | #define NEWLESSON 2 32 | #define BACK 3 33 | 34 | #define N 8 35 | #define ESC 27 36 | #define CLRSCR window(1,1,80,25);textbackground(0);clrscr(); 37 | /*keys*/ 38 | #define UP 72 39 | #define DOWN 80 40 | 41 | typedef struct 42 | { 43 | char name[N]; 44 | }user; 45 | 46 | typedef struct 47 | { 48 | char title[N]; 49 | unsigned long length; /*total length of characters in the text file*/ 50 | }lesson; 51 | 52 | typedef struct 53 | { 54 | user u; /*present user*/ 55 | struct tm dtntm; /*session date and time*/ 56 | lesson lsn; /*lession title*/ 57 | unsigned int wpm; /*average words per minute*/ 58 | float accuracy; 59 | }session; 60 | //struct tm { 61 | // int tm_sec; /* Seconds */ 62 | // int tm_min; /* Minutes */ 63 | // int tm_hour; /* Hour (0--23) */ 64 | // int tm_mday; /* Day of month (1--31) */ 65 | // int tm_mon; /* Month (0--11) */ 66 | // int tm_year; /* Year (calendar year minus 1900) */ 67 | // int tm_wday; /* Weekday (0--6; Sunday = 0) */ 68 | // int tm_yday; /* Day of year (0--365) */ 69 | // int tm_isdst; /* 0 if daylight savings time is not in effect) */ 70 | //}; 71 | /*define current user and current lesson*/ 72 | /*current user is int or char[]???*/ 73 | void loadFiles(void); 74 | int mainmenu(void); 75 | int userSelectMenu(void); 76 | void viewStat(void); 77 | void createUser(void); 78 | int listUser(void); 79 | //int lessonMenu(void); 80 | int listLesson(void); 81 | void beginSession(void); 82 | void addRecord(session); 83 | void sortSession(session[],int); 84 | void LoadPage(int *,int,int,int); 85 | void box(int x,int y,int width,int height,int fc,int bc); 86 | void DrawMenu(int,char **,int); 87 | void viewRecords(void); 88 | void about(void); 89 | 90 | -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/TT.OBJ: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/TT.OBJ -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/TT.PIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/C/Typing Tutor Project in C/Typing Tutor Coding/TT.PIF -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/USERLIST.DAT: -------------------------------------------------------------------------------- 1 | pushkarsuyogyareganasujanaasdnapankajJack -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/Typing Tutor Coding/WELCOME.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void WS2(void) 6 | { 7 | int i; 8 | char text[500]="\ 9 | 000000 00 00 00000 0000 00 00 0000\n\r\ 10 | 00 0000 00 00 00 0000 00 00\n\r\ 11 | 00 00 00000 00 00 00 00 00 00\n\r\ 12 | 00 00 00 00 00 0000 00 00\n\r\ 13 | 00 00 00 0000 00 00 0000\n\r\ 14 | \n\r\ 15 | 000000 000000 0000 000000 000000 00000\n\r\ 16 | 00 00 00 00 00 00 00\n\r\ 17 | 00 0000 00 00 0000 00000\n\r\ 18 | 00 00 00 00 00 00 00\n\r\ 19 | 00 00000 0000 00 000000 00 00\ 20 | \n\n\n\n\n\n\rPushkar Shakya (063/BCT/524)"; 21 | 22 | textmode(C80); 23 | textbackground(0); 24 | _setcursortype(_NOCURSOR); 25 | window(15,5,80,25); 26 | for(i=0;i<16;i++) 27 | { 28 | textcolor(i); 29 | clrscr(); 30 | cprintf(text); 31 | delay(100); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /C/Typing Tutor Project in C/readme.md: -------------------------------------------------------------------------------- 1 | 2 | Typing is the basic command to run a computer and your typing speed plays vital role while working in computer to save time. Here I have presented a Typing Tutor Project in C to measure user’s typing speed and to enhance their typing speed. This is a simple console application written in C programming language, and compiled using GNU GCC compiler on Code::Blocks IDE. 3 | 4 | The source code of Typing Tutor Project in C consists of over 600 lines of code which are made user friendly by attaching comments to describe the function of each command. The project source code effectively utilizes the user defined functions and the concept of file handling. 5 | 6 | Features: 7 | 8 | This project provides certain name as user by default and there a facility to create a new user too. You can enter your name as user. 9 | In the project, you can view the total statistics. 10 | The record option in the project is for displaying the data of user which has already used the application. you can view typing speed, accuracy etc. 11 | If you want to terminate the program without any use, you can select exit option in the main menu. 12 | 13 | 14 | -------------------------------------------------------------------------------- /C/countSort.c: -------------------------------------------------------------------------------- 1 | //Problem statement: Count sort for an array 2 | //Its running time complexity is O(n) 3 | #include 4 | #include 5 | #include 6 | void printArray(int* A, int high){ //function to print array 7 | for(int i=0; imax){ 16 | max = A[i]; 17 | } 18 | } 19 | return max; 20 | } 21 | void countSort(int *A, int n){ 22 | int max = maximum(A, n); 23 | int *count = (int *)malloc((max+1)*(sizeof(int))); //Create an array to count elements of array 24 | for(int i=0; i<=max; i++){ 25 | count[i] = 0; 26 | } 27 | // Store count of each character 28 | for(int i=0; i0){ 35 | A[j] = i; 36 | count[i] = count[i]-1; 37 | j++; 38 | } 39 | else{ 40 | i++; 41 | } 42 | } 43 | } 44 | int main(){ 45 | int n; 46 | scanf("%d", &n); 47 | int b[n]; 48 | for(int i=0; i 2 | int main() { 3 | printf("Hello World!"); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /C/mergeSort.c: -------------------------------------------------------------------------------- 1 | //Problem statement: Merge sort for an array 2 | //Complexity: Time complexity of Merge Sort is θ(nLogn) 3 | #include 4 | void printArray(int* A, int high){ //function to print array 5 | for(int i=0; i 5 | 6 | #define MAX 50 7 | 8 | void insert(); 9 | void delete(); 10 | void display(); 11 | int queue_array[MAX]; 12 | int rear = - 1; 13 | int front = - 1; 14 | main() 15 | { 16 | int choice; 17 | while (1) 18 | { 19 | printf("1.Insert element to queue \n"); 20 | printf("2.Delete element from queue \n"); 21 | printf("3.Display all elements of queue \n"); 22 | printf("4.Quit \n"); 23 | printf("Enter your choice : "); 24 | scanf("%d", &choice); 25 | switch (choice) 26 | { 27 | case 1: 28 | insert(); 29 | break; 30 | case 2: 31 | delete(); 32 | break; 33 | case 3: 34 | display(); 35 | break; 36 | case 4: 37 | exit(1); 38 | default: 39 | printf("Wrong choice \n"); 40 | } /* End of switch */ 41 | } /* End of while */ 42 | } /* End of main() */ 43 | 44 | void insert() 45 | { 46 | int add_item; 47 | if (rear == MAX - 1) 48 | printf("Queue Overflow \n"); 49 | else 50 | { 51 | if (front == - 1) 52 | /*If queue is initially empty */ 53 | front = 0; 54 | printf("Inset the element in queue : "); 55 | scanf("%d", &add_item); 56 | rear = rear + 1; 57 | queue_array[rear] = add_item; 58 | } 59 | } /* End of insert() */ 60 | 61 | void delete() 62 | { 63 | if (front == - 1 || front > rear) 64 | { 65 | printf("Queue Underflow \n"); 66 | return ; 67 | } 68 | else 69 | { 70 | printf("Element deleted from queue is : %d\n", queue_array[front]); 71 | front = front + 1; 72 | } 73 | } /* End of delete() */ 74 | 75 | void display() 76 | { 77 | int i; 78 | if (front == - 1) 79 | printf("Queue is empty \n"); 80 | else 81 | { 82 | printf("Queue is : \n"); 83 | for (i = front; i <= rear; i++) 84 | printf("%d ", queue_array[i]); 85 | printf("\n"); 86 | } 87 | } /* End of display() */ 88 | -------------------------------------------------------------------------------- /C/quickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int swap = 0; 3 | void printArray(int A[], int n){ 4 | printf("Array:"); 5 | for(int i=0; ipivot){ 20 | j--; 21 | } 22 | if(i 2 | void input(int A[],int n) 3 | { 4 | int i; 5 | printf("Enter %d number of elements: ",n); 6 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 9 | unsigned int n; 10 | scanf("%u",&n); 11 | unsigned int a[n],sum=0; 12 | for (int i=0; i 2 | #include 3 | 4 | int main() 5 | { 6 | int num, *arr, i; 7 | scanf("%d", &num); 8 | arr = (int*) malloc(num * sizeof(int)); 9 | for(i = 0; i < num; i++) { 10 | scanf("%d", arr + i); 11 | } 12 | 13 | 14 | /* Write the logic to reverse the array. */ 15 | int temp=0; 16 | for(int i=0,j=num-1;i<=j;i++,j--){ 17 | temp=arr[i]; 18 | arr[i]=arr[j]; 19 | arr[j]=temp; 20 | } 21 | 22 | for(i = 0; i < num; i++) 23 | printf("%d ", *(arr + i)); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /HackerRank-C-main/Bitwise Operators.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //Complete the following function. 6 | 7 | 8 | void calculate_the_maximum(int n, int k) { 9 | int AND=0,OR=0,XOR=0; 10 | for (int i=1;i<=n;i++){ 11 | for(int j=i+1;j<=n;j++){ 12 | if (((i&j) > AND) && ((i&j) < k)) { 13 | AND = i&j; 14 | } 15 | if (((i|j) > OR) && ((i|j) < k)) { 16 | OR = i|j; 17 | } 18 | if (((i^j) > XOR) && ((i^j) < k)) { 19 | XOR = i^j; 20 | } 21 | } 22 | } 23 | printf("%d\n%d\n%d\n",AND,OR,XOR); 24 | } 25 | 26 | int main() { 27 | int n, k; 28 | 29 | scanf("%d %d", &n, &k); 30 | calculate_the_maximum(n, k); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /HackerRank-C-main/Boxes through a Tunnel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_HEIGHT 41 4 | 5 | struct box 6 | { 7 | /* Define three fields of type int: length, width and height*/ 8 | int length, width, height; 9 | }; 10 | 11 | typedef struct box box; 12 | 13 | int get_volume(box b) { 14 | /*Return the volume of the box*/ 15 | return (b.length)*(b.width)*(b.height); //volume=length*width*height 16 | } 17 | 18 | int is_lower_than_max_height(box b) { 19 | /* Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise*/ 20 | if (b.height 2 | #include 3 | #include 4 | #include 5 | //Complete the following function. 6 | 7 | int find_nth_term(int n, int a, int b, int c) { 8 | //Write your code here. 9 | if (n<=4) 10 | return 6; 11 | return find_nth_term(n-1,a-1,b-2,c-3); 12 | } 13 | 14 | int main() { 15 | int n, a, b, c; 16 | 17 | scanf("%d %d %d %d", &n, &a, &b, &c); 18 | int ans = find_nth_term(n, a, b, c); 19 | 20 | printf("%d", ans); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank-C-main/Conditional Statements in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | 13 | 14 | 15 | int main() 16 | { 17 | char* n_endptr; 18 | char* n_str = readline(); 19 | int n = strtol(n_str, &n_endptr, 10); 20 | 21 | if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } 22 | 23 | // Write Your Code Here 24 | if (n==1) 25 | printf("one"); 26 | else if (n==2) 27 | printf("two"); 28 | else if (n==3) 29 | printf("three"); 30 | else if (n==4) 31 | printf("four"); 32 | else if (n==5) 33 | printf("five"); 34 | else if (n==6) 35 | printf("six"); 36 | else if (n==7) 37 | printf("seven"); 38 | else if (n==8) 39 | printf("eight"); 40 | else if (n==9) 41 | printf("nine"); 42 | else 43 | printf("Greater than 9"); 44 | return 0; 45 | } 46 | 47 | char* readline() { 48 | size_t alloc_length = 1024; 49 | size_t data_length = 0; 50 | char* data = malloc(alloc_length); 51 | 52 | while (true) { 53 | char* cursor = data + data_length; 54 | char* line = fgets(cursor, alloc_length - data_length, stdin); 55 | 56 | if (!line) { break; } 57 | 58 | data_length += strlen(cursor); 59 | 60 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 61 | 62 | size_t new_length = alloc_length << 1; 63 | data = realloc(data, new_length); 64 | 65 | if (!data) { break; } 66 | 67 | alloc_length = new_length; 68 | } 69 | 70 | if (data[data_length - 1] == '\n') { 71 | data[data_length - 1] = '\0'; 72 | } 73 | 74 | data = realloc(data, data_length); 75 | 76 | return data; 77 | } 78 | -------------------------------------------------------------------------------- /HackerRank-C-main/Digit Frequency.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | char S[1000]; 9 | int h[10]={0,0,0,0,0,0,0,0,0,0} ; //hasing technique 10 | 11 | scanf("%s",S); 12 | 13 | for(int i = 0 ; i <= strlen(S);i++){ 14 | if(S[i]>='0' && S[i]<='9') 15 | h[S[i]-'0']++ ; 16 | } 17 | 18 | for(int i = 0 ; i <= 9 ;i++) 19 | printf("%d ",h[i]) ; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank-C-main/Dynamic Array in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int* total_number_of_books; 5 | 6 | int** total_number_of_pages; 7 | 8 | int main() 9 | { 10 | int total_number_of_shelves; 11 | scanf("%d", &total_number_of_shelves); 12 | 13 | int total_number_of_queries; 14 | scanf("%d", &total_number_of_queries); 15 | 16 | total_number_of_books=(int*)malloc(sizeof(int)*total_number_of_shelves); 17 | 18 | total_number_of_pages=(int**)malloc(sizeof(int*)*total_number_of_shelves); 19 | 20 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | 8 | int main() 9 | { 10 | int a, b; 11 | scanf("%d\n%d", &a, &b); 12 | // Complete the code. 13 | char c[10][10]={"","one","two","three","four","five","six","seven","eight","nine"}; 14 | for (int n=a; n<=b; n++) { 15 | ((n<=9)?printf("%s\n",c[n]):(n%2==0)?printf("even\n"):printf("odd\n")); 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /HackerRank-C-main/Functions in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | Add `int max_of_four(int a, int b, int c, int d)` here. 4 | */ 5 | int max_of_four(int w,int x,int y,int z){ 6 | return ((w>x && w>y && w>z)?w:(x>w && x>y && x>z)?x:(y>w && y>x && y>z)?y:z); 7 | } 8 | 9 | int main() { 10 | int a, b, c, d; 11 | scanf("%d %d %d %d", &a, &b, &c, &d); 12 | int ans = max_of_four(a, b, c, d); 13 | printf("%d", ans); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HackerRank-C-main/Playing With Characters.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 10 | char ch,s[100],sen[100]; 11 | scanf("%c",&ch); 12 | scanf("%s",s); 13 | scanf(" %[^\n]s",sen); 14 | printf("%c\n",ch); 15 | printf("%s\n",s); 16 | printf("%s\n",sen); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /HackerRank-C-main/Pointers in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void update(int *a,int *b) { 4 | // Complete this function 5 | int bb; 6 | bb = *a; 7 | *a=*a+*b; 8 | if (bb>*b) 9 | *b=bb-*b; 10 | else 11 | *b=*b-bb; 12 | } 13 | 14 | int main() { 15 | int a, b; 16 | int *pa = &a, *pb = &b; 17 | 18 | scanf("%d %d", &a, &b); 19 | update(pa, pb); 20 | printf("%d\n%d", a, b); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /HackerRank-C-main/Printing Pattern Using Loops.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | int n; 10 | scanf("%d", &n); 11 | // Complete the code to print the pattern. 12 | int len = n*2 - 1; 13 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | char *s; 9 | s = malloc(1024 * sizeof(char)); 10 | scanf("%[^\n]", s); 11 | s = realloc(s, strlen(s) + 1); 12 | //Write your logic to print the tokens of the sentence here. 13 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | struct triangle 6 | { 7 | int a; 8 | int b; 9 | int c; 10 | }; 11 | 12 | typedef struct triangle triangle; 13 | 14 | /*for finding area of triangle*/ 15 | double area(triangle tr){ 16 | double p=(tr.a+tr.b+tr.c)/2.0; 17 | return sqrt(p*(p-tr.a)*(p-tr.b)*(p-tr.c)); 18 | } 19 | /*for swapping triangles*/ 20 | void swap(triangle* t1,triangle* t2){ 21 | triangle temp; 22 | temp=*t1; 23 | *t1=*t2; 24 | *t2=temp; 25 | } 26 | void sort_by_area(triangle* tr, int n) { 27 | /* Sort an array a of the length n*/ 28 | /*Bubble sort*/ 29 | for (int i=0;iarea(tr[j+1])) 32 | swap(&tr[j],&tr[j+1]); 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | int n; 39 | scanf("%d", &n); 40 | triangle *tr = malloc(n * sizeof(triangle)); 41 | for (int i = 0; i < n; i++) { 42 | scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c); 43 | } 44 | sort_by_area(tr, n); 45 | for (int i = 0; i < n; i++) { 46 | printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /HackerRank-C-main/Students Marks Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //Complete the following function. 7 | 8 | int marks_summation(int* marks, int number_of_students, char gender) { 9 | //Write your code here. 10 | int girls_sum=0; 11 | int boys_sum=0; 12 | for (int i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | int a,b; 10 | float c,d; 11 | scanf("%d %d",&a,&b); 12 | scanf("%f %f",&c,&d); 13 | printf("%d %d\n",a+b,a-b); 14 | printf("%.1f %.1f",c+d,c-d); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HackerRank-C-main/Sum of Digits of a Five Digit Number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int n; 9 | scanf("%d", &n); 10 | //Complete the code to calculate the sum of the five digits on n. 11 | int sum=0; 12 | while (n>0){ 13 | sum+=n%10; 14 | n/=10; 15 | } 16 | printf("%d\n",sum); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /HackerRank-C-main/_Hello World!_ in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | char s[100]; 10 | scanf("%[^\n]%*c", &s); 11 | 12 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 13 | printf("Hello, World!\n"); 14 | printf("%s",s); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Alphabet Rangoli.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | def print_rangoli(n): 4 | alpha = string.ascii_lowercase 5 | L = [] 6 | for i in range(n): 7 | s = "-".join(alpha[i:n]) 8 | L.append((s[::-1]+s[1:]).center(4*n-3, "-")) 9 | print('\n'.join(L[:0:-1]+L)) 10 | if __name__ == '__main__': 11 | n = int(input()) 12 | print_rangoli(n) 13 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Arithmetic Operators.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | a = int(input()) 3 | b = int(input()) 4 | print(a+b) 5 | print(a-b) 6 | print(a*b) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Calendar Module.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | import calendar 3 | m,d,y=map(int,input().split()) 4 | print(list(calendar.day_name)[calendar.weekday(y, m, d)].upper()) 5 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Capitalize!.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | import string 9 | 10 | # Complete the solve function below. 11 | def solve(s): 12 | return string.capwords(s, ' ') 13 | 14 | if __name__ == '__main__': 15 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 16 | 17 | s = input() 18 | 19 | result = solve(s) 20 | 21 | fptr.write(result + '\n') 22 | 23 | fptr.close() 24 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Designer Door Mat.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | n, m = map(int,input().split()) 3 | pattern = [('.|.'*(2*i + 1)).center(m, '-') for i in range(n//2)] 4 | print('\n'.join(pattern + ['WELCOME'.center(m, '-')] + pattern[::-1])) 5 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Find a String.py: -------------------------------------------------------------------------------- 1 | def count_substring(string, sub_string): 2 | count =0 3 | for i in range(len(string)-len(sub_string)+1): 4 | try: 5 | if string[i:i+len(sub_string)]==sub_string: 6 | count+=1 7 | except IndexError: 8 | break 9 | return count 10 | 11 | if __name__ == '__main__': 12 | string = input().strip() 13 | sub_string = input().strip() 14 | 15 | count = count_substring(string, sub_string) 16 | print(count) 17 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Find the Runner-Up Score!.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | arr = list(map(int, input().split())) 4 | arr.sort(reverse=True) 5 | first=arr[0] 6 | for i in range(len(arr)): 7 | if (first>arr[i]): 8 | print(arr[i]) 9 | break 10 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Finding the percentage.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | student_marks = {} 4 | for _ in range(n): 5 | name, *line = input().split() 6 | scores = list(map(float, line)) 7 | student_marks[name] = scores 8 | query_name = input() 9 | print('{:.2f}'.format(sum(student_marks[query_name])/len(student_marks[query_name]))) 10 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Incorrect Regex.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | import re 3 | for t in range(int(input())): 4 | try: 5 | print(bool(re.compile(input()))) 6 | except: 7 | print('False') 8 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Input().py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | x,k=map(int,input().split()) 3 | p=input() 4 | if eval(p)==k: 5 | print(True) 6 | else: 7 | print(False) 8 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Integers Come In All Sizes.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | a=int(input()) 3 | b=int(input()) 4 | c=int(input()) 5 | d=int(input()) 6 | print(pow(a,b)+pow(c,d)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/List Comprehensions.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | x, y, z, n = int(input()), int(input()), int(input()), int(input()) 3 | print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if a + b + c != n ]) 4 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Lists.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for _ in range(n): 4 | s = input().split() 5 | cmd = s[0] 6 | args = s[1:] 7 | if (cmd !="print"): 8 | cmd += "("+ ",".join(args) +")" 9 | eval("l."+cmd) 10 | else: 11 | print (l) 12 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Loops.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | for i in range(n): 4 | print(i**2) 5 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Mod Divmod.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | a=int(input()) 3 | b=int(input()) 4 | print(a//b) 5 | print(a%b) 6 | print(divmod(a,b)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Mutations.py: -------------------------------------------------------------------------------- 1 | def mutate_string(string, position, character): 2 | return (string[:position]+character+string[position+1:]) 3 | 4 | if __name__ == '__main__': 5 | s = input() 6 | i, c = input().split() 7 | s_new = mutate_string(s, int(i), c) 8 | print(s_new) 9 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Nested Lists.py: -------------------------------------------------------------------------------- 1 | marksheet = [] 2 | for _ in range(0,int(input())): 3 | marksheet.append([input(), float(input())]) 4 | 5 | second_highest = sorted(list(set([marks for name, marks in marksheet])))[1] 6 | print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest])) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Polar Coordinates.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from cmath import polar 3 | print ('{}\n{}'.format(*polar(complex(input())))) 4 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Power - Mod Power.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | a=int(input()) 3 | b=int(input()) 4 | m=int(input()) 5 | print(pow(a,b)) 6 | print(pow(a,b,m)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Print Function.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | for i in range(1,n+1): 4 | print(i,end='') 5 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Python If-Else.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | if __name__ == '__main__': 12 | n = int(input().strip()) 13 | if (n%2==0 and n in range(2,6)): 14 | print('Not Weird') 15 | elif (n%2==0 and n in range(6,21)): 16 | print('Weird') 17 | elif(n%2==0 and n>20): 18 | print('Not Weird') 19 | elif(n%2!=0): 20 | print('Weird') 21 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Python_ Division.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | a = int(input()) 3 | b = int(input()) 4 | print(a//b) 5 | print(a/b) 6 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Say _Hello, World!_ With Python.py: -------------------------------------------------------------------------------- 1 | print("Hello, World!") 2 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Set .difference() Operation.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | ne=int(input()) 3 | re=set(map(int,input().split())) 4 | nb=int(input()) 5 | rb=set(map(int,input().split())) 6 | print(len(re-rb)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Set .discard(), .remove() & .pop().py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = set(map(int, input().split())) 3 | for i in range(int(input())): 4 | eval('s.{0}({1})'.format(*input().split()+[''])) 5 | 6 | print(sum(s)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Set .intersection() Operation.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | ne=int(input()) 3 | re=set(map(int,input().split())) 4 | nb=int(input()) 5 | rb=set(map(int,input().split())) 6 | print(len(re&rb)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Set .union() Operation.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | ne=int(input()) 3 | re=set(map(int,input().split())) 4 | nb=int(input()) 5 | rb=set(map(int,input().split())) 6 | print(len(re|rb)) 7 | -------------------------------------------------------------------------------- /HackerRank-Python-main/String Formatting.py: -------------------------------------------------------------------------------- 1 | def print_formatted(number): 2 | # your code goes here 3 | width = len("{0:b}".format(number)) 4 | for i in range(1,n+1): 5 | print( "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i, width=width)) 6 | if __name__ == '__main__': 7 | n = int(input()) 8 | print_formatted(n) 9 | -------------------------------------------------------------------------------- /HackerRank-Python-main/String Split and Join.py: -------------------------------------------------------------------------------- 1 | def split_and_join(line): 2 | return ("-".join(line.split())) 3 | 4 | if __name__ == '__main__': 5 | line = input() 6 | result = split_and_join(line) 7 | print(result) 8 | -------------------------------------------------------------------------------- /HackerRank-Python-main/String Validators.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | s = input() 3 | for test in ('isalnum', 'isalpha', 'isdigit', 'islower', 'isupper'): 4 | print(any(eval("c." + test + "()") for c in s)) 5 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Text Alignment.py: -------------------------------------------------------------------------------- 1 | #Replace all ______ with rjust, ljust or center. 2 | 3 | thickness = int(input()) #This must be an odd number 4 | c = 'H' 5 | 6 | #Top Cone 7 | for i in range(thickness): 8 | print((c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1)) 9 | 10 | #Top Pillars 11 | for i in range(thickness+1): 12 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 13 | 14 | #Middle Belt 15 | for i in range((thickness+1)//2): 16 | print((c*thickness*5).center(thickness*6)) 17 | 18 | #Bottom Pillars 19 | for i in range(thickness+1): 20 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 21 | 22 | #Bottom Cone 23 | for i in range(thickness): 24 | print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6)) 25 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Text Wrap.py: -------------------------------------------------------------------------------- 1 | import textwrap 2 | 3 | def wrap(string, max_width): 4 | 5 | return '\n'.join(textwrap.wrap(string,max_width)) 6 | 7 | if __name__ == '__main__': 8 | string, max_width = input(), int(input()) 9 | result = wrap(string, max_width) 10 | print(result) 11 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Triangle Quest.py: -------------------------------------------------------------------------------- 1 | for i in range(1,int(input())): 2 | print((10**(i)//9)*i) 3 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Tuples.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | t=tuple(map(int,input().split())) 3 | print(hash(t)) 4 | -------------------------------------------------------------------------------- /HackerRank-Python-main/What's Your Name_.py: -------------------------------------------------------------------------------- 1 | def print_full_name(a, b): 2 | print(f"Hello {a} {b}! You just delved into python.") 3 | 4 | if __name__ == '__main__': 5 | first_name = input() 6 | last_name = input() 7 | print_full_name(first_name, last_name) 8 | -------------------------------------------------------------------------------- /HackerRank-Python-main/Write a function.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | 4 | # Write your logic here 5 | if (year%4==0 and year%100!=0 or year%400==0 ): 6 | leap=True 7 | 8 | return leap 9 | 10 | year = int(input()) 11 | print(is_leap(year)) 12 | -------------------------------------------------------------------------------- /HackerRank-Python-main/python.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/HackerRank-Python-main/python.png -------------------------------------------------------------------------------- /HackerRank-Python-main/sWAP cASE.py: -------------------------------------------------------------------------------- 1 | def swap_case(s): 2 | a = "" 3 | for let in s: 4 | if (let.isupper() == True): 5 | a+=(let.lower()) 6 | else: 7 | a+=(let.upper()) 8 | return a 9 | 10 | if __name__ == '__main__': 11 | s = input() 12 | result = swap_case(s) 13 | print(result) 14 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Binary Tree Nodes.sql: -------------------------------------------------------------------------------- 1 | select CONCAT(N," ", 2 | (CASE 3 | when P is null THEN "Root" 4 | when N in (select P from BST where P is not null) then "Inner" 5 | else "Leaf" 6 | end) 7 | ) from BST order by N; -------------------------------------------------------------------------------- /HackerRank-SQL-main/Employee Names.sql: -------------------------------------------------------------------------------- 1 | -- Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order. 2 | 3 | SELECT NAME FROM EMPLOYEE ORDER BY NAME ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Employee Salaries.sql: -------------------------------------------------------------------------------- 1 | /* Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than $2000 2 | per month who have been employees for less than 10 months. Sort your result by ascending employee_id. */ 3 | 4 | SELECT NAME FROM EMPLOYEE WHERE SALARY > 2000 AND MONTHS <10 ORDER BY EMPLOYEE_ID ; 5 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Higher Than 75 Marks.sql: -------------------------------------------------------------------------------- 1 | /* Query the Name of any student in STUDENTS who scored higher than 75 Marks. Order your output by the last three characters of each name. 2 | If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.*/ 3 | 4 | SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY RIGHT(NAME, 3), ID; 5 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Japanese Cities' Attributes.sql: -------------------------------------------------------------------------------- 1 | --Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN. 2 | 3 | SELECT * FROM CITY WHERE COUNTRYCODE = "JPN" ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Japanese Cities' Names.sql: -------------------------------------------------------------------------------- 1 | --Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN. 2 | 3 | SELECT NAME FROM CITY WHERE COUNTRYCODE = "JPN" ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Revising the Select Query I.sql: -------------------------------------------------------------------------------- 1 | --Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA. 2 | 3 | SELECT * FROM CITY WHERE COUNTRYCODE = "USA" AND POPULATION > 100000 ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Revising the Select Query II.sql: -------------------------------------------------------------------------------- 1 | --Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA. 2 | 3 | SELECT NAME FROM CITY WHERE COUNTRYCODE="USA" AND POPULATION >120000 ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Select All.sql: -------------------------------------------------------------------------------- 1 | --Query all columns (attributes) for every row in the CITY table. 2 | 3 | SELECT * FROM CITY ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Select By ID.sql: -------------------------------------------------------------------------------- 1 | -- Query all columns for a city in CITY with the ID 1661. 2 | 3 | SELECT * FROM CITY WHERE ID=1661 ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Tables/regexp_sql.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/HackerRank-SQL-main/Tables/regexp_sql.png -------------------------------------------------------------------------------- /HackerRank-SQL-main/The PADS.sql: -------------------------------------------------------------------------------- 1 | select CONCAT(Name,'(',SUBSTRING(Occupation,1,1),')') from Occupations Order by Name; 2 | select CONCAT("There are a total of ",count(occupation)," ",LOWER(occupation),"s.") from Occupations group by occupation order by count(occupation); -------------------------------------------------------------------------------- /HackerRank-SQL-main/Type of Triangle.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table: 3 | 4 | Equilateral: It's a triangle with 3 sides of equal length. 5 | Isosceles: It's a triangle with 2 sides of equal length. 6 | Scalene: It's a triangle with 3 sides of differing lengths. 7 | Not A Triangle: The given values of A, B, and C don't form a triangle. 8 | */ 9 | 10 | SELECT CASE 11 | WHEN A + B > C AND B + C > A AND A + C > B THEN 12 | CASE 13 | WHEN A = B AND B = C THEN 'Equilateral' 14 | WHEN A = B OR B = C OR A = C THEN 'Isosceles' 15 | ELSE 'Scalene' 16 | END 17 | ELSE 'Not A Triangle' 18 | END 19 | FROM TRIANGLES; 20 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 1.sql: -------------------------------------------------------------------------------- 1 | --Query a list of CITY and STATE from the STATION table. 2 | 3 | SELECT CITY, STATE FROM STATION ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 10.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '[^aeiou]$' ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 11.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[^aieou]|.*[^aeiou]$' ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 12.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[^aeiou].*[^aeiou]$'; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 3.sql: -------------------------------------------------------------------------------- 1 | --Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer. 2 | 3 | SELECT DISTINCT CITY FROM STATION WHERE ID%2=0 ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 4.sql: -------------------------------------------------------------------------------- 1 | --Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table. 2 | 3 | SELECT COUNT(CITY)- COUNT(DISTINCT CITY) FROM STATION ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 5.sql: -------------------------------------------------------------------------------- 1 | --Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically. 2 | 3 | (SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY ASC LIMIT 1) 4 | UNION 5 | (SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) ASC, CITY ASC LIMIT 1); 6 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 6.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[aeiou]+'; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 7.sql: -------------------------------------------------------------------------------- 1 | -- Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '[aeiou]$' ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 8.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[aeiou].+[aeiou]$' ; 4 | -------------------------------------------------------------------------------- /HackerRank-SQL-main/Weather Observation Station 9.sql: -------------------------------------------------------------------------------- 1 | --Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates. 2 | 3 | SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[^AEIOU]' ; 4 | -------------------------------------------------------------------------------- /Java/Anagrams.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Anagrams { 3 | 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | String a = sc.next(); 8 | String b = sc.next(); 9 | boolean isAnagram = false; 10 | boolean visited[] = new boolean[b.length()]; 11 | 12 | if(a.length() == b.length()) { 13 | 14 | for(int i = 0; i < a.length(); i++) 15 | { 16 | char c = a.charAt(i); 17 | isAnagram = false; 18 | for(int j = 0; j < b.length(); j++) 19 | { 20 | if(b.charAt(j) == c && !visited[j]) 21 | { 22 | visited[j] = true; 23 | isAnagram = true; 24 | break; 25 | } 26 | } 27 | if(!isAnagram) 28 | break; 29 | } 30 | } 31 | if(isAnagram) 32 | System.out.println("anagram"); 33 | else 34 | System.out.println("not anagram"); 35 | 36 | sc.close(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Java/ArrayToSet.java: -------------------------------------------------------------------------------- 1 | //Convert Array to Set 2 | 3 | import java.util.*; 4 | 5 | public class ArraySet { 6 | 7 | public static void main(String[] args) { 8 | 9 | String[] array = {"a", "b", "c"}; 10 | Set set = new HashSet<>(Arrays.asList(array)); 11 | 12 | System.out.println("Set: " + set); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Java/B_Shifting_Sort.java: -------------------------------------------------------------------------------- 1 | // link to problem:- https://codeforces.com/problemset/problem/1579/B 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | 6 | public class B_Shifting_Sort{ 7 | 8 | public static void main(String[] args){ 9 | Scanner sc=new Scanner(System.in); 10 | int t=sc.nextInt(); 11 | while(t-->0){ 12 | int n=sc.nextInt(); 13 | int arr[]=new int[n]; 14 | for(int i=0;i ans=new ArrayList<>(); 19 | 20 | for(int i=0;i=i;k--){ 37 | int temp=arr[k]; 38 | arr[k]=arr[k-1]; 39 | arr[k-1]=temp; 40 | } 41 | 42 | 43 | } 44 | 45 | 46 | // System.out.println("Updated Array"); 47 | // System.out.println(); 48 | // for(int l:arr) System.out.print(l+" "); 49 | 50 | 51 | } 52 | 53 | System.out.println(ans.size()); 54 | for(int i=0;i arr[mid]) { 23 | start = mid + 1; 24 | } else { 25 | // ans found 26 | return mid; 27 | } 28 | } 29 | return -1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Java/Calculator1.java: -------------------------------------------------------------------------------- 1 | package Calculator; 2 | 3 | import java.awt.Color; 4 | import java.awt.Dimension; 5 | import java.awt.Font; 6 | import java.text.Format; 7 | import java.util.stream.DoubleStream.Builder; 8 | 9 | import javax.swing.BorderFactory; 10 | import javax.swing.JButton; 11 | import javax.swing.JFrame; 12 | import javax.swing.JPanel; 13 | import javax.swing.JTextArea; 14 | import javax.swing.border.Border; 15 | 16 | 17 | public class Calculator{ 18 | JFrame frame = new JFrame(); 19 | JPanel panel = new JPanel(); 20 | JTextArea textarea = new JTextArea(2,10); 21 | 22 | JButton button1 = new JButton(); 23 | JButton button2 = new JButton(); 24 | JButton button3 = new JButton(); 25 | JButton button4 = new JButton(); 26 | JButton button5= new JButton(); 27 | JButton button6 = new JButton(); 28 | JButton button7 = new JButton(); 29 | JButton button8 = new JButton(); 30 | JButton button9= new JButton(); 31 | JButton button0 = new JButton(); 32 | 33 | JButton buttonadd = new JButton(); 34 | JButton buttonsub = new JButton(); 35 | JButton buttonmul = new JButton(); 36 | JButton buttondiv = new JButton(); 37 | JButton buttonclear = new JButton(); 38 | JButton buttondot = new JButton(); 39 | JButton buttonequal = new JButton(); 40 | 41 | double number1, number2, result; 42 | int add =0, mul =0, sub =0, div =0; 43 | 44 | public Calculator(){ 45 | frame.setSize(340,440); 46 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 47 | frame.setTitle("Simple Calculator "); 48 | 49 | //frame.setResizable(false) 50 | frame.add(panel); 51 | Border border =BorderFactory.createLineBorder(Color.WHITE,4); 52 | panel.add(textarea); 53 | textarea.setBackground(Color.BLACK); 54 | Border tborder = BorderFactory.createLineBorder(Color.BLUE,3); 55 | Font font = new Font("Times New Roman", Font.BOLD,33); 56 | textarea.setFont(font); 57 | textarea.setForeground(Color.WHITE); 58 | 59 | textarea.setPreferredSize(new Dimension(2,10)); 60 | textarea.setLineWrap(true); 61 | } 62 | } -------------------------------------------------------------------------------- /Java/CocktailSort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Cocktail Sort 2 | public class CocktailSort 3 | { 4 | void cocktailSort(int a[]) 5 | { 6 | boolean swapped = true; 7 | int start = 0; 8 | int end = a.length; 9 | 10 | while (swapped == true) 11 | { 12 | // reset the swapped flag on entering the 13 | // loop, because it might be true from a 14 | // previous iteration. 15 | swapped = false; 16 | 17 | // loop from bottom to top same as 18 | // the bubble sort 19 | for (int i = start; i < end - 1; ++i) 20 | { 21 | if (a[i] > a[i + 1]) { 22 | int temp = a[i]; 23 | a[i] = a[i + 1]; 24 | a[i + 1] = temp; 25 | swapped = true; 26 | } 27 | } 28 | 29 | // if nothing moved, then array is sorted. 30 | if (swapped == false) 31 | break; 32 | 33 | // otherwise, reset the swapped flag so that it 34 | // can be used in the next stage 35 | swapped = false; 36 | 37 | // move the end point back by one, because 38 | // item at the end is in its rightful spot 39 | end = end - 1; 40 | 41 | // from top to bottom, doing the 42 | // same comparison as in the previous stage 43 | for (int i = end - 1; i >= start; i--) 44 | { 45 | if (a[i] > a[i + 1]) 46 | { 47 | int temp = a[i]; 48 | a[i] = a[i + 1]; 49 | a[i + 1] = temp; 50 | swapped = true; 51 | } 52 | } 53 | 54 | // increase the starting point, because 55 | // the last stage would have moved the next 56 | // smallest number to its rightful spot. 57 | start = start + 1; 58 | } 59 | } 60 | 61 | /* Prints the array */ 62 | void printArray(int a[]) 63 | { 64 | int n = a.length; 65 | for (int i = 0; i < n; i++) 66 | System.out.print(a[i] + " "); 67 | System.out.println(); 68 | } 69 | 70 | // Driver code 71 | public static void main(String[] args) 72 | { 73 | CocktailSort ob = new CocktailSort(); 74 | int a[] = { 5, 1, 4, 2, 8, 0, 2 }; 75 | ob.cocktailSort(a); 76 | System.out.println("Sorted array"); 77 | ob.printArray(a); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Java/Duplicate_in_Array_O(N)_solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int findDuplicate(int[] arr) { 4 | 5 | int sum=0, sum2=0; 6 | for (int i=0; i= 0; i--) { 8 | result.append(targetAsString.charAt(i)); 9 | } 10 | 11 | return result.toString().equals(targetAsString); 12 | } 13 | 14 | public static boolean isPalindromeNumberWithAnotherApproach(int target) { 15 | if(target < 0 || (target % 10 == 0 && target != 0)) { 16 | return false; 17 | } 18 | 19 | int revertedNumber = 0; 20 | while(target > revertedNumber) { 21 | revertedNumber = revertedNumber * 10 + target % 10; 22 | target /= 10; 23 | } 24 | 25 | return target == revertedNumber || target == revertedNumber / 10; 26 | } 27 | 28 | public static void main(String [] args) { 29 | boolean isPalindrome = PalindromeNumber.isPalindromeNumber(121); 30 | if(isPalindrome) { 31 | System.out.println("121 is palindrome"); 32 | } else { 33 | System.out.println("121 is not palindrome"); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Java/RemoveDuplicatesInSortedArray.java: -------------------------------------------------------------------------------- 1 | //remove duplicates in sorted array 2 | 3 | class RemoveDuplicatesInSortedArray 4 | { 5 | 6 | static int removeDuplicates(int arr[], int n) 7 | { 8 | if (n == 0 || n == 1) 9 | return n; 10 | int j = 0; 11 | 12 | for (int i = 0; i < n-1; i++) 13 | if (arr[i] != arr[i+1]) 14 | arr[j++] = arr[i]; 15 | 16 | arr[j++] = arr[n-1]; 17 | 18 | return j; 19 | } 20 | 21 | public static void main (String[] args) 22 | { 23 | int arr[] = {2, 2, 2, 4, 4, 4, 4, 5, 5, 6}; 24 | int n = arr.length; 25 | 26 | n = removeDuplicates(arr, n); 27 | 28 | for (int i=0; iperi): 6 | print("Area") 7 | print(Area) 8 | elif(peri>Area): 9 | print("Peri") 10 | print(peri) 11 | else: 12 | print("Eq") 13 | print(Area) -------------------------------------------------------------------------------- /Python/BinaryNumbers.py: -------------------------------------------------------------------------------- 1 | from itertools import groupby 2 | n=262141 3 | rem="" 4 | while(n!=0): 5 | rem+=str(n%2) 6 | n=n//2 7 | binary_num=int(rem[:: -1]) 8 | print(binary_num) 9 | groups = groupby(str(binary_num)) 10 | result = [(label, sum(1 for _ in group)) for label, group in groups] 11 | print(result) 12 | size1=len(result)-1 13 | max=0 14 | for i in range(len(result)): 15 | if(result[i][0]=='1'): 16 | if(result[i][1]>result[size1][1] or result[i][1]==result[size1][1]): 17 | max=result[i][1] 18 | size1-=1 19 | print(max) 20 | """ 21 | #!/bin/python3 22 | 23 | import math 24 | import os 25 | import random 26 | import re 27 | import sys 28 | from itertools import groupby 29 | 30 | 31 | if __name__ == '__main__': 32 | n = int(input().strip()) 33 | rem="" 34 | while(n!=0): 35 | rem+=str(n%2) 36 | n=n//2 37 | binary_num=int(rem[:: -1]) 38 | groups = groupby(str(binary_num)) 39 | result = [(label, sum(1 for _ in group)) for label, group in groups] 40 | print(result[0][1]) 41 | """ 42 | 43 | """ 44 | size1=len(result)-1 45 | size2=len(result[0])-1 46 | max=0 47 | for i in range(len(result)): 48 | size1-=1 49 | for j in range(len(result[i])): 50 | if(int(result[i][j])>int(result[size1][size2]) or int(result[i][j])==int(result[size1][size2])): 51 | max=int(result[i][j]) 52 | else: 53 | continue 54 | print(max) 55 | """ -------------------------------------------------------------------------------- /Python/CarGame.py: -------------------------------------------------------------------------------- 1 | quit=True 2 | while(quit): 3 | command=input(">") 4 | if(command.lower()=="help"): 5 | print("start - to strat the car") 6 | print("stop - to stop the car") 7 | print("quit - to exit") 8 | elif(command.lower()=="start"): 9 | print("Car started...Ready to go!") 10 | elif(command.lower()=="stop"): 11 | print("Car stopped") 12 | elif(command.lower()=='quit'): 13 | quit=False 14 | else: 15 | print("I don't understand that...") 16 | -------------------------------------------------------------------------------- /Python/FactorialOfNumbers.py: -------------------------------------------------------------------------------- 1 | for i in range(int(input())): 2 | fact=1 3 | a=int(input()) 4 | for j in range(1,a+1,1): 5 | fact=fact*j 6 | print(fact) -------------------------------------------------------------------------------- /Python/File_handling.py: -------------------------------------------------------------------------------- 1 | #this is the example of file handling using python 2 | 3 | def game(): 4 | return int(input("enter score ")) 5 | score=game() 6 | with open("Highscore.txt") as f: 7 | hiScoreStr=f.read() 8 | if hiScoreStr=='': 9 | with open("Highscore.txt","w") as f: 10 | f.write(str(score)) 11 | print("updated") 12 | elif int(hiScoreStr)=score: 17 | print(hiScoreStr) 18 | 19 | print("do you want to reset?") 20 | choice = input("enter yes or no in lowercase ") 21 | if choice == "yes": 22 | score = int(input("enter new value ")) 23 | with open("Highscore.txt","w") as f: 24 | f.write(str(score)) 25 | print("updated") 26 | else: 27 | print("Appreciate your patience") 28 | print(f.read()) 29 | -------------------------------------------------------------------------------- /Python/FizzBuzz.py: -------------------------------------------------------------------------------- 1 | for fizzbuzz in range(51): 2 | if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0: 3 | print("fizzbuzz") 4 | continue 5 | elif fizzbuzz % 3 == 0: 6 | print("fizz") 7 | continue 8 | elif fizzbuzz % 5 == 0: 9 | print("buzz") 10 | continue 11 | print(fizzbuzz) 12 | -------------------------------------------------------------------------------- /Python/HeapSort.py: -------------------------------------------------------------------------------- 1 | def heapify(array, a, b): 2 | largest = b 3 | l = 2 * b + 1 4 | root = 2 * b + 2 5 | 6 | if l < a and array[b] < array[l]: 7 | largest = l 8 | 9 | if root < a and array[largest] < array[root]: 10 | largest = root 11 | 12 | # Change root 13 | if largest != b: 14 | array[b], array[largest] = array[largest], array[b] 15 | heapify(array, a, largest) 16 | 17 | # sort an array of given size 18 | def Heap_Sort(array): 19 | a = len(array) 20 | 21 | # maxheap.. 22 | for b in range(a // 2 - 1, -1, -1): 23 | heapify(array, a, b) 24 | 25 | # extract elements 26 | for b in range(a-1, 0, -1): 27 | array[b], array[0] = array[0], array[b] # swap 28 | heapify(array, b, 0) 29 | 30 | # Driver code 31 | array = [ 7, 2, 5, 6, 3, 1, 8, 4] 32 | print("The original array is: ", array) 33 | Heap_Sort(array) 34 | a = len(array) 35 | print ("Array after sorting is: ", array) 36 | -------------------------------------------------------------------------------- /Python/Monte_Carlo_Algorithm.py: -------------------------------------------------------------------------------- 1 | # Estimate the value of pi in range of 0 to 1 2 | 3 | import random 4 | 5 | n = int(input("Enter value of n:")) 6 | no_of_point = 0 7 | total_no_of_point = 0 8 | 9 | for _ in range(n): 10 | x = random.uniform(0,1) 11 | y = random.uniform(0,1) 12 | distance = x**2 + y**2 13 | if distance <= 1: 14 | no_of_point += 1 15 | total_no_of_point += 1 16 | 17 | pi = 4 * no_of_point/total_no_of_point 18 | print(pi) 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Python/Pet Race.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Question :--- 3 | There are M number of boys, N number of girls and P number of pets given. A pet race is held for a fun-filled Sunday in a town. Each pet animal is accompanied by two persons for the race. 4 | The organisers are planning for the seating arrangements by making groups such that each group has three members, and among these members, there is at least one boy and at least one 5 | girl, the third member being a boy, a girl or a pet. One bench is required for each such group. 6 | The task here is to find the maximum number of benches required for this. 7 | Example 1: 8 | Input: 9 | 5 -> Value of M 10 | 4 -> Value of N 11 | 2 -> Value of P 12 | Output: 13 | 3 14 | ''' 15 | 16 | 17 | 18 | 19 | >> 20 | m,n,p=int(input()),int(input()),int(input()) 21 | c=0 22 | 23 | while (m>0): 24 | if (n>0): 25 | if (p>0): 26 | m-=1 27 | n-=1 28 | p-=1 29 | c+=1 30 | 31 | # Maximum Sets 32 | elif (n>m and n>1): 33 | m-=1 34 | n-=2 35 | c+=1 36 | 37 | elif (n1): 38 | m-=2 39 | n-=1 40 | c+=1 41 | 42 | else: break 43 | else: break 44 | 45 | 46 | ''' 47 | ~ @TSG405 48 | ''' 49 | -------------------------------------------------------------------------------- /Python/Remote Procedure Call/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # Remote Procedure Call in Python 3 | 4 | A simple area and perimeter calculator app implemented using Remote Procedure Call(RPC) in python via xmlrpc module to demonstrate how a procedure call is made and how client and server communicate. 5 | 6 | -------------------------------------------------------------------------------- /Python/Remote Procedure Call/rpc_client.py: -------------------------------------------------------------------------------- 1 | from xmlrpc.client import ServerProxy 2 | client = ServerProxy('http://localhost:3000') 3 | 4 | 5 | # Perimeter of rectangle function 6 | def perimeter_of_rect(l, b): 7 | return 2 * (l + b) 8 | 9 | 10 | if __name__ == '__main__': 11 | print("Area and Perimeter of Rectangle") 12 | length = int(input("Enter length of rectangle:")) # Taking input from user 13 | breadth = int(input("Enter breadth of rectangle:")) # Taking input from user 14 | perimeter = perimeter_of_rect(length, breadth) # Calling function residing at client side 15 | area = client.area_of_rect(length, breadth) # Calling function residing at server side 16 | print(f"The perimeter of rectangle of length {length} and breadth {breadth} is {perimeter}.") 17 | print(f"The area of rectangle of length {length} and breadth {breadth} is {area}.") 18 | -------------------------------------------------------------------------------- /Python/Remote Procedure Call/rpc_server.py: -------------------------------------------------------------------------------- 1 | # importing modules 2 | from xmlrpc.server import SimpleXMLRPCServer 3 | 4 | # Define server and create it 5 | server = SimpleXMLRPCServer(('localhost', 3000), logRequests=True) 6 | 7 | 8 | # Define any functions we want available 9 | def area_of_rect(length, breadth): 10 | return length * breadth 11 | 12 | 13 | # Register these functions with the server interface 14 | server.register_function(area_of_rect) 15 | 16 | # if this module is run from command line as opposed to being imported 17 | # then go into serve forever mode 18 | if __name__ == '__main__': 19 | try: 20 | print('Serving...') 21 | server.serve_forever() 22 | except KeyboardInterrupt: 23 | print("Exiting") 24 | -------------------------------------------------------------------------------- /Python/Shell_Sort.py: -------------------------------------------------------------------------------- 1 | # Shell sort in python 2 | 3 | 4 | def shellSort(array, n): 5 | 6 | # Rearrange elements at each n/2, n/4, n/8, ... intervals 7 | interval = n // 2 8 | while interval > 0: 9 | for i in range(interval, n): 10 | temp = array[i] 11 | j = i 12 | while j >= interval and array[j - interval] > temp: 13 | array[j] = array[j - interval] 14 | j -= interval 15 | 16 | array[j] = temp 17 | interval //= 2 18 | 19 | 20 | data = [9, 8, 3, 7, 5, 6, 4, 1] 21 | size = len(data) 22 | shellSort(data, size) 23 | print('Sorted Array in Ascending Order:') 24 | print(data) -------------------------------------------------------------------------------- /Python/binary_search.py: -------------------------------------------------------------------------------- 1 | # Iterative Binary Search Function method Python Implementation 2 | # It returns index of n in given list1 if present, 3 | # else returns -1 4 | def binary_search(list1, n): 5 | low = 0 6 | high = len(list1) - 1 7 | mid = 0 8 | 9 | while low <= high: 10 | # for get integer result 11 | mid = (high + low) // 2 12 | 13 | # Check if n is present at mid 14 | if list1[mid] < n: 15 | low = mid + 1 16 | 17 | # If n is greater, compare to the right of mid 18 | elif list1[mid] > n: 19 | high = mid - 1 20 | 21 | # If n is smaller, compared to the left of mid 22 | else: 23 | return mid 24 | 25 | # element was not present in the list, return -1 26 | return -1 27 | 28 | 29 | # Initial list1 30 | list1 = [12, 24, 32, 39, 45, 50, 54] 31 | n = 45 32 | 33 | # Function call 34 | result = binary_search(list1, n) 35 | 36 | if result != -1: 37 | print("Element is present at index", str(result)) 38 | else: 39 | print("Element is not present in list1") 40 | -------------------------------------------------------------------------------- /Python/binary_to_decimal.py: -------------------------------------------------------------------------------- 1 | def binary_to_decimal(binary): 2 | i,integer = 0,0 3 | size = len(binary) 4 | while i < len(binary): 5 | integer += int(binary[size - 1 - i])*pow(2,i) 6 | i+=1 7 | print(integer) 8 | binary_to_decimal("001") 9 | binary_to_decimal("010") 10 | binary_to_decimal("011") 11 | -------------------------------------------------------------------------------- /Python/bubble_sort.py: -------------------------------------------------------------------------------- 1 | def bubble_sort(list1): 2 | # Outer loop for traverse the entire list 3 | for i in range(0,len(list1)-1): 4 | for j in range(len(list1)-1): 5 | if(list1[j]>list1[j+1]): 6 | temp = list1[j] 7 | list1[j] = list1[j+1] 8 | list1[j+1] = temp 9 | return list1 10 | 11 | list1 = [5, 3, 8, 6, 7, 2] 12 | print("The unsorted list is: ", list1) 13 | # Calling the bubble sort function 14 | print("The sorted list is: ", bubble_sort(list1)) 15 | -------------------------------------------------------------------------------- /Python/decimal_to_hexadecimal.py: -------------------------------------------------------------------------------- 1 | print("Enter the Decimal Number: ", end="") 2 | dn = int(input()) 3 | 4 | i = 0 5 | hdn = [] 6 | 7 | while dn!=0: 8 | rem = dn % 16 9 | if rem<10: 10 | rem = rem+48 11 | else: 12 | rem = rem+55 13 | hdn.insert(i, chr(rem)) 14 | i = i+1 15 | dn = int(dn / 16) 16 | 17 | print("\nEquivalent Hexadecimal Value = ", end="") 18 | i = i-1 19 | while i>=0: 20 | print(end=hdn[i]) 21 | i = i-1 22 | 23 | print() 24 | -------------------------------------------------------------------------------- /Python/diffie_hellman.py: -------------------------------------------------------------------------------- 1 | # Diffie–Hellman key exchange[nb 1] is a method of securely exchanging cryptographic keys over a public channel and 2 | # was one of the first public-key protocols as conceived by Ralph Merkle and named after Whitfield Diffie and Martin Hellman. 3 | 4 | # You may find more information about these parameters via https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange 5 | p = int(input('Enter p parameter: ')) # 13 6 | g = int(input('Enter g parameter: ')) # 11 7 | x = int(input('Enter x parameter: ')) # 9 8 | y = int(input('Enter y parameter: ')) # 6 9 | 10 | # Calculation of the own secret parameter based on received parameter from the second user. 11 | x_a = pow(g, x) % p 12 | print("xa:", x_a) 13 | 14 | # Calculation of the own secret parameter based on received parameter from the first user. 15 | y_b = pow(g, y) % p 16 | print("yb:", y_b) 17 | 18 | 19 | # Calculation of the secrets for each user (they should be the same) 20 | k_a = pow(y_b, x) % p 21 | print("ka:", k_a) 22 | k_b = pow(x_a, y) % p 23 | print("kb:", k_b) -------------------------------------------------------------------------------- /Python/el_gamal_encryption.py: -------------------------------------------------------------------------------- 1 | # In cryptography, the ElGamal encryption system is an asymmetric key encryption algorithm for public-key cryptography 2 | # which is based on the Diffie–Hellman key exchange. It was described by Taher Elgamal in 1985. 3 | # ElGamal encryption is used in the free GNU Privacy Guard software, recent versions of PGP, and other cryptosystems. 4 | # The Digital Signature Algorithm (DSA) is a variant of the ElGamal signature scheme, which should not be confused with ElGamal encryption. 5 | 6 | # to undestand described parameters, please, refer to https://en.wikipedia.org/wiki/ElGamal_encryption 7 | p = int(input('Enter p paremeter: ')) #13 8 | g = int(input('Enter g paremeter: ')) #7 9 | x = int(input('Enter x paremeter: ')) #10 10 | k = int(input('Enter k paremeter: ')) #11 11 | m = int(input('Enter m paremeter: ')) #7 12 | 13 | # Caluclation of the public key 14 | h = pow(g, x) % p 15 | print("h:", h) 16 | 17 | # Caluclation of the ciphertext 18 | c1 = pow(g, k) % p 19 | c2 = pow(h, k) * m % p 20 | print("Ciphertext: (" + str(c1) + ", " + str(c2) + ")") 21 | 22 | # Description of the ciphertext 23 | decrypted_m = c2 * pow(c1, p - 1 - x) % p 24 | print("Decrypted value:", decrypted_m) -------------------------------------------------------------------------------- /Python/hello.py: -------------------------------------------------------------------------------- 1 | print("Hello coders!") 2 | -------------------------------------------------------------------------------- /Python/insertion_sort.py: -------------------------------------------------------------------------------- 1 | # Function to do insertion sort 2 | def insertionSort(arr): 3 | 4 | # Traverse through 1 to len(arr) 5 | for i in range(1, len(arr)): 6 | 7 | key = arr[i] 8 | 9 | # Move elements of arr[0..i-1], that are 10 | # greater than key, to one position ahead 11 | # of their current position 12 | j = i-1 13 | while j >=0 and key < arr[j] : 14 | arr[j+1] = arr[j] 15 | j -= 1 16 | arr[j+1] = key 17 | 18 | 19 | # Driver code to test above 20 | arr = [12, 11, 13, 5, 6] 21 | insertionSort(arr) 22 | print ("Sorted array is:") 23 | for i in range(len(arr)): 24 | print ("%d" %arr[i]) 25 | -------------------------------------------------------------------------------- /Python/linear_search.py: -------------------------------------------------------------------------------- 1 | def linear_Search(list1, n, key): 2 | 3 | # Searching list1 sequentially 4 | for i in range(0, n): 5 | if (list1[i] == key): 6 | return i 7 | return -1 8 | 9 | 10 | list1 = [1 ,3, 5, 4, 7, 9] 11 | key = 7 12 | 13 | n = len(list1) 14 | res = linear_Search(list1, n, key) 15 | if(res == -1): 16 | print("Element not found") 17 | else: 18 | print("Element found at index: ", res) 19 | -------------------------------------------------------------------------------- /Python/mongean.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[11]: 5 | 6 | 7 | def momgean_shuffle(new_msg): 8 | new_msg=''.join(msg.split(' ')) 9 | new_msg=list(new_msg) 10 | odd_msg=new_msg[1::2] 11 | even_msg=new_msg[0::2] 12 | enc_msg=odd_msg[::-1] 13 | enc_msg.extend(even_msg) 14 | return ''.join(enc_msg),len(odd_msg) 15 | 16 | 17 | # In[12]: 18 | 19 | 20 | msg=input('Enter the message') 21 | enc_msg,length=momgean_shuffle(msg) 22 | print(enc_msg) 23 | 24 | 25 | # In[15]: 26 | 27 | 28 | # decryption 29 | dec_msg=[] 30 | for i in range(0,length): 31 | dec_msg.append(list(enc_msg[length:])[i]) 32 | dec_msg.append(list(enc_msg[0:length])[::-1][i]) 33 | if len(list(enc_msg[length:]))>length: 34 | dec_msg.append(list(enc_msg[length:])[-1]) 35 | print(''.join(dec_msg)) 36 | 37 | 38 | # In[ ]: 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Python/open-cv/face-recoginition.py: -------------------------------------------------------------------------------- 1 | from cv2 import cv2 2 | cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) 3 | # link of pretrained model https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml 4 | 5 | 6 | detector = cv2.CascadeClassifier( 7 | "haarcascade_frontalface_default.xml") 8 | 9 | while True: 10 | 11 | ret, img = cap.read() 12 | 13 | if ret: 14 | 15 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | 17 | faces = detector.detectMultiScale(gray, 1.1, 4) 18 | 19 | for face in faces: 20 | x, y, w, h = face 21 | if face.any(): 22 | cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) 23 | 24 | cv2.imshow("Face", img) 25 | 26 | key = cv2.waitKey(1) 27 | 28 | if key == ord("q"): 29 | break 30 | 31 | cap.release() 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Python/rsa_encryption_algorithm.py: -------------------------------------------------------------------------------- 1 | # The RSA algorithm is an asymmetric cryptography algorithm; this means that it uses a public key and a private key (i.e two different, mathematically linked keys). 2 | # As their names suggest, a public key is shared publicly, while a private key is secret and must not be shared with anyone. 3 | 4 | # You may find more information about these parameters via https://www.educative.io/edpresso/what-is-the-rsa-algorithm 5 | p = int(input('Enter p parameter: ')) # 37 6 | q = int(input('Enter q parameter: ')) # 47 7 | e = int(input('Enter e parameter: ')) # 19 8 | n = p * q 9 | 10 | # implementation of the totient function 11 | def eiler(p, q): 12 | return (p - 1) * (q - 1) 13 | 14 | # function that preform caluclation of the LCD 15 | def nsd(a, b): 16 | while a != 0 and b != 0: 17 | if a > b: 18 | a = a % b 19 | else: 20 | b = b % a 21 | return a + b 22 | 23 | # Caluclation of the φ(n). This value is needed to find co-prime. 24 | n_e = eiler(p, q) 25 | print("n:", n) 26 | print("φ(n):", n_e) 27 | 28 | # Function that perform calculation of the co-prime 29 | def ext_evcl(n_e, e): 30 | matrix = [] 31 | matrix.append([n_e, 0, 1, 0]) 32 | matrix.append([e, 0, 0, 1]) 33 | last_elem = 1 34 | while matrix[last_elem][0] != 1 or matrix[last_elem][3] < 0: 35 | if matrix[last_elem][0] == 1: 36 | matrix[last_elem][0], matrix[last_elem][1] = matrix[last_elem][1], matrix[last_elem][0] 37 | r = matrix[last_elem - 1][0] % matrix[last_elem][0] 38 | q = matrix[last_elem - 1][0] // matrix[last_elem][0] 39 | u = matrix[last_elem - 1][2] - q * matrix[last_elem][2] 40 | v = matrix[last_elem - 1][3] - q * matrix[last_elem][3] 41 | matrix.append([r, q, u, v]) 42 | last_elem += 1 43 | return matrix[last_elem][3] 44 | 45 | def crypt(m, n, e): 46 | return pow(m, e) % n 47 | 48 | # Integer value to be encrypt. In case if string should be encrypted, it is possible to convert string to array of chars and 49 | # convert them to integers according to the ASCII. 50 | m = 7 51 | 52 | # Encryption 53 | encrypted_value = crypt(m, n, e) 54 | print("Encrypted value:", encrypted_value) 55 | 56 | # Decryption 57 | d = ext_evcl(n_e, e) 58 | print("d:", d) 59 | decrypted_value = crypt(encrypted_value, n, d) 60 | print("Decrypted value:", decrypted_value) -------------------------------------------------------------------------------- /Python/screenshot.py: -------------------------------------------------------------------------------- 1 | import pyautogui 2 | screenshot = pyautogui.screenshot() 3 | screenshot.save("screenshot.png") 4 | -------------------------------------------------------------------------------- /Python/selection_sort.py: -------------------------------------------------------------------------------- 1 | A = ['t','u','t','o','r','i','a','l'] 2 | for i in range(len(A)): 3 | min_= i 4 | for j in range(i+1, len(A)): 5 | if A[min_] > A[j]: 6 | min_ = j 7 | #swap 8 | A[i], A[min_] = A[min_], A[i] 9 | # main 10 | for i in range(len(A)): 11 | print(A[i]) 12 | -------------------------------------------------------------------------------- /Radix - sort: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A utility function to get maximum value in arr[] 5 | int getMax(int arr[], int size) 6 | { 7 | int max = arr[0]; 8 | for (int i = 1; i < size; i++) 9 | if (arr[i] > max) 10 | max = arr[i]; 11 | return max; 12 | } 13 | 14 | void CountingSort(int arr[], int size, int div) 15 | { 16 | int output[size]; 17 | int count[10] = {0}; 18 | 19 | for (int i = 0; i < size; i++) 20 | count[ (arr[i]/div)%10 ]++; 21 | 22 | for (int i = 1; i < 10; i++) 23 | count[i] += count[i - 1]; 24 | 25 | for (int i = size - 1; i >= 0; i--) 26 | { 27 | output[count[ (arr[i]/div)%10 ] - 1] = arr[i]; 28 | count[ (arr[i]/div)%10 ]--; 29 | } 30 | 31 | for (int i = 0; i < size; i++) 32 | arr[i] = output[i]; 33 | } 34 | 35 | 36 | void RadixSort(int arr[], int size) 37 | { 38 | int m = getMax(arr, size); 39 | for (int div = 1; m/div > 0; div *= 10) 40 | CountingSort(arr, size, div); 41 | } 42 | 43 | 44 | int main() 45 | { 46 | int size; 47 | cout<<"Enter the size of the array: "<>size; 49 | int arr[size]; 50 | cout<<"Enter "<>arr[i]; 54 | } 55 | cout< 2 | using namespace std; 3 | int merge(int arr[], int low, int mid, int high) 4 | { 5 | int i = low, j = mid + 1, k = low; 6 | int b[high]; 7 | int count=0; 8 | while (i <= mid && j <= high) 9 | { 10 | if (arr[i] <= arr[j]) 11 | { 12 | b[k] = arr[i]; 13 | i++; 14 | k++; 15 | } 16 | else 17 | { 18 | b[k] = arr[j]; 19 | j++; 20 | k++; 21 | count+=mid+1-i; 22 | } 23 | } 24 | if (i > mid) 25 | { 26 | while (j<=high) 27 | { 28 | b[k]=arr[j]; 29 | j++; 30 | k++; 31 | } 32 | 33 | } 34 | else{ 35 | while (i<=mid) 36 | { 37 | b[k]=arr[i]; 38 | i++; 39 | k++; 40 | } 41 | 42 | } 43 | for(k=low;k<=high;k++) 44 | arr[k]=b[k]; 45 | 46 | return count; 47 | } 48 | int mergesort(int arr[], int low, int high) 49 | { 50 | int mid; 51 | int count = 0; 52 | if (high > low) 53 | { 54 | mid = (high + low) / 2; 55 | count += mergesort(arr, low, mid); 56 | count += mergesort(arr, mid + 1, high); 57 | count += merge(arr, low, mid, high); 58 | } 59 | 60 | return count; 61 | } 62 | int main() 63 | { 64 | 65 | int n; 66 | cout << "enter array length="; 67 | cin >> n; 68 | int arr[n]; 69 | cout << "\n enter array to be sorted="; 70 | for (int i = 0; i < n; i++) 71 | { 72 | cin >> arr[i]; 73 | } 74 | 75 | cout << mergesort(arr, 0, n); 76 | cout << endl; 77 | return 0; 78 | } -------------------------------------------------------------------------------- /javascript/7.Variables/alert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/alert.png -------------------------------------------------------------------------------- /javascript/7.Variables/const.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/const.png -------------------------------------------------------------------------------- /javascript/7.Variables/op.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/op.png -------------------------------------------------------------------------------- /javascript/7.Variables/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/output.png -------------------------------------------------------------------------------- /javascript/7.Variables/word-image-109.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/word-image-109.png -------------------------------------------------------------------------------- /javascript/7.Variables/word-image-110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rishabh062/Hacktoberfest2021/82c8015523f13125b4bc96e515c2ebe5cd952b6f/javascript/7.Variables/word-image-110.png -------------------------------------------------------------------------------- /javascript/CanvasImages.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

Image to use:

6 | 7 | The Scream 9 | 10 |

Canvas:

11 | 12 | 14 | Your browser does not support the HTML5 canvas tag. 15 | 16 | 17 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /javascript/Celciuc_to_fahrenheit.js: -------------------------------------------------------------------------------- 1 | var celc = prompt("enter temp ::: "); 2 | fahren = celc*9/5+32; 3 | alert("in fahrenheit ::: " + fahren); 4 | -------------------------------------------------------------------------------- /javascript/bubble_sort.js: -------------------------------------------------------------------------------- 1 | function bubbleSort(arr){ 2 | 3 | for(let i = 0; i < arr.length; i++){ 4 | 5 | 6 | for(let j = 0; j < ( arr.length - i -1 ); j++){ 7 | 8 | if(arr[j] > arr[j+1]){ 9 | let temp = arr[j] 10 | arr[j] = arr[j + 1] 11 | arr[j+1] = temp 12 | } 13 | } 14 | } 15 | console.log(arr); 16 | } 17 | 18 | var arr = [95, 34, 65, 89, 2, 78, 12, 8, 57]; 19 | 20 | bubbleSort(arr); 21 | -------------------------------------------------------------------------------- /javascript/clock.js: -------------------------------------------------------------------------------- 1 | function currentTime() { 2 | var date = new Date(); 3 | var hour = date.getHours(); 4 | var min = date.getMinutes(); 5 | var sec = date.getSeconds(); 6 | hour = updateTime(hour); 7 | min = updateTime(min); 8 | sec = updateTime(sec); 9 | 10 | document.getElementById("clock").innerText = hour + " : " + min + " : " + sec; 11 | var t = setTimeout(function(){ currentTime() }, 1000); 12 | } 13 | 14 | function updateTime(k) { 15 | if (k < 10) { 16 | return "0" + k; 17 | } 18 | else { 19 | return k; 20 | } 21 | } 22 | 23 | currentTime(); -------------------------------------------------------------------------------- /javascript/hello.js: -------------------------------------------------------------------------------- 1 | console.log("Hello coders!") 2 | -------------------------------------------------------------------------------- /javascript/kadanes_algorithm.js: -------------------------------------------------------------------------------- 1 | function maxSubArraySum(a, size) 2 | { 3 | var maxint = Math.pow(2, 53) 4 | var max_so_far = -maxint - 1 5 | var max_ending_here = 0 6 | 7 | for (var i = 0; i < size; i++) 8 | { 9 | max_ending_here = max_ending_here + a[i] 10 | if (max_so_far < max_ending_here) 11 | max_so_far = max_ending_here 12 | 13 | if (max_ending_here < 0) 14 | max_ending_here = 0 15 | } 16 | return max_so_far 17 | } 18 | 19 | // Driver code 20 | var a = [ -2, -3, 4, -1, -2, 1, 5, -3 ] 21 | document.write("Maximum contiguous sum is", 22 | maxSubArraySum(a, a.length)) 23 | -------------------------------------------------------------------------------- /php/GET.php: -------------------------------------------------------------------------------- 1 | "; 4 | echo "You are ". $_GET['age']. " years old."; 5 | 6 | exit(); 7 | } 8 | ?> 9 | 10 | 11 | 12 |
13 | Name: 14 | Age: 15 | 16 |
17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /php/POST.php: -------------------------------------------------------------------------------- 1 | "; 7 | echo "You are ". $_POST['age']. " years old."; 8 | 9 | exit(); 10 | } 11 | ?> 12 | 13 | 14 | 15 |
16 | Name: 17 | Age: 18 | 19 |
20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /php/String.php: -------------------------------------------------------------------------------- 1 | "; 7 | 8 | $literally = "This $variable will print!\\n"; 9 | 10 | print($literally); 11 | ?> 12 | -------------------------------------------------------------------------------- /php/array.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | "; 10 | } 11 | 12 | /* Second method to create array. */ 13 | $numbers[0] = "one"; 14 | $numbers[1] = "two"; 15 | $numbers[2] = "three"; 16 | $numbers[3] = "four"; 17 | $numbers[4] = "five"; 18 | 19 | foreach( $numbers as $value ) { 20 | echo "Value is $value
"; 21 | } 22 | 23 | 24 | 25 | 26 | /*Check if the key "test1" exists in an array:*/ 27 | $a=array("test1"=>"val1","test2"=>"val2"); 28 | if (array_key_exists("test1",$a)) 29 | { 30 | echo "Key exists!"; 31 | } 32 | else 33 | { 34 | echo "Key does not exist!"; 35 | } 36 | 37 | 38 | 39 | ?> 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /php/encrypt.php: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /php/phpajax.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 28 | 29 | 30 | 31 | 32 |

Search your favourite tutorials:

33 | 34 |
35 | 36 |
37 | 38 |

Entered Course name:

39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /php/validation.php: -------------------------------------------------------------------------------- 1 | // Validate a user credential in php 2 | --------------------------------------------------------------------------------