├── Dev ├── README.md ├── printnumber.cpp ├── Arduino Functions ├── Rotate Image ├── Set_Pairs ├── Largest Number ├── Golomb sequence 1 ├── Climbing Stairs ├── 3sum closest.java ├── SortCircularRotatedArray.py ├── heart star pattern ├── bipartite_check.cpp ├── binary search.cpp ├── insertionsort.cpp ├── function ├── function1 ├── function2 ├── set union ├── selection sort .cpp ├── House Robber ├── Minimum Path Sum ├── K Centers Problem ├── middleof linkedlist ├── Travelling_Sales_Person_Problem.cpp ├── unrolled linkedlist ├── CIRCULAR LINKED LIST ├── delete linkedlist ├── Water Connection Problem ├── merge sort └── Pattern Searching using a Trie of all Suffixes /Dev: -------------------------------------------------------------------------------- 1 | #include 2 | Using namespace std; 3 | Int main() 4 | Hlo 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cpp 2 | Practice codes 3 | star the repo and follow and genuine PRs will be accepted 4 | -------------------------------------------------------------------------------- /printnumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int number; 6 | 7 | cout << "Enter an integer: "; 8 | cin >> number; 9 | 10 | cout << "You entered " << number; 11 | return 0; 12 | } 13 | 14 | Outpu 15 | -------------------------------------------------------------------------------- /Arduino Functions: -------------------------------------------------------------------------------- 1 | // Header file 2 | // Make Sure your file name 3 | // should be my_library.h 4 | #ifndef MY_LIBRARY_H 5 | #define MY_LIBRARY_H 6 | #include 7 | 8 | class DCMotor { 9 | private: 10 | byte pin1; 11 | byte pin2; 12 | int speed; 13 | 14 | public: 15 | DCMotor(byte, byte, int); 16 | void clockwise(); 17 | void antiClockwise(); 18 | void stop(); 19 | void motorDelay(); 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /Rotate Image: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n=matrix.size(); 5 | int m=matrix[0].size(); 6 | int i,j; 7 | for(i=0;i 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | vector> v; 9 | v.push_back({1,2}); 10 | v.push_back({3,4}); 11 | v.push_back({5,6}); 12 | v.push_back({4,12}); 13 | v.push_back({7,8}); 14 | v.push_back({9,10}); 15 | sort(v.begin(),v.end()); 16 | 17 | for (int i=0;i& nums) { 2 | vector numbs; 3 | for(auto i : nums) 4 | { 5 | numbs.push_back(to_string(i)); 6 | } 7 | sort(numbs.begin(),numbs.end(),[](string &a, string &b) 8 | { 9 | return a+b>b+a; 10 | }); 11 | string ans = ""; 12 | for(auto i: numbs) 13 | ans+=i; 14 | 15 | int i=0; 16 | while(ans[i]=='0') 17 | { 18 | ans = ans.substr(i+1,n); 19 | } 20 | 21 | return ans==""?"0":ans; 22 | } 23 | -------------------------------------------------------------------------------- /Golomb sequence 1: -------------------------------------------------------------------------------- 1 | // C++ Program to find first 2 | // n terms of Golomb sequence. 3 | #include 4 | using namespace std; 5 | 6 | // Print the first n term 7 | // of Golomb Sequence 8 | void printGolomb(int n) 9 | { 10 | int dp[n + 1]; 11 | 12 | // base cases 13 | dp[1] = 1; 14 | cout << dp[1] << " "; 15 | 16 | // Finding and printing first 17 | // n terms of Golomb Sequence. 18 | for (int i = 2; i <= n; i++) 19 | { 20 | dp[i] = 1 + dp[i - dp[dp[i - 1]]]; 21 | cout << dp[i] << " "; 22 | } 23 | } 24 | // Driver Code 25 | int main() 26 | { 27 | int n = 9; 28 | 29 | printGolomb(n); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Climbing Stairs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int solve(int n,vector&dp){ 4 | 5 | if(n<0){ 6 | return 0; 7 | } 8 | if(n==0){ 9 | return 1; 10 | } 11 | if(dp[n]!=-1){ 12 | return dp[n]; 13 | } 14 | int left=solve(n-1,dp); 15 | int right=solve(n-2,dp); 16 | dp[n]=left+right; 17 | return dp[n]; 18 | } 19 | 20 | 21 | public: 22 | int climbStairs(int n) { 23 | // int index=0; 24 | 25 | vectordp(n+1,-1); 26 | return solve(n,dp); 27 | 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /3sum closest.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int threeSumClosest(int[] num, int target) { 3 | int result = num[0] + num[1] + num[num.length - 1]; 4 | Arrays.sort(num); 5 | for (int i = 0; i < num.length - 2; i++) { 6 | int start = i + 1, end = num.length - 1; 7 | while (start < end) { 8 | int sum = num[i] + num[start] + num[end]; 9 | if (sum > target) { 10 | end--; 11 | } else { 12 | start++; 13 | } 14 | if (Math.abs(sum - target) < Math.abs(result - target)) { 15 | result = sum; 16 | } 17 | } 18 | } 19 | return result; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /SortCircularRotatedArray.py: -------------------------------------------------------------------------------- 1 | 2 | # function to reverse the array list 3 | def reverse_array(arr, start=0, end=0): 4 | if end == 0: 5 | end = len(arr)-1 6 | while start<=end: 7 | temp = arr[start] 8 | arr[start] = arr[end] 9 | arr[end] = temp 10 | start += 1 11 | end -= 1 12 | 13 | # function to sort cicular roated array list 14 | def sort_circular_rotated_array(arr): 15 | index_smallest = 0 16 | for i in range(len(arr)-1): 17 | if arr[i]>arr[i+1]: 18 | index_smallest = i+1 19 | break 20 | 21 | reverse_array(arr=arr, end=index_smallest-1) 22 | reverse_array(arr=arr, start=index_smallest) 23 | reverse_array(arr=arr) 24 | return arr 25 | 26 | 27 | arr = [3, 4, 5, 1, 2] 28 | print(sort_circular_rotated_array(arr)) 29 | 30 | -------------------------------------------------------------------------------- /heart star pattern: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int binarySearch(int arr[], int size, int key) 4 | { 5 | int start = 0; 6 | int end = size - 1; 7 | int mid = (start + end) / 2; 8 | while (start <= end) 9 | { 10 | if (arr[mid] == key) 11 | { 12 | return mid; 13 | } 14 | // go to right wala part 15 | if (key > arr[mid]) 16 | { 17 | start = mid + 1; 18 | } 19 | else 20 | { 21 | end = mid - 1; 22 | } 23 | mid = (start + end) / 2; 24 | } 25 | return -1; 26 | } 27 | int main() 28 | { 29 | int even[6] = {2, 4, 6, 8, 12, 18}; 30 | 31 | int evenIndex = binarySearch(even, 6, 4); 32 | 33 | cout << "index of 18-->" << evenIndex << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /bipartite_check.cpp: -------------------------------------------------------------------------------- 1 | bool isBipartite(vector>& graph) { 2 | int n= graph.size(); 3 | vector vis(n,0); 4 | vector q; 5 | int k, col; 6 | for (int i=0;i0){ 12 | k=q.back(); 13 | q.pop_back(); 14 | if (vis[k]==1) col=2; 15 | else col=1; 16 | for (int j=0; j num) 9 | return binarySearch(arr, p, mid-1, num); 10 | if (arr[mid] < num) 11 | return binarySearch(arr, mid+1, r, num); 12 | } 13 | return -1; 14 | } 15 | int main(void) { 16 | int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40}; 17 | int n = sizeof(arr)/ sizeof(arr[0]); 18 | int num; 19 | cout << "Enter the number to search: \n"; 20 | cin >> num; 21 | int index = binarySearch (arr, 0, n-1, num); 22 | if(index == -1){ 23 | cout<< num <<" is not present in the array"; 24 | }else{ 25 | cout<< num <<" is present at index "<< index <<" in the array"; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /insertionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void display(int *array, int size) { 4 | for(int i = 0; i 0 && array[j-1]>key) { 14 | array[j] = array[j-1]; 15 | j--; 16 | } 17 | array[j] = key; //insert in right place 18 | } 19 | } 20 | int main() { 21 | int n; 22 | cout << "Enter the number of elements: "; 23 | cin >> n; 24 | int arr[n]; //create an array with given number of elements 25 | cout << "Enter elements:" << endl; 26 | for(int i = 0; i> arr[i]; 28 | } 29 | cout << "Array before Sorting: "; 30 | display(arr, n); 31 | insertionSort(arr, n); 32 | cout << "Array after Sorting: "; 33 | display(arr, n); 34 | } 35 | -------------------------------------------------------------------------------- /function: -------------------------------------------------------------------------------- 1 | // Recursive C++ program to reverse an array 2 | #include 3 | using namespace std; 4 | 5 | /* Function to reverse arr[] from start to end*/ 6 | void rvereseArray(int arr[], int start, int end) 7 | { 8 | if (start >= end) 9 | return; 10 | 11 | int temp = arr[start]; 12 | arr[start] = arr[end]; 13 | arr[end] = temp; 14 | 15 | // Recursive Function calling 16 | rvereseArray(arr, start + 1, end - 1); 17 | } 18 | 19 | 20 | /* Utility function to print an array */ 21 | void printArray(int arr[], int size) 22 | { 23 | for (int i = 0; i < size; i++) 24 | cout << arr[i] << " "; 25 | 26 | cout << endl; 27 | } 28 | 29 | /* Driver function to test above functions */ 30 | int main() 31 | { 32 | int arr[] = {1, 2, 3, 4, 5, 6}; 33 | 34 | // To print original array 35 | printArray(arr, 6); 36 | 37 | // Function calling 38 | rvereseArray(arr, 0, 5); 39 | 40 | cout << "Reversed array is" << endl; 41 | 42 | // To print the Reversed array 43 | printArray(arr, 6); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /function1: -------------------------------------------------------------------------------- 1 | // Recursive C++ program to reverse an array 2 | #include 3 | using namespace std; 4 | 5 | /* Function to reverse arr[] from start to end*/ 6 | void rvereseArray(int arr[], int start, int end) 7 | { 8 | if (start >= end) 9 | return; 10 | 11 | int temp = arr[start]; 12 | arr[start] = arr[end]; 13 | arr[end] = temp; 14 | 15 | // Recursive Function calling 16 | rvereseArray(arr, start + 1, end - 1); 17 | } 18 | 19 | 20 | /* Utility function to print an array */ 21 | void printArray(int arr[], int size) 22 | { 23 | for (int i = 0; i < size; i++) 24 | cout << arr[i] << " "; 25 | 26 | cout << endl; 27 | } 28 | 29 | /* Driver function to test above functions */ 30 | int main() 31 | { 32 | int arr[] = {1, 2, 3, 4, 5, 6}; 33 | 34 | // To print original array 35 | printArray(arr, 6); 36 | 37 | // Function calling 38 | rvereseArray(arr, 0, 5); 39 | 40 | cout << "Reversed array is" << endl; 41 | 42 | // To print the Reversed array 43 | printArray(arr, 6); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /function2: -------------------------------------------------------------------------------- 1 | // Recursive C++ program to reverse an array 2 | #include 3 | using namespace std; 4 | 5 | /* Function to reverse arr[] from start to end*/ 6 | void rvereseArray(int arr[], int start, int end) 7 | { 8 | if (start >= end) 9 | return; 10 | 11 | int temp = arr[start]; 12 | arr[start] = arr[end]; 13 | arr[end] = temp; 14 | 15 | // Recursive Function calling 16 | rvereseArray(arr, start + 1, end - 1); 17 | } 18 | 19 | 20 | /* Utility function to print an array */ 21 | void printArray(int arr[], int size) 22 | { 23 | for (int i = 0; i < size; i++) 24 | cout << arr[i] << " "; 25 | 26 | cout << endl; 27 | } 28 | 29 | /* Driver function to test above functions */ 30 | int main() 31 | { 32 | int arr[] = {1, 2, 3, 4, 5, 6}; 33 | 34 | // To print original array 35 | printArray(arr, 6); 36 | 37 | // Function calling 38 | rvereseArray(arr, 0, 5); 39 | 40 | cout << "Reversed array is" << endl; 41 | 42 | // To print the Reversed array 43 | printArray(arr, 6); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /set union: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int a[100],i,j,b[100],c[100],d[100],m,n,l,f; 4 | printf("please enter no. of element in set 1 "); 5 | scanf("%d",&m); 6 | printf("please enter no. of element in set 2 "); 7 | scanf("%d",&n); 8 | printf("please enter elements of set 1 "); 9 | for(i=0;i 2 | using namespace std; 3 | void swapping(int &a, int &b) { 4 | int temp; 5 | temp = a; 6 | a = b; 7 | b = temp; 8 | } 9 | void display(int *array, int size) { 10 | for(int i = 0; i> n; 29 | int arr[n]; 30 | cout << "Enter elements:" << endl; 31 | for(int i = 0; i> arr[i]; 33 | } 34 | cout << "Array before Sorting: "; 35 | display(arr, n); 36 | selectionSort(arr, n); 37 | cout << "Array after Sorting: "; 38 | display(arr, n); 39 | } 40 | -------------------------------------------------------------------------------- /House Robber: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int solve(int index, vector&dp,vector& nums){ 4 | if(index==0){ 5 | return nums[index]; 6 | } 7 | if(index<0){ 8 | return 0; 9 | } 10 | int nottake=0+solve(index-1,dp,nums); 11 | int take =nums[index]+solve(index-2,dp,nums); 12 | dp[index]=max(take,nottake); 13 | return dp[index]; 14 | } 15 | public: 16 | int rob(vector& nums) { 17 | int n=nums.size(); 18 | // vectordp(n,-1); 19 | // return solve(n-1,dp,nums); 20 | vectordp(n,0); 21 | 22 | dp[0]=nums[0]; 23 | int neg=0; 24 | int i; 25 | for(i=1;i1){ 30 | take+=dp[i-2];} 31 | 32 | int nottake=0+dp[i-1]; 33 | 34 | dp[i]=max(take,nottake); 35 | 36 | } 37 | return dp[n-1]; 38 | 39 | 40 | 41 | 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Minimum Path Sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /* private: 3 | int solve(int i,int j,vector>& grid,vector>dp){ 4 | if(i==0 && j==0){ 5 | return grid[i][j]; 6 | 7 | } 8 | if(i<0|| j<0){ 9 | return 1e8; 10 | } 11 | if(dp[i][j]!=-1){ 12 | return dp[i][j]; 13 | } 14 | int left=grid[i][j]+solve(i-1,j,grid,dp); 15 | int right=grid[i][j]+solve(i,j-1,grid,dp); 16 | dp[i][j]=min(left,right); 17 | return dp[i][j]; 18 | 19 | }*/ 20 | public: 21 | int minPathSum(vector>& grid) { 22 | int n=grid.size(); 23 | int m=grid[0].size(); 24 | // vector>dp(r,vector(c,-1)); 25 | // return solve(r-1,c-1,grid,dp); 26 | 27 | vector>dp(n,vector(m,0)); 28 | for(int i=0; i0) up += dp[i-1][j]; 36 | else up += 1e9; 37 | 38 | int left = grid[i][j]; 39 | if(j>0) left+=dp[i][j-1]; 40 | else left += 1e9; 41 | 42 | 43 | 44 | dp[i][j]=min(up,left); 45 | 46 | } 47 | } 48 | } 49 | return dp[n-1][m-1]; 50 | 51 | 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /K Centers Problem: -------------------------------------------------------------------------------- 1 | // C++ program for the above approach 2 | #include 3 | using namespace std; 4 | 5 | int maxindex(int* dist, int n) 6 | { 7 | int mi = 0; 8 | for (int i = 0; i < n; i++) { 9 | if (dist[i] > dist[mi]) 10 | mi = i; 11 | } 12 | return mi; 13 | } 14 | 15 | void selectKcities(int n, int weights[4][4], int k) 16 | { 17 | int* dist = new int[n]; 18 | vector centers; 19 | for (int i = 0; i < n; i++) { 20 | dist[i] = INT_MAX; 21 | } 22 | 23 | // index of city having the 24 | // maximum distance to it's 25 | // closest center 26 | int max = 0; 27 | for (int i = 0; i < k; i++) { 28 | centers.push_back(max); 29 | for (int j = 0; j < n; j++) { 30 | 31 | // updating the distance 32 | // of the cities to their 33 | // closest centers 34 | dist[j] = min(dist[j], weights[max][j]); 35 | } 36 | 37 | // updating the index of the 38 | // city with the maximum 39 | // distance to it's closest center 40 | max = maxindex(dist, n); 41 | } 42 | 43 | // Printing the maximum distance 44 | // of a city to a center 45 | // that is our answer 46 | cout << endl << dist[max] << endl; 47 | 48 | // Printing the cities that 49 | // were chosen to be made 50 | // centers 51 | for (int i = 0; i < centers.size(); i++) { 52 | cout << centers[i] << " "; 53 | } 54 | cout << endl; 55 | } 56 | 57 | // Driver Code 58 | int main() 59 | { 60 | int n = 4; 61 | int weights[4][4] = { { 0, 4, 8, 5 }, 62 | { 4, 0, 10, 7 }, 63 | { 8, 10, 0, 9 }, 64 | { 5, 7, 9, 0 } }; 65 | int k = 2; 66 | 67 | // Function Call 68 | selectKcities(n, weights, k); 69 | } 70 | // Contributed by Balu Nagar 71 | -------------------------------------------------------------------------------- /middleof linkedlist: -------------------------------------------------------------------------------- 1 | // C++ program for the above approach 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node* next; 10 | }; 11 | 12 | class NodeOperation { 13 | public: 14 | // Function to add a new node 15 | void pushNode(class Node** head_ref, int data_val) 16 | { 17 | 18 | // Allocate node 19 | class Node* new_node = new Node(); 20 | 21 | // Put in the data 22 | new_node->data = data_val; 23 | 24 | // Link the old list off the new node 25 | new_node->next = *head_ref; 26 | 27 | // move the head to point to the new node 28 | *head_ref = new_node; 29 | } 30 | 31 | // A utility function to print a given linked list 32 | void printNode(class Node* head) 33 | { 34 | while (head != NULL) { 35 | cout << head->data << "->"; 36 | head = head->next; 37 | } 38 | cout << "NULL" << endl; 39 | } 40 | 41 | /* Utility Function to find length of linked list */ 42 | int getLen(class Node* head) 43 | { 44 | int len = 0; 45 | class Node* temp = head; 46 | while (temp) { 47 | len++; 48 | temp = temp->next; 49 | } 50 | return len; 51 | } 52 | 53 | void printMiddle(class Node* head) 54 | { 55 | 56 | if (head) { 57 | // find length 58 | int len = getLen(head); 59 | class Node* temp = head; 60 | 61 | // trvaers till we reached half of length 62 | int midIdx = len / 2; 63 | while (midIdx--) { 64 | temp = temp->next; 65 | } 66 | // temp will be storing middle element 67 | cout << "The middle element is [" << temp->data 68 | << "]" << endl; 69 | } 70 | } 71 | }; 72 | 73 | // Driver Code 74 | int main() 75 | { 76 | class Node* head = NULL; 77 | class NodeOperation* temp = new NodeOperation(); 78 | for (int i = 5; i > 0; i--) { 79 | temp->pushNode(&head, i); 80 | temp->printNode(head); 81 | temp->printMiddle(head); 82 | } 83 | return 0; 84 | } 85 | 86 | // This code is contributed by Tapesh(tapeshdua420) 87 | -------------------------------------------------------------------------------- /Travelling_Sales_Person_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // there are four nodes in example graph (graph is 1-based) 6 | const int n = 4; 7 | // give appropriate maximum to avoid overflow 8 | const int MAX = 1000000; 9 | 10 | // dist[i][j] represents shortest distance to go from i to j 11 | // this matrix can be calculated for any given graph using 12 | // all-pair shortest path algorithms 13 | int dist[n + 1][n + 1] = { 14 | { 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 }, 15 | { 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 }, 16 | { 0, 20, 25, 30, 0 }, 17 | }; 18 | 19 | // memoization for top down recursion 20 | int memo[n + 1][1 << (n + 1)]; 21 | 22 | int fun(int i, int mask) 23 | { 24 | // base case 25 | // if only ith bit and 1st bit is set in our mask, 26 | // it implies we have visited all other nodes already 27 | if (mask == ((1 << i) | 3)) 28 | return dist[1][i]; 29 | // memoization 30 | if (memo[i][mask] != 0) 31 | return memo[i][mask]; 32 | 33 | int res = MAX; // result of this sub-problem 34 | 35 | // we have to travel all nodes j in mask and end the 36 | // path at ith node so for every node j in mask, 37 | // recursively calculate cost of travelling all nodes in 38 | // mask except i and then travel back from node j to 39 | // node i taking the shortest path take the minimum of 40 | // all possible j nodes 41 | 42 | for (int j = 1; j <= n; j++) 43 | if ((mask & (1 << j)) && j != i && j != 1) 44 | res = std::min(res, fun(j, mask & (~(1 << i))) 45 | + dist[j][i]); 46 | return memo[i][mask] = res; 47 | } 48 | // Driver program to test above logic 49 | int main() 50 | { 51 | int ans = MAX; 52 | for (int i = 1; i <= n; i++) 53 | // try to go from node 1 visiting all nodes in 54 | // between to i then return from i taking the 55 | // shortest route to 1 56 | ans = std::min(ans, fun(i, (1 << (n + 1)) - 1) 57 | + dist[i][1]); 58 | 59 | printf("The cost of most efficient tour = %d", ans); 60 | 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /unrolled linkedlist: -------------------------------------------------------------------------------- 1 | // Java program to implement unrolled 2 | // linked list and traversing it. 3 | import java.util.*; 4 | 5 | class GFG{ 6 | 7 | static final int maxElements = 4; 8 | 9 | // Unrolled Linked List Node 10 | static class Node 11 | { 12 | int numElements; 13 | int []array = new int[maxElements]; 14 | Node next; 15 | }; 16 | 17 | // Function to traverse an unrolled 18 | // linked list and print all the elements 19 | static void printUnrolledList(Node n) 20 | { 21 | while (n != null) 22 | { 23 | 24 | // Print elements in current node 25 | for(int i = 0; i < n.numElements; i++) 26 | System.out.print(n.array[i] + " "); 27 | 28 | // Move to next node 29 | n = n.next; 30 | } 31 | } 32 | 33 | // Program to create an unrolled linked list 34 | // with 3 Nodes 35 | public static void main(String[] args) 36 | { 37 | Node head = null; 38 | Node second = null; 39 | Node third = null; 40 | 41 | // Allocate 3 Nodes 42 | head = new Node(); 43 | second = new Node(); 44 | third = new Node(); 45 | 46 | // Let us put some values in second 47 | // node (Number of values must be 48 | // less than or equal to maxElement) 49 | head.numElements = 3; 50 | head.array[0] = 1; 51 | head.array[1] = 2; 52 | head.array[2] = 3; 53 | 54 | // Link first Node with the 55 | // second Node 56 | head.next = second; 57 | 58 | // Let us put some values in 59 | // second node (Number of values 60 | // must be less than or equal to 61 | // maxElement) 62 | second.numElements = 3; 63 | second.array[0] = 4; 64 | second.array[1] = 5; 65 | second.array[2] = 6; 66 | 67 | // Link second Node with the third Node 68 | second.next = third; 69 | 70 | // Let us put some values in third 71 | // node (Number of values must be 72 | // less than or equal to maxElement) 73 | third.numElements = 3; 74 | third.array[0] = 7; 75 | third.array[1] = 8; 76 | third.array[2] = 9; 77 | third.next = null; 78 | 79 | printUnrolledList(head); 80 | } 81 | } 82 | 83 | // This code is contributed by amal kumar choubey 84 | -------------------------------------------------------------------------------- /CIRCULAR LINKED LIST: -------------------------------------------------------------------------------- 1 | // Java program for the above methods 2 | 3 | class GFG { 4 | static class Node { 5 | int data; 6 | Node next; 7 | }; 8 | 9 | static Node addToEmpty(Node last, int data) 10 | { 11 | // This function is only for empty list 12 | 13 | if (last != null) 14 | return last; 15 | // Creating a node dynamically 16 | 17 | Node temp = new Node(); 18 | // Assigning the data. 19 | 20 | temp.data = data; 21 | last = temp; 22 | // Creating the link. 23 | 24 | last.next = last; 25 | return last; 26 | } 27 | 28 | static Node addBegin(Node last, int data) 29 | { 30 | if (last == null) 31 | return addToEmpty(last, data); 32 | 33 | Node temp = new Node(); 34 | temp.data = data; 35 | temp.next = last.next; 36 | last.next = temp; 37 | return last; 38 | } 39 | 40 | static Node addEnd(Node last, int data) 41 | { 42 | if (last == null) 43 | return addToEmpty(last, data); 44 | 45 | Node temp = new Node(); 46 | temp.data = data; 47 | temp.next = last.next; 48 | last.next = temp; 49 | last = temp; 50 | return last; 51 | } 52 | 53 | static Node addAfter(Node last, int data, int item) 54 | { 55 | if (last == null) 56 | return null; 57 | 58 | Node temp, p; 59 | p = last.next; 60 | do { 61 | if (p.data == item) { 62 | temp = new Node(); 63 | temp.data = data; 64 | temp.next = p.next; 65 | p.next = temp; 66 | if (p == last) 67 | last = temp; 68 | return last; 69 | } 70 | p = p.next; 71 | } while (p != last.next); 72 | 73 | System.out.println(item 74 | + " not present in the list."); 75 | return last; 76 | } 77 | 78 | static void traverse(Node last) 79 | { 80 | Node p; 81 | // If list is empty, return. 82 | 83 | if (last == null) { 84 | System.out.println("List is empty."); 85 | return; 86 | } 87 | // Pointing to first Node of the list. 88 | 89 | p = last.next; 90 | // Traversing the list. 91 | 92 | do { 93 | System.out.print(p.data + " "); 94 | p = p.next; 95 | } while (p != last.next); 96 | } 97 | 98 | // Driver code 99 | public static void main(String[] args) 100 | { 101 | Node last = null; 102 | last = addToEmpty(last, 6); 103 | last = addBegin(last, 4); 104 | last = addBegin(last, 2); 105 | last = addEnd(last, 8); 106 | last = addEnd(last, 12); 107 | last = addAfter(last, 10, 8); 108 | 109 | // Function call 110 | traverse(last); 111 | } 112 | } 113 | /* This code contributed by PrinciRaj1992 */ 114 | -------------------------------------------------------------------------------- /delete linkedlist: -------------------------------------------------------------------------------- 1 | // A complete working Java program to delete a node in a 2 | // linked list at a given position 3 | class LinkedList { 4 | Node head; // head of list 5 | 6 | /* Linked list Node*/ 7 | class Node { 8 | int data; 9 | Node next; 10 | Node(int d) 11 | { 12 | data = d; 13 | next = null; 14 | } 15 | } 16 | 17 | /* Inserts a new Node at front of the list. */ 18 | public void push(int new_data) 19 | { 20 | /* 1 & 2: Allocate the Node & 21 | Put in the data*/ 22 | Node new_node = new Node(new_data); 23 | 24 | /* 3. Make next of new Node as head */ 25 | new_node.next = head; 26 | 27 | /* 4. Move the head to point to new Node */ 28 | head = new_node; 29 | } 30 | 31 | /* Given a reference (pointer to pointer) to the head of 32 | a list 33 | and a position, deletes the node at the given 34 | position */ 35 | void deleteNode(int position) 36 | { 37 | // If linked list is empty 38 | if (head == null) 39 | return; 40 | 41 | // Store head node 42 | Node temp = head; 43 | 44 | // If head needs to be removed 45 | if (position == 0) { 46 | head = temp.next; // Change head 47 | return; 48 | } 49 | 50 | // Find previous node of the node to be deleted 51 | for (int i = 0; temp != null && i < position - 1; 52 | i++) 53 | temp = temp.next; 54 | 55 | // If position is more than number of nodes 56 | if (temp == null || temp.next == null) 57 | return; 58 | 59 | // Node temp->next is the node to be deleted 60 | // Store pointer to the next of node to be deleted 61 | Node next = temp.next.next; 62 | 63 | temp.next 64 | = next; // Unlink the deleted node from list 65 | } 66 | 67 | /* This function prints contents of linked list starting 68 | from the given node */ 69 | public void printList() 70 | { 71 | Node tnode = head; 72 | while (tnode != null) { 73 | System.out.print(tnode.data + " "); 74 | tnode = tnode.next; 75 | } 76 | } 77 | 78 | /* Driver program to test above functions. Ideally this 79 | function should be in a separate user class. It is 80 | kept here to keep code compact */ 81 | public static void main(String[] args) 82 | { 83 | /* Start with the empty list */ 84 | LinkedList llist = new LinkedList(); 85 | 86 | llist.push(7); 87 | llist.push(1); 88 | llist.push(3); 89 | llist.push(2); 90 | llist.push(8); 91 | 92 | System.out.println("\nCreated Linked list is: "); 93 | llist.printList(); 94 | 95 | llist.deleteNode(4); // Delete node at position 4 96 | 97 | System.out.println( 98 | "\nLinked List after Deletion at position 4: "); 99 | llist.printList(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Water Connection Problem: -------------------------------------------------------------------------------- 1 | // C++ program to find efficient 2 | // solution for the network 3 | #include // For memset 4 | #include 5 | #include 6 | 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | using std::memset; 11 | using std::vector; 12 | // number of houses and number 13 | // of pipes 14 | int number_of_houses, number_of_pipes; 15 | 16 | // Array rd stores the 17 | // ending vertex of pipe 18 | int ending_vertex_of_pipes[1100]; 19 | 20 | // Array wd stores the value 21 | // of diameters between two pipes 22 | int diameter_between_two_pipes[1100]; 23 | 24 | // Array cd stores the 25 | // starting end of pipe 26 | int starting_vertex_of_pipes[1100]; 27 | 28 | // Vector a, b, c are used 29 | // to store the final output 30 | vector a; 31 | vector b; 32 | vector c; 33 | 34 | int ans; 35 | 36 | int dfs(int w) 37 | { 38 | if (starting_vertex_of_pipes[w] == 0) 39 | return w; 40 | if (diameter_between_two_pipes[w] < ans) 41 | ans = diameter_between_two_pipes[w]; 42 | return dfs(starting_vertex_of_pipes[w]); 43 | } 44 | 45 | // Function performing calculations. 46 | void solve(int arr[][3]) 47 | { 48 | for (int i = 0; i < number_of_pipes; ++i) { 49 | 50 | int house_1 = arr[i][0], house_2 = arr[i][1], 51 | pipe_diameter = arr[i][2]; 52 | 53 | starting_vertex_of_pipes[house_1] = house_2; 54 | diameter_between_two_pipes[house_1] = pipe_diameter; 55 | ending_vertex_of_pipes[house_2] = house_1; 56 | } 57 | 58 | a.clear(); 59 | b.clear(); 60 | c.clear(); 61 | 62 | for (int j = 1; j <= number_of_houses; ++j) 63 | 64 | /*If a pipe has no ending vertex 65 | but has starting vertex i.e is 66 | an outgoing pipe then we need 67 | to start DFS with this vertex.*/ 68 | if (ending_vertex_of_pipes[j] == 0 69 | && starting_vertex_of_pipes[j]) { 70 | ans = 1000000000; 71 | int w = dfs(j); 72 | 73 | // We put the details of component 74 | // in final output array 75 | a.push_back(j); 76 | b.push_back(w); 77 | c.push_back(ans); 78 | } 79 | 80 | cout << a.size() << endl; 81 | for (int j = 0; j < a.size(); ++j) 82 | cout << a[j] << " " << b[j] << " " << c[j] << endl; 83 | } 84 | 85 | // driver function 86 | int main() 87 | { 88 | number_of_houses = 9, number_of_pipes = 6; 89 | 90 | memset(ending_vertex_of_pipes, 0, 91 | sizeof(ending_vertex_of_pipes)); 92 | memset(starting_vertex_of_pipes, 0, 93 | sizeof(starting_vertex_of_pipes)); 94 | memset(diameter_between_two_pipes, 0, 95 | sizeof(diameter_between_two_pipes)); 96 | 97 | int arr[][3] 98 | = { { 7, 4, 98 }, { 5, 9, 72 }, { 4, 6, 10 }, 99 | { 2, 8, 22 }, { 9, 7, 17 }, { 3, 1, 66 } }; 100 | 101 | solve(arr); 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /merge sort: -------------------------------------------------------------------------------- 1 | // C++ program for Merge Sort 2 | #include 3 | using namespace std; 4 | 5 | // Merges two subarrays of array[]. 6 | // First subarray is arr[begin..mid] 7 | // Second subarray is arr[mid+1..end] 8 | void merge(int array[], int const left, int const mid, 9 | int const right) 10 | { 11 | auto const subArrayOne = mid - left + 1; 12 | auto const subArrayTwo = right - mid; 13 | 14 | // Create temp arrays 15 | auto *leftArray = new int[subArrayOne], 16 | *rightArray = new int[subArrayTwo]; 17 | 18 | // Copy data to temp arrays leftArray[] and rightArray[] 19 | for (auto i = 0; i < subArrayOne; i++) 20 | leftArray[i] = array[left + i]; 21 | for (auto j = 0; j < subArrayTwo; j++) 22 | rightArray[j] = array[mid + 1 + j]; 23 | 24 | auto indexOfSubArrayOne 25 | = 0, // Initial index of first sub-array 26 | indexOfSubArrayTwo 27 | = 0; // Initial index of second sub-array 28 | int indexOfMergedArray 29 | = left; // Initial index of merged array 30 | 31 | // Merge the temp arrays back into array[left..right] 32 | while (indexOfSubArrayOne < subArrayOne 33 | && indexOfSubArrayTwo < subArrayTwo) { 34 | if (leftArray[indexOfSubArrayOne] 35 | <= rightArray[indexOfSubArrayTwo]) { 36 | array[indexOfMergedArray] 37 | = leftArray[indexOfSubArrayOne]; 38 | indexOfSubArrayOne++; 39 | } 40 | else { 41 | array[indexOfMergedArray] 42 | = rightArray[indexOfSubArrayTwo]; 43 | indexOfSubArrayTwo++; 44 | } 45 | indexOfMergedArray++; 46 | } 47 | // Copy the remaining elements of 48 | // left[], if there are any 49 | while (indexOfSubArrayOne < subArrayOne) { 50 | array[indexOfMergedArray] 51 | = leftArray[indexOfSubArrayOne]; 52 | indexOfSubArrayOne++; 53 | indexOfMergedArray++; 54 | } 55 | // Copy the remaining elements of 56 | // right[], if there are any 57 | while (indexOfSubArrayTwo < subArrayTwo) { 58 | array[indexOfMergedArray] 59 | = rightArray[indexOfSubArrayTwo]; 60 | indexOfSubArrayTwo++; 61 | indexOfMergedArray++; 62 | } 63 | delete[] leftArray; 64 | delete[] rightArray; 65 | } 66 | 67 | // begin is for left index and end is 68 | // right index of the sub-array 69 | // of arr to be sorted */ 70 | void mergeSort(int array[], int const begin, int const end) 71 | { 72 | if (begin >= end) 73 | return; // Returns recursively 74 | 75 | auto mid = begin + (end - begin) / 2; 76 | mergeSort(array, begin, mid); 77 | mergeSort(array, mid + 1, end); 78 | merge(array, begin, mid, end); 79 | } 80 | 81 | // UTILITY FUNCTIONS 82 | // Function to print an array 83 | void printArray(int A[], int size) 84 | { 85 | for (auto i = 0; i < size; i++) 86 | cout << A[i] << " "; 87 | } 88 | 89 | // Driver code 90 | int main() 91 | { 92 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 93 | auto arr_size = sizeof(arr) / sizeof(arr[0]); 94 | 95 | cout << "Given array is \n"; 96 | printArray(arr, arr_size); 97 | 98 | mergeSort(arr, 0, arr_size - 1); 99 | 100 | cout << "\nSorted array is \n"; 101 | printArray(arr, arr_size); 102 | return 0; 103 | } 104 | 105 | // This code is contributed by Mayank Tyagi 106 | // This code was revised by Joshua Estes 107 | -------------------------------------------------------------------------------- /Pattern Searching using a Trie of all Suffixes: -------------------------------------------------------------------------------- 1 | // A simple C++ implementation of substring search using trie of suffixes 2 | #include 3 | #include 4 | #define MAX_CHAR 256 5 | using namespace std; 6 | 7 | // A Suffix Trie (A Trie of all suffixes) Node 8 | class SuffixTrieNode 9 | { 10 | private: 11 | SuffixTrieNode *children[MAX_CHAR]; 12 | list *indexes; 13 | public: 14 | SuffixTrieNode() // Constructor 15 | { 16 | // Create an empty linked list for indexes of 17 | // suffixes starting from this node 18 | indexes = new list; 19 | 20 | // Initialize all child pointers as NULL 21 | for (int i = 0; i < MAX_CHAR; i++) 22 | children[i] = NULL; 23 | } 24 | 25 | // A recursive function to insert a suffix of the txt 26 | // in subtree rooted with this node 27 | void insertSuffix(string suffix, int index); 28 | 29 | // A function to search a pattern in subtree rooted 30 | // with this node.The function returns pointer to a linked 31 | // list containing all indexes where pattern is present. 32 | // The returned indexes are indexes of last characters 33 | // of matched text. 34 | list* search(string pat); 35 | }; 36 | 37 | // A Trie of all suffixes 38 | class SuffixTrie 39 | { 40 | private: 41 | SuffixTrieNode root; 42 | public: 43 | // Constructor (Builds a trie of suffixes of the given text) 44 | SuffixTrie(string txt) 45 | { 46 | // Consider all suffixes of given string and insert 47 | // them into the Suffix Trie using recursive function 48 | // insertSuffix() in SuffixTrieNode class 49 | for (int i = 0; i < txt.length(); i++) 50 | root.insertSuffix(txt.substr(i), i); 51 | } 52 | 53 | // Function to searches a pattern in this suffix trie. 54 | void search(string pat); 55 | }; 56 | 57 | // A recursive function to insert a suffix of the txt in 58 | // subtree rooted with this node 59 | void SuffixTrieNode::insertSuffix(string s, int index) 60 | { 61 | // Store index in linked list 62 | indexes->push_back(index); 63 | 64 | // If string has more characters 65 | if (s.length() > 0) 66 | { 67 | // Find the first character 68 | char cIndex = s.at(0); 69 | 70 | // If there is no edge for this character, add a new edge 71 | if (children[cIndex] == NULL) 72 | children[cIndex] = new SuffixTrieNode(); 73 | 74 | // Recur for next suffix 75 | children[cIndex]->insertSuffix(s.substr(1), index+1); 76 | } 77 | } 78 | 79 | // A recursive function to search a pattern in subtree rooted with 80 | // this node 81 | list* SuffixTrieNode::search(string s) 82 | { 83 | // If all characters of pattern have been processed, 84 | if (s.length() == 0) 85 | return indexes; 86 | 87 | // if there is an edge from the current node of suffix trie, 88 | // follow the edge. 89 | if (children[s.at(0)] != NULL) 90 | return (children[s.at(0)])->search(s.substr(1)); 91 | 92 | // If there is no edge, pattern doesn’t exist in text 93 | else return NULL; 94 | } 95 | 96 | /* Prints all occurrences of pat in the Suffix Trie S (built for text)*/ 97 | void SuffixTrie::search(string pat) 98 | { 99 | // Let us call recursive search function for root of Trie. 100 | // We get a list of all indexes (where pat is present in text) in 101 | // variable 'result' 102 | list *result = root.search(pat); 103 | 104 | // Check if the list of indexes is empty or not 105 | if (result == NULL) 106 | cout << "Pattern not found" << endl; 107 | else 108 | { 109 | list::iterator i; 110 | int patLen = pat.length(); 111 | for (i = result->begin(); i != result->end(); ++i) 112 | cout << "Pattern found at position " << *i - patLen<< endl; 113 | } 114 | } 115 | 116 | // driver program to test above functions 117 | int main() 118 | { 119 | // Let us build a suffix trie for text "geeksforgeeks.org" 120 | string txt = "geeksforgeeks.org"; 121 | SuffixTrie S(txt); 122 | 123 | cout << "Search for 'ee'" << endl; 124 | S.search("ee"); 125 | 126 | cout << "\nSearch for 'geek'" << endl; 127 | S.search("geek"); 128 | 129 | cout << "\nSearch for 'quiz'" << endl; 130 | S.search("quiz"); 131 | 132 | cout << "\nSearch for 'forgeeks'" << endl; 133 | S.search("forgeeks"); 134 | 135 | return 0; 136 | } 137 | --------------------------------------------------------------------------------