├── .github └── workflows │ └── codeql_analysis.yml ├── .gitignore ├── .replit ├── Algorithms ├── Arrays │ ├── BubbleSort.py │ ├── Dijstras_algorithm.py │ ├── QuickSort.py │ ├── binarysearch.cpp │ └── kadaneAlgorithm.cpp ├── Graphs │ ├── Dijkstras_Algorithm.cpp │ ├── bfs-algorithm.cpp │ └── dfs-algorithm.cpp ├── Lists │ ├── linked-list.cpp │ ├── queue (list-two-headed).cpp │ └── stacks.java ├── Operating_Systems │ ├── CPU_Scheduling │ │ ├── fcfs.cpp │ │ ├── multilevel.cpp │ │ ├── round_robin.cpp │ │ ├── sjf_non_preemptive.cpp │ │ └── sjf_preemptive.cpp │ ├── Disk_Scheduling │ │ ├── c_look.cpp │ │ ├── c_scan.cpp │ │ ├── fcfs_disk.cpp │ │ ├── scan.cpp │ │ └── sstf.cpp │ ├── Page_Replacement │ │ ├── fifo.cpp │ │ ├── lru.cpp │ │ └── optimal.cpp │ └── deadlock │ │ └── bankers algorithm.cpp ├── README.md ├── Sorting & Searching │ ├── Array-Insertion Sort.cpp │ ├── Array-selectionSort.cpp │ ├── Binary_Search.cpp │ ├── BubbleSort.cpp │ ├── List-Insertion Sort.cpp │ ├── bin-sort.cpp │ ├── dynamicLinearSearch.cpp │ ├── heap_sort.cpp │ ├── merge-sort.cpp │ ├── merge_sort_algorithm.jpg │ ├── quick_sort.cpp │ └── radixsort.cpp ├── Trees │ └── Binary_Tree.cpp └── mo's_algorithm.cpp ├── CONTRIBUTING.md ├── CP Solutions ├── CSES │ ├── Dice Combinations-1633.cpp │ ├── Graph │ │ ├── Building Roads.cpp │ │ ├── Building Teams.cpp │ │ ├── Counting Rooms.cpp │ │ └── High score.cpp │ ├── Grid Paths-1638.cpp │ ├── Increasing Subsequence-1145.cpp │ ├── Missing Coin Sum-2183.cpp │ ├── Money Sums-1745 │ ├── Palindrome Reorder-1755.cpp │ ├── Sub Array Sums 2-1661.cpp │ └── Two Sets II-1093.cpp ├── Codeforces │ ├── 1114B.cpp │ ├── 116B.cpp │ ├── 1393B.cpp │ ├── 1408.cpp │ ├── 1538B.cpp │ ├── 1547B.cpp │ ├── 1549B.cpp │ ├── 1560B.cpp │ ├── 1560C.cpp │ ├── 1579D.cpp │ ├── 1579E1.cpp │ ├── 1579E2.cpp │ ├── 1581A.cpp │ ├── 1582F1.cpp │ ├── 1592A.cpp │ ├── 1592B.cpp │ ├── 1593A.cpp │ ├── 1593B.cpp │ ├── 1593C.cpp │ ├── 1593D1.cpp │ ├── 1593E.cpp │ ├── 1600J.cpp │ ├── 1692B.cpp │ ├── 1692C.cpp │ ├── 1712B.cpp │ ├── 1712C.cpp │ ├── 1715B.cpp │ ├── 1715D.cpp │ ├── 1718A1.cpp │ ├── 1719C.cpp │ ├── 1720D1.cpp │ ├── 1721B.cpp │ ├── 1735B.cpp │ ├── 231A.cpp │ ├── 260A.cpp │ ├── 315A.cpp │ ├── 451A.cpp │ └── 810C.cpp ├── README.md └── leetcode │ ├── Contains Duplicate │ └── Contains_Duplicate.cpp │ └── Knight_Tour.cpp ├── Contributors ├── Contributers-main │ ├── .codesandbox │ │ └── workspace.json │ ├── README.md │ ├── package.json │ ├── public │ │ └── index.html │ └── src │ │ ├── App.js │ │ ├── Footer.jsx │ │ ├── Grid.jsx │ │ ├── Mycard.jsx │ │ ├── Navbar.jsx │ │ ├── data.js │ │ ├── index.js │ │ └── styles.css ├── Contributors.json ├── README.md ├── Static site │ └── Static-main │ │ ├── asset-manifest.json │ │ ├── index.html │ │ ├── readme.md │ │ └── static │ │ ├── css │ │ ├── 2.c0897e43.chunk.css │ │ ├── 2.c0897e43.chunk.css.map │ │ ├── main.2ec8b1da.chunk.css │ │ └── main.2ec8b1da.chunk.css.map │ │ └── js │ │ ├── 2.44b0e230.chunk.js │ │ ├── 2.44b0e230.chunk.js.LICENSE.txt │ │ ├── 2.44b0e230.chunk.js.map │ │ ├── main.996f98f1.chunk.js │ │ ├── main.996f98f1.chunk.js.map │ │ ├── runtime-main.a7488486.js │ │ └── runtime-main.a7488486.js.map └── lekhit_borole.md ├── Graphics ├── README.md ├── algorithms │ ├── Cohen_Sutherland.cpp │ ├── Liang_Barsky.cpp │ ├── boundry fill.cpp │ ├── dda.cpp │ ├── generic.cpp │ ├── tr.cpp │ └── translation.cpp ├── c++ bindings │ ├── graphics.cpp │ └── graphics.hpp ├── example_Bresenham.cpp ├── example_circle.cpp ├── example_dda.cpp ├── example_ellipse.cpp ├── graphics.cpp └── python │ ├── Graphics.h.py │ └── requirements.txt ├── LICENSE ├── Parser ├── Hello_World.cpp ├── Hello_World.cpp.md ├── __pycache__ │ └── parser.cpython-38.pyc ├── parser.py ├── test.cpp └── test.cpp.md ├── README.md └── convert.py /.github/workflows/codeql_analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | analyze: 6 | name: Analyze 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | language: [ 'cpp' ] 13 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 14 | # Learn more: 15 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@main 20 | 21 | # Initializes the CodeQL tools for scanning. 22 | - name: Initialize CodeQL 23 | uses: github/codeql-action/init@main 24 | with: 25 | languages: ${{ matrix.language }} 26 | # If you wish to specify custom queries, you can do so here or in a config file. 27 | # By default, queries listed here will override any specified in a config file. 28 | # Prefix the list here with "+" to use these queries and those in the config file. 29 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 30 | 31 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 32 | # If this step fails, then you should remove it and run the build manually (see below) 33 | - name: Autobuild 34 | uses: github/codeql-action/autobuild@main 35 | 36 | # ℹ️ Command-line programs to run using the OS shell. 37 | # 📚 https://git.io/JvXDl 38 | 39 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 40 | # and modify them (or add more) to build your code if your project 41 | # uses a compiled language 42 | 43 | #- run: | 44 | # make bootstrap 45 | # make release 46 | 47 | - name: Perform CodeQL Analysis 48 | uses: github/codeql-action/analyze@main 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | -------------------------------------------------------------------------------- /.replit: -------------------------------------------------------------------------------- 1 | language = "bash" 2 | run = "" -------------------------------------------------------------------------------- /Algorithms/Arrays/BubbleSort.py: -------------------------------------------------------------------------------- 1 | # Bubble sort in Python 2 | 3 | def bubbleSort(array): 4 | 5 | # loop to access each array element 6 | for i in range(len(array)): 7 | 8 | # loop to compare array elements 9 | for j in range(0, len(array) - i - 1): 10 | 11 | # compare two adjacent elements 12 | # change > to < to sort in descending order 13 | if array[j] > array[j + 1]: 14 | 15 | # swapping elements if elements 16 | # are not in the intended order 17 | temp = array[j] 18 | array[j] = array[j+1] 19 | array[j+1] = temp 20 | 21 | 22 | data = [-2, 45, 0, 11, -9] 23 | 24 | bubbleSort(data) 25 | 26 | print('Sorted Array in Ascending Order:') 27 | print(data) 28 | -------------------------------------------------------------------------------- /Algorithms/Arrays/Dijstras_algorithm.py: -------------------------------------------------------------------------------- 1 | # Python program for Dijkstra's single 2 | # source shortest path algorithm. The program is 3 | # for adjacency matrix representation of the graph 4 | class Graph(): 5 | 6 | def __init__(self, vertices): 7 | self.V = vertices 8 | self.graph = [[0 for column in range(vertices)] 9 | for row in range(vertices)] 10 | 11 | def printSolution(self, dist): 12 | print("Vertex \t Distance from Source") 13 | for node in range(self.V): 14 | print(node, "\t\t", dist[node]) 15 | 16 | # A utility function to find the vertex with 17 | # minimum distance value, from the set of vertices 18 | # not yet included in shortest path tree 19 | def minDistance(self, dist, sptSet): 20 | 21 | # Initialize minimum distance for next node 22 | min = 1e7 23 | 24 | # Search not nearest vertex not in the 25 | # shortest path tree 26 | for v in range(self.V): 27 | if dist[v] < min and sptSet[v] == False: 28 | min = dist[v] 29 | min_index = v 30 | 31 | return min_index 32 | 33 | # Function that implements Dijkstra's single source 34 | # shortest path algorithm for a graph represented 35 | # using adjacency matrix representation 36 | def dijkstra(self, src): 37 | 38 | dist = [1e7] * self.V 39 | dist[src] = 0 40 | sptSet = [False] * self.V 41 | 42 | for cout in range(self.V): 43 | 44 | # Pick the minimum distance vertex from 45 | # the set of vertices not yet processed. 46 | # u is always equal to src in first iteration 47 | u = self.minDistance(dist, sptSet) 48 | 49 | # Put the minimum distance vertex in the 50 | # shortest path tree 51 | sptSet[u] = True 52 | 53 | # Update dist value of the adjacent vertices 54 | # of the picked vertex only if the current 55 | # distance is greater than new distance and 56 | # the vertex in not in the shortest path tree 57 | for v in range(self.V): 58 | if (self.graph[u][v] > 0 and 59 | sptSet[v] == False and 60 | dist[v] > dist[u] + self.graph[u][v]): 61 | dist[v] = dist[u] + self.graph[u][v] 62 | 63 | self.printSolution(dist) 64 | 65 | # Driver program 66 | g = Graph(9) 67 | g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0], 68 | [4, 0, 8, 0, 0, 0, 0, 11, 0], 69 | [0, 8, 0, 7, 0, 4, 0, 0, 2], 70 | [0, 0, 7, 0, 9, 14, 0, 0, 0], 71 | [0, 0, 0, 9, 0, 10, 0, 0, 0], 72 | [0, 0, 4, 14, 10, 0, 2, 0, 0], 73 | [0, 0, 0, 0, 0, 2, 0, 1, 6], 74 | [8, 11, 0, 0, 0, 0, 1, 0, 7], 75 | [0, 0, 2, 0, 0, 0, 6, 7, 0] 76 | ] 77 | 78 | g.dijkstra(0) 79 | -------------------------------------------------------------------------------- /Algorithms/Arrays/QuickSort.py: -------------------------------------------------------------------------------- 1 | # Quick sort in Python 2 | 3 | # function to find the partition position 4 | def partition(array, low, high): 5 | 6 | # choose the rightmost element as pivot 7 | pivot = array[high] 8 | 9 | # pointer for greater element 10 | i = low - 1 11 | 12 | # traverse through all elements 13 | # compare each element with pivot 14 | for j in range(low, high): 15 | if array[j] <= pivot: 16 | # if element smaller than pivot is found 17 | # swap it with the greater element pointed by i 18 | i = i + 1 19 | 20 | # swapping element at i with element at j 21 | (array[i], array[j]) = (array[j], array[i]) 22 | 23 | # swap the pivot element with the greater element specified by i 24 | (array[i + 1], array[high]) = (array[high], array[i + 1]) 25 | 26 | # return the position from where partition is done 27 | return i + 1 28 | 29 | # function to perform quicksort 30 | def quickSort(array, low, high): 31 | if low < high: 32 | 33 | # find pivot element such that 34 | # element smaller than pivot are on the left 35 | # element greater than pivot are on the right 36 | pi = partition(array, low, high) 37 | 38 | # recursive call on the left of pivot 39 | quickSort(array, low, pi - 1) 40 | 41 | # recursive call on the right of pivot 42 | quickSort(array, pi + 1, high) 43 | 44 | 45 | data = [8, 7, 2, 1, 0, 9, 6] 46 | print("Unsorted Array") 47 | print(data) 48 | 49 | size = len(data) 50 | 51 | quickSort(data, 0, size - 1) 52 | 53 | print('Sorted Array in Ascending Order:') 54 | print(data) 55 | -------------------------------------------------------------------------------- /Algorithms/Arrays/binarysearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Anurag Tripathi 3 | Link: https://www.geeksforgeeks.org/binary-search/ 4 | */ 5 | 6 | // Binary Search 7 | // Note: The array should be sorted in ascending order 8 | #include 9 | using namespace std; 10 | 11 | int binarySearch(int arr[],int n,int key){ 12 | 13 | // search for the required number will begin from index 's' to index 'e', which here initially are 0 and n. 14 | int s=0,e=n; 15 | while(s<=e){ 16 | // this middle index will be used in searching the required number. 17 | int mid=(s+e)/2; 18 | 19 | if(arr[mid]==key) 20 | return mid; 21 | 22 | // if the number being searched is less than the mid element of the alloted array, then all elements greater than mid term will be excluded, 23 | // and our new alloted array's end point will be this mid term. 24 | else if(arr[mid]>key){ 25 | e=mid-1; 26 | } 27 | 28 | // if the number being searched is more than the mid element of the alloted array, then all elements less than mid term will be excluded, 29 | // and our new alloted array's starting point will be this mid term. 30 | else{ 31 | s=mid+1; 32 | } 33 | 34 | } 35 | return -1; 36 | } 37 | 38 | int main(){ 39 | // Enter the length of the array 40 | int n; 41 | cin>>n; 42 | 43 | int arr[n]; 44 | // Enter your array 45 | for(int i=0;i>arr[i]; 47 | 48 | // Enter the number to be searched in the array 49 | int key; 50 | cin>>key; 51 | 52 | cout< 9 | using namespace std; 10 | int main(){ 11 | // Enter the array's size 12 | int n; 13 | cin>>n; 14 | 15 | // MAX variable denotes the maximum sum of subarray observed in whole process 16 | int arr1[n],currentSum=0,MAX=INT_MIN; 17 | 18 | // Enter elemets of the array 19 | for(int i=0;i>arr1[i]; 21 | } 22 | for(int i=0;i 7 | using namespace std; 8 | const int INF = 1e9; 9 | 10 | vector dijkstra(int source, vector>> adj) { 11 | int n = adj.size(); 12 | vector dist(n + 1); 13 | for (int i = 1; i <= n; ++i) dist[i] = INF; // initialise all distances to infinity 14 | dist[source] = 0; // initialize source distance to 0 15 | set> s; //{dist from source, node} 16 | s.insert({0, source}); 17 | while (!s.empty()) { 18 | int dis = s.begin()->first; 19 | int vertex = s.begin()->second; 20 | s.erase(s.begin()); 21 | for (pair x : adj[vertex]) { 22 | int newDis = dis + x.second; 23 | int newVer = x.first; 24 | if (newDis < dist[newVer]) { 25 | s.erase({dist[newVer], newVer}); 26 | dist[newVer] = newDis; // update the distance to smaller one 27 | s.insert({dist[newVer], newVer}); 28 | } 29 | } 30 | } 31 | } 32 | 33 | int main() { 34 | int n, m; // vertices, edges 35 | cin >> n >> m; 36 | vector>> adj(n + 1); // adjacency list 37 | for (int i = 0; i < m; i++) { 38 | int x, y, w; 39 | cin >> x >> y >> w; // w - weight 40 | adj[x].push_back({y, w}); 41 | adj[y].push_back({x, w}); 42 | } 43 | 44 | // Run dijkstras algorithm with source as node number 1 45 | vector d = dijkstra(1, adj); 46 | 47 | for (int i = 1; i <= n; ++i) cout << d[i] << ' '; 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Algorithms/Graphs/bfs-algorithm.cpp: -------------------------------------------------------------------------------- 1 | //Priyanshu Agarwal 2 | // problem - to traverse the graph using BFS - here in question the starting point is mentioned to be 0 but for general purpose you can start from where the graph is starting 3 | /* 4 | Edit by - Sahil Verma (sahil19-19) 5 | 6 | Links 7 | BFS - https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1 8 | - https://youtu.be/M8jdDR5kV3k 9 | multi souce BFS - https://www.geeksforgeeks.org/multi-source-shortest-path-in-unweighted-graph/ 10 | 0-1 BFS - https://www.geeksforgeeks.org/0-1-bfs-shortest-path-binary-graph/ 11 | 12 | Multi source BFS 13 | In this type of BFS there are multiple sources hence it is called multi source BFS 14 | This algo gives the shortest path to a node from all te starting nodes. 15 | 16 | 0-1 BFS 17 | BFS can only be applied if the graph is non weighted OR if the weights of all the edges are the same, 18 | but there is a special condition when BFS can be applied to a weighted graph of different weights. 19 | This is when we are given that the weight of any given edges is either 0 or 1. 20 | */ 21 | #include 22 | 23 | using namespace std; 24 | 25 | int main() 26 | { 27 | int n, m; //n corresponds to number of vertices and m corresponds to number of edges 28 | cin >> n >> m; 29 | vector adj[n + 1]; // an adjacency list to store which node is connected to which node 30 | // for 0-1 BFS we also need to store the weight of the edge between the 2 nodes so, we use vector> adj[n+1]; 31 | for (int i = 0; i < m; i++) 32 | { 33 | int x, y; 34 | cin >> x >> y; 35 | adj[x].push_back(y); 36 | adj[y].push_back(x); 37 | } 38 | int source; 39 | 40 | /*To detect the source here i have traversed to a point where the size of the adjacency list there will not be 0*/ 41 | 42 | for (int i = 0; i < n + 1; i++) 43 | { 44 | if (adj[i].size() != 0) 45 | { 46 | source = i; 47 | break; 48 | } 49 | } 50 | // a marked array is created to get to know which nodes are visited ,else we'll get stuck in a infinite loop 51 | bool marked[n] = {false}; 52 | 53 | /* queue is used for bfs traversal as we have to traverse more horizotally so we have to look for max number of nodes which are closest to source*/ 54 | 55 | queue q; // for 0-1 BFS we use a dequeue 56 | 57 | /* 58 | For multisource BFS we push all the souce nodes into the queue at this point.By doing so we ensure that any node is reached from the 59 | source node that is at the shortest distance from that node before any other source node is marked 'visited' / 'true'. 60 | */ 61 | 62 | q.push(source); 63 | marked[source] = true; 64 | while (!q.empty()) 65 | { 66 | int k = q.front(); 67 | q.pop(); 68 | cout << k << " "; 69 | for (int i = 0; i < adj[k].size(); i++) 70 | { 71 | if (marked[adj[k][i]] == false) 72 | { 73 | q.push(adj[k][i]); 74 | /* 75 | For 0-1 BFS we check the weight of the edge 76 | if weight == 0 we push node to the front of the dequeue - makes the level of both the nodes equal 77 | if weight == 1 we push node to the end of the dequeue 78 | 79 | this justifies the use of a dequeue in 0-1 BFS as we push elements on both the head and the tail 80 | */ 81 | marked[adj[k][i]] = true; 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Algorithms/Graphs/dfs-algorithm.cpp: -------------------------------------------------------------------------------- 1 | // Author: prashant_th18 2 | // Link: https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/ 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | vector> v; 6 | vector vis; 7 | void dfs(int node) { // Prints nodes of the graph in dfs order 8 | vis[node] = true; 9 | cout << node << ' '; 10 | for(const auto& val : v[node]) { 11 | if(!vis[val]) { 12 | dfs(val); 13 | } 14 | } 15 | } 16 | int main() { 17 | int nodes, edges; cin >> nodes >> edges; 18 | v.assign(nodes + 1, vector()); 19 | vis.assign(nodes + 1, false); 20 | for(int i = 0; i < edges; ++i) { 21 | int a, b; cin >> a >> b; // An edge exists between a and b 22 | v[a].push_back(b); 23 | v[b].push_back(a); 24 | } 25 | dfs(1); // Starting from node "1" 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Algorithms/Lists/linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author: steven anthony 3 | brief: linked list 4 | advantage: gives ability to traverse a list in forward direction 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | class Node 11 | { 12 | public: 13 | int p; // index 14 | Node* next; // pointer to the next node 15 | }; 16 | 17 | Node* newNode(int p) 18 | { 19 | Node* node = new Node; 20 | node->p = p; 21 | node->next = nullptr; 22 | return node; 23 | } 24 | 25 | Node* constructList() 26 | { 27 | Node* first = newNode(1); 28 | Node* second = newNode(2); 29 | Node* third = newNode(3); 30 | 31 | Node* head = first; 32 | first->next = second; 33 | second->next = third; 34 | 35 | return head; 36 | } 37 | 38 | void printList(Node* head) 39 | { 40 | Node* ptr = head; 41 | while (ptr) 42 | { 43 | cout << ptr->p << " -> "; 44 | ptr = ptr->next; 45 | } 46 | cout << "nullptr \n"; 47 | } 48 | 49 | int main() 50 | { 51 | Node *head = constructList(); 52 | printList(head); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Algorithms/Lists/queue (list-two-headed).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author: lekhit borole 3 | brief: two headed list with start and end node 4 | advantage: appending in simple list is O(n) with addition of end node appending at both end is O(1) 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | //creating list class 10 | class list{ 11 | // node for storing data and next link 12 | struct node{ 13 | int data; 14 | node *link; 15 | } 16 | //used two nodes to keep track of start and end point of list. 17 | //end node is very useful because it facilitates O(1) runtime for appending compared to O(n) runtime required in case if only start node is used. 18 | *start,*end; 19 | public: 20 | //constructor used to set start to null 21 | list(){ 22 | start=NULL; 23 | } 24 | // destructor used to free memory space 25 | ~list(){ 26 | while (start!=NULL){ 27 | //temp is used to store the start node because we need to move start to next node otherwise we would have simply used delete start 28 | node *temp=start; 29 | start=start->link; //start is moved to next node; 30 | delete temp; 31 | } 32 | } 33 | void append(int num); 34 | void display(); 35 | 36 | }; 37 | void list::append(int num){ 38 | //create new node 39 | node *item=new node; 40 | //fill in data 41 | item->data=num; 42 | //check if list is empty 43 | if (start == NULL){ 44 | //since we know that list is empty set start and end to new node 45 | start=item; 46 | end=item; 47 | } 48 | else { 49 | //here we now that list has atleast one element so we just need to extend that element with new node 50 | end->link=item; 51 | end=item; 52 | } 53 | } 54 | void list::display(){ 55 | //looping through entire list to 56 | for (node *i=start;i!=NULL;i=i->link){ 57 | cout<data< 7 | #include 8 | using namespace std; 9 | 10 | int main () 11 | { 12 | cout<>n; 16 | int total_time=0; 17 | vector process(n),burst(n),arrival(n),waiting(n),turnaround(n); 18 | cout<<"enter arrival time and burst time for the processes\n"; 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin>>arrival[i]>>burst[i]; 22 | total_time+= burst[i]; 23 | process[i]=i+1; 24 | } 25 | vector rep(n); 26 | rep[0]=0; 27 | for (int i = 1; i < n; i++) 28 | { 29 | rep[i]=rep[i-1] +burst[i-1]; 30 | if(rep[i]-arrival[i]<0) waiting[i]=0; 31 | else waiting[i]=(rep[i]-arrival[i]); 32 | } 33 | for (int i = 0; i < n; i++) 34 | { 35 | turnaround[i]= waiting[i]+burst[i]; 36 | } 37 | 38 | cout<<"Process | Arrival time | Burst time | Turnaround time | Waiting time | \n"; 39 | for (int i = 0; i < n; i++) 40 | { 41 | cout<>n; 24 | int total_time=0; // total time 25 | int m=0; 26 | cout<<"enter queue no. - 0/1 (sjf preemptive/ fcfs) and burst time for the processes\n"; // 0-> sjf preemptive 1-> fcfs 27 | for (int i = 0; i < n; i++) 28 | { 29 | cin>>arr[i][1]>>arr[i][2]; // queue, burst time 30 | arr[i][0]=i+1; // PID 31 | if(arr[i][1]==0) m++; // number of processes in queue 1 32 | total_time+= arr[i][2]; 33 | 34 | } 35 | int remaining_t[n]; 36 | for (int i = 0; i < n; i++) 37 | { 38 | remaining_t[i] = arr[i][2]; 39 | } 40 | 41 | int complete = 0, min_time = INT_MAX,shortest = 0, completion_time=0; 42 | // bool check = false; 43 | 44 | 45 | // queue 1 will be given priority over and processes in queue 1 will undergo sf=jf non preemptive scheduling 46 | while (complete != m) { 47 | 48 | for (int j = 0; j < n; j++) { 49 | if ((remaining_t[j] < min_time) && remaining_t[j] > 0 && !arr[j][1]) { 50 | min_time = remaining_t[j]; 51 | shortest = j; 52 | } 53 | } 54 | 55 | remaining_t[shortest]=0; 56 | min_time = 0; 57 | 58 | // when the process gets completely executed 59 | if (min_time == 0) 60 | { 61 | min_time = INT_MAX; 62 | complete++; 63 | completion_time += arr[shortest][2]; 64 | arr[shortest][4]= completion_time; // completion time 65 | arr[shortest][3] = completion_time -arr[shortest][2]; 66 | // waiting time = completion time - burst time - arrival time( arrival time is 0 for all processes) 67 | 68 | if (arr[shortest][3] < 0) 69 | arr[shortest][3] = 0; 70 | } 71 | 72 | } 73 | 74 | // Now remaining processes of queue 2 will be done according to fcfs 75 | for (int i = 0; i < n ; i++) 76 | { 77 | if(arr[i][1]) 78 | { 79 | arr[i][3]= completion_time; 80 | arr[i][4]= arr[i][3]+ arr[i][2]; 81 | completion_time+= arr[i][2]; 82 | } 83 | } 84 | 85 | int total_turnaround=0,total_waiting=0; 86 | for (int i = 0; i < n; i++) 87 | { 88 | total_waiting+= arr[i][3]; 89 | total_turnaround += arr[i][4]; 90 | } 91 | 92 | cout<<"Process Id\tBurst time\tTurnaround time\tWaiting time\n"; 93 | for (int i = 0; i < n; i++) 94 | { 95 | cout<>n; 16 | int arr[n][3]; 17 | //[][0]-PID,[][1]- burst time, [][2]- waiting time, [][3]-turn_around time 18 | 19 | int total_time=0; 20 | int rem_time[n]; 21 | cout<<"enter process id and burst time for the no processes \n"; 22 | for (int i = 0; i < n; i++) 23 | { 24 | cin>>arr[i][0]>>arr[i][1]; 25 | total_time += arr[i][1]; 26 | rem_time[i]=arr[i][1]; 27 | } 28 | int complete=0,quan; 29 | cout<<"enter time quantum: "; 30 | cin>>quan; 31 | int t=0; 32 | while (complete != n) 33 | { 34 | for (int i = 0; i < n; i++) 35 | { 36 | if(rem_time[i]>0) 37 | { 38 | int x; 39 | x= min(quan,rem_time[i]); 40 | t += max(x,0); // increasing time 41 | rem_time[i]-=quan; 42 | if(rem_time[i]<=0) 43 | { 44 | complete++; // incrementing "complete" when a process in=s completed 45 | arr[i][3]=t; 46 | } 47 | } 48 | } 49 | 50 | } 51 | int total_turn=0,total_wait=0; 52 | for (int i = 0; i < n; i++) 53 | { 54 | arr[i][2]=arr[i][3]-arr[i][1]; // turnaround time = waiting time + burst time 55 | total_wait+= arr[i][2]; 56 | total_turn+= arr[i][3]; 57 | } 58 | 59 | cout<<"Process Id\tBurst time\tTurnaround time\tWaiting time\n"; 60 | for (int i = 0; i < n; i++) 61 | { 62 | cout<>n; 19 | int total_time=0; 20 | 21 | cout<<"enter process id,arrival time and burst time for the no processes in the order in which they arrive\n"; 22 | 23 | for (int i = 0; i < n; i++) 24 | { 25 | cin>>aar[i][0]>>aar[i][1]>>aar[i][2]; 26 | total_time += aar[i][2]; 27 | } 28 | aar[0][3]=aar[0][1]+aar[0][2]; // completion time 29 | aar[0][5] = aar[0][2]; // turnaround time 30 | aar[0][4]= 0; //waiting time 31 | for (int i = 1; i < n; i++) 32 | { 33 | int x,y,val; 34 | x = aar[i - 1][3]; 35 | y = aar[i][2]; 36 | for (int j = i; j < n; j++) { 37 | if (x >= aar[j][1] && y >= aar[j][2]) { 38 | y = aar[j][2]; 39 | val = j; 40 | } 41 | } 42 | aar[val][3] = x + aar[val][2]; 43 | aar[val][5] = aar[val][3] - aar[val][1]; 44 | aar[val][4] = aar[val][5] - aar[val][2]; 45 | for (int k = 0; k < 6; k++) { 46 | swap(aar[val][k], aar[i][k]); 47 | 48 | } 49 | } 50 | double avg_turnaround=0,avg_waiting=0; 51 | int t_turn=0,t_wait=0; 52 | for (int i = 0; i < n; i++) 53 | { 54 | t_turn += aar[i][5]; 55 | t_wait += aar[i][4]; 56 | } 57 | avg_turnaround= (double)t_turn/double(n); 58 | avg_waiting= (double)t_wait/(double)n; 59 | 60 | cout<<"Process Id\tArrival time\tBurst time\tCompletion time\tTurnaround time\tWaiting time\n"; 61 | for (int i = 0; i < n; i++) 62 | { 63 | cout<> n; 20 | int total_time=0; 21 | cout<<"enter process id,arrival time and burst time for the no processes \n"; 22 | for (int i = 0; i < n; i++) 23 | { 24 | cin>>arr[i][0]>>arr[i][1]>>arr[i][2]; 25 | total_time += arr[i][2]; 26 | } 27 | 28 | int remaining_t[n]; 29 | for (int i = 0; i < n; i++) 30 | { 31 | remaining_t[i] = arr[i][2]; 32 | } 33 | 34 | int complete = 0, t = 0, min_time = INT_MAX,shortest = 0, completion_time; 35 | bool check = false; 36 | 37 | while (complete != n) { 38 | 39 | for (int j = 0; j < n; j++) { 40 | if ((arr[j][1] <= t) &&(remaining_t[j] < min_time) && remaining_t[j] > 0) { 41 | min_time = remaining_t[j]; 42 | shortest = j; 43 | check = true; 44 | } 45 | } 46 | 47 | if (check == false) { 48 | t++; 49 | continue; 50 | } 51 | 52 | remaining_t[shortest]--; 53 | min_time = remaining_t[shortest]; 54 | 55 | // when the process gets completely executed 56 | if (min_time == 0) 57 | { 58 | min_time = INT_MAX; 59 | complete++; 60 | check = false; 61 | completion_time = t + 1; 62 | arr[shortest][5]= completion_time; // completion time 63 | arr[shortest][3] = completion_time -arr[shortest][2] - arr[shortest][1]; 64 | // waiting time = completion time - burst time - arrival time 65 | 66 | if (arr[shortest][3] < 0) 67 | arr[shortest][3] = 0; 68 | } 69 | 70 | t++; 71 | } 72 | int total_turnaround=0,total_waiting=0; 73 | for (int i = 0; i < n; i++) 74 | { 75 | arr[i][4]=arr[i][3]+ arr[i][2]; 76 | // turnaround time = waiting time + burst time 77 | total_waiting+= arr[i][3]; 78 | total_turnaround += arr[i][4]; 79 | 80 | } 81 | 82 | cout<<"Process Id\tArrival time\tBurst time\tTurnaround time\tWaiting time\n"; 83 | for (int i = 0; i < n; i++) 84 | { 85 | cout<>n; 15 | vector v(n+1); 16 | cout<<"enter the string \n"; 17 | for (int i = 1; i <= n; i++) 18 | { 19 | cin>>v[i]; 20 | } 21 | cout<<"enter head :"; 22 | cin>>v[0]; 23 | head= v[0]; 24 | // moving to left first 25 | sort(v.begin(),v.end()); 26 | auto it = find(v.begin(),v.end(),head); 27 | int ind = it- v.begin(); 28 | sum = v[n]- v[0]; 29 | sum += head - v[0]; 30 | sum += (v[n]-v[ind+1]); 31 | cout< 8 | using namespace std; 9 | 10 | int main () 11 | { 12 | int n,sum=0, head; 13 | cout<<"enter size of string :"; 14 | cin>>n; 15 | vector v(n+1); 16 | cout<<"enter the string \n"; 17 | for (int i = 1; i <= n; i++) 18 | { 19 | cin>>v[i]; 20 | } 21 | cout<<"enter head :"; 22 | cin>>v[0]; 23 | head= v[0]; 24 | v.push_back(0); // vector now of size n+2 25 | sort(v.begin(),v.end()); 26 | auto it = find(v.begin(),v.end(),head); 27 | int ind = it- v.begin(); 28 | sum = 199 - v[ind+1]; 29 | sum+= ( head + 199); 30 | cout< 7 | using namespace std; 8 | 9 | int main () 10 | { 11 | int n; 12 | int head; 13 | cout<<"enter the size of refrence string :"; 14 | cin>>n; 15 | cout<<"enter head :"; 16 | cin>>head; 17 | vector v(n+1); 18 | 19 | int sum=0; 20 | cout<<"enter string :\n"; 21 | for (int i = 1; i <= n; i++) 22 | { 23 | cin>>v[i]; 24 | } 25 | v[0]= head; 26 | for (int i = 1; i <= n; i++) 27 | { 28 | sum += max((v[i]-v[i-1]),(v[i-1]-v[i])); 29 | } 30 | cout< 8 | using namespace std; 9 | 10 | int main () 11 | { 12 | int head,n,sum=0; 13 | cout<<"enter size of string :"; 14 | cin>>n; 15 | vector v(n+1); 16 | cout<<"enter the string \n"; 17 | for (int i = 1; i <= n; i++) 18 | { 19 | cin>>v[i]; 20 | } 21 | cout<<"enter head :"; 22 | cin>>v[0]; 23 | head= v[0]; 24 | v.push_back(0); 25 | sort(v.begin(),v.end()); 26 | //moving towards left first 27 | sum = v[n+1]; 28 | cout< 8 | using namespace std; 9 | 10 | int main () 11 | { 12 | int head,n; 13 | cout<<"enter size of string :"; 14 | cin>>n; 15 | vector v(n+1); 16 | cout<<"enter the string \n"; 17 | for (int i = 1; i <= n; i++) 18 | { 19 | cin>>v[i]; 20 | } 21 | cout<<"enter head :"; 22 | cin>>v[0]; 23 | head= v[0]; 24 | int sum=0; 25 | int y=0,x; 26 | for (int i = 0; i < n; i++) 27 | { 28 | int min=INT_MAX; 29 | for (int j = 1; j <= n-i; j++) 30 | { 31 | if(max((v[j]-head),(head-v[j])) < min) 32 | { 33 | min =max((v[j]-head),(head-v[j])); 34 | x=j; 35 | } 36 | } 37 | sum+= min; 38 | y=v[x]; 39 | v.erase(v.begin()+x); 40 | // y=x; 41 | head = y; 42 | 43 | } 44 | cout< 6 | #include 7 | using namespace std; 8 | 9 | int main () 10 | { 11 | cout<<"Enter size of reference string: "; 12 | int n; cin>>n; 13 | 14 | cout<<"Enter the refernce string: \n"; 15 | vector v(n),v_frames; 16 | for (int i = 0; i < n; i++) 17 | { 18 | cin>>v[i]; 19 | if(i==0) v_frames.push_back(v[i]); 20 | } 21 | int frm,faults=0;; 22 | cout<<"enter the no of page frames: "; 23 | cin>>frm; 24 | 25 | 26 | for (int i = 1; i < n; i++) 27 | { 28 | bool flag= true; 29 | for (int j = 0; j < v_frames.size() ; j++) 30 | { 31 | if(v[i]== v_frames[j]){ 32 | flag= false; 33 | break; 34 | } 35 | } 36 | if(!flag) continue; 37 | if(v_frames.size() 6 | #include 7 | using namespace std; 8 | 9 | int main () 10 | { 11 | cout<<"\nEnter size of reference string: "; 12 | int n; cin>>n; 13 | 14 | cout<<"Enter the refernce string: \n"; 15 | vector v(n),v_frames; 16 | for (int i = 0; i < n; i++) 17 | { 18 | cin>>v[i]; 19 | if(i==0) v_frames.push_back(v[i]); 20 | } 21 | int frm,faults=0; 22 | cout<<"enter the no of page frames: "; 23 | cin>>frm; 24 | int x; 25 | 26 | for (int i = 1; i < n; i++) 27 | { 28 | bool flag= true; 29 | for (int j = 0; j < v_frames.size() ; j++) 30 | { 31 | if(v[i]== v_frames[j]) 32 | { 33 | flag= false; 34 | x=j; 35 | break; 36 | } 37 | } 38 | if(flag) 39 | { 40 | if(v_frames.size() < frm) 41 | { 42 | v_frames.push_back(v[i]); 43 | faults++; 44 | } 45 | else 46 | { 47 | v_frames.erase(v_frames.begin()); 48 | v_frames.push_back(v[i]); 49 | faults++; 50 | } 51 | } 52 | 53 | else { 54 | int y = v_frames[x]; 55 | v_frames.erase(v_frames.begin()+x); 56 | v_frames.push_back(y); 57 | } 58 | 59 | } 60 | cout<<"faults are: "< 6 | #include 7 | using namespace std; 8 | 9 | int main () 10 | { 11 | cout<<"\nEnter size of reference string: "; 12 | int n; cin>>n; 13 | 14 | cout<<"Enter the refernce string: \n"; 15 | vector v(n),v_frames; 16 | for (int i = 0; i < n; i++) 17 | { 18 | cin>>v[i]; 19 | if(i==0) v_frames.push_back(v[i]); 20 | } 21 | int frm,faults=0; 22 | cout<<"enter the no of page frames: "; 23 | cin>>frm; 24 | int x; 25 | 26 | for (int i = 1; i < n; i++) 27 | { 28 | bool flag= true; 29 | for (int j = 0; j < v_frames.size() ; j++) 30 | { 31 | if(v[i]== v_frames[j]) 32 | { 33 | flag= false; 34 | x=j; 35 | break; 36 | } 37 | } 38 | if(flag) 39 | { 40 | 41 | if(v_frames.size() < frm) 42 | { 43 | v_frames.push_back(v[i]); 44 | faults++; 45 | } 46 | else 47 | { 48 | vector> vec; 49 | for (int a = 0; a < frm;a++) 50 | { 51 | for (int j = i+1; j < n ; j++) 52 | { 53 | if(v_frames[a]==v[j]) 54 | { 55 | vec.push_back({j,a}); 56 | break; 57 | } 58 | vec.push_back({n+1,a}); 59 | 60 | } 61 | 62 | } 63 | 64 | sort(vec.begin(),vec.end()); 65 | x = vec[frm-1].second; 66 | 67 | v_frames[x]=v[i]; 68 | faults++; 69 | 70 | } 71 | } 72 | 73 | } 74 | cout<<"faults are: "< 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | int cc=0; 10 | class process{public: 11 | vector allocated; 12 | vector max_need; 13 | vector demand; 14 | int id; 15 | process( vector a, 16 | vector m, 17 | vector d){ 18 | allocated=a; 19 | max_need=m; 20 | demand=d; 21 | id=cc++; 22 | } 23 | 24 | }; 25 | class Deadlock{ 26 | public: 27 | vector current; 28 | vector total; 29 | vector prs; 30 | Deadlock( 31 | vector tota){ 32 | total=tota; 33 | } 34 | void insert_process(process p){ 35 | 36 | prs.push_back(p); 37 | } 38 | bool safty(process p){ 39 | auto a=p.demand,mn=p.max_need; 40 | for(int i=0;imn[i]) { return false; } 42 | } 43 | cout<<"\nProcess "<current[i]) return false; 50 | } 51 | return true; 52 | } 53 | void set_current(){ 54 | vector s; 55 | 56 | for(auto I:total) s.push_back(0); 57 | //cout<<"total size"<total[i]) {cout<<"error total resources are less than allocated";// exit(0); 66 | 67 | } 68 | else{ 69 | //cout<<"\nss: "<> lt; 85 | for (auto i:prs) lt.push_back(make_pair(i,true)); 86 | int c1=0; 87 | 88 | do{ 89 | for (auto &i:lt) 90 | if(!resource_allocation(i.first) or !i.second) {continue;} 91 | else { 92 | auto p=i.first; 93 | cout<<"\ntaken process : P"< 5 | Algorithms/ 6 | ├─── Topic 1/ 7 | │ └─── Algorithm_Name.cpp 8 | └─── Topic 2/ 9 | 10 | 11 | * Check if an algorithm is already present, before adding it. 12 | 13 | * The name of added file must be the algorithm's name and it must added in the relevant folder(must be created if not already present). 14 | 15 | * Add your name as well as link of the algorithm at the top. And try to give some explaination of your code as well. -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/Array-Insertion Sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geeks-hub-nith/Programming/fb71161f6c11f549b4a28c07f0cb24c497f54c22/Algorithms/Sorting & Searching/Array-Insertion Sort.cpp -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/Array-selectionSort.cpp: -------------------------------------------------------------------------------- 1 | // Reference - https://www.programiz.com/dsa/selection-sort 2 | // C++ program for implementation of 3 | // selection sort 4 | #include 5 | using namespace std; 6 | 7 | // Swap function 8 | void sp(int *x, int *y) 9 | { 10 | int temp = *x; 11 | *x = *y; 12 | *y = temp; 13 | } 14 | 15 | void selectionSort(int array[], int n) 16 | { 17 | int i, j, min_ind; 18 | 19 | // One by one move boundary of 20 | // unsorted subarray 21 | for (i = 0; i < n - 1; i++) 22 | { 23 | 24 | // Find the minimum element in 25 | // unsorted array 26 | min_ind = i; 27 | for (j = i + 1; j < n; j++) 28 | if (array[j] < array[min_ind]) 29 | min_ind = j; 30 | 31 | // Swap the found minimum element 32 | // with the first element 33 | if (min_ind != i) 34 | sp(&array[min_ind], &array[i]); 35 | } 36 | } 37 | 38 | // Function to print the array 39 | void printArray(int array[], int size) 40 | { 41 | int i; 42 | for (i = 0; i < size; i++) 43 | cout << array[i] << " "; 44 | cout << endl; 45 | } 46 | 47 | // Driver program 48 | int main() 49 | { 50 | int array[] = {39, 8, 16, 22, 15}; 51 | int n = sizeof(array) / sizeof(array[0]); 52 | selectionSort(array, n); 53 | cout << "Sorted array: \n"; 54 | printArray(array, n); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/Binary_Search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Sahil (sahil180702@gmail.com) 3 | Link : https://www.geeksforgeeks.org/binary-search/ 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | /* 9 | Binary Search 10 | Binary Search: Search a sorted array by repeatedly dividing the search interval in half. 11 | Begin with an interval covering the whole array. 12 | 13 | 14 | Time complexity : O(n) , Space complexity : O(1) 15 | */ 16 | 17 | template 18 | bool binary_search(T *a,T key,int low,int high){ 19 | int mid; 20 | while (low<=high) // it is the base condition to terminate the loop 21 | { 22 | mid = (low+high)/2; 23 | if(a[mid]==key) return true; // if value at mid of array is equal to key , then we return true and function stop executing. 24 | if(a[mid](a,3,0,5)<(a,13,0,5)< 7 | using namespace std; 8 | void bubblesort(int arr[], int n) 9 | { 10 | if (n == 0 || n == 1) 11 | { 12 | return; 13 | } 14 | for (int i = 0; i < n - 1; i++) 15 | { 16 | if (arr[i] > arr[i + 1]) 17 | { 18 | swap(arr[i], arr[i + 1]); 19 | } 20 | } 21 | bubblesort(arr, n - 1); 22 | } 23 | int main() 24 | { 25 | int arr[5] = {2, 5, 1, 6, 9}; 26 | bubblesort(arr, 5); 27 | for (int i = 0; i < 5; i++) 28 | { 29 | cout << arr[i] << " "; 30 | } 31 | return 0; 32 | } 33 | /*OUTPUT: 34 | 1 2 5 6 9 */ -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/List-Insertion Sort.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | using namespace std; 6 | 7 | //creating list class 8 | struct node{ 9 | int data; 10 | node *link; 11 | } 12 | //used two nodes to keep track of start and end point of list. 13 | //end node is very useful because it facilitates O(1) runtime for appending compared to O(n) runtime required in case if only start node is used. 14 | ; 15 | 16 | class list{ 17 | public: 18 | // node for storing data and next link 19 | struct node *start,*end; 20 | 21 | //constructor used to set start to null 22 | list(){ 23 | start=NULL; 24 | } 25 | // destructor used to free memory space 26 | ~list(){ 27 | while (start!=NULL){ 28 | //temp is used to store the start node because we need to move start to next node otherwise we would have simply used delete start 29 | node *temp=start; 30 | start=start->link; //start is moved to next node; 31 | delete temp; 32 | } 33 | } 34 | void append(int num); 35 | void display(); 36 | void sort(); 37 | void position(node**,node**); 38 | }; 39 | void list::position(node **j1,node **i){ 40 | 41 | if (start==NULL) 42 | return ; 43 | for (node *j=start;j!=*i;j=j->link) 44 | {if (j->data > (*i)->data) 45 | return ; 46 | *j1=j;} 47 | } 48 | void list::sort(){ 49 | //if list is empty return 50 | if (start==NULL) 51 | return; 52 | //setting i to 2nd element 53 | //setting q to 1st element 54 | node *i=start->link,*q=start; 55 | while(i!=NULL){ 56 | //if the previous data is bigger than current data 57 | if (q->data>i->data){ 58 | node *j1=NULL,*temp=i; 59 | //find the position of nodes between which we need to insert current node 60 | position(&j1,&i); 61 | //moving i to next node 62 | i=i->link; 63 | q->link=temp->link; 64 | //if returned value of position is NULL then we are inserting at starting position 65 | if (j1==NULL){ 66 | temp->link=start; 67 | start=temp; 68 | } 69 | // inserting between two nodes 70 | else{ 71 | temp->link=j1->link; 72 | j1->link=temp; 73 | } 74 | } 75 | else{ 76 | q=i; 77 | i=i->link; 78 | } 79 | } 80 | 81 | 82 | 83 | } 84 | void list::append(int num){ 85 | //create new node 86 | node *item=new node; 87 | //fill in data 88 | item->data=num; 89 | //check if list is empty 90 | if (start == NULL){ 91 | //since we know that list is empty set start and end to new node 92 | start=item; 93 | end=item; 94 | } 95 | else { 96 | //here we now that list has atleast one element so we just need to extend that element with new node 97 | end->link=item; 98 | end=item; 99 | } 100 | } 101 | void list::display(){ 102 | //looping through entire list to 103 | for (node *i=start;i!=NULL;i=i->link){ 104 | cout<data<data=2; 122 | // lt.position(&j1,&k); 123 | // cout<data< 6 | #define M 10 /* Maximum of data range */ 7 | 8 | /** 9 | * numbers[] Array to be sorted 10 | * number_of_item 11 | * Number of data to be sorted; the number of data to be sorted in numbers. 12 | */ 13 | void bucket_sort(int numbers[], int number_of_item) { 14 | int buckets[M]; 15 | int i; 16 | 17 | /* prepare buckets */ 18 | for (i = 0; i < M; i++) { 19 | buckets[i] = 0; 20 | } 21 | /* putting in bucket */ 22 | for (i = 0; i < number_of_item; i++) { 23 | buckets[numbers[i]] = numbers[i]; 24 | } 25 | /* back into the original array */ 26 | int j = 0; 27 | for (i = 0; i < M; i++) { 28 | if (0 < buckets[i]) { 29 | numbers[j++] = buckets[i]; 30 | } 31 | } 32 | } 33 | 34 | void print_data(int numbers[]) { 35 | int i; 36 | for (i = 0; i < M; i++) { 37 | if (numbers[i] == 0) { 38 | continue; 39 | } 40 | printf("[%d] %d\n", i, numbers[i]); 41 | } 42 | } 43 | 44 | int main() { 45 | int numbers[M] = {7, 5, 4, 3, 2, 1}; 46 | 47 | printf("unsorted\n"); 48 | print_data(numbers); 49 | printf("\n"); 50 | 51 | /* bucket sort */ 52 | bucket_sort(numbers, 6); 53 | 54 | printf("sorted\n"); 55 | print_data(numbers); 56 | printf("\n"); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/dynamicLinearSearch.cpp: -------------------------------------------------------------------------------- 1 | /*[PROG] -> LINEAR SEARCH USING C++ 2 | 3 | In this program, linear search algorithm is implemented on a dynamic array. 4 | If the element is present in the array, then print the index number else print "Element not found" 5 | 6 | Time Complexity : O(n) 7 | 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main(){ 14 | 15 | // Creating a dynamic array 16 | int n,*arr,key,i; 17 | 18 | //Entering the size of array 19 | cout<<"Enter the size of array: "; 20 | cin>>n; 21 | arr = new int [n]; 22 | 23 | //Inserting the values in the array 24 | cout<<"Enter the values in the array: "; 25 | for(int j=0;j>*(arr+j); 27 | } 28 | 29 | //Entering the search value 30 | cout<<"Enter the value to search: "; 31 | cin>>key; 32 | 33 | //Traversing the array to search the value which was entered by the user 34 | for(i=0;i github username 67 | */ 68 | -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/heap_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*question: https://practice.geeksforgeeks.org/problems/heap-sort/1*/ 5 | 6 | 7 | //function to swap elements 8 | void swap(int *a, int *b) { 9 | int temp = *a; 10 | *a = *b; 11 | *b = temp; 12 | } 13 | 14 | void heapify(int arr[], int n, int index) { 15 | int largest = index; 16 | int left = 2 * index + 1; 17 | int right = 2 * index + 2; 18 | 19 | if (left < n && arr[left] > arr[largest]) 20 | largest = left; 21 | 22 | if (right < n && arr[right] > arr[largest]) 23 | largest = right; 24 | 25 | if (largest != index) { 26 | swap(&arr[index], &arr[largest]); 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | 31 | void buildHeap(int arr[], int n) 32 | { 33 | int startIdx = (n / 2) - 1; 34 | for (int i = startIdx; i >= 0; i--) { 35 | heapify(arr, n, i); 36 | } 37 | } 38 | 39 | void heapSort(int arr[], int n) { 40 | 41 | buildHeap(arr,n); 42 | for (int i = n - 1; i >= 0; i--) { 43 | swap(&arr[0], &arr[i]); 44 | heapify(arr, i, 0); 45 | } 46 | } 47 | int main(){ 48 | int arr[10] = {3,42,1,4,76,9,54,8,87,23}; 49 | heapSort(arr,10); 50 | for(int i=0; i<10; ++i){ 51 | cout<< arr[i] << " "; 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/merge-sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author :- Sanchit Sharma (sanchitsharma543@gmail.com) 3 | Reference :- https://www.geeksforgeeks.org/merge-sort/ 4 | */ 5 | 6 | // Merge-Sort Algorithm 7 | 8 | #include 9 | using namespace std; 10 | 11 | 12 | void merge(int arr[], int start, int mid, int end) { 13 | 14 | // temp array to store array before merging 15 | int A[end - start + 1]; 16 | 17 | // using two variables to index first and second half of the given array 18 | int f = start, s = mid + 1; 19 | 20 | // to index array temp A 21 | int k = 0; 22 | 23 | while (k <= end - start + 1) { 24 | // checks if first iterator has crossed mid mark if yes then just push element in second half 25 | if (f > mid) { 26 | A[k++] = arr[s++]; 27 | } 28 | // same check for the second half 29 | else if (s > end) { 30 | A[k++] = arr[f++]; 31 | } 32 | // if none of the above holds true then just add the min element from the two indexes in the temp array 33 | else { 34 | if (arr[f] < arr[s]) { 35 | A[k++] = arr[f++]; 36 | } else { 37 | A[k++] = arr[s++]; 38 | } 39 | } 40 | 41 | } 42 | // overriding the given array with the temp array 43 | for (int i = 0; i < k; i++) { 44 | arr[start++] = A[i]; 45 | } 46 | 47 | } 48 | 49 | void merge_sort(int A[], int start, int end) { 50 | 51 | if (start < end) { 52 | 53 | int mid = (start + end) / 2; 54 | 55 | merge_sort(A, start, mid); 56 | merge_sort(A, mid+1, end); 57 | 58 | merge(A, start, mid, end); 59 | 60 | } 61 | 62 | } 63 | 64 | // This algorithm sorts an array in non-decreasing order with worst case time complexity of O(nlogn). 65 | 66 | int main() { 67 | int arr[5] = {-1, 4, -2, 2, 11}; 68 | 69 | merge_sort(arr, 0, 4); 70 | 71 | for (int i = 0; i < 5; i++) cout << arr[i] << ' '; 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/merge_sort_algorithm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geeks-hub-nith/Programming/fb71161f6c11f549b4a28c07f0cb24c497f54c22/Algorithms/Sorting & Searching/merge_sort_algorithm.jpg -------------------------------------------------------------------------------- /Algorithms/Sorting & Searching/quick_sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author - Sahil Verma (sahil19-19) 3 | link - https://www.geeksforgeeks.org/quick-sort/ 4 | */ 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int partition(int arr[], int low, int high) 10 | { 11 | int i = low; 12 | int j = high; 13 | int pivot = arr[low]; 14 | while (i < j) 15 | { 16 | while (pivot >= arr[i]) 17 | i++; 18 | while (pivot < arr[j]) 19 | j--; 20 | if (i < j) 21 | swap(arr[i], arr[j]); 22 | } 23 | swap(arr[low], arr[j]); 24 | return j; 25 | } 26 | 27 | void quickSort(int arr[], int low, int high) 28 | { 29 | if (low < high) 30 | { 31 | int pivot = partition(arr, low, high); 32 | quickSort(arr, low, pivot - 1); 33 | quickSort(arr, pivot + 1, high); 34 | } 35 | } 36 | 37 | void printArray(int arr[], int size) 38 | { 39 | for (int i = 0; i < size; i++) 40 | { 41 | cout << arr[i] << " "; 42 | } 43 | cout << endl; 44 | } 45 | 46 | int main() 47 | { 48 | int arr[] = {4, 2, 8, 3, 1, 5, 7,11,6}; 49 | int size = sizeof(arr) / sizeof(int); 50 | cout<<"Before Sorting"< 9 | using namespace std; 10 | 11 | // Function to get the largest element from an array 12 | int getMax(int array[], int n) { 13 | int max = array[0]; 14 | for (int i = 1; i < n; i++) 15 | if (array[i] > max) 16 | max = array[i]; 17 | return max; 18 | } 19 | 20 | // Using counting sort to sort the elements in the basis of significant places 21 | void countingSort(int array[], int size, int place) { 22 | const int max = 10; 23 | int output[size]; 24 | int count[max]; 25 | 26 | for (int i = 0; i < max; ++i) 27 | count[i] = 0; 28 | 29 | // Calculate count of elements 30 | for (int i = 0; i < size; i++) 31 | count[(array[i] / place) % 10]++; 32 | 33 | // Calculate cumulative count 34 | for (int i = 1; i < max; i++) 35 | count[i] += count[i - 1]; 36 | 37 | // Place the elements in sorted order 38 | for (int i = size - 1; i >= 0; i--) { 39 | output[count[(array[i] / place) % 10] - 1] = array[i]; 40 | count[(array[i] / place) % 10]--; 41 | } 42 | 43 | for (int i = 0; i < size; i++) 44 | array[i] = output[i]; 45 | } 46 | 47 | // Main function to implement radix sort 48 | void radixsort(int array[], int size) { 49 | // Get maximum element 50 | int max = getMax(array, size); 51 | 52 | // Apply counting sort to sort elements based on place value. 53 | for (int place = 1; max / place > 0; place *= 10) 54 | countingSort(array, size, place); 55 | } 56 | 57 | // Print an array 58 | void printArray(int array[], int size) { 59 | int i; 60 | for (i = 0; i < size; i++) 61 | cout << array[i] << " "; 62 | cout << endl; 63 | } 64 | 65 | // Driver code 66 | int main() { 67 | int array[] = {121, 432, 564, 23, 1, 45, 788}; 68 | int n = sizeof(array) / sizeof(array[0]); 69 | radixsort(array, n); 70 | printArray(array, n); 71 | } 72 | -------------------------------------------------------------------------------- /Algorithms/Trees/Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // node is basic unit of tree which will be responsible to hold data and two children 4 | class node 5 | { 6 | public: 7 | node * left; 8 | node *right; 9 | //varible type can be altered as per requirement 10 | int data; //customize 11 | }; 12 | class Btree 13 | { 14 | public: 15 | int size; 16 | node *root; 17 | // storing inorder,preorder as array to rebuild tree 18 | int *inarr, *prearr; 19 | Btree(); 20 | ~Btree(); 21 | // buildtree will be accessed by the user 22 | void buildtree (int num); 23 | //insert will be used internally 24 | static void insert (node ** sr, int num); 25 | // display will show result of 26 | // - inorder 27 | // - preorder 28 | // -postorder 29 | void display (); 30 | static void inorder (node * sr, int &v, int *inarr); 31 | static void preorder (node * sr, int &v, int *prearr); 32 | static void postorder(node *sr); 33 | void deltree(); 34 | static void del(node *sr); 35 | void rebuild(); 36 | static node *recons(int *pre,int *in,int nodes,int& size);//nodes represent size; 37 | }; 38 | void Btree::rebuild(){ 39 | 40 | root=recons(prearr,inarr,size,size); 41 | } 42 | node* Btree::recons(int *pre,int* in,int nodes,int& size){ 43 | if (nodes==0) 44 | return NULL; 45 | node* temp; 46 | temp=new node; 47 | temp->data=pre[0]; 48 | temp->right=NULL; 49 | temp->left=NULL; 50 | if(nodes==1){ 51 | return temp; 52 | } 53 | int i,j,tempin[size],temppre[size]; 54 | for(i=0;in[i]!=pre[0];) 55 | i++; 56 | 57 | if(i>0){ 58 | for(j=0;jleft=recons(temppre,tempin,i,size); 64 | if(iright=recons(temppre,tempin,nodes-i-1,size); 72 | return temp; 73 | } 74 | Btree::~Btree(){ 75 | deltree(); 76 | } 77 | void Btree::deltree(){ 78 | del(root); 79 | root=NULL; 80 | } 81 | void Btree::del(node *sr){ 82 | if (sr!=NULL){ 83 | del(sr->left); 84 | del(sr->right); 85 | }delete sr; 86 | } 87 | Btree::Btree(){ 88 | size=0; 89 | root=NULL; 90 | } 91 | void Btree::postorder (node * sr ) 92 | { 93 | if (sr != NULL) 94 | { 95 | postorder (sr->left ); 96 | postorder (sr->right); 97 | cout << sr->data << "\t"; 98 | } 99 | } 100 | 101 | void Btree::preorder (node * sr, int &v, int* prearr ) 102 | { 103 | if (sr != NULL) 104 | { 105 | cout << sr->data << "\t"; 106 | prearr[v++]= sr->data; 107 | preorder (sr->left, v, prearr); 108 | preorder (sr->right, v, prearr); 109 | 110 | } 111 | } 112 | void Btree::inorder (node * sr, int &v, int* arrin ) 113 | { 114 | if (sr != NULL) 115 | { 116 | inorder (sr->left, v, arrin); 117 | cout << sr->data << "\t"; 118 | arrin[v++]= sr->data; 119 | inorder (sr->right, v, arrin); 120 | } 121 | } 122 | 123 | void Btree::buildtree (int num) 124 | { 125 | size++; 126 | insert (&root, num); 127 | } 128 | 129 | void Btree::insert (node **sr, int num) 130 | { 131 | if ((*sr) == NULL) 132 | { 133 | (*sr) = new node; 134 | (*sr)->data = num; 135 | (*sr)->left =NULL; (*sr)->right = NULL; 136 | } 137 | else 138 | { 139 | if (((*sr)->data) > num) 140 | insert (&((*sr)->left), num); 141 | else 142 | insert (&((*sr)->right), num); 143 | } 144 | } 145 | 146 | void Btree::display () 147 | { 148 | int n = size; 149 | inarr=new int[n]; 150 | prearr=new int[n]; 151 | int v = 0; 152 | cout << "\nInorder display" << endl; 153 | inorder (root, v, inarr); 154 | cout<<"\nPreorder display\n"; 155 | v=0; 156 | preorder(root,v,prearr); 157 | cout<<"\nPostorder display\n"; 158 | postorder(root); 159 | 160 | } 161 | 162 | int 163 | main () 164 | { 165 | 166 | Btree bt; 167 | 168 | cout<<"Enter y if you wish to run trial version, else enter n for custom input \n"; 169 | int n; 170 | char ch; 171 | cin>>ch; 172 | if(ch=='n'){ 173 | cout<<"enter number of elements you like to insert\n"; 174 | cin>>n; 175 | 176 | for(int i=0,j=0;i>j; 179 | bt.buildtree(j); 180 | } 181 | } 182 | else{ 183 | 184 | int arr[]={5,8,3,4,6,9}; 185 | cout<<"inserting elements\n"; 186 | for(auto i: arr) 187 | {bt.buildtree (i);cout< mo_s_algorithm(vector queries) { 24 | vector answers(queries.size()); 25 | sort(queries.begin(), queries.end()); 26 | 27 | // TODO: initialize data structure 28 | 29 | int cur_l = 0; 30 | int cur_r = -1; 31 | // invariant: data structure will always reflect the range [cur_l, cur_r] 32 | for (Query q : queries) { 33 | while (cur_l > q.l) { 34 | cur_l--; 35 | add(cur_l); 36 | } 37 | while (cur_r < q.r) { 38 | cur_r++; 39 | add(cur_r); 40 | } 41 | while (cur_l < q.l) { 42 | remove(cur_l); 43 | cur_l++; 44 | } 45 | while (cur_r > q.r) { 46 | remove(cur_r); 47 | cur_r--; 48 | } 49 | answers[q.idx] = get_answer(); 50 | } 51 | return answers; 52 | } 53 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution 2 | ## How to Contribute to this repository 3 | 4 | * Fork the repository (Click the Fork button in the top right of this page, click your Profile Image) 5 | 6 | * Clone the forked repository to your local machine. 7 | ```bash 8 | git clone https://github.com/your-username/Programming/.git 9 | ``` 10 | 11 | * change the present working directory 12 | ```bash 13 | cd Programming/ 14 | ``` 15 | 16 | * Add/Create your project to the specific folder as described in what you can contribute section. 17 | 18 | * Make a new branch 19 | ```bash 20 | git checkout -b branch-name 21 | ``` 22 | 23 | * Make change in the repository with the new branch. 24 | 25 | * push the changes. 26 | ```bash 27 | git add . 28 | git commit -m "Your commit Message" 29 | git push origin branch-name 30 | ``` 31 | 32 | * Make a pull request. 33 | 34 | ## NOTE 35 | * Make Sure you commit your changes in a new branch. 36 | * Make Sure you Give proper name to your files describing the addition. 37 | * Also Make Sure you comment your code whereever necessary. 38 | * Plese avoid spam PRs. 39 | 40 | *** 41 | 42 | ### Keep these things in mind when adding an algorithm : 43 | 44 | * The folder structure must remain as follows: 45 |
46 | 	Algorithms/
47 | 		├─── Topic 1/
48 | 		│       └─── Algorithm_Name.cpp     
49 | 		└─── Topic 2/
50 | 
51 | 52 | * Check if an algorithm is already present, before adding it. 53 | * The name of added file must be the algorithm's name and it must added in the relevant folder(must be created if not already present). 54 | 55 | *** 56 | 57 | ### Keep these things in mind when adding a solution : 58 | 59 | - The folder structure must remain as follows: 60 |
61 | 	CP Solutions/
62 | 		├─── Codeforces/
63 | 		│       └─── 1599A.cpp     
64 | 		└─── Codechef
65 | 
66 | - Check if a problem is already present, before adding it. 67 | - The first line of code must contain a link to corresponding question. 68 | - The name of added file must be the problem code and it must added in the relevant folder. 69 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Dice Combinations-1633.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://cses.fi/problemset/task/1633/ 2 | // AUTHOR - DEEPAK SRIVASTAVA 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int m = (int) 1e9 + 7; 9 | int cache[1000005]; 10 | 11 | int dp(int req) { 12 | if (req == 0) return 1; 13 | if (req < 0) return 0; 14 | int &res = cache[req]; 15 | if (res != -1) return res; 16 | res = 0; 17 | for (int i = 1; i <= 6; ++i) { 18 | res = (res + dp(req - i)) % m; 19 | } 20 | return res; 21 | } 22 | 23 | int main() { 24 | ios_base::sync_with_stdio(!cin.tie(nullptr)); 25 | int n; 26 | cin >> n; 27 | memset(cache, -1, sizeof cache); 28 | cout << dp(n); 29 | } -------------------------------------------------------------------------------- /CP Solutions/CSES/Graph/Building Roads.cpp: -------------------------------------------------------------------------------- 1 | // name: lekhit borole email: blekhit@gmail.com 2 | 3 | #include 4 | using namespace std; 5 | 6 | const int MaxN=1e5+7; 7 | int id[MaxN],N,M,sz[MaxN]; 8 | 9 | void QuickUnion(int N) 10 | { 11 | 12 | for (int i = 0; i < N; i++) id[i] = i; 13 | } 14 | int root(int i) 15 | { 16 | while (i != id[i]) {id[i] = id[id[i]];i=id[i];} 17 | return i; 18 | } 19 | bool find(int p, int q) 20 | { 21 | return root(p) == root(q); 22 | } 23 | void unite(int p, int q) 24 | { 25 | int i = root(p), j = root(q); 26 | if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; } 27 | else { id[j] = i; sz[i] += sz[j]; } 28 | 29 | } 30 | 31 | int main(){ 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | // freopen("test_input_1.txt", "r", stdin); 35 | // freopen("test_output_1.txt", "w", stdout); 36 | cin>>N>>M; 37 | for(int i=0;i>a>>b; 42 | unite(a-1,b-1); 43 | } 44 | unordered_set uns; 45 | int p; 46 | for(int i=0;i1) 53 | { 54 | cout<<(uns.size()-1)< 2 | using namespace std; 3 | const int maxN=1e5+8; 4 | vector adj[maxN]; 5 | bool visited[maxN];int teams[maxN]; 6 | int main(){ 7 | memset(teams,-1,sizeof teams); 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | // freopen("input.txt", "r", stdin); 11 | // freopen("output.txt", "w", stdout); 12 | int n,m;scanf("%d %d",&n,&m); 13 | while(m--){ 14 | int a,b;scanf("%d %d",&a,&b); 15 | adj[a].push_back(b); 16 | adj[b].push_back(a); 17 | } 18 | 19 | queue q; 20 | for(int i=1;i<=n;i++){ 21 | if(teams[i]==-1){ 22 | teams[i]=1; 23 | q.push(i); 24 | } 25 | while(!q.empty()){ 26 | int node=q.front();q.pop(); 27 | if(teams[node]==-1){ 28 | teams[node]=1; 29 | } 30 | for(auto j:adj[node]){ 31 | 32 | if(teams[node]==teams[j]) 33 | { 34 | //cout< 2 | using std::cin; 3 | using namespace std; 4 | char c; 5 | int m,n; 6 | int v[]={0,0,1,-1},h[]={1,-1,0,0}; 7 | 8 | bool vis[1000][1000]; 9 | 10 | void dfs(int x,int y){ 11 | vis[x][y]=true; 12 | for(int i=0;i<4;i++){ 13 | int dx=x+v[i],dy=y+h[i]; 14 | if(dx=0 && dy=0 && !vis[dx][dy]){ 15 | dfs(dx,dy); 16 | } 17 | } 18 | } 19 | 20 | int solve(){ 21 | cin>>m>>n; 22 | int cnt=0; 23 | for(int i=0;i>c; 26 | vis[i][j]=(c=='#'); 27 | } 28 | } 29 | for(int i=0;i 2 | using namespace std; 3 | #define int long long 4 | 5 | //#define LOCAL true 6 | template 7 | void see(T&... args) { ((cin >> args), ...);} 8 | const int maxN=2507,NINF=-1e18; 9 | int N,M; 10 | vector> adj[maxN]; 11 | vector dist,G[maxN],G2[maxN]; 12 | bool check=false,MARK[maxN],VIS[maxN],VIS2[maxN]; 13 | bool bellmanF(){ 14 | 15 | for(int i=0;i>N>>M; 75 | dist.assign(maxN,NINF); 76 | dist[1]=0; 77 | for(int i=0;i 4 | using namespace std; 5 | #define M 1000000007 6 | using namespace std; 7 | int main() 8 | { 9 | int n; 10 | scanf("%d", &n); 11 | char arr[n + 1][n + 1]; 12 | long long int ar[n + 1][n + 1]; 13 | for (int i = 1; i <= n; i++) 14 | { 15 | scanf("%s", &arr[i][1]); 16 | } 17 | arr[0][0] = 0; 18 | for (int j = 1; j <= n; j++) 19 | { 20 | ar[0][j] = 0; 21 | } 22 | for (int j = 1; j <= n; j++) 23 | { 24 | ar[j][0] = 0; 25 | } 26 | for (int i = 1; i <= n; i++) 27 | { 28 | for (int j = 1; j <= n; j++) 29 | { 30 | if (i == 1 && j == 1) 31 | { 32 | if (arr[1][1] != '*') 33 | { 34 | ar[1][1] = 1; 35 | } 36 | else 37 | { 38 | ar[1][1] = 0; 39 | } 40 | } 41 | else 42 | { 43 | if (arr[i][j] != '*') 44 | { 45 | ar[i][j] = (ar[i - 1][j] + ar[i][j - 1]) % M; 46 | } 47 | else 48 | { 49 | ar[i][j] = 0; 50 | } 51 | } 52 | } 53 | } 54 | printf("%d", ar[n][n]); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Increasing Subsequence-1145.cpp: -------------------------------------------------------------------------------- 1 | // Link-https://cses.fi/problemset/task/1145/ 2 | // AUTHOR- SHIVANSH 3 | 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n; 9 | scanf("%d", &n); 10 | int arr[n + 1], ar[n + 1], p = 1; 11 | for (int i = 1; i <= n; i++) 12 | { 13 | scanf("%d", &arr[i]); 14 | } 15 | ar[1] = arr[1]; 16 | for (int i = 2; i <= n; i++) 17 | { 18 | if (arr[i] > ar[p] || arr[i] <=ar[1]) 19 | { 20 | if (arr[i] > ar[p]) 21 | { 22 | p++; 23 | ar[p] = arr[i]; 24 | } 25 | else 26 | { 27 | ar[1] = arr[i]; 28 | } 29 | } 30 | else 31 | { 32 | int l = 1, r = p; 33 | while (r - l > 1) 34 | { 35 | int mid = (r + l) / 2; 36 | if (ar[mid] < arr[i]) 37 | { 38 | l = mid; 39 | } 40 | else 41 | { 42 | r = mid; 43 | } 44 | } 45 | ar[r] = arr[i]; 46 | } 47 | } 48 | printf("%d", p); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Missing Coin Sum-2183.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://cses.fi/problemset/task/2183/ 2 | // AUTHOR - DEEPAK SRIVASTAVA 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | int32_t main() { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(nullptr); 11 | int n; 12 | cin >> n; 13 | vector a(n); 14 | for (int i = 0; i < n; ++i) cin >> a[i]; 15 | sort(a.begin(), a.end()); 16 | long long sum = 0; 17 | for (int i = 0; i < n; ++i) { 18 | if (sum + 1 < a[i]) { 19 | cout << sum + 1; 20 | exit(0); 21 | } 22 | sum += a[i]; 23 | } 24 | cout << sum + 1; 25 | } 26 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Money Sums-1745: -------------------------------------------------------------------------------- 1 | //Link- https://cses.fi/problemset/task/1745 2 | //AUTHOR- SHIVANSH 3 | 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n, sum = 0; 9 | scanf("%d", &n); 10 | int arr[n + 1]; 11 | for (int i = 1; i <= n; i++) 12 | { 13 | scanf("%d", &arr[i]); 14 | sum = sum + arr[i]; 15 | } 16 | int dp[n + 2][sum + 2]; 17 | dp[0][0] = 1; 18 | for (int i = 1; i <= sum; i++) 19 | { 20 | dp[0][i] = 0; 21 | } 22 | for (int i = 1; i <= n; i++) 23 | { 24 | for (int j = 0; j <= sum; j++) 25 | { 26 | dp[i][j] = 0; 27 | } 28 | } 29 | for (int i = 1; i <= n; i++) 30 | { 31 | for (int j = 0; j <= sum; j++) 32 | { 33 | int l = j - arr[i]; 34 | if (l >= 0) 35 | { 36 | if (dp[i - 1][j] == 1 || (dp[i - 1][l] == 1)) 37 | { 38 | dp[i][j] = 1; 39 | } 40 | } 41 | else 42 | { 43 | dp[i][j] = dp[i - 1][j]; 44 | } 45 | } 46 | } 47 | int u = 0; 48 | int ar[sum + 2]; 49 | for (int i = 1; i <= sum; i++) 50 | { 51 | if (dp[n][i] == 1) 52 | { 53 | u++; 54 | ar[u] = i; 55 | } 56 | } 57 | printf("%d\n", u); 58 | for (int i = 1; i <= u; i++) 59 | { 60 | printf("%d ", ar[i]); 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Palindrome Reorder-1755.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://cses.fi/problemset/task/1755 2 | // Author: aryanpathania 3 | #include 4 | #define MOD 1'000'000'007 5 | //#define MOD = 998'244'353 6 | #define fo(x) for(int i=0;i> s; 16 | std::map mp; 17 | for (auto &&i : s){ 18 | mp[i]++; 19 | } 20 | int oddc = 0; 21 | char oddchar; 22 | for (auto &&i : mp) 23 | { 24 | if(i.second&1){ 25 | oddchar = i.first; 26 | oddc++; 27 | } 28 | } 29 | if(oddc > 1){ 30 | std::cout << "NO SOLUTION" << '\n'; 31 | return; 32 | } 33 | 34 | std::string opt; 35 | for (auto &&i : mp){ 36 | for (int r = 0; r < i.second/2; r++){ 37 | opt += i.first; 38 | } 39 | } 40 | std::cout << opt; 41 | if(oddc){ 42 | std::cout << oddchar; 43 | } 44 | std::reverse(opt.begin(),opt.end()); 45 | std::cout << opt; 46 | std::cout << '\n'; 47 | }//solve 48 | 49 | int32_t main(){ 50 | std::ios::sync_with_stdio(false); std::cin.tie(NULL); 51 | auto time_start = chrono::high_resolution_clock::now(); 52 | 53 | int testcases = 1; 54 | // std::cin >> testcases; 55 | 56 | while(testcases--) { 57 | solve(); 58 | }//testcases 59 | 60 | auto time_end = chrono::high_resolution_clock::now(); 61 | double time_taken = chrono::duration_cast(time_end - time_start).count(); 62 | 63 | time_taken *= 1e-9; 64 | cerr << setprecision(6) << time_taken << " ms" << '\n'; 65 | return 0; 66 | }//main 67 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Sub Array Sums 2-1661.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Sanchit Sharma 3 | link: https://cses.fi/problemset/task/1661/ (SubArray Sums 2) 4 | */ 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | string to_string(string s) { 11 | return '"' + s + '"'; 12 | } 13 | string to_string(char ch) { 14 | string s(1, ch); 15 | return '\'' + s + '\''; 16 | } 17 | string to_string(const char *s) { 18 | return to_string((string)s); 19 | } 20 | string to_string(bool b) { 21 | return (b ? "true" : "false"); 22 | } 23 | template 24 | string to_string(pair p) { 25 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 26 | } 27 | template 28 | string to_string(A v) { 29 | bool first = true; 30 | string res = "{"; 31 | for (const auto &x : v) { 32 | if (!first) { 33 | res += ", "; 34 | } 35 | first = false; 36 | res += to_string(x); 37 | } 38 | res += "}"; 39 | return res; 40 | } 41 | void debug_out() { cerr << endl; } 42 | template 43 | void debug_out(Head H, Tail... T) { 44 | cerr << " " << to_string(H); 45 | debug_out(T...); 46 | } 47 | 48 | #ifndef ONLINE_JUDGE 49 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 50 | #else 51 | #define debug(...) 42 52 | #endif 53 | 54 | #define f(i, x, n) for (int i = x; i < n; i++) 55 | #define all(x) (x).begin(), (x).end() 56 | #define rall(x) (x).rbegin(), (x).rend() 57 | #define F first 58 | #define S second 59 | #define pb push_back 60 | #define endl "\n" 61 | #define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) 62 | 63 | typedef long long int ll; 64 | #define int ll 65 | typedef pair pll; 66 | typedef vector> matrix; 67 | typedef vector vll; 68 | 69 | const ll mod = 1e9 + 7; 70 | const ll inf = LLONG_MAX; 71 | const ll N = 1e5 + 10; 72 | 73 | void solve() { 74 | 75 | int n, x; cin >> n >> x; 76 | 77 | vll a(n); f (i, 0, n) cin >> a[i]; 78 | 79 | int prefSum = 0, ans = 0; 80 | map sum; // keeps track of prefix array sum 81 | sum[0] = 1; 82 | 83 | f (i, 0, n) { 84 | prefSum += a[i]; 85 | 86 | ans += sum[prefSum - x]; 87 | // agar koi pehle subsequence esa ban rha ho jiska sum 88 | // prefSum - x ban rha h to usko delete krke ek soln ban skta hai 89 | 90 | sum[prefSum]++; 91 | } 92 | 93 | cout << ans; 94 | 95 | } 96 | 97 | int32_t main() { 98 | fast_io(); 99 | 100 | int t_c = 1; 101 | // cin >> t_c; 102 | 103 | while (t_c--) { 104 | solve(); 105 | // cout << endl; 106 | } 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /CP Solutions/CSES/Two Sets II-1093.cpp: -------------------------------------------------------------------------------- 1 | // AUTHOR -SHIVANSH 2 | //LINK-https://cses.fi/problemset/task/1093/ 3 | #include 4 | using namespace std; 5 | #define M 1000000007 6 | int arr[502][62627]; 7 | long long int modinverse(long long int c, long long int m); 8 | void extendeuclid(long long int a, long long int b); 9 | long long int d, x, y; 10 | long long int modinverse(long long int c, long long int m) 11 | { 12 | extendeuclid(c, m); 13 | return (x % m + m) % m; 14 | } 15 | void extendeuclid(long long int a, long long int b) 16 | { 17 | if (b == 0) 18 | { 19 | d = a; 20 | x = 1; 21 | y = 0; 22 | } 23 | else 24 | { 25 | extendeuclid(b, a % b); 26 | int temp = x; 27 | x = y; 28 | y = temp - (a / b) * y; 29 | } 30 | } 31 | int main() 32 | { 33 | int n; 34 | scanf("%d", &n); 35 | int s = (n * (n + 1)) / 2; 36 | if (s % 2) 37 | { 38 | printf("0\n"); 39 | } 40 | else 41 | { 42 | s = s / 2; 43 | arr[0][0] = 1; 44 | for (int i = 1; i <= n; i++) 45 | { 46 | int p = (i * (i + 1)) / 2; 47 | if(i==n) 48 | { 49 | p=s; 50 | } 51 | for (int j = 0; j <= p; j++) 52 | { 53 | if (j < i) 54 | { 55 | arr[i][j] = arr[i - 1][j]; 56 | } 57 | else 58 | { 59 | arr[i][j] = ((arr[i - 1][j - i]) + (arr[i - 1][j])) % M; 60 | } 61 | } 62 | } 63 | long long int q = modinverse(2, M), r = arr[n][s]; 64 | // printf("%lld %lld\n", q, r); 65 | printf("%lld\n", (q * r) % M); 66 | } 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1114B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1114/problem/B 2 | #ifdef LOCAL 3 | #define _GLIBCXX_DEBUG 4 | #endif 5 | // #pragma GCC optimize("O3") 6 | // #pragma GCC target("popcnt") 7 | #include 8 | // #include 9 | // #include 10 | // using namespace __gnu_pbds; 11 | using namespace std; 12 | #define MOD 1000000007 13 | typedef long long ll; 14 | typedef long double ld; 15 | #define sz(s) ((int)s.size()) 16 | #define all(v) begin(v), end(v) 17 | #define ff first 18 | #define ss second 19 | #ifdef LOCAL 20 | #define debug(x) cout << '\n' << "----------------" << '\n' << #x << " : "; _print(x); cout << '\n' << "-------------" << '\n'; 21 | #else 22 | #define debug(x) 23 | #endif 24 | 25 | // mt19937 rnd(239); 26 | mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); 27 | 28 | // #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> /* Ordered Set */ 29 | // #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> /* Ordered MultiSet */ 30 | template void _print(vector v) { cout << "[ "; for (auto myval : v) cout << myval << " "; cout << "]"; } 31 | template void _print(vector v) { cout << "[ "; for (auto myval : v) cout << myval.ff << ' ' << myval.ss << " "; cout << "]"; } 32 | template void _print(set v) { cout << "[ "; for (auto myval : v) cout << myval << " "; cout << "]"; } 33 | template void _print(map v) { cout << "[ "; for (auto myval : v) cout << myval.ff << ' ' << myval.ss << " "; cout << "]"; } 34 | void _print(int a) {cout << a;} 35 | void _print(ll a) {cout << a;} 36 | void _print(char a) {cout << a;} 37 | void _print(string a) {cout << a;} 38 | void _print(double a) {cout << a;} 39 | // *-> KISS* 40 | int solve() { 41 | ll n, m, k; cin >> n >> m >> k; 42 | ll take = m * k; 43 | ll cnt = 0; 44 | vector a(n); 45 | for (int i = 0; i < n; ++i) { 46 | cin >> a[i]; 47 | } 48 | ll sum {}; 49 | decltype(a) copy(a); 50 | sort(copy.rbegin(), copy.rend()); 51 | unordered_map mp; 52 | for (int i = 0; i < take; i++) { 53 | mp[copy[i]]++; 54 | sum += copy[i]; 55 | } 56 | vector pos; 57 | for (int i = 0; i < n; i++) { 58 | if(mp[a[i]] > 0) 59 | { 60 | mp[a[i]]--; 61 | ++cnt; 62 | if(cnt % m == 0) 63 | pos.push_back(i + 1); 64 | } 65 | } 66 | cout << sum << '\n'; 67 | for(int i = 0; i < sz(pos) - 1; ++i) cout << pos[i] << ' '; 68 | return 0; 69 | } 70 | int32_t main() { 71 | ios::sync_with_stdio(0); 72 | cin.tie(0); 73 | int TET = 1; 74 | //cin >> TET; 75 | cout << fixed << setprecision(6); 76 | for (int i = 1; i <= TET; i++) { 77 | #ifdef LOCAL 78 | cout << "##################" << '\n'; 79 | #endif 80 | if(solve()) 81 | { 82 | break; 83 | } 84 | cout << '\n'; 85 | } 86 | #ifdef LOCAL 87 | cout << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; 88 | #endif 89 | } 90 | // -> Keep It Simple Stupid! 91 | 92 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/116B.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Sanchit Sharma (sanchit06) 3 | link: https://codeforces.com/contest/116/problem/B 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | string to_string(string s) { 10 | return '"' + s + '"'; 11 | } 12 | string to_string(char ch) { 13 | string s(1, ch); 14 | return '\'' + s + '\''; 15 | } 16 | string to_string(const char *s) { 17 | return to_string((string)s); 18 | } 19 | string to_string(bool b) { 20 | return (b ? "true" : "false"); 21 | } 22 | template 23 | string to_string(pair p) { 24 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 25 | } 26 | template 27 | string to_string(A v) { 28 | bool first = true; 29 | string res = "{"; 30 | for (const auto &x : v) { 31 | if (!first) { 32 | res += ", "; 33 | } 34 | first = false; 35 | res += to_string(x); 36 | } 37 | res += "}"; 38 | return res; 39 | } 40 | void debug_out() { cerr << endl; } 41 | template 42 | void debug_out(Head H, Tail... T) { 43 | cerr << " " << to_string(H); 44 | debug_out(T...); 45 | } 46 | 47 | #ifndef ONLINE_JUDGE 48 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 49 | #else 50 | #define debug(...) 42 51 | #endif 52 | 53 | #define f(i, x, n) for (int i = x; i < n; i++) 54 | #define all(x) (x).begin(), (x).end() 55 | #define rall(x) (x).rbegin(), (x).rend() 56 | #define F first 57 | #define S second 58 | #define pb push_back 59 | #define endl "\n" 60 | #define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) 61 | 62 | typedef long long int ll; 63 | #define int ll 64 | typedef pair pll; 65 | typedef vector> matrix; 66 | typedef vector vll; 67 | 68 | const ll mod = 1e9 + 7; 69 | const ll inf = LLONG_MAX; 70 | const ll N = 1e5 + 10; 71 | 72 | int32_t main() { 73 | fast_io(); 74 | 75 | int n, m, d, res = 0; cin >> n >> m; d = m + 2; 76 | string s[n+2] , b; 77 | 78 | while (d--) b += '.'; 79 | 80 | s[0] = b; s[n+1] = b; 81 | f (i, 1, n+1) { 82 | s[i] += '.'; 83 | string temp; cin >> temp; 84 | s[i] += temp; s[i] += '.'; 85 | } 86 | 87 | f (i, 1, n+1) { 88 | f (j, 1, m+1) { 89 | if (s[i][j] == 'W') { 90 | if (s[i][j-1] == 'P') { res++; s[i][j-1] = '.';continue; } 91 | if (s[i-1][j] == 'P') { res++; s[i-1][j] = '.';continue; } 92 | if (s[i][j+1] == 'P') { res++; s[i][j+1] = '.';continue; } 93 | if (s[i+1][j] == 'P') { res++; s[i+1][j] = '.';continue; } 94 | } 95 | } 96 | } 97 | 98 | cout << res; 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1393B.cpp: -------------------------------------------------------------------------------- 1 | // link - https://codeforces.com/problemset/problem/1393/B 2 | // AUTHOR : SHIVANSH 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | int main() 10 | { 11 | int n; 12 | scanf("%d", &n); 13 | int arr[n + 1]; 14 | unordered_map hr; 15 | unordered_map::iterator it; 16 | for (int i = 1; i <= n; i++) 17 | { 18 | scanf("%d", &arr[i]); 19 | hr[arr[i]]++; 20 | } 21 | pair a; 22 | set> st; 23 | set>::reverse_iterator ti; 24 | for (it = hr.begin(); it != hr.end(); it++) 25 | { 26 | a.second = (*it).first; 27 | a.first = (*it).second; 28 | st.insert(a); 29 | } 30 | int q; 31 | scanf("%d", &q); 32 | while (q--) 33 | { 34 | char p; 35 | int b; 36 | pair e, u, v, s; 37 | getchar(); 38 | scanf("%c %d", &p, &b); 39 | if (p == '+') 40 | { 41 | e.first = hr[b]; 42 | e.second = b; 43 | st.erase(e); 44 | hr[b]++; 45 | e.first = hr[b]; 46 | e.second = b; 47 | st.insert(e); 48 | } 49 | else 50 | { 51 | e.first = hr[b]; 52 | e.second = b; 53 | st.erase(e); 54 | hr[b]--; 55 | e.first = hr[b]; 56 | e.second = b; 57 | st.insert(e); 58 | } 59 | u = *(st.rbegin()); 60 | ti = st.rbegin(); 61 | if (st.size() >= 2) 62 | { 63 | ti++; 64 | v = *ti; 65 | } 66 | if (st.size() >= 3) 67 | { 68 | ti++; 69 | s = *ti; 70 | } 71 | if (u.first >= 8) 72 | { 73 | printf("YES\n"); 74 | } 75 | else 76 | { 77 | if (st.size() >= 2) 78 | { 79 | if (u.first >= 4) 80 | { 81 | if (u.first >= 6 && v.first >= 2) 82 | { 83 | printf("YES\n"); 84 | } 85 | else if (v.first >= 4) 86 | { 87 | printf("YES\n"); 88 | } 89 | else 90 | { 91 | if (st.size()>= 3 && s.first >= 2) 92 | { 93 | printf("YES\n"); 94 | } 95 | else 96 | { 97 | printf("NO\n"); 98 | } 99 | } 100 | } 101 | else 102 | { 103 | printf("NO\n"); 104 | } 105 | } 106 | else 107 | { 108 | printf("NO\n"); 109 | } 110 | } 111 | } 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1408.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1408/problem/C 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(0); 9 | int tt; 10 | cin >> tt; 11 | while (tt--) { 12 | int n, l; 13 | cin >> n >> l; 14 | vector a(n); 15 | for (int i = 0; i < n; i++) { 16 | cin >> a[i]; 17 | } 18 | double low = 0; 19 | double high = l; 20 | for (int it = 0; it < 100; it++) { 21 | double mid = 0.5 * (low + high); 22 | double total = 0; 23 | { 24 | double t = mid; 25 | double x = 0; 26 | double v = 1; 27 | for (int i = 0; i < n; i++) { 28 | double until = (a[i] - x) / v; 29 | if (until <= t) { 30 | t -= until; 31 | x = a[i]; 32 | v += 1; 33 | continue; 34 | } 35 | break; 36 | } 37 | x += v * t; 38 | total += x; 39 | } 40 | { 41 | double t = mid; 42 | double x = l; 43 | double v = 1; 44 | for (int i = n - 1; i >= 0; i--) { 45 | double until = (x - a[i]) / v; 46 | if (until <= t) { 47 | t -= until; 48 | x = a[i]; 49 | v += 1; 50 | continue; 51 | } 52 | break; 53 | } 54 | x -= v * t; 55 | total += l - x; 56 | } 57 | if (total >= l) { 58 | high = mid; 59 | } else { 60 | low = mid; 61 | } 62 | } 63 | cout << fixed << setprecision(6) << 0.5 * (low + high) << '\n'; 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1538B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1538/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | long long int t; 7 | cin >> t; 8 | while(t--){ 9 | long long int n; 10 | cin >> n; 11 | vector v; 12 | long long int i, div = 0; 13 | for(i = 0; i < n; i++){ 14 | long long int x; 15 | cin >> x; 16 | v.push_back(x); 17 | div += x; 18 | } 19 | if(div % n != 0){ 20 | cout << "-1\n"; 21 | continue; 22 | } 23 | div /= n; 24 | sort(v.begin(), v.end()); 25 | long long int need = 0; 26 | for(auto value: v){ 27 | if(div - value > 0) need += div - value; 28 | else break; 29 | } 30 | auto it = v.end(); 31 | long long int total = 0, count = 0; 32 | while(total - count * div < need){ 33 | it--; 34 | count++; 35 | total += *it; 36 | } 37 | cout << count << endl; 38 | } 39 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1547B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1547/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | long long t; 7 | cin >> t; 8 | while(t--){ 9 | string s; 10 | cin >> s; 11 | vector v; 12 | for(auto letter: s) v.push_back(letter); 13 | char prev = '0'; 14 | while(v.begin() != v.end()){ 15 | vector :: iterator max = v.end(); 16 | max--; 17 | if(*(v.begin()) > *max) max = v.begin(); 18 | if(prev != '0' && *max != prev - 1){ 19 | cout << "NO\n"; 20 | break; 21 | } 22 | prev = *max; 23 | v.erase(max); 24 | } 25 | if(v.begin() == v.end()){ 26 | if(*(v.begin()) == 'a') cout << "YES\n"; 27 | else cout << "NO\n"; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1549B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1549/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | long long t; 7 | cin >> t; 8 | while(t--){ 9 | long long n, i, c = 0; 10 | cin >> n; 11 | string s1, s2; 12 | cin >> s1 >> s2; 13 | for(i = 0; i < n; i++){ 14 | if(s2[i] == '1'){ 15 | if(s1[i] == '0') c++; 16 | else if(i != 0 && s1[i - 1] == '1'){ 17 | s1[i - 1] = '0'; 18 | c++; 19 | } 20 | else if(i != n - 1 && s1[i + 1] == '1'){ 21 | s1[i + 1] = '0'; 22 | c++; 23 | } 24 | } 25 | } 26 | cout << c << endl; 27 | } 28 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1560B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1560/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | long long int t; 7 | cin >> t; 8 | while(t--){ 9 | long long a, b, c, n; 10 | cin >> a >> b >> c; 11 | n = (b - a) * 2; 12 | if(n < 0) n *= -1; 13 | if(b > n || a > n || c > n){ 14 | cout << "-1" << endl; 15 | } 16 | else{ 17 | long long x = c - n / 2; 18 | x > 0 ? cout << x << endl : cout << n + x << endl; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1560C.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1560/problem/C 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int t; 7 | cin >> t; 8 | while(t--){ 9 | long long k, i = 1, sum = 1; 10 | cin >> k; 11 | while(k > i * i){ 12 | sum += 2 * i; 13 | // cout << "added " << 2 * i << endl; 14 | i++; 15 | } 16 | // cout << "sum is " << sum << endl; 17 | if(k < sum){ 18 | cout << i - (sum - k) << " " << i << endl; 19 | } 20 | else{ 21 | cout << i << " " << i - (k - sum) << endl; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1579D.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1579/problem/D 2 | // Author: master._.mind 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | 6 | // -------------------------------------------------------- 7 | /* 8 | #pragma GCC optimize("Ofast,unroll-loops") 9 | #pragma GCC target("avx,avx2,fma") 10 | */ 11 | // ------------------------------------------------------- 12 | 13 | // ----------------------------------------------------------------- 14 | #include 15 | #include 16 | using namespace __gnu_pbds; 17 | template 18 | using o_set = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 19 | template 20 | using o_multiset = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 21 | template > 22 | using o_map = __gnu_pbds::tree; 23 | //member functions : 24 | // 1. order_of_key (val): returns the no. of values less than val 25 | // 2. find_by_order (k): returns the iterator to kth largest element.(0-based) 26 | // for ordered_multiset, lower_bound and upper_bound swap roles 27 | // ---------------------------------------------------------------- 28 | 29 | // ------------------------------------------------- 30 | using ll = int64_t; 31 | using db = double; 32 | using str = string; 33 | using ull = unsigned long long; 34 | #define fo(i, n) for (ll i = 0; i < n; i++) 35 | #define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--) 36 | #define set_bits(x) __builtin_popcountll(x) 37 | #define eb emplace_back 38 | #define ff first 39 | #define ss second 40 | #define all(x) (x).begin(), (x).end() 41 | #define uniq(x) (x).erase(unique(all(x)), (x).end()) 42 | #define rall(x) (x).rbegin(), (x).rend() 43 | #define ps(x, y) fixed << setprecision(y) << x 44 | #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) 45 | #define sz(x) ((ll)(x).size()) 46 | template 47 | using V = vector; 48 | using vl = V; 49 | using vvl = V; 50 | template 51 | using P = pair; 52 | using pl = P; 53 | using vpl = V; 54 | // ------------------------------------------------ 55 | 56 | // --------------------------------------------------------------- 57 | const ll mod = 1e9 + 7; //1000000007 58 | const ll mod2 = 998244353; 59 | const ll inf = LLONG_MAX; 60 | const db eps = 1e-12; 61 | const double PI = acos(-1); 62 | // -------------------------------------------------------------- 63 | 64 | // ------------------------------------------------------------ 65 | template 66 | bool amin(T& a, U&& b) { 67 | return b < a ? a = std::forward(b), true : false; 68 | } 69 | template 70 | bool amax(T& a, U&& b) { 71 | return a < b ? a = std::forward(b), true : false; 72 | } 73 | // ----------------------------------------------------------- 74 | 75 | // ---------------------------------------------------------------- 76 | 77 | void Solution() { 78 | ll n, num; 79 | cin >> n; 80 | set> a; 81 | fo(i, n) { 82 | cin >> num; 83 | if (num == 0) continue; 84 | a.insert({num, i + 1}); 85 | } 86 | vector> ans; 87 | while (sz(a) > 1) { 88 | pair f = *a.begin(); 89 | pair b = *a.rbegin(); 90 | a.erase(f); 91 | a.erase(b); 92 | f.first -= 1; 93 | b.first -= 1; 94 | // cout << f.second << ' ' << b.second << '\n'; 95 | ans.push_back({f.second, b.second}); 96 | if (f.first) a.insert(f); 97 | if (b.first) a.insert(b); 98 | } 99 | cout << sz(ans) << '\n'; 100 | for (auto [x, y] : ans) cout << x << ' ' << y << '\n'; 101 | } 102 | 103 | // --------------------------------------------------------------- 104 | 105 | int main() { 106 | #ifndef ONLINE_JUDGE 107 | freopen("input.txt", "r", stdin); 108 | freopen("output.txt", "w", stdout); 109 | #endif 110 | cin.tie(nullptr)->sync_with_stdio(false); 111 | #ifdef NCR 112 | init(); 113 | #endif 114 | ll tc = 1; 115 | cin >> tc; 116 | while (tc--) { 117 | Solution(); 118 | } 119 | #ifndef ONLINE_JUDGE 120 | cerr << fixed << setprecision(4) << (double)clock() / CLOCKS_PER_SEC << " secs" << endl; 121 | #endif 122 | return 0; 123 | } 124 | 125 | /* 126 | ? Stuff to look for -> 127 | * stay organised 128 | * int overflows, array bounds, etc. 129 | * special cases (n=1)? 130 | * do something instead of nothing 131 | * n % mod = (n % mod + mod) % mod; (Handling negatives) 132 | * When using a set, lower_bound(all(set),l) is slower than set.lower_bound(l) because of random iterators 133 | * string .append() or += is O1, but s = s + s is On (as it creates a copy first), use wisely 134 | * DONT GET STUCK ON ONE APPROACH 135 | * use __lg(n) instead of log2(n), int: 32 - __builtin_clz(n), ll: 63 - __builtin_clzll(n), https://codeforces.com/blog/entry/45966 136 | * string.rfind() finds first occurence from end 137 | * (a & b) + (a | b) = a + b 138 | * TLE due to ll ? or multiple copies of large DS? 139 | */ 140 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1579E1.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1579/problem/E1 2 | // Author: master._.mind 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | 6 | // -------------------------------------------------------- 7 | /* 8 | #pragma GCC optimize("Ofast,unroll-loops") 9 | #pragma GCC target("avx,avx2,fma") 10 | */ 11 | // ------------------------------------------------------- 12 | 13 | // ----------------------------------------------------------------- 14 | #include 15 | #include 16 | using namespace __gnu_pbds; 17 | template 18 | using o_set = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 19 | template 20 | using o_multiset = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 21 | template > 22 | using o_map = __gnu_pbds::tree; 23 | //member functions : 24 | // 1. order_of_key (val): returns the no. of values less than val 25 | // 2. find_by_order (k): returns the iterator to kth largest element.(0-based) 26 | // for ordered_multiset, lower_bound and upper_bound swap roles 27 | // ---------------------------------------------------------------- 28 | 29 | // ------------------------------------------------- 30 | using ll = int64_t; 31 | using db = double; 32 | using str = string; 33 | using ull = unsigned long long; 34 | #define fo(i, n) for (ll i = 0; i < n; i++) 35 | #define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--) 36 | #define set_bits(x) __builtin_popcountll(x) 37 | #define eb emplace_back 38 | #define ff first 39 | #define ss second 40 | #define all(x) (x).begin(), (x).end() 41 | #define uniq(x) (x).erase(unique(all(x)), (x).end()) 42 | #define rall(x) (x).rbegin(), (x).rend() 43 | #define ps(x, y) fixed << setprecision(y) << x 44 | #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) 45 | #define sz(x) ((ll)(x).size()) 46 | template 47 | using V = vector; 48 | using vl = V; 49 | using vvl = V; 50 | template 51 | using P = pair; 52 | using pl = P; 53 | using vpl = V; 54 | // ------------------------------------------------ 55 | 56 | // --------------------------------------------------------------- 57 | const ll mod = 1e9 + 7; //1000000007 58 | const ll mod2 = 998244353; 59 | const ll inf = LLONG_MAX; 60 | const db eps = 1e-12; 61 | const double PI = acos(-1); 62 | // -------------------------------------------------------------- 63 | 64 | // ------------------------------------------------------------ 65 | template 66 | bool amin(T& a, U&& b) { 67 | return b < a ? a = std::forward(b), true : false; 68 | } 69 | template 70 | bool amax(T& a, U&& b) { 71 | return a < b ? a = std::forward(b), true : false; 72 | } 73 | // ----------------------------------------------------------- 74 | 75 | // ---------------------------------------------------------------- 76 | 77 | void Solution() { 78 | ll n; 79 | cin >> n; 80 | deque dq; 81 | ll num; 82 | fo(i, n) { 83 | cin >> num; 84 | if (dq.empty()) 85 | dq.push_back(num); 86 | else { 87 | if (num < dq.front()) { 88 | dq.push_front(num); 89 | } else 90 | dq.push_back(num); 91 | } 92 | } 93 | fo(i, n) cout << dq[i] << ' '; 94 | cout << '\n'; 95 | } 96 | 97 | // --------------------------------------------------------------- 98 | 99 | int main() { 100 | #ifndef ONLINE_JUDGE 101 | freopen("input.txt", "r", stdin); 102 | freopen("output.txt", "w", stdout); 103 | #endif 104 | cin.tie(nullptr)->sync_with_stdio(false); 105 | #ifdef NCR 106 | init(); 107 | #endif 108 | ll tc = 1; 109 | cin >> tc; 110 | while (tc--) { 111 | Solution(); 112 | } 113 | #ifndef ONLINE_JUDGE 114 | cerr << fixed << setprecision(4) << (double)clock() / CLOCKS_PER_SEC << " secs" << endl; 115 | #endif 116 | return 0; 117 | } 118 | 119 | /* 120 | ? Stuff to look for -> 121 | * stay organised 122 | * int overflows, array bounds, etc. 123 | * special cases (n=1)? 124 | * do something instead of nothing 125 | * n % mod = (n % mod + mod) % mod; (Handling negatives) 126 | * When using a set, lower_bound(all(set),l) is slower than set.lower_bound(l) because of random iterators 127 | * string .append() or += is O1, but s = s + s is On (as it creates a copy first), use wisely 128 | * DONT GET STUCK ON ONE APPROACH 129 | * use __lg(n) instead of log2(n), int: 32 - __builtin_clz(n), ll: 63 - __builtin_clzll(n), https://codeforces.com/blog/entry/45966 130 | * string.rfind() finds first occurence from end 131 | * (a & b) + (a | b) = a + b 132 | * TLE due to ll ? or multiple copies of large DS? 133 | */ 134 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1579E2.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1579/problem/E2 2 | // Author: jesuswr 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | using namespace std; 21 | 22 | typedef long long ll; 23 | typedef long double ld; 24 | typedef pair pii; 25 | typedef pair> piii; 26 | typedef vector vi; 27 | typedef vector vii; 28 | 29 | int dadsadasda; 30 | 31 | #define ri(a) dadsadasda=scanf("%d", &a) 32 | #define rii(a,b) dadsadasda=scanf("%d %d", &a, &b) 33 | #define riii(a,b,c) dadsadasda=scanf("%d %d %d", &a, &b, &c) 34 | #define rl(a) dadsadasda=scanf("%lld", &a) 35 | #define rll(a,b) dadsadasda=scanf("%lld %lld", &a, &b) 36 | #define FOR(i,n,m) for(int i=n; im; i--) 38 | #define pb push_back 39 | #define lb lower_bound 40 | #define ub upper_bound 41 | #define F first 42 | #define S second 43 | #define ALL(s) s.begin(),s.end() 44 | #define SZ(s) (int)s.size() 45 | 46 | const int INF = 0x3f3f3f3f; 47 | const ll LLINF = 1e18; 48 | const int MAXN = 2e5 + 69; 49 | 50 | // Segment tree for sum in a range 51 | int N, A[MAXN], B[MAXN]; 52 | struct STN { 53 | int sm = 0; 54 | void merge(STN& L, STN& R) { 55 | sm = L.sm + R.sm; 56 | } 57 | void operator=(int a) { 58 | sm += a; 59 | } 60 | }; 61 | STN ST[4 * MAXN]; 62 | void STB(int id = 1, int l = 0, int r = N) { 63 | if (r - l < 2) { 64 | ST[id].sm = 0; 65 | return; 66 | } 67 | int m = (l + r) >> 1, L = id << 1, R = L | 1; 68 | STB(L, l, m); STB(R, m, r); 69 | ST[id].merge(ST[L], ST[R]); 70 | } 71 | STN STQ(int x, int y, int id = 1, int l = 0, int r = N) { 72 | if (x == l && y == r) return ST[id]; 73 | int m = (l + r) >> 1, L = id << 1, R = L | 1; 74 | if (x >= m) return STQ(x, y, R, m, r); 75 | if (y <= m) return STQ(x, y, L, l, m); 76 | STN res, ln = STQ(x, m, L, l, m), rn = STQ(m, y, R, m, r); 77 | return res.merge(ln, rn), res; 78 | } 79 | void STU(int p, int x, int id = 1, int l = 0, int r = N) { 80 | if (r - l < 2) { 81 | ST[id] = x; 82 | return; 83 | } 84 | int m = (l + r) >> 1, L = id << 1, R = L | 1; 85 | if (p < m) STU(p, x, L, l, m); 86 | else STU(p, x, R, m, r); 87 | ST[id].merge(ST[L], ST[R]); 88 | } 89 | 90 | // compress coordinates because the only important thing is that they remain is their relative orders, 91 | // for example, we care that a > b, the values of a and b are not important, so we can make them smaller 92 | // so we can use the segment tree 93 | void comp() { 94 | FOR(i, 0, N) B[i] = A[i]; 95 | sort(B, B + N); 96 | FOR(i, 0, N) A[i] = (lb(B, B + N, A[i]) - B) + 1; 97 | } 98 | 99 | void solve() { 100 | ri(N); 101 | FOR(i, 0, N) ri(A[i]); 102 | comp(); 103 | int M = N; N += 10; 104 | STB(); 105 | ll ans = 0; 106 | FOR(i, 0, M) { 107 | // choose to put the current element before or after the previous ones, 108 | ans += min(STQ(0, A[i]).sm, STQ(A[i] + 1, N).sm); 109 | STU(A[i], 1); 110 | } 111 | printf("%lld\n", ans); 112 | } 113 | 114 | int main() { 115 | int t; ri(t); 116 | while (t--) solve(); 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1581A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1581/problem/A 2 | // Author: master._.mind 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | using ll = int64_t; 6 | 7 | #define NCR 8 | const ll N = 200043; 9 | ll MOD = 1e9 + 7; 10 | ll M(ll x) { return ((x % MOD) + MOD) % MOD; } 11 | ll add(ll x, ll y) { return (M(x) + M(y)) % MOD; } 12 | ll mul(ll x, ll y) { return (M(x) * M(y)) % MOD; } 13 | ll fact[N]; 14 | ll modpow(ll x, ll y, ll m) { 15 | if (y == 0) return 1 % m; 16 | ll u = modpow(x, y / 2, m); 17 | u = (u * u) % m; 18 | if (y % 2 == 1) u = (u * x) % m; 19 | return u; 20 | } 21 | // a**(p - 1) % p == 1 22 | // a*(a**(p - 2)) % p == 1 23 | // a**(p - 2) % p == 1 / a 24 | ll inv(ll a, ll mod) { return modpow(a, mod - 2, mod); } //Fermats little theorem 25 | void init() { 26 | fact[0] = 1; 27 | for (ll i = 1; i < N; i++) fact[i] = (fact[i - 1] * i) % MOD; 28 | } 29 | ll C(ll a, ll b) { 30 | if (a < b) return 0; 31 | return mul(mul(fact[a], inv(fact[b], MOD)), inv(fact[a - b], MOD)); 32 | } 33 | 34 | void Solution() { 35 | ll n; 36 | cin >> n; 37 | cout << mul(fact[2 * n], inv(2LL, MOD)) << '\n'; // (2n)!/2 38 | } 39 | 40 | int main() { 41 | cin.tie(nullptr)->sync_with_stdio(false); 42 | #ifndef ONLINE_JUDGE 43 | freopen("input.txt", "r", stdin); 44 | freopen("output.txt", "w", stdout); 45 | #endif 46 | init(); 47 | ll tc = 1; 48 | cin >> tc; 49 | for (ll i = 1; i <= tc; ++i) { 50 | Solution(); 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1582F1.cpp: -------------------------------------------------------------------------------- 1 | // Author: prashant_th18 2 | #ifdef LOCAL 3 | #define _GLIBCXX_DEBUG 4 | #endif 5 | #include "bits/stdc++.h" 6 | using namespace std; 7 | typedef long long ll; 8 | #define sz(s) ((int)s.size()) 9 | #define all(v) begin(v), end(v) 10 | 11 | typedef long double ld; 12 | const int MOD = 1000000007; 13 | #define ff first 14 | #define ss second 15 | 16 | mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | // *-> KISS* 19 | int solve() { 20 | int n; cin >> n; 21 | vector v(n); 22 | for(int i = 0; i < n; ++i) cin >> v[i]; 23 | vector ans(513, false); 24 | vector> dp(2, vector(513, 1e6)); 25 | // dp[i][j] -> If we consider values from a[1] to a[i], and we have a sub-sequence whose xor is j 26 | // then what is the minimum last value for that subsequence 27 | dp[0][0] = 0; ans[0] = true; 28 | for(int i = 1; i <= n; ++i) { 29 | for(int j = 0; j < 512; ++j) { 30 | // j banana hai 31 | // j ^ v[i - 1] 32 | dp[i % 2][j] = min(dp[i % 2][j], dp[(i - 1) % 2][j]); 33 | if(dp[(i - 1) % 2][j ^ v[i - 1]] < v[i - 1]) { 34 | dp[i % 2][j] = min(dp[i % 2][j], v[i - 1]); 35 | ans[j] = true; 36 | } 37 | } 38 | } 39 | int t = 0; for(int i = 0; i < 512; ++i) t += ans[i]; 40 | cout << t << '\n'; 41 | for(int i = 0; i < 512; ++i) { 42 | if(ans[i]) cout << i << ' '; 43 | } 44 | return 0; 45 | } 46 | int32_t main() { 47 | ios::sync_with_stdio(0); 48 | cin.tie(0); 49 | bool test = false; 50 | int TET = 1; 51 | if(test) cin >> TET; 52 | cout << fixed << setprecision(6); 53 | for (int i = 1; i <= TET; i++) { 54 | #ifdef LOCAL 55 | cout << "##################" << '\n'; 56 | #endif 57 | 58 | if (solve()) { 59 | break; 60 | } 61 | cout << '\n'; 62 | } 63 | #ifdef LOCAL 64 | cout << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl; 65 | #endif 66 | return 0; 67 | } 68 | // -> Keep It Simple Stupid! 69 | 70 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1592A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1592/problem/A 2 | // Author: master._.mind 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | // -------------------------------------------------------- 6 | /* 7 | #pragma GCC optimize("Ofast,unroll-loops") 8 | #pragma GCC target("avx,avx2,fma") 9 | */ 10 | // ------------------------------------------------------- 11 | 12 | // ----------------------------------------------------------------- 13 | #include 14 | #include 15 | using namespace __gnu_pbds; 16 | template 17 | using o_set = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 18 | template 19 | using o_multiset = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 20 | template > 21 | using o_map = __gnu_pbds::tree; 22 | //member functions : 23 | // 1. order_of_key (val): returns the no. of values less than val 24 | // 2. find_by_order (k): returns the iterator to kth largest element.(0-based) 25 | // for ordered_multiset, lower_bound and upper_bound swap roles 26 | // ---------------------------------------------------------------- 27 | 28 | // ------------------------------------------------- 29 | using ll = int64_t; 30 | using db = double; 31 | using str = string; 32 | using ull = unsigned long long; 33 | #define fo(i, n) for (ll i = 0; i < n; i++) 34 | #define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--) 35 | #define set_bits(x) __builtin_popcountll(x) 36 | #define eb emplace_back 37 | #define ff first 38 | #define ss second 39 | #define all(x) (x).begin(), (x).end() 40 | #define uniq(x) (x).erase(unique(all(x)), (x).end()) 41 | #define rall(x) (x).rbegin(), (x).rend() 42 | #define ps(x, y) fixed << setprecision(y) << x 43 | #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) 44 | #define sz(x) ((ll)(x).size()) 45 | template 46 | using V = vector; 47 | using vl = V; 48 | using vvl = V; 49 | template 50 | using P = pair; 51 | using pl = P; 52 | using vpl = V; 53 | // ------------------------------------------------ 54 | 55 | // --------------------------------------------------------------- 56 | const ll mod = 1e9 + 7; //1000000007 57 | const ll mod2 = 998244353; 58 | const ll inf = LLONG_MAX; 59 | const db eps = 1e-12; 60 | const double PI = acos(-1); 61 | // -------------------------------------------------------------- 62 | 63 | // ------------------------------------------------------------ 64 | template 65 | bool amin(T& a, U&& b) { 66 | return b < a ? a = std::forward(b), true : false; 67 | } 68 | template 69 | bool amax(T& a, U&& b) { 70 | return a < b ? a = std::forward(b), true : false; 71 | } 72 | // ----------------------------------------------------------- 73 | 74 | // ---------------------------------------------------------------- 75 | 76 | void Solution() { 77 | ll n, H; 78 | cin >> n >> H; 79 | vl a(n); 80 | fo(i, n) { 81 | cin >> a[i]; 82 | } 83 | sort(all(a)); 84 | ll times = 0; 85 | ll sum = a[sz(a) - 1] + a[sz(a) - 2]; 86 | ll ans = 0; 87 | if (H > sum) { 88 | times = H / sum; 89 | H -= times * sum; 90 | ans = 2 * times; 91 | } 92 | if (H > 0) H -= a[sz(a) - 1], ans++; 93 | if (H > 0) H -= a[sz(a) - 2], ans++; 94 | if (H > 0) H -= a[sz(a) - 1], ans++; 95 | if (H > 0) H -= a[sz(a) - 2], ans++; 96 | if (H > 0) H -= a[sz(a) - 1], ans++; 97 | if (H > 0) H -= a[sz(a) - 2], ans++; 98 | if (H > 0) H -= a[sz(a) - 1], ans++; 99 | cout << ans << '\n'; 100 | } 101 | 102 | // --------------------------------------------------------------- 103 | 104 | int main() { 105 | #ifndef ONLINE_JUDGE 106 | freopen("input.txt", "r", stdin); 107 | freopen("output.txt", "w", stdout); 108 | #endif 109 | cin.tie(nullptr)->sync_with_stdio(false); 110 | #ifdef NCR 111 | init(); 112 | #endif 113 | ll tc = 1; 114 | cin >> tc; 115 | while (tc--) { 116 | Solution(); 117 | } 118 | #ifndef ONLINE_JUDGE 119 | cerr << fixed << setprecision(4) << (double)clock() / CLOCKS_PER_SEC << " secs" << endl; 120 | #endif 121 | return 0; 122 | } 123 | 124 | /* 125 | ? Stuff to look for -> 126 | * stay organised 127 | * int overflows, array bounds, etc. 128 | * special cases (n=1)? 129 | * do something instead of nothing 130 | * n % mod = (n % mod + mod) % mod; (Handling negatives) 131 | * When using a set, lower_bound(all(set),l) is slower than set.lower_bound(l) because of random iterators 132 | * string .append() or += is O1, but s = s + s is On (as it creates a copy first), use wisely 133 | * DONT GET STUCK ON ONE APPROACH 134 | * use __lg(n) instead of log2(n), int: 32 - __builtin_clz(n), ll: 63 - __builtin_clzll(n), https://codeforces.com/blog/entry/45966 135 | * string.rfind() finds first occurence from end 136 | * (a & b) + (a | b) = a + b 137 | * TLE due to ll ? or multiple copies of large DS? 138 | * didnt use inv() in modular combinatorics? 139 | */ 140 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1593D1.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1593/problem/D1 2 | // Author: master._.mind 3 | #include "bits/stdc++.h" 4 | using namespace std; 5 | #define dbg(...) 6 | 7 | // -------------------------------------------------------- 8 | 9 | // #pragma GCC optimize("Ofast,unroll-loops") 10 | // #pragma GCC target("avx,avx2,fma") 11 | 12 | // ------------------------------------------------------- 13 | 14 | // ----------------------------------------------------------------- 15 | #include 16 | #include 17 | using namespace __gnu_pbds; 18 | template 19 | using o_set = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 20 | template 21 | using o_multiset = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; 22 | template > 23 | using o_map = __gnu_pbds::tree; 24 | //member functions : 25 | // 1. order_of_key (val): returns the no. of values less than val 26 | // 2. find_by_order (k): returns the iterator to kth largest element.(0-based) 27 | // for ordered_multiset, lower_bound and upper_bound swap roles 28 | // ---------------------------------------------------------------- 29 | 30 | // ------------------------------------------------- 31 | using ll = int64_t; 32 | using db = double; 33 | using ld = long double; 34 | using str = string; 35 | using ull = unsigned long long; 36 | #define fo(i, n) for (ll i = 0; i < n; i++) 37 | #define set_bits(x) __builtin_popcountll(x) 38 | #define eb emplace_back 39 | #define ff first 40 | #define ss second 41 | #define all(x) (x).begin(), (x).end() 42 | #define uniq(x) (x).erase(unique(all(x)), (x).end()) 43 | #define rall(x) (x).rbegin(), (x).rend() 44 | #define ps(x, y) fixed << setprecision(y) << x 45 | #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) 46 | #define sz(x) ((ll)(x).size()) 47 | template 48 | using V = vector; 49 | template 50 | using P = pair; 51 | // ------------------------------------------------ 52 | 53 | // ------------------------------------------------------------- 54 | struct custom_hash { 55 | static uint64_t splitmix64(uint64_t x) { 56 | x += 0x9e3779b97f4a7c15; 57 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 58 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 59 | return x ^ (x >> 31); 60 | } 61 | size_t operator()(uint64_t x) const { 62 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 63 | return splitmix64(x + FIXED_RANDOM); 64 | } 65 | }; 66 | template // Key should be integer type 67 | using safe_map = unordered_map; 68 | // ------------------------------------------------------------ 69 | 70 | // --------------------------------------------------------------- 71 | constexpr ll mod = 1e9 + 7; //1000000007 72 | constexpr ll mod2 = 998244353; 73 | constexpr ll inf = LLONG_MAX; 74 | constexpr db eps = 1e-12; 75 | const double PI = acos(-1); 76 | // -------------------------------------------------------------- 77 | 78 | // ------------------------------------------------------------ 79 | template 80 | bool amin(T &a, U &&b) { return b < a ? a = std::forward(b), true : false; } 81 | template 82 | bool amax(T &a, U &&b) { return a < b ? a = std::forward(b), true : false; } 83 | // ----------------------------------------------------------- 84 | 85 | // ---------------------------------------------------------------- 86 | // clang-format on 87 | 88 | void Solution() { 89 | ll n; 90 | cin >> n; 91 | V a(n); 92 | fo(i, n) cin >> a[i]; 93 | ll mn = *min_element(all(a)); 94 | V diff; 95 | fo(i, n) { 96 | ll t = a[i] - mn; 97 | if (t) diff.push_back(t); 98 | } 99 | if (diff.empty()) { 100 | cout << -1 << '\n'; 101 | return; 102 | } 103 | ll g = diff.back(); 104 | diff.pop_back(); 105 | if (!diff.empty()) 106 | for (auto &x : diff) g = gcd(g, x); 107 | cout << g << '\n'; 108 | } 109 | 110 | // --------------------------------------------------------------- 111 | 112 | // clang-format off 113 | int32_t main() { 114 | cin.tie(nullptr)->sync_with_stdio(false); 115 | #ifndef ONLINE_JUDGE 116 | freopen("input.txt", "r", stdin); 117 | freopen("output.txt", "w", stdout); 118 | #endif 119 | cout << fixed << setprecision(12); 120 | #ifdef NCR 121 | init(); 122 | #endif 123 | ll tc = 1; cin >> tc; while (tc--) 124 | Solution(); 125 | cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; 126 | return 0; 127 | } 128 | 129 | /* 130 | ? Stuff to look for -> 131 | * stay organised 132 | * int overflows, array bounds, etc. 133 | * special cases (n=1)? 134 | * do something instead of nothing 135 | * n % mod = (n % mod + mod) % mod; (Handling negatives) 136 | * When using a set, lower_bound(all(set),l) is slower than set.lower_bound(l) because of random iterators 137 | * string .append() or += is O1, but s = s + s is On (as it creates a copy first), use wisely 138 | * DONT GET STUCK ON ONE APPROACH 139 | * use __lg(n) instead of log2(n), int: 32 - __builtin_clz(n), ll: 63 - __builtin_clzll(n), https://codeforces.com/blog/entry/45966 140 | * string.rfind() finds first occurence from end 141 | * (a & b) + (a | b) = a + b 142 | * TLE due to ll ? or multiple copies of large DS? 143 | * didnt use inv() in modular combinatorics? 144 | */ 145 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1600J.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1600/problem/J 2 | // Author: master._.mind 3 | #include 4 | using namespace std; 5 | using ll = int64_t; 6 | 7 | int main() { 8 | #ifndef ONLINE_JUDGE 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | #endif 12 | cin.tie(nullptr)->sync_with_stdio(false); 13 | 14 | ll n, m; 15 | cin >> n >> m; 16 | vector> adj(n, vector(m)), vis(n, vector(m, 0)); 17 | vector rooms; 18 | for (ll i = 0; i < n; ++i) { 19 | for (ll j = 0; j < m; ++j) { 20 | cin >> adj[i][j]; 21 | } 22 | } 23 | 24 | auto valid = [&](ll x, ll y) -> bool { 25 | if (x < 0 || y < 0 || x >= n || y >= m) return false; 26 | if (vis[x][y]) return false; 27 | return true; 28 | }; 29 | 30 | ll ctr = 0; 31 | ll dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; 32 | function dfs = [&](ll i, ll j) -> void { 33 | vis[i][j] = 1; 34 | ctr++; 35 | for (ll X = 0; X < 4; ++X) { 36 | ll mask = (1LL << X); 37 | if (!(adj[i][j] & mask)) { // not a wall 38 | ll ni = i + dx[X]; 39 | ll nj = j + dy[X]; 40 | if (!valid(ni, nj)) continue; 41 | dfs(ni, nj); 42 | } 43 | } 44 | }; 45 | 46 | for (ll i = 0; i < n; ++i) { 47 | for (ll j = 0; j < m; ++j) { 48 | if (vis[i][j]) continue; 49 | ctr = 0; 50 | dfs(i, j); 51 | if (ctr) rooms.push_back(ctr); 52 | } 53 | } 54 | 55 | sort(rooms.begin(), rooms.end(), greater()); 56 | for (ll &x : rooms) cout << x << ' '; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1692B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1692/problem/B 2 | // Author: darkwhite0 3 | #include 4 | int countDistinct(int a[], int n) 5 | { 6 | int i, j, count = 1; 7 | for (i = 1; i < n; i++) 8 | { 9 | for (j = 0; j < i; j++) 10 | { 11 | if (a[i] == a[j]) 12 | { 13 | break; 14 | } 15 | } 16 | if (i == j) 17 | { 18 | count++; 19 | } 20 | } 21 | return count; 22 | } 23 | int main(){ 24 | int t, i, n, j, x; 25 | scanf("%d", &t); 26 | for(i = 0; i < t; i++){ 27 | scanf("%d", &n); 28 | int arr[n]; 29 | for(j = 0; j < n; j++){ 30 | scanf("%d", &arr[j]); 31 | } 32 | x = countDistinct(arr, n); 33 | if((n - x) % 2 == 0){ 34 | printf("%d\n", x); 35 | } else{ 36 | printf("%d\n", x - 1); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1692C.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1692/problem/C 2 | // Author: darkwhite0 3 | #include 4 | int main(){ 5 | int t, i, j, k; 6 | char str[8][8]; 7 | scanf("%d", &t); 8 | for(i = 0; i < t; i++){ 9 | for(j = 0; j < 8; j++){ 10 | scanf("%s", &str[j]); 11 | } 12 | for(j = 0; j < 8; j++){ 13 | for(k = 0; k < 8; k++){ 14 | if(str[j][k] == '#'){ 15 | break; 16 | } 17 | } 18 | if(str[j - 1][k - 1] == '#' 19 | && str[j - 1][k + 1] == '#' 20 | && str[j + 1][k - 1] == '#' 21 | && str[j + 1][k + 1] == '#'){ 22 | printf("%d %d\n", j+1, k+1); 23 | break; 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1712B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1712/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n, i; 11 | cin >> n; 12 | int arr[n]; 13 | for(i = 0; i < n; i++) arr[i] = i + 1; 14 | for(i = n - 1; i > 0; i--){ 15 | if((n - i) % 2 == 1){ 16 | int temp = arr[i]; 17 | arr[i] = arr[i - 1]; 18 | arr[i - 1] = temp; 19 | } 20 | } 21 | for(auto val: arr) cout << val << " "; 22 | cout << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1712C.cpp: -------------------------------------------------------------------------------- 1 | //Link: https://codeforces.com/contest/1712/problem/C 2 | #include 3 | using namespace std; 4 | #define int long long 5 | 6 | constexpr int N = 2e5 + 5; 7 | 8 | void solve_case() { 9 | 10 | int n; 11 | cin >> n; 12 | vector v(n); 13 | for (auto &x : v)cin >> x; 14 | 15 | if (is_sorted(v.begin(), v.end())) 16 | { 17 | cout << "0\n"; 18 | return; 19 | } 20 | 21 | vector last (n + 1); 22 | for (int i = 0; i < n; i++) { 23 | int x = v[i]; 24 | last[x] = i; 25 | } 26 | 27 | int idx = n - 1; 28 | for (int i = n - 1; i > 0; i--) { 29 | if (v[i - 1] <= v[i])continue; 30 | idx = i; 31 | break; 32 | } 33 | 34 | set s; 35 | int last_id = 0; 36 | for (int i = 0; i < idx; i++) { 37 | last_id = max(last_id, last[v[i]]); 38 | } 39 | 40 | for (int i = 0; i <= last_id; i++) { 41 | s.insert(v[i]); 42 | } 43 | 44 | cout << s.size() << '\n'; 45 | } 46 | 47 | int32_t main() { 48 | cin.tie(nullptr)->sync_with_stdio(false); 49 | int T; 50 | cin >> T; 51 | while (T-- > 0) 52 | solve_case(); 53 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1715B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1715/problem/B 2 | #include 3 | using namespace std; 4 | #define int long long 5 | 6 | constexpr int N = 2e5 + 5; 7 | 8 | void solve_case() { 9 | int n, k, b, s; 10 | cin >> n >> k >> b >> s; 11 | 12 | if (s >= k * b && s <= k * b + n * (k - 1)) { 13 | 14 | int rem = s - k * b; 15 | for (int i = 0; i < n - 1; ++i) { 16 | int mn = min(rem, k - 1); 17 | cout << mn << ' '; 18 | rem -= mn; 19 | } 20 | cout << rem + b*k << '\n'; 21 | } 22 | 23 | else 24 | cout << "-1\n"; 25 | } 26 | 27 | int32_t main() { 28 | cin.tie(nullptr)->sync_with_stdio(false); 29 | int T; 30 | cin >> T; 31 | while (T-- > 0) 32 | solve_case(); 33 | } 34 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1715D.cpp: -------------------------------------------------------------------------------- 1 | //Link: https://codeforces.com/contest/1715/problem/D 2 | #include 3 | using namespace std; 4 | #define int long long 5 | 6 | constexpr int N = 2e5 + 5; 7 | constexpr short M = 30; 8 | int n, q; 9 | vector> queries[N]; 10 | 11 | int32_t main() 12 | { 13 | cin.tie(nullptr)->sync_with_stdio(false); 14 | cin >> n >> q; 15 | 16 | bool bit_zero[n][M + 1] = {}; 17 | bool bit_one[n][M + 1] = {}; 18 | 19 | for (int i = 0; i < n; i++) { 20 | for (int j = 0; j <= M; j++) { 21 | bit_one[i][j] = bit_zero[i][j] = false; 22 | } 23 | } 24 | 25 | 26 | for (int i = 0, a, b, x; i < q; i++) { 27 | cin >> a >> b >> x; 28 | a--, b--; 29 | 30 | if (a > b) 31 | swap(a, b); 32 | if (a != b) 33 | queries[a].push_back({b, x}); 34 | for (int j = M; j >= 0; j--) { 35 | int bit_x = x >> j & 1; 36 | if (a != b && !bit_x) 37 | bit_zero[a][j] = bit_zero[b][j] = true; 38 | if (a == b && bit_x) { 39 | bit_one[a][j] = true; 40 | } 41 | if (a == b && !bit_x) 42 | bit_zero[a][j] = true; 43 | } 44 | } 45 | 46 | 47 | 48 | 49 | 50 | for (int i = 0; i < n; i++) { 51 | for (auto [j, x] : queries[i]) { 52 | for (int b = M; b >= 0; b--) { 53 | int bit_x = (x >> b & 1); 54 | if (bit_x == 1) { 55 | if (bit_zero[i][b] == true) { 56 | bit_one[j][b] = true; 57 | } 58 | if (bit_zero[j][b] == true) { 59 | bit_one[i][b] = true; 60 | } 61 | } 62 | } 63 | } 64 | } 65 | 66 | 67 | for (int i = 0; i < n; i++) { 68 | 69 | for (int b = M; b >= 0; b--) 70 | if (bit_zero[i][b] == false && bit_one[i][b] == false) 71 | bit_zero[i][b] = true; 72 | 73 | for (auto [j, x] : queries[i]) { 74 | for (int b = M; b >= 0; b--) { 75 | int bit_x = (x >> b & 1); 76 | if (bit_x == 1 && bit_zero[i][b]) { 77 | bit_one[j][b] = true; 78 | } 79 | } 80 | } 81 | } 82 | 83 | vector res(n); 84 | for (int i = 0; i < n; i++) { 85 | for (int b = M; b >= 0; b--) { 86 | if (bit_one[i][b]) 87 | res[i] += (1LL << b); 88 | } 89 | } 90 | 91 | for (auto &x : res)cout << x << ' '; 92 | cout << '\n'; 93 | return 0; 94 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1718A1.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1718/problem/A1 2 | 3 | #include 4 | using namespace std; 5 | 6 | constexpr int N = 5010, M = 8191, inf = 1e9; 7 | 8 | 9 | void solve_case() { 10 | int n; 11 | cin >> n; 12 | 13 | vector a(n + 1); 14 | for (int i = 1; i <= n; i++) 15 | cin >> a[i]; 16 | 17 | vector>dp(n + 2, vector(M + 1, inf)); 18 | 19 | 20 | for (int i = 0; i <= M; i++) 21 | dp[0][i] = 0; 22 | 23 | dp[1][0] = 0; 24 | 25 | for (int i = 1; i <= M; i++) 26 | dp[1][i] = 1; 27 | 28 | for (int i = 2; i <= n; i++) { 29 | dp[i][0] = dp[i - 1][a[i - 1]]; 30 | for (int j = 1; j <= M; j++) 31 | dp[i][j] = min(dp[i - 1][a[i - 1]] + 1, 1 + dp[i - 1][a[i - 1] ^ j]); 32 | } 33 | 34 | cout << dp[n][a[n]] << '\n'; 35 | } 36 | 37 | int32_t main() { 38 | cin.tie(nullptr)->sync_with_stdio(false); 39 | int T; 40 | cin >> T; 41 | while (T-- > 0) 42 | solve_case(); 43 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1719C.cpp: -------------------------------------------------------------------------------- 1 | //Link: https://codeforces.com/contest/1719/problem/C 2 | 3 | #include 4 | using namespace std; 5 | #define int long long 6 | 7 | constexpr int N = 2e5 + 5; 8 | int n, q; 9 | int a[N]; 10 | 11 | void solve_case() { 12 | cin >> n >> q; 13 | for (int i = 0; i < n; i++) 14 | cin >> a[i]; 15 | 16 | vector> qs(n); 17 | int idx = 0; 18 | 19 | for (int i = 1; i < n; i++) { 20 | if (a[i] > a[idx]) { 21 | idx = i; 22 | } 23 | qs[idx].push_back(i); 24 | } 25 | 26 | while (q--) { 27 | int i, k; 28 | cin >> i >> k; 29 | i--; 30 | 31 | int wins = lower_bound(qs[i].begin(), qs[i].end(), k + 1) - qs[i].begin(); 32 | if (i == idx) 33 | wins += max(0LL, k - (n - 1)); 34 | 35 | cout << wins << '\n'; 36 | } 37 | } 38 | 39 | int32_t main() { 40 | cin.tie(nullptr)->sync_with_stdio(false); 41 | int T; 42 | cin >> T; 43 | while (T-- > 0) 44 | solve_case(); 45 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1720D1.cpp: -------------------------------------------------------------------------------- 1 | //Link: https://codeforces.com/contest/1720/problem/D1 2 | #include 3 | using namespace std; 4 | #define int long long 5 | 6 | constexpr int N = 3e5 + 5; 7 | const int Maxm = 260; 8 | 9 | int a[N], dp[N]; 10 | int n; 11 | 12 | void solve_case() { 13 | cin >> n; 14 | int res(0); 15 | 16 | for (int i = 0; i < n; i++) { 17 | cin >> a[i]; 18 | dp[i] = 1; 19 | for (int j = max(0LL, i - Maxm); j < i; j++) { 20 | if ((a[j]^i) < (a[i]^j)) 21 | dp[i] = max(dp[i], dp[j] + 1); 22 | res = max(res, dp[i]); 23 | } 24 | } 25 | 26 | 27 | cout << res << '\n'; 28 | 29 | } 30 | 31 | int32_t main() { 32 | cin.tie(nullptr)->sync_with_stdio(false); 33 | int T; 34 | cin >> T; 35 | while (T-- > 0) 36 | solve_case(); 37 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1721B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1721/problem/B 2 | // Author: darkwhite0 3 | #include 4 | using namespace std; 5 | int main(){ 6 | long long int t; 7 | cin >> t; 8 | while(t--){ 9 | int n, m, x, y, d; 10 | cin >> n >> m >> x >> y >> d; 11 | if(((abs(n - x) + abs(m - y)) <= d) || (m - y <= d && n - x <= d) || (x - 1 <= d && n - x <= d) || (y - 1 <= d && m - y <= d) || (x - 1 <= d && y - 1 <= d)) cout << "-1\n"; 12 | else cout << n + m - 2 << endl; 13 | } 14 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/1735B.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/1735/problem/B 2 | #include "bits/stdc++.h" 3 | using namespace std; 4 | #define max(a, b) (a < b ? b : a) 5 | #define min(a, b) ((a > b) ? b : a) 6 | typedef long long int ll; 7 | #define ff first 8 | #define ss second 9 | #define pb push_back 10 | #define pob pop_back 11 | #define mp make_pair 12 | #define endl '\n' 13 | #define vl vector 14 | int dx[] = {-1 , 0 , 1 , 0}; 15 | int dy[] = {0 , 1 , 0 , -1}; 16 | 17 | void solve_case() 18 | { 19 | ll n ; cin>>n; 20 | 21 | vl a(n); 22 | for(int i=0 ; i>a[i]; 24 | 25 | ll mn = 2*a[0] - 1; 26 | ll ans = 0; 27 | for(int i=1 ; i> T; 43 | while (T--) { 44 | solve_case(); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/231A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/231/problem/A 2 | // Author: cvanshul 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | std::ios::sync_with_stdio(false); 10 | std::cin.tie(NULL); 11 | 12 | int n; cin >> n; 13 | int ans = 0; 14 | 15 | for (int i = 0; i < n; i++) { 16 | int a, b, c; cin >> a >> b >> c; 17 | int tmp = (a + b + c) / 2; 18 | ans += tmp; 19 | } 20 | 21 | cout << ans << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /CP Solutions/Codeforces/260A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/315/problem/A 2 | // Author: aryanpathania 3 | #include 4 | #define MOD 1'000'000'007 5 | //#define MOD = 998'244'353 6 | #define int int64_t 7 | typedef long long ll; 8 | typedef long double ld; 9 | using namespace std; 10 | 11 | int countDigits(int n); 12 | 13 | void solve(){ 14 | int a,b,n,m,k,p,q,r,count = 0, sum = 0, ans = 0; 15 | std::cin >> a >> b >> n; 16 | if(a%b == 0){ 17 | std::cout << a; 18 | for (int i = 0; i < n; i++){ 19 | std::cout << 0; 20 | } 21 | return; 22 | } 23 | for (int i = 0; i < 10; i++) 24 | { 25 | if((10*a + i)%b == 0){ 26 | std::cout << a << i; 27 | for (int i = 0; i < n-1; i++){ 28 | std::cout << 0; 29 | } 30 | return; 31 | } 32 | } 33 | std::cout << -1 << '\n'; 34 | }//solve 35 | 36 | int32_t main(){ 37 | std::ios::sync_with_stdio(false); std::cin.tie(NULL); 38 | auto time_start = chrono::high_resolution_clock::now(); 39 | 40 | int testcases = 1; 41 | // std::cin >> testcases; 42 | 43 | while(testcases--) { 44 | solve(); 45 | }//testcases 46 | 47 | auto time_end = chrono::high_resolution_clock::now(); 48 | double time_taken = chrono::duration_cast(time_end - time_start).count(); 49 | 50 | time_taken *= 1e-9; 51 | cerr << setprecision(6) << time_taken << " ms" << '\n'; 52 | return 0; 53 | }//main 54 | 55 | int count_digit(int number) { 56 | return int(log10(number) + 1); 57 | } 58 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/315A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/315/problem/A 2 | // Author: aryanpathania 3 | #include 4 | #define MOD 1'000'000'007 5 | //#define MOD = 998'244'353 6 | #define int int64_t 7 | typedef long long ll; 8 | typedef long double ld; 9 | using namespace std; 10 | 11 | void solve(){ 12 | int n; 13 | std::cin >> n; 14 | int a[n],b[n]; 15 | for (int i = 0; i < n; i++){ 16 | std::cin >> a[i] >> b[i]; 17 | } 18 | int count = n; 19 | for (int i = 0; i < n; i++) 20 | { 21 | for (int j = 0; j < n; j++) 22 | { 23 | if(a[i] == b[j] && i!=j){ 24 | // std::cout << a[i] << '\n'; 25 | count --; 26 | break; 27 | } 28 | } 29 | 30 | } 31 | std::cout << count << '\n'; 32 | } 33 | 34 | int32_t main(){ 35 | std::ios::sync_with_stdio(false); 36 | std::cin.tie(NULL); 37 | 38 | solve(); 39 | 40 | return 0; 41 | }//main 42 | -------------------------------------------------------------------------------- /CP Solutions/Codeforces/451A.cpp: -------------------------------------------------------------------------------- 1 | // Link: https://codeforces.com/contest/451/problem/A 2 | // Author: cvanshul 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n,m; 10 | cin>>n>>m; 11 | 12 | if (min(n,m)%2==0) 13 | { 14 | cout<<"Malvika"< 5 | 6 | #define int long long 7 | 8 | using namespace std; 9 | 10 | long long Binexp(long long a, long long b, long long m) { 11 | a %= m; 12 | long long res = 1; 13 | while (b > 0) { 14 | if (b & 1) 15 | res = res * a % m; 16 | a = a * a % m; 17 | b >>= 1; 18 | } 19 | return res; 20 | } 21 | 22 | signed main() { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(0); 25 | int n; 26 | cin >> n; 27 | vector a(n); 28 | for (int i = 0; i < n; ++i) cin >> a[i]; 29 | sort(a.begin(), a.end()); 30 | const int M = (int) 1e9 + 7; 31 | int A = 0, B = 0; 32 | for (int i = n - 1, x; i >= 0; --i) { 33 | x = Binexp(2LL, i, M) - 1; 34 | A = (A + (a[i] * x) % M) % M; 35 | B = (B + (a[n - 1 - i] * x) % M) % M; 36 | } 37 | cout << (A - B + M) % M; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /CP Solutions/README.md: -------------------------------------------------------------------------------- 1 | Keep these things in mind when adding a solution : 2 | 3 | * The folder structure must remain as follows: 4 |
 5 | CP Solutions/
 6 | ├─── Codeforces/
 7 | │       └─── 1599A.cpp     
 8 | └─── Codechef
 9 | 
10 | 11 | * Check if a problem is already present, before adding it. 12 | 13 | * The first line of code must contain a link to corresponding question. 14 | 15 | * The name of added file must be the problem code and it must added in the relevant folder(must be created if not already present). -------------------------------------------------------------------------------- /CP Solutions/leetcode/Contains Duplicate/Contains_Duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | unordered_set p; 5 | for(int i = 0;i 6 | #include 7 | using namespace std; 8 | 9 | #define vvi vector> 10 | #define vi vector 11 | 12 | #define loop(i, a, n) for (int i = a; i < n; i++) 13 | 14 | void print2d(vector> bord) 15 | { 16 | cout << endl; 17 | for (int i = 0; i < bord.size(); i++) 18 | { 19 | for (int j = 0; j < bord[0].size(); j++) 20 | { 21 | cout << bord[i][j] << ' '; 22 | } 23 | cout << endl; 24 | } 25 | cout << endl; 26 | } 27 | 28 | void solve(vvi &bord, int row, int col, int count) 29 | { 30 | if (row < 0 || col < 0 || row >= bord.size() || col >= bord.size()) 31 | { 32 | return; 33 | } 34 | if (count == bord.size() * bord.size()) 35 | { 36 | bord[row][col] = count; 37 | print2d(bord); 38 | bord[row][col] = 0; 39 | cout << row << col << endl; 40 | return; 41 | } 42 | 43 | if (!bord[row][col]) 44 | { 45 | bord[row][col] = count + 1; 46 | 47 | solve(bord, row - 2, col + 1, count + 1); 48 | solve(bord, row - 1, col + 2, count + 1); 49 | solve(bord, row + 1, col + 2, count + 1); 50 | solve(bord, row + 2, col + 1, count + 1); 51 | solve(bord, row + 2, col - 1, count + 1); 52 | solve(bord, row + 1, col - 2, count + 1); 53 | solve(bord, row - 1, col - 2, count + 1); 54 | solve(bord, row - 2, col - 1, count + 1); 55 | 56 | bord[row][col] = 0; 57 | } 58 | } 59 | 60 | int main() 61 | { 62 | int n, row, col; 63 | cin >> n >> row >> col; 64 | vvi bord(n, vi(n, 0)); 65 | solve(bord, row, col, 0); 66 | } -------------------------------------------------------------------------------- /Contributors/Contributers-main/.codesandbox/workspace.json: -------------------------------------------------------------------------------- 1 | { 2 | "responsive-preview": { 3 | "Mobile": [ 4 | 320, 5 | 675 6 | ], 7 | "Tablet": [ 8 | 1024, 9 | 765 10 | ], 11 | "Desktop": [ 12 | 1400, 13 | 800 14 | ], 15 | "Desktop HD": [ 16 | 1920, 17 | 1080 18 | ] 19 | } 20 | } -------------------------------------------------------------------------------- /Contributors/Contributers-main/README.md: -------------------------------------------------------------------------------- 1 | # Site link https://csb-ns5cv.netlify.app/ 2 | 3 | ## feel free to make changes 4 | ### link https://codesandbox.io/s/github/lekhit/Contributers 5 | #### design needs attention 6 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "notes-model", 3 | "version": "1.0.0", 4 | "description": "", 5 | "keywords": [], 6 | "main": "src/index.js", 7 | "dependencies": { 8 | "bootstrap": "5.1.1", 9 | "mdb-react-ui-kit": "1.4.0", 10 | "react": "17.0.2", 11 | "react-async": "10.0.1", 12 | "react-bootstrap": "1.6.3", 13 | "react-dom": "17.0.2", 14 | "react-scripts": "4.0.0" 15 | }, 16 | "devDependencies": { 17 | "@babel/runtime": "7.13.8", 18 | "typescript": "4.1.3" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test --env=jsdom", 24 | "eject": "react-scripts eject" 25 | }, 26 | "browserslist": [ 27 | ">0.2%", 28 | "not dead", 29 | "not ie <= 11", 30 | "not op_mini all" 31 | ] 32 | } -------------------------------------------------------------------------------- /Contributors/Contributers-main/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 25 | React App 26 | 27 | 28 | 29 | 32 |
33 | 43 | 47 | 48 | 52 | 53 | 57 | 58 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/App.js: -------------------------------------------------------------------------------- 1 | import "./styles.css"; 2 | import "bootstrap/dist/css/bootstrap.min.css"; 3 | import Grid from "./Grid"; 4 | import Navbar from "./Navbar"; 5 | import Footer from "./Footer"; 6 | 7 | export default function App() { 8 | return ( 9 |
10 | 11 |
12 | 13 | 14 |

Hello CodeSandbox

15 |

Start editing to see some magic happen!

16 |
17 |
18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/Footer.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | export default function Footer() { 4 | var d = new Date(); 5 | 6 | return ( 7 |
8 |

© {d.getFullYear()} Notes

9 |
10 | ); 11 | } 12 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/Grid.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Row, Col } from "react-bootstrap"; 3 | import Mycard from "./Mycard"; 4 | import { useAsync } from "react-async"; 5 | 6 | const fetchPerson = async ({ id }, { signal }) => { 7 | const response = await fetch( 8 | `https://api.github.com/repos/geeks-hub-nith/C-Plus-Plus/contributors 9 | `, 10 | { signal } 11 | ); 12 | if (!response.ok) throw new Error(response.status); 13 | return response.json(); 14 | }; 15 | 16 | const CreateGrid = () => { 17 | const { data, error } = useAsync({ promiseFn: fetchPerson }); 18 | console.log(data); 19 | if (error) return error.message; 20 | if (data) 21 | return ( 22 | 23 | {data.map((item) => ( 24 | 25 | {" "} 26 | 32 | 33 | ))} 34 | 35 | ); 36 | return null; 37 | }; 38 | 39 | export default function Grid() { 40 | // var data=getData(); 41 | 42 | //const { data, error } = useAsync({ promiseFn: fetchPerson ,id}) 43 | // console.log(data) 44 | //data.map(Mycard); 45 | return ( 46 |
47 | 48 |
49 | ); 50 | } 51 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/Mycard.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Card, Button } from "react-bootstrap"; 3 | import { 4 | MDBCard, 5 | MDBCardBody, 6 | MDBCardTitle, 7 | MDBCardText, 8 | MDBCardImage, 9 | MDBBtn, 10 | MDBRipple 11 | } from "mdb-react-ui-kit"; 12 | 13 | export function Mycard1(props) { 14 | return ( 15 |
16 | 17 | 21 | {props.name} 22 | 23 | 24 | Info Card Title 25 | 26 | Some quick example text to build on the card title and make up the 27 | bulk of the card's content. 28 | 29 | 30 | 31 |
32 | ); 33 | } 34 | 35 | export function Mycard2(props) { 36 | return ( 37 | 38 | 43 | 44 | 49 | 50 |
54 |
55 | 56 | 57 | {props.name} 58 | 59 | Contributions : {props.contributions} 60 | Button 61 | 62 |
63 | ); 64 | } 65 | 66 | export default function Mycard(props) { 67 | return ( 68 |
69 | 70 | 71 | 72 | 73 | 74 | {props.name} 75 | Contributions : {props.contributions} 76 | 79 | 80 | 81 |
82 | ); 83 | } 84 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/Navbar.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Container, Navbar, Nav } from "react-bootstrap"; 3 | export default function mynavbar() { 4 | return ( 5 | 6 | 7 | Contributers 8 | 11 | 12 | 13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/data.js: -------------------------------------------------------------------------------- 1 | var realData = ""; 2 | 3 | const getData = async () => { 4 | const api = 5 | "https://api.github.com/repos/geeks-hub-nith/C-Plus-Plus/contributors"; 6 | try { 7 | let data = await fetch(api); 8 | let realData1 = await data.json(); 9 | let realData = realData1; 10 | console.log(realData); 11 | } catch (error) { 12 | console.log(error); 13 | } 14 | return realData; 15 | }; 16 | export default realData; 17 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/index.js: -------------------------------------------------------------------------------- 1 | import { StrictMode } from "react"; 2 | import ReactDOM from "react-dom"; 3 | 4 | import App from "./App"; 5 | 6 | const rootElement = document.getElementById("root"); 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | rootElement 12 | ); 13 | -------------------------------------------------------------------------------- /Contributors/Contributers-main/src/styles.css: -------------------------------------------------------------------------------- 1 | .App { 2 | font-family: sans-serif; 3 | text-align: center; 4 | } 5 | .card { 6 | position: relative; 7 | min-width: 150px; 8 | width: 100%; 9 | height: auto; 10 | overflow: hidden; 11 | border-radius: 15px; 12 | margin: 0 auto; 13 | padding: 20px 10px; 14 | transition: 0.5s; 15 | } 16 | .card:hover { 17 | transform: scale(1.1); 18 | } 19 | .card_red, 20 | .card_red .title .fa { 21 | background: #4d79af; 22 | } 23 | .card_violet, 24 | .card_violet .title .fa { 25 | background: linear-gradient(-45deg, #f1ecf1, #349aec); 26 | } 27 | .card_three, 28 | .card_three .title .fa { 29 | } 30 | 31 | .card:before { 32 | content: ""; 33 | position: absolute; 34 | bottom: 0; 35 | left: 0; 36 | width: 100%; 37 | height: 40%; 38 | background: rgba(255, 255, 255, 0.1); 39 | z-index: 1; 40 | transform: skewY(-5deg) scale(1.5); 41 | } 42 | -------------------------------------------------------------------------------- /Contributors/Contributors.json: -------------------------------------------------------------------------------- 1 | {"data":[{ 2 | "name":"Lekhit Borole", 3 | "url":"https://github.com/lekhit/", 4 | "about":"Hi I love open source software." 5 | }, 6 | {} 7 | 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /Contributors/README.md: -------------------------------------------------------------------------------- 1 | # This folder contains Information about all our contributers 2 | ## [Contributors Dynamic site](https://csb-ns5cv.netlify.app/) 3 | ## [Contributors Static site](https://lekhit.github.io/csb-ns5cv/) 4 | ## To add your details 5 | - make a folder with your name eg 6 | - your are free to decide what information your will have. 7 | - add your details in Contributors.json 8 | Format for this is 9 | ``` 10 | { 11 | "name":"your name", 12 | "url":"your profile url", 13 | "about":"Brief information about you" 14 | } 15 | ``` -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/asset-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": { 3 | "main.css": "https://lekhit.github.io/csb-ns5cv/static/css/main.2ec8b1da.chunk.css", 4 | "main.js": "https://lekhit.github.io/csb-ns5cv/static/js/main.996f98f1.chunk.js", 5 | "main.js.map": "https://lekhit.github.io/csb-ns5cv/static/js/main.996f98f1.chunk.js.map", 6 | "runtime-main.js": "https://lekhit.github.io/csb-ns5cv/static/js/runtime-main.a7488486.js", 7 | "runtime-main.js.map": "https://lekhit.github.io/csb-ns5cv/static/js/runtime-main.a7488486.js.map", 8 | "static/css/2.c0897e43.chunk.css": "https://lekhit.github.io/csb-ns5cv/static/css/2.c0897e43.chunk.css", 9 | "static/js/2.44b0e230.chunk.js": "https://lekhit.github.io/csb-ns5cv/static/js/2.44b0e230.chunk.js", 10 | "static/js/2.44b0e230.chunk.js.map": "https://lekhit.github.io/csb-ns5cv/static/js/2.44b0e230.chunk.js.map", 11 | "index.html": "https://lekhit.github.io/csb-ns5cv/index.html", 12 | "static/css/2.c0897e43.chunk.css.map": "https://lekhit.github.io/csb-ns5cv/static/css/2.c0897e43.chunk.css.map", 13 | "static/css/main.2ec8b1da.chunk.css.map": "https://lekhit.github.io/csb-ns5cv/static/css/main.2ec8b1da.chunk.css.map", 14 | "static/js/2.44b0e230.chunk.js.LICENSE.txt": "https://lekhit.github.io/csb-ns5cv/static/js/2.44b0e230.chunk.js.LICENSE.txt" 15 | }, 16 | "entrypoints": [ 17 | "static/js/runtime-main.a7488486.js", 18 | "static/css/2.c0897e43.chunk.css", 19 | "static/js/2.44b0e230.chunk.js", 20 | "static/css/main.2ec8b1da.chunk.css", 21 | "static/js/main.996f98f1.chunk.js" 22 | ] 23 | } -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/index.html: -------------------------------------------------------------------------------- 1 | React App
-------------------------------------------------------------------------------- /Contributors/Static site/Static-main/readme.md: -------------------------------------------------------------------------------- 1 | # static site 2 | ### suitable for github pages 3 | ### similar design to dynamic site 4 | #### [link](https://lekhit.github.io/csb-ns5cv/) 5 | -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/static/css/main.2ec8b1da.chunk.css: -------------------------------------------------------------------------------- 1 | .App{font-family:sans-serif;text-align:center}.card{position:relative;min-width:150px;width:100%;height:auto;overflow:hidden;border-radius:15px;margin:0 auto;padding:20px 10px;transition:.5s}.card:hover{-webkit-transform:scale(1.1);transform:scale(1.1)}.card_red,.card_red .title .fa{background:#4d79af}.card_violet,.card_violet .title .fa{background:linear-gradient(-45deg,#f1ecf1,#349aec)}.card:before{content:"";position:absolute;bottom:0;left:0;width:100%;height:40%;background:hsla(0,0%,100%,.1);z-index:1;-webkit-transform:skewY(-5deg) scale(1.5);transform:skewY(-5deg) scale(1.5)} 2 | /*# sourceMappingURL=main.2ec8b1da.chunk.css.map */ -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/static/css/main.2ec8b1da.chunk.css.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["webpack://src/styles.css"],"names":[],"mappings":"AAAA,KACE,sBAAuB,CACvB,iBACF,CACA,MACE,iBAAkB,CAClB,eAAgB,CAChB,UAAW,CACX,WAAY,CACZ,eAAgB,CAChB,kBAAmB,CACnB,aAAc,CACd,iBAAkB,CAClB,cACF,CACA,YACE,4BAAqB,CAArB,oBACF,CACA,+BAEE,kBACF,CACA,qCAEE,kDACF,CAKA,aACE,UAAW,CACX,iBAAkB,CAClB,QAAS,CACT,MAAO,CACP,UAAW,CACX,UAAW,CACX,6BAAoC,CACpC,SAAU,CACV,yCAAkC,CAAlC,iCACF","file":"main.2ec8b1da.chunk.css","sourcesContent":[".App {\n font-family: sans-serif;\n text-align: center;\n}\n.card {\n position: relative;\n min-width: 150px;\n width: 100%;\n height: auto;\n overflow: hidden;\n border-radius: 15px;\n margin: 0 auto;\n padding: 20px 10px;\n transition: 0.5s;\n}\n.card:hover {\n transform: scale(1.1);\n}\n.card_red,\n.card_red .title .fa {\n background: #4d79af;\n}\n.card_violet,\n.card_violet .title .fa {\n background: linear-gradient(-45deg, #f1ecf1, #349aec);\n}\n.card_three,\n.card_three .title .fa {\n}\n\n.card:before {\n content: \"\";\n position: absolute;\n bottom: 0;\n left: 0;\n width: 100%;\n height: 40%;\n background: rgba(255, 255, 255, 0.1);\n z-index: 1;\n transform: skewY(-5deg) scale(1.5);\n}\n"]} -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/static/js/2.44b0e230.chunk.js.LICENSE.txt: -------------------------------------------------------------------------------- 1 | /* 2 | object-assign 3 | (c) Sindre Sorhus 4 | @license MIT 5 | */ 6 | 7 | /*! 8 | Copyright (c) 2018 Jed Watson. 9 | Licensed under the MIT License (MIT), see 10 | http://jedwatson.github.io/classnames 11 | */ 12 | 13 | /** @license React v0.20.2 14 | * scheduler.production.min.js 15 | * 16 | * Copyright (c) Facebook, Inc. and its affiliates. 17 | * 18 | * This source code is licensed under the MIT license found in the 19 | * LICENSE file in the root directory of this source tree. 20 | */ 21 | 22 | /** @license React v17.0.2 23 | * react-dom.production.min.js 24 | * 25 | * Copyright (c) Facebook, Inc. and its affiliates. 26 | * 27 | * This source code is licensed under the MIT license found in the 28 | * LICENSE file in the root directory of this source tree. 29 | */ 30 | 31 | /** @license React v17.0.2 32 | * react-jsx-runtime.production.min.js 33 | * 34 | * Copyright (c) Facebook, Inc. and its affiliates. 35 | * 36 | * This source code is licensed under the MIT license found in the 37 | * LICENSE file in the root directory of this source tree. 38 | */ 39 | 40 | /** @license React v17.0.2 41 | * react.production.min.js 42 | * 43 | * Copyright (c) Facebook, Inc. and its affiliates. 44 | * 45 | * This source code is licensed under the MIT license found in the 46 | * LICENSE file in the root directory of this source tree. 47 | */ 48 | -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/static/js/main.996f98f1.chunk.js: -------------------------------------------------------------------------------- 1 | (this["webpackJsonpnotes-model"]=this["webpackJsonpnotes-model"]||[]).push([[0],{49:function(e,t,r){},59:function(e,t,r){"use strict";r.r(t);var n=r(0),c=r(7),s=r.n(c),a=(r(49),r(50),r(30)),i=r.n(a),j=r(35),o=r(65),l=r(66),u=r(70),h=r(64),b=(r(13),r(1));function d(e){return Object(b.jsx)("div",{className:"card",children:Object(b.jsxs)(u.a,{style:{maxWidth:"22rem"},children:[Object(b.jsx)("a",{href:e.html_url,children:Object(b.jsx)(u.a.Img,{variant:"top",src:e.avatar})}),Object(b.jsxs)(u.a.Body,{children:[Object(b.jsx)(u.a.Title,{children:e.name}),Object(b.jsxs)(u.a.Text,{children:["Contributions : ",e.contributions]}),Object(b.jsx)(h.a,{variant:"primary",href:"{props.html_url}",children:"Github"})]})]})})}var x=r(37),O=function(){var e=Object(j.a)(i.a.mark((function e(t,r){var n,c;return i.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t.id,n=r.signal,e.next=4,fetch("https://api.github.com/repos/geeks-hub-nith/C-Plus-Plus/contributors\n ",{signal:n});case 4:if((c=e.sent).ok){e.next=7;break}throw new Error(c.status);case 7:return e.abrupt("return",c.json());case 8:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),m=function(){var e=Object(x.a)({promiseFn:O}),t=e.data,r=e.error;return console.log(t),r?r.message:t?Object(b.jsx)(o.a,{xs:1,md:3,className:"g-4",children:t.map((function(e){return Object(b.jsxs)(l.a,{children:[" ",Object(b.jsx)(d,{name:e.login,html_url:e.html_url,contributions:e.contributions,avatar:e.avatar_url})]})}))}):null};function p(){return Object(b.jsx)("div",{className:"container",children:Object(b.jsx)(m,{})})}var f=r(68),v=r(67),g=r(69);function k(){return Object(b.jsx)(f.a,{bg:"warning",variant:"dark",children:Object(b.jsxs)(v.a,{children:[Object(b.jsx)(f.a.Brand,{href:"#home",children:"Contributers"}),Object(b.jsx)(g.a,{className:"me",children:Object(b.jsx)(g.a.Link,{href:"#ADD",children:"+Whatsapp Chat"})})]})})}function w(){var e=new Date;return Object(b.jsx)("footer",{children:Object(b.jsxs)("p",{children:["\xa9 ",e.getFullYear()," Notes"]})})}function N(){return Object(b.jsxs)("div",{className:"App",children:[Object(b.jsx)(k,{}),Object(b.jsx)("br",{}),Object(b.jsx)(p,{}),Object(b.jsx)("h1",{children:"Hello CodeSandbox"}),Object(b.jsx)("h2",{children:"Start editing to see some magic happen!"}),Object(b.jsx)(w,{})]})}var y=document.getElementById("root");s.a.render(Object(b.jsx)(n.StrictMode,{children:Object(b.jsx)(N,{})}),y)}},[[59,1,2]]]); 2 | //# sourceMappingURL=main.996f98f1.chunk.js.map -------------------------------------------------------------------------------- /Contributors/Static site/Static-main/static/js/runtime-main.a7488486.js: -------------------------------------------------------------------------------- 1 | !function(e){function t(t){for(var n,l,i=t[0],f=t[1],p=t[2],c=0,s=[];c 3 | 4 | hi, i'm Lekhit Borole, a passionate self-taught full stack web developer and a freelance software engineer from india. my passion for software lies with dreaming up ideas and making them come true with elegant interfaces. i take great care in the experience, architecture, and code quality of the things I build. 5 | 6 | i am also an open-source enthusiast and maintainer. i learned a lot from the open-source community and i love how collaboration and knowledge sharing happened through open-source. 7 | 8 | 9 | GIF 10 | 11 | - 💼 any freelance work? do reach, [email](mailto:blekhit@gmail.com) :) 12 | - 💬 ask me about anything, i am happy to help; 13 | 14 | **languages and tools:** 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 🚧 **my todoist stats:** 32 | 33 | 🏆 Build a website for project Programming 34 | 🌸 Completed 0 tasks today 35 | ✅ Completed 4 tasks so far 36 | -------------------------------------------------------------------------------- /Graphics/README.md: -------------------------------------------------------------------------------- 1 | # Graphics.h 2 | Graphics programming in C used to drawing various geometrical shapes(rectangle, circle eclipse etc), use of mathematical function in drawing curves, coloring an object with different colors and patterns and simple animation programs like jumping ball and moving cars. 3 | 4 | --- 5 | # How to use 6 | 1. clone this folder 7 | 2. create new file in this folder itself or you can add graphics to include files of your computer 8 | 3. ```include "graphics.cpp"``` 9 | 4. use put_points(x,y) to plot the points 10 | 5. start the python display change folder in terminal to python folder in this folder then ``` python3 graphicss.py``` 11 | 6. press the plot button to see result 12 | --- 13 | # Limitations of graphics.h (old) 14 | - incompetable with mac/linux based system 15 | - high dependance on turbo c compiler 16 | - not updated any more 17 | - not open source 18 | # Proposed Solution 19 | - built a pythonic module to use instead of graphics.h 20 | - multisystem module 21 | - only python installation is necessary 22 | - open source so highly customisable 23 | ## screen short 24 | ![Screenshot 2022-10-18 at 10 51 06 PM](https://user-images.githubusercontent.com/82832791/196933424-c86e9dff-d964-4563-8363-a496e75f427e.png) 25 | ### examples 26 | - dda 27 | ![Screenshot 2022-10-22 at 8 46 40 PM](https://user-images.githubusercontent.com/82832791/197348532-aeddca8c-8994-4506-a367-ba4451a99611.png) 28 | 29 | - breshman 30 | ![Screenshot 2022-10-22 at 8 39 41 PM](https://user-images.githubusercontent.com/82832791/197348544-e521bb8d-3fa7-4edb-8f8a-69a5c6002d4b.png) 31 | - circle 32 | ![Screenshot 2022-10-22 at 8 56 17 PM](https://user-images.githubusercontent.com/82832791/197348579-85f62159-4b8e-4fc2-874d-769288b47a21.png) 33 | 34 | - ellipse 35 | ![Screenshot 2022-10-22 at 9 18 28 PM](https://user-images.githubusercontent.com/82832791/197348621-aecdbf9b-743a-4634-a7ca-9592d4ca7f94.png) 36 | 37 | # TODOs 38 | - [x] add c++ bindings for pythonic files 39 | - [x] add some examples to use the new bindings 40 | - [x] line 41 | - [x] dda 42 | - [x] breshman 43 | - [x] circle 44 | - [x] ellipse 45 | - [x] add documentation 46 | -------------------------------------------------------------------------------- /Graphics/algorithms/Cohen_Sutherland.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int INSIDE=0,TOP=1,BOTTOM=2,RIGHT=4,LEFT=8;//0001 5 | 6 | const int xmin=10,ymin=10,xmax=100,ymax=100; 7 | 8 | int compute_points(double x,double y){ 9 | int code=INSIDE; 10 | if(xxmax) code|=RIGHT; 12 | if(yymax)code|=TOP; 14 | return code; 15 | } 16 | 17 | void clip(double x1,double y1,double x2,double y2){ 18 | vector codes={compute_points(x1,y1),compute_points(x2,y2)}; 19 | bool accepted=false; 20 | while (true){ 21 | if(codes[0]==INSIDE and codes[1]==INSIDE) {accepted=true; break;} 22 | else if(codes[0]&codes[1]){ 23 | break; 24 | } 25 | else{ 26 | int code_out=codes[0]; 27 | double x,y; 28 | if(codes[1]!=INSIDE){ 29 | code_out=codes[1]; 30 | } 31 | if(code_out & TOP){ 32 | y=ymax; 33 | x=x1+(x2-x1)*(y-y1)/(y2-y1); 34 | } 35 | else if(code_out & BOTTOM){ 36 | y=ymin; 37 | x=x1+(x2-x1)*(y-y1)/(y2-y1); 38 | } 39 | else if(code_out & RIGHT){ 40 | x=xmax; 41 | y=y1+(y2-y1)*(x-x1)/(x2-x1); 42 | } 43 | else if(code_out & LEFT){ 44 | x=xmin; 45 | y=y1+(y2-y1)*(x-x1)/(x2-x1); 46 | } 47 | 48 | if(code_out ==codes[0]){ 49 | x1=x,y1=y; 50 | } 51 | else{ 52 | x2=x,y2=y; 53 | } 54 | codes={compute_points(x1,y1),compute_points(x2,y2)}; 55 | } 56 | } 57 | if(accepted){ 58 | printf("{%f, %f} , {%f, %f}\n",x1,y1,x2,y2); 59 | } 60 | else{ 61 | printf("outside\n"); 62 | } 63 | } 64 | 65 | int main(){ 66 | clip(9,11,99,111); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Graphics/algorithms/Liang_Barsky.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | double xmin=10,ymin=10,xmax=100,ymax=100,u1=0.0,u2=1.0; 4 | bool clip_test(double p,double q){//,double &u1,double &u2){ 5 | if(p==0) return q<0?false:true; // line is out of window 6 | double r=q/p; 7 | if(p<0.0){// line outside->inside 8 | if(r>u2) return false; 9 | if(r>u1) u1=r; 10 | } 11 | if(p>0.0)//overkill 12 | { 13 | if(r> clip(vector> p={{1,2},{2,11}}){ 19 | double x1=p[0][0],y1=p[0][1]; 20 | double dx=p[1][0]-x1,dy=p[1][1]-y1;//x2-x1 y2-y1 21 | if(clip_test(-dx,x1-xmin)) 22 | if(clip_test(dx,xmax)) 23 | if(clip_test(-dy,y1-ymin)) 24 | if(clip_test(dy,ymax-y1)){ 25 | return {{x1+u1*dx, y1+u1*dy},{x1+u2*dx, y1+u2*dy}}; 26 | } 27 | return p; 28 | 29 | } 30 | void print(vector> i){ 31 | for(auto j:i){ 32 | for(auto k:j){ 33 | cout< 2 | using namespace std; 3 | const int maxN=1e3; 4 | int graph[maxN][maxN]; 5 | int boundry,color; 6 | 7 | vector v={1,-1,0,0},h={0,0,-1,1}; 8 | bool check(int x,int y){ 9 | if(x>=0 and x=0 and y a={0,10,100}; 14 | for(auto i:a){ 15 | for(int j=0;j<100;j++){ 16 | graph[j][i]=graph[i][j]=boundry; 17 | 18 | } 19 | } 20 | } 21 | void floodfill(int x,int y){ 22 | if(!check(x,y))return ; 23 | if( graph[x][y]!=boundry and graph[x][y]!=color){ 24 | graph[x][y]=color; 25 | for(int k=0;k 2 | using namespace std; 3 | #define F first 4 | bool mycomp(float a,float b){ 5 | return abs(a) p1,vector p2){ 8 | vector d; 9 | 10 | for(int i=0;i<=1;i++){ 11 | d.push_back(p1[i]-p2[i]); 12 | } 13 | int steps=*max_element(d.begin(),d.end()); 14 | for(auto &i:d){ 15 | i/=steps; 16 | } 17 | float x=p1[0],y=p1[1]; 18 | vector> co={{p1[0],p1[1]}}; 19 | while (steps--){ 20 | auto i=co.back(); 21 | 22 | co.push_back({floor(x),floor(y)}); 23 | } 24 | } 25 | 26 | 27 | 28 | int main(){ 29 | pair a={1,2}; 30 | cout< 2 | using namespace std; 3 | double PI; 4 | #define LOCAL 5 | vector> matrix_multipication(vector> a,vector> b){ 6 | int ar=a.size(),ac=a[0].size(),br=b.size(),bc=b[0].size(); 7 | if(ac!=br) return a; 8 | vector> ans(ar,vector(bc,0)); 9 | for(int i=0;i> chain_multipication(vector>> a){ 21 | auto i=a[0]; 22 | for(auto j:a){ 23 | if(j!=i){ 24 | i=matrix_multipication(i,j); 25 | } 26 | } 27 | return i; 28 | } 29 | 30 | void print(vector> i){ 31 | for(auto j:i){ 32 | for(auto k:j){ 33 | cout<> rotate(vector> points,double angle){ 39 | vector> a={{},{},{}}; 40 | for(auto i:points){ 41 | a[0].push_back(i[0]); 42 | a[1].push_back(i[1]); 43 | a[2].push_back(1); 44 | } 45 | auto i=matrix_multipication({{cos(angle),sin(angle),0},{sin(angle),-cos(angle),0},{0,0,1}},a); 46 | //print(i); 47 | return i; 48 | } 49 | 50 | 51 | vector rotate_about(double x,double y,double ax,double ay,double angle){ 52 | auto i=rotate({{x-ax*1.0,y*1.0-ay}},angle); 53 | for(auto &j:i[0]) j+=ax; 54 | for(auto &j:i[1])j+=ay; 55 | 56 | return i[0]; 57 | } 58 | vector> rotate_matrix_about(vector> points,vector about,double angle){ 59 | double ax=about[0],ay=about[1]; 60 | vector> ans; 61 | 62 | for(auto point:points){ 63 | ans.push_back(rotate_about(point[0],point[1],ax,ay,angle)); 64 | } 65 | return ans; 66 | } 67 | vector> scaling_matrix_maker(double sx,double sy){ 68 | return {{sx,0,0},{0,sy,0},{0,0,1}}; 69 | } 70 | vector> scaling(vector> points,double sx,double sy){ 71 | vector> a={{},{},{}}; 72 | for(auto i:points){ 73 | a[0].push_back(i[0]); 74 | a[1].push_back(i[1]); 75 | a[2].push_back(1); 76 | } 77 | 78 | auto ans=matrix_multipication(scaling_matrix_maker(sx,sy),a); 79 | return ans; 80 | } 81 | vector> traslation(vector> points,double tx,double ty){ 82 | vector> a={{},{},{}},tr_matrix={{1,0,tx},{0,1,ty},{0,0,1}}; 83 | for(auto i:points){ 84 | a[0].push_back(i[0]); 85 | a[1].push_back(i[1]); 86 | a[2].push_back(1); 87 | } 88 | return matrix_multipication(tr_matrix,a); 89 | } 90 | vector> points_to_matrix(vector> points){ 91 | vector> a={{},{},{}}; 92 | for(auto i:points){ 93 | a[0].push_back(i[0]); 94 | a[1].push_back(i[1]); 95 | a[2].push_back(1); 96 | } 97 | return a; 98 | } 99 | 100 | vector> to_points(vector> a){ 101 | vector> ans(a[0].size()); 102 | for(int i=0;i> reflection(vector> points,vector> directions){ 110 | return matrix_multipication(directions, points_to_matrix(points)); 111 | } 112 | 113 | vector> sheer(vector> points,vector> sheer){ 114 | return matrix_multipication(sheer, points_to_matrix(points)); 115 | } 116 | vector> sheer_x_y(vector> points,double sx=0,double sy=0,double xref=0,double yref=0){ 117 | vector> a={{1,sx,-1*sx*xref},{sy,1,-1*sy*yref},{0,0,1}}; 118 | return sheer(a,points); 119 | } 120 | vector> get_points(){ 121 | //cout<<"enter number of points in input\n"; 122 | int n; cin>>n; 123 | //cout<<"enter points\n"; 124 | vector> v; 125 | string p;cin>>p; 126 | 127 | for(int i=1;i<=n;i++){ 128 | // cout<>a>>b; 131 | if(p!="Y") 132 | cout<> points=get_points(); 151 | // {{10,22},{1,33},{11,22}} 152 | auto i=scaling(points,2,3); 153 | i=to_points(i); 154 | print(i); 155 | } 156 | -------------------------------------------------------------------------------- /Graphics/algorithms/tr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class PT{ 4 | public: 5 | double x,y; 6 | PT(int x1,int y1){x=x1,y=y1;} 7 | PT(){} 8 | PT operator + (PT const &obj) { 9 | PT res; 10 | res.x = x + obj.x; 11 | res.y = y + obj.y; 12 | return res; 13 | } 14 | void print(){ 15 | printf(" %f , %f \n",x,y); 16 | } 17 | 18 | }; 19 | void move(PT &x, PT &t){ 20 | x=x+t; 21 | } 22 | int main(){ 23 | PT x(1,10),t(11,12); 24 | move(x,t); 25 | x.print(); 26 | } 27 | -------------------------------------------------------------------------------- /Graphics/algorithms/translation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int maxN=1e3; 4 | int graph[maxN][maxN]; 5 | int boundry,color; 6 | 7 | vector v={1,-1,0,0},h={0,0,-1,1}; 8 | bool check(int x,int y){ 9 | if(x>=0 and x=0 and y 2 | #include 3 | #include "graphics.hpp" 4 | 5 | using namespace std; 6 | class graphics{ 7 | public: 8 | FILE * pFile; 9 | graphics(){ 10 | //clear_screen(); 11 | } 12 | int clear_screen(){ 13 | pFile = fopen ("python/output.txt","w"); 14 | return 0; 15 | } 16 | int put_points(int x,int y){ 17 | pFile = fopen ("python/output.txt","a+"); 18 | fprintf(pFile,"%d %d\n",x,y); 19 | fclose(pFile); 20 | return 1; 21 | } 22 | int put_points(double x,double y){ 23 | pFile = fopen ("python/output.txt","a+"); 24 | fprintf(pFile,"%d %d\n",(int)x,(int)y); 25 | fclose(pFile); 26 | return 1; 27 | } 28 | }; 29 | 30 | 31 | 32 | 33 | 34 | // namespace std 35 | 36 | -------------------------------------------------------------------------------- /Graphics/c++ bindings/graphics.hpp: -------------------------------------------------------------------------------- 1 | int clear_screen(); 2 | int put_points(int x,int y); 3 | int put_points(double x,double y); 4 | -------------------------------------------------------------------------------- /Graphics/example_Bresenham.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for implementing 2 | // Mid-Point Circle Drawing Algorithm 3 | #include 4 | #include "graphics.cpp" 5 | using namespace std; 6 | 7 | // Implementing Mid-Point Circle Drawing Algorithm 8 | void midPointCircleDraw(int x_centre, int y_centre, int r) 9 | { 10 | graphics g; 11 | int x = r, y = 0; 12 | 13 | // Printing the initial point on the axes 14 | // after translation 15 | g.put_points( x_centre , y + y_centre); 16 | 17 | // When radius is zero only a single 18 | // point will be printed 19 | if (r > 0) 20 | { 21 | g.put_points( x + x_centre ,-y + y_centre ); 22 | g.put_points( y + x_centre ,x + y_centre ); 23 | g.put_points( -y + x_centre, x + y_centre); 24 | } 25 | 26 | // Initialising the value of P 27 | int P = 1 - r; 28 | while (x > y) 29 | { 30 | y++; 31 | 32 | // Mid-point is inside or on the perimeter 33 | if (P <= 0) 34 | P = P + 2*y + 1; 35 | // Mid-point is outside the perimeter 36 | else 37 | { 38 | x--; 39 | P = P + 2*y - 2*x + 1; 40 | } 41 | 42 | // All the perimeter points have already been printed 43 | if (x < y) 44 | break; 45 | 46 | // Printing the generated point and its reflection 47 | // in the other octants after translation 48 | g.put_points( x + x_centre , y + y_centre ); 49 | g.put_points( -x + x_centre , y + y_centre ); 50 | g.put_points( x + x_centre , -y + y_centre); 51 | g.put_points( -x + x_centre , -y + y_centre); 52 | 53 | // If the generated point is on the line x = y then 54 | // the perimeter points have already been printed 55 | if (x != y) 56 | { 57 | g.put_points( y + x_centre , x + y_centre ); 58 | g.put_points( -y + x_centre , x + y_centre ); 59 | g.put_points( y + x_centre , -x + y_centre ); 60 | g.put_points( -y + x_centre , -x + y_centre ); 61 | } 62 | } 63 | } 64 | 65 | // Driver code 66 | int main() 67 | { 68 | // To draw a circle of radius 3 centered at (0, 0) 69 | midPointCircleDraw(300, 300, 100); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Graphics/example_circle.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for implementing 2 | // Mid-Point Circle Drawing Algorithm 3 | #include 4 | #include "graphics.cpp" 5 | using namespace std; 6 | 7 | // Implementing Mid-Point Circle Drawing Algorithm 8 | void midPointCircleDraw(int x_centre, int y_centre, int r) 9 | { 10 | graphics g; 11 | int x = r, y = 0; 12 | 13 | // Printing the initial point on the axes 14 | // after translation 15 | g.put_points( x_centre , y + y_centre); 16 | 17 | // When radius is zero only a single 18 | // point will be printed 19 | if (r > 0) 20 | { 21 | g.put_points( x + x_centre ,-y + y_centre ); 22 | g.put_points( y + x_centre ,x + y_centre ); 23 | g.put_points( -y + x_centre, x + y_centre); 24 | } 25 | 26 | // Initialising the value of P 27 | int P = 1 - r; 28 | while (x > y) 29 | { 30 | y++; 31 | 32 | // Mid-point is inside or on the perimeter 33 | if (P <= 0) 34 | P = P + 2*y + 1; 35 | // Mid-point is outside the perimeter 36 | else 37 | { 38 | x--; 39 | P = P + 2*y - 2*x + 1; 40 | } 41 | 42 | // All the perimeter points have already been printed 43 | if (x < y) 44 | break; 45 | 46 | // Printing the generated point and its reflection 47 | // in the other octants after translation 48 | g.put_points( x + x_centre , y + y_centre ); 49 | g.put_points( -x + x_centre , y + y_centre ); 50 | g.put_points( x + x_centre , -y + y_centre); 51 | g.put_points( -x + x_centre , -y + y_centre); 52 | 53 | // If the generated point is on the line x = y then 54 | // the perimeter points have already been printed 55 | if (x != y) 56 | { 57 | g.put_points( y + x_centre , x + y_centre ); 58 | g.put_points( -y + x_centre , x + y_centre ); 59 | g.put_points( y + x_centre , -x + y_centre ); 60 | g.put_points( -y + x_centre , -x + y_centre ); 61 | } 62 | } 63 | } 64 | 65 | // Driver code 66 | int main() 67 | { 68 | // To draw a circle of radius 3 centered at (0, 0) 69 | midPointCircleDraw(300, 300, 100); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Graphics/example_dda.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for DDA line generation 2 | 3 | #include 4 | #include "graphics.cpp" 5 | using namespace std; 6 | 7 | // function for rounding off the pixels 8 | 9 | 10 | // Function for line generation 11 | void DDALine(int x0, int y0, int x1, int y1,auto g) 12 | { 13 | 14 | // Calculate dx and dy 15 | int dx = x1 - x0; 16 | int dy = y1 - y0; 17 | 18 | int step; 19 | 20 | // If dx > dy we will take step as dx 21 | // else we will take step as dy to draw the complete 22 | // line 23 | if (abs(dx) > abs(dy)) 24 | step = abs(dx); 25 | else 26 | step = abs(dy); 27 | 28 | // Calculate x-increment and y-increment for each step 29 | float x_incr = (float)dx / step; 30 | float y_incr = (float)dy / step; 31 | 32 | // Take the initial points as x and y 33 | float x = x0; 34 | float y = y0; 35 | 36 | for (int i = 0; i < step; i++) { 37 | 38 | // putpixel(round(x), round(y), WHITE); 39 | g.put_points(round(x),round(y)); 40 | x += x_incr; 41 | y += y_incr; 42 | // delay(10); 43 | } 44 | } 45 | 46 | // Driver code 47 | int main() 48 | { 49 | graphics g; 50 | int x0 = 400, y0 = 380, x1 = 180, y1 = 160; 51 | 52 | // Function call 53 | DDALine(x0, y0, x1, y1,g); 54 | 55 | return 0; 56 | } 57 | 58 | // all functions regarding to graphichs.h are commented out 59 | // contributed by hopelessalexander 60 | -------------------------------------------------------------------------------- /Graphics/example_ellipse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "graphics.cpp" 3 | 4 | using namespace std; 5 | 6 | void midptellipse(int rx, int ry, 7 | int xc, int yc) 8 | { 9 | graphics g; 10 | float dx, dy, d1, d2, x, y; 11 | x = 0; 12 | y = ry; 13 | 14 | // Initial decision parameter of region 1 15 | d1 = (ry * ry) - (rx * rx * ry) + 16 | (0.25 * rx * rx); 17 | dx = 2 * ry * ry * x; 18 | dy = 2 * rx * rx * y; 19 | int mg=100; 20 | // For region 1 21 | while (dx < dy) 22 | { 23 | 24 | // Print points based on 4-way symmetry 25 | //cout << x + xc << " , " << y + yc << endl; 26 | g.put_points(mg+x+xc,mg+y+yc); 27 | g.put_points(mg-x+xc,mg+y+yc); 28 | g.put_points(mg+x+xc,mg-y+yc); 29 | g.put_points(mg-x+xc,mg-y+yc); 30 | // cout << -x + xc << " , " << y + yc << endl; 31 | // cout << x + xc << " , " << -y + yc << endl; 32 | // cout << -x + xc << " , " << -y + yc << endl; 33 | 34 | // Checking and updating value of 35 | // decision parameter based on algorithm 36 | if (d1 < 0) 37 | { 38 | x++; 39 | dx = dx + (2 * ry * ry); 40 | d1 = d1 + dx + (ry * ry); 41 | } 42 | else 43 | { 44 | x++; 45 | y--; 46 | dx = dx + (2 * ry * ry); 47 | dy = dy - (2 * rx * rx); 48 | d1 = d1 + dx - dy + (ry * ry); 49 | } 50 | } 51 | 52 | // Decision parameter of region 2 53 | d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + 54 | ((rx * rx) * ((y - 1) * (y - 1))) - 55 | (rx * rx * ry * ry); 56 | 57 | // Plotting points of region 2 58 | while (y >= 0) 59 | { 60 | g.put_points(mg+x+xc,mg+y+yc); 61 | g.put_points(mg-x+xc,mg+y+yc); 62 | g.put_points(mg+x+xc,mg-y+yc); 63 | g.put_points(mg-x+xc,mg-y+yc); 64 | 65 | // Print points based on 4-way symmetry 66 | // cout << x + xc << " , " << y + yc << endl; 67 | // cout << -x + xc << " , " << y + yc << endl; 68 | // cout << x + xc << " , " << -y + yc << endl; 69 | // cout << -x + xc << " , " << -y + yc << endl; 70 | 71 | // Checking and updating parameter 72 | // value based on algorithm 73 | if (d2 > 0) 74 | { 75 | y--; 76 | dy = dy - (2 * rx * rx); 77 | d2 = d2 + (rx * rx) - dy; 78 | } 79 | else 80 | { 81 | y--; 82 | x++; 83 | dx = dx + (2 * ry * ry); 84 | dy = dy - (2 * rx * rx); 85 | d2 = d2 + dx - dy + (rx * rx); 86 | } 87 | } 88 | } 89 | 90 | int ellipse(){ 91 | graphics g; 92 | double x,y,a,b,d1,d2; 93 | a=300,b=300; 94 | x=0,y=b; 95 | d1=b*b*(x+1)*(x+1)+a*a*(y-.5)*(y-.5)-a*a*b*b; 96 | 97 | 98 | // while((pow(a,2)*pow(y-.5,2)0){ 110 | g.put_points(100+x,100+y); 111 | if(d2<0){ 112 | d2+=b*b*(2*x+2)+a*a*(-2*y+3); 113 | x++; 114 | } 115 | else{ 116 | d2+=a*a*(-2*y+3); 117 | y--; 118 | } 119 | } 120 | return 0; 121 | } 122 | int main(){ 123 | midptellipse(225, 115, 250, 350); 124 | 125 | 126 | 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /Graphics/graphics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "graphics.hpp" 4 | 5 | using namespace std; 6 | class graphics{ 7 | public: 8 | FILE * pFile; 9 | graphics(){ 10 | //clear_screen(); 11 | } 12 | int clear_screen(){ 13 | pFile = fopen ("python/output.txt","w"); 14 | return 0; 15 | } 16 | int put_points(int x,int y){ 17 | pFile = fopen ("python/output.txt","a+"); 18 | fprintf(pFile,"%d %d\n",x,y); 19 | fclose(pFile); 20 | return 1; 21 | } 22 | int put_points(double x,double y){ 23 | pFile = fopen ("python/output.txt","a+"); 24 | fprintf(pFile,"%d %d\n",(int)x,(int)y); 25 | fclose(pFile); 26 | return 1; 27 | } 28 | }; 29 | 30 | 31 | 32 | 33 | 34 | // namespace std 35 | 36 | -------------------------------------------------------------------------------- /Graphics/python/Graphics.h.py: -------------------------------------------------------------------------------- 1 | from random import random 2 | from turtle import onclick 3 | from kivy.app import App 4 | from kivy.uix.widget import Widget 5 | from kivy.uix.button import Button 6 | from kivy.uix.boxlayout import BoxLayout 7 | from kivy.graphics import Color, Ellipse, Line 8 | import time 9 | 10 | class MyPaintWidget(Widget): 11 | 12 | def on_touch_down(self, touch): 13 | color = (random(), 1, 1) 14 | with self.canvas: 15 | Color(*color, mode='hsv') 16 | d = 30. 17 | Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d)) 18 | touch.ud['line'] =Line() 19 | 20 | def on_touch_move(self, touch): 21 | touch.ud['line'].points += [touch.x, touch.y] 22 | 23 | from kivy.lang import Builder 24 | design=""" 25 | 26 | GridLayout: 27 | cols: 1 28 | size_hint: None, None 29 | size: self.minimum_size 30 | Button: 31 | size_hint: None, None 32 | size: 100, 44 33 | text: 'clear' 34 | on_state: self.clear_canvas 35 | Button: 36 | size_hint: None, None 37 | size: 100, 44 38 | text: 'plot' 39 | on_press: self.plot 40 | """ 41 | class MyPaintApp(App): 42 | 43 | def build(self): 44 | parent = Widget() 45 | self.painter = MyPaintWidget() 46 | box = BoxLayout() 47 | p=Button(text='Plot') 48 | p.bind(on_release=self.plot_btn) 49 | box.add_widget(p) 50 | 51 | clearbtn = Button(text='Clear') 52 | clearbtn.bind(on_release=self.clear_canvas) 53 | parent.add_widget(self.painter) 54 | #parent.add_widget(clearbtn) 55 | box.add_widget(clearbtn) 56 | parent.add_widget(box) 57 | return parent 58 | 59 | def clear_canvas(self, obj): 60 | self.painter.canvas.clear() 61 | with open('output.txt','w') as f: 62 | pass 63 | def plot_btn(self,obj): 64 | self.plot() 65 | def plot(self): 66 | with open('output.txt','r') as f: 67 | points=[] 68 | lt=f.readlines() 69 | for item in lt: 70 | #print(item.split()) 71 | points.append(list(map(int,item.split()))) 72 | #print(points) 73 | #lt=map(lambda a: a.split(),lt) 74 | color=(random(),1,1) 75 | with self.painter.canvas: 76 | Color(*color,mode='hsv') 77 | for point in points: 78 | d=10.0 79 | Ellipse(pos=(point[0]-d/2,point[1]-d/2),size=(d,d)) 80 | 81 | 82 | if __name__ == '__main__': 83 | MyPaintApp().run() 84 | -------------------------------------------------------------------------------- /Graphics/python/requirements.txt: -------------------------------------------------------------------------------- 1 | kivy 2 | python>=3.9 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Geek's Hub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Parser/Hello_World.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //This program is for demonstration of File_Parser 3 | //Run this file using 4 | //>>> g++ -o myprogram source.cpp 5 | //>>> ./myprogram 6 | using namespace std; 7 | //main part of program 8 | int main(){ 9 | //simple hello world program 10 | cout<<"hello world"; //now we print stuff 11 | //default return type 0 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Parser/Hello_World.cpp.md: -------------------------------------------------------------------------------- 1 | # Hello_World.cpp 2 | 3 | 4 | ```cpp 5 | #include 6 | 7 | 8 | ``` 9 | > ##### This program is for demonstration of File_Parser 10 | 11 | > ##### Run this file using 12 | 13 | 14 | > ##### >>> g++ -o myprogram source.cpp 15 | 16 | 17 | > ##### >>> ./myprogram 18 | 19 | 20 | 21 | ```cpp 22 | using namespace std; 23 | 24 | 25 | ``` 26 | > ##### main part of program 27 | 28 | 29 | ```cpp 30 | int main(){ 31 | 32 | 33 | ``` 34 | > ##### simple hello world program 35 | 36 | 37 | ```cpp 38 | cout<<"hello world"; //now we print stuff 39 | 40 | 41 | ``` 42 | > ##### default return type 0 43 | 44 | 45 | ```cpp 46 | return 0; 47 | 48 | 49 | } 50 | 51 | ``` 52 | ## Complete program 53 | ```cpp 54 | #include 55 | //This program is for demonstration of File_Parser 56 | //Run this file using 57 | //>>> g++ -o myprogram source.cpp 58 | //>>> ./myprogram 59 | using namespace std; 60 | //main part of program 61 | int main(){ 62 | //simple hello world program 63 | cout<<"hello world"; //now we print stuff 64 | //default return type 0 65 | return 0; 66 | } 67 | ``` -------------------------------------------------------------------------------- /Parser/__pycache__/parser.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geeks-hub-nith/Programming/fb71161f6c11f549b4a28c07f0cb24c497f54c22/Parser/__pycache__/parser.cpython-38.pyc -------------------------------------------------------------------------------- /Parser/parser.py: -------------------------------------------------------------------------------- 1 | import re,glob 2 | from pathlib import PurePath 3 | def parser(filepath): 4 | comment=re.compile(r'^//(.+)') 5 | path=PurePath(filepath) 6 | filename=path.name 7 | start=False 8 | with open (filepath,'r') as cpp: 9 | with open(f'{path.parent}/{filename}.md','w') as md: 10 | data=cpp.readlines() 11 | md.write(f'# {filename}\n ') 12 | 13 | for line in data: 14 | #print(line) 15 | 16 | rs=comment.search(line.strip()) 17 | if rs: 18 | #line=line.capitalize() 19 | if start: 20 | line=f'```\n{line.strip()}' 21 | start=False 22 | # print(rs.group()) 23 | line=line.replace('//',"> ##### ") 24 | else: 25 | if start==False: 26 | line=f"\n```cpp\n{line}" 27 | start=True 28 | else: 29 | pass 30 | if line not in ["",'\n']: 31 | md.write("\n"+line+'\n') 32 | md.write('\n ```') 33 | md.write("\n## Complete program ") 34 | md.write(f'\n ```cpp \n{"".join(data)} \n```') 35 | 36 | 37 | def find_files(dir): 38 | cpp=set() 39 | md=set() 40 | for file in glob.glob(dir,recursive= 41 | True): 42 | if file.endswith('.cpp'): 43 | cpp.add(file[:-4]) 44 | if file.endswith(".md"): 45 | md.add(file[:-3]) 46 | #print(file) 47 | return map(lambda item: f'{item}.cpp',cpp-md) 48 | 49 | # files_without_md = find_files('./**') 50 | # for file in files_without_md: 51 | # parser(file) 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Parser/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //This program is for demonstration of File_Parser 3 | //Run this file using 4 | //>>> g++ -o myprogram source.cpp 5 | //>>> ./myprogram 6 | using namespace std; 7 | //main part of program 8 | int main(){ 9 | //simple hello world program 10 | cout<<"hello world"; //now we print stuff 11 | //default return type 0 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Parser/test.cpp.md: -------------------------------------------------------------------------------- 1 | # test.cpp 2 | 3 | 4 | ```cpp 5 | #include 6 | 7 | 8 | ``` 9 | > ##### This program is for demonstration of File_Parser 10 | 11 | > ##### Run this file using 12 | 13 | 14 | > ##### >>> g++ -o myprogram source.cpp 15 | 16 | 17 | > ##### >>> ./myprogram 18 | 19 | 20 | 21 | ```cpp 22 | using namespace std; 23 | 24 | 25 | ``` 26 | > ##### main part of program 27 | 28 | 29 | ```cpp 30 | int main(){ 31 | 32 | 33 | ``` 34 | > ##### simple hello world program 35 | 36 | 37 | ```cpp 38 | cout<<"hello world"; //now we print stuff 39 | 40 | 41 | ``` 42 | > ##### default return type 0 43 | 44 | 45 | ```cpp 46 | return 0; 47 | 48 | 49 | } 50 | 51 | ``` 52 | ## Complete program 53 | ```cpp 54 | #include 55 | //This program is for demonstration of File_Parser 56 | //Run this file using 57 | //>>> g++ -o myprogram source.cpp 58 | //>>> ./myprogram 59 | using namespace std; 60 | //main part of program 61 | int main(){ 62 | //simple hello world program 63 | cout<<"hello world"; //now we print stuff 64 | //default return type 0 65 | return 0; 66 | } 67 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ✨ hacktoberfest2022 💫 2 | 3 | ![banner](https://hacktoberfest.digitalocean.com/_nuxt/img/logo-hacktoberfest-full.f42e3b1.svg) 4 | 5 | ## 🌱 Contributing to hacktoberfest 2022 6 | 7 | ### Welcome to hacktoberfest 2022 Public Repository. 👨🏻‍💻 8 |

A month-long celebration from October 1st - 31st sponsored by Digital Ocean and GitHub to get people involved in Open Source. Create your very first pull request to any public repository on GitHub and contribute to the open source developer community. 9 | 10 | https://hacktoberfest.digitalocean.com/

11 | 12 | ----- 13 | 14 | ### Process are as follows :- 15 | * Login using github @ [Hacktoberfest](https://hacktoberfest.digitalocean.com/) 16 | * Create four valid pull requests (PRs) between 1st - 31st October 17 | 18 | ------ 19 | ### How to Contribute to this repository 20 | 21 | * **Fork the repository (Click the Fork button in the top right of this page, click your Profile Image)** 22 | 23 | * Clone the forked repository to your local machine. 24 | ```bash 25 | git clone https://github.com/your-username/Programming/.git 26 | ``` 27 | 28 | * change the present working directory 29 | ```bash 30 | cd Programming/ 31 | ``` 32 | 33 | * Add/Create your project to the specific folder as described in what you can contribute section. 34 | 35 | * Make a new branch 36 | ```bash 37 | git checkout -b branch-name 38 | ``` 39 | 40 | * Make change in the repository with the new branch. 41 | 42 | * push the changes. 43 | ```bash 44 | git add . 45 | git commit -m "Your commit Message" 46 | git push origin branch-name 47 | ``` 48 | 49 | * Make a pull request. 50 | 51 | * Star the repository. 52 | 53 | ### NOTE 54 | 55 | * Make Sure you commit your changes in a new branch. 56 | * Make Sure you Give proper name to your files describing the addition. 57 | * Also Make Sure you comment your code whereever necessary. 58 | 59 | 60 | -------------------------------------------------------------------------------- /convert.py: -------------------------------------------------------------------------------- 1 | from Parser.parser import * 2 | import argparse 3 | 4 | 5 | # Initialize parser 6 | parser1 = argparse.ArgumentParser() 7 | 8 | # Adding optional argument 9 | parser1.add_argument("--f",nargs="+",help="enter files") 10 | 11 | # Read arguments from command line 12 | args = parser1.parse_args() 13 | 14 | if args.f: 15 | files_without_md = args.f 16 | else: 17 | files_without_md = find_files('./**') 18 | 19 | 20 | for file in files_without_md: 21 | parser(file) 22 | print("...Done") --------------------------------------------------------------------------------