Skip to content

Commit fac9aa5

Browse files
authored
Merge pull request div-bargali#870
Addition of Readme file contents for each item and addition of Cycle Sort to C++ Algorithm Sorting part etc...
2 parents 8dba327 + 72dc725 commit fac9aa5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1955
-747
lines changed
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
### Backtracking-Algorithms
1+
### Backtracking-Algorithms
2+
- [CROSSWARD](CROSSWARD.cpp)
3+
4+
Using Crossward Algorithm, user inputs 10 words
5+
6+
Timing complexity = O(k)
7+
8+
- [Permutation of String](Permutation_of_string.cpp)
9+
10+
Premutation of string is a algorithm that functions to find all possible permutations of a given string, for example if there are given string "ABC" then ABC,ACB,BAC,BCA,CBA,CAB is the result
11+
12+
Time complexity = O(n^2*n!)
13+
14+
- [RAT IN MAZE](RAT_IN_MAZE_problem.cpp)
15+
16+
RAT IN MAZE is a algorithm to find all possible path in maze to escape using backtracking
17+
18+
Time complexity = O(2^(n^2))
19+
20+
- [Subsets](Subsets.cpp)
21+
22+
Subset is a alogrithm for a given set of positive integers that functions to find all its subset
23+
24+
Time complexity = O(2^n)
25+
26+
- [SudokuSolver](sudokuSolver.cpp)
27+
28+
SudokuSolver is a algorithm to solve sudoku which starts with some cells containing numbers, and the goal is to solve the remaining cells
29+
30+
Time complexity = O(9^(n*n))
31+
32+
- [NQueens](nqueens.cpp)
33+
34+
NQueens is a alogrithm when there is an arrangement of N queens on a N*N chess board, find out the number of N queens in which they do not collide with each other
35+
36+
Time complexity = O(2^n)
37+
38+
- [Word Break Problem](word_break_problem.cpp)
39+
40+
Word_Break_Problem is a algorithm when there is a valid sentence without any spaces between the words and a dictionary of valid English words, find all possible ways to break the sentence in individual dictionary words. For example when there is a example like "ilikeicecreamandmango" outputs will be "i like ice cream and mango", "i like ice cream and mango" if the dictionary were {i,like, ice, cream, and, icecream, mango}
41+
42+
Time complexity = O(2^(n*s)) (since we approach using recursive)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
### Divide-and-Conquer
2+
- [Painter's Partition Problem](painter%E2%80%99s%20partition%20problem.cpp)
3+
4+
Painter's Partition Problem is to find the minimum time when we have to paint n boards of length {A1,A2,....,An} and there are k painters available and each takes 1 unit time to paint 1 unit of board under the constraints that any painter will only paint continuous sections of boards like board {2,3,4} or only board {1}, or nothing but not board {2,4,5}
5+
6+
Time complexity = O(k*N^3)
7+
8+
- [Tower of Hanoi1](tower_of_hanoi.cpp), [Tower of Hanoi2](TowerOfHanoi.cpp)
9+
10+
Tower of Hanoi is a mathematic puzzle which consists of three towers and more than one rings. There are three rules in this algorithm in moving all the disks to some another tower without violationg the sequence of arrangement. 1. Only one disk can be moved among the towers at any given time 2. Only the top disk can be removed 3. No large disk can sit over a small disk
11+
12+
Time complexity = O(n)
Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
### Dynamic-Programming
1+
### Dynamic-Programming
2+
- [0-1 Knapsack Problem](0-1%20Knapsack%20Problem/0-1%20Knapsack%20Problem.cpp)
3+
4+
0-1 Knapsack Problem is a problem in combinatiorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. And for the 0-1 Knapsack problem, you cannot break an item, either pick the complete item or don't pick it(0-1 property)
5+
6+
Time complexity = O(2^n)
7+
8+
- [Matrix-Chain Multiplication](Matrix-Chain-Multiplication/matrix_chain_multiplication.cpp)
9+
10+
Matrix-Chain-Multiplication is a algorithm when there is a given sequence of matrices, find the most efficient way to multiply these matrices together. However the problem is not actually to perform the multiplications, but merly to decide in which order to perform the multiplications.
11+
12+
Time complexity = O(n^3)
13+
14+
- [SubsetSum](SubsetSum/Subset_Sum_dp.cpp)
15+
16+
SubsetSum is a algorithm with a given set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum. For example if set is {3,34,4,12,5,2} and sum is 9 then output will be true because there is a subset (4,5) with sum 9.
17+
18+
Time complexity = O(sum*n)
19+
20+
- [Activity Selection](Activity_Selection.cpp)
21+
22+
Activity Selection is a problem that given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
23+
24+
Time complexity = O(nlog n) (since input didn't sorted)
25+
26+
- [Coin Change Problem](Coin%20Change%20Problem.cpp)
27+
28+
Coin change problem, with a given value N, if we want to make change for N cents, and we have infinite supply of each of S = {S1, S2,... ,Sm} valued coins
29+
30+
Time complexity = O(mn)
31+
32+
- [Egg Droping Problem](egg_dropping_problem.cpp)
33+
34+
Egg dropping problem, suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. There are few assumptions which is an egg that survives a fall can be used again, a broken egg must be discarded, the effect of a fall is the same for all eggs, if an egg breaks when dropped, then it would break if dropped from a higher floor, if an egg survives a fall then it would break if dropped from a higher floor, if an egg survives a fall then it would survive a shorter fall, and it is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break.
35+
36+
Time complexity = O(n*k^2)
37+
38+
- [Josephus Problem](Josephus%20problem.cpp)
39+
40+
Josephus problem is a theoretical problem. There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. Goal is to choose the pace in the initial circle so that you are the last one remaining and so survive.
41+
42+
Time complexity = O(n)
43+
44+
- [Kadene Algorithm](Kadane-Algorithm.cpp)
45+
46+
kadane's algorithm is a algorithm with an given array of n integers, we have to look for all positive continuous segments of the array, and keep track of maximum sum contiguous segment among all positive segments.
47+
48+
Time complexity = O(n)
49+
50+
- [Longest Palindromic Sequence](Longest%20%20Palindromic%20Sequence.cpp)
51+
52+
Longest Palindromic Sequence which is Longest palindromic Subsequence. With a given sequence, find the length of the longest palindromic subsequence in it. For example, if the given sequence is BBABCBCAB then output will be 7 which is BABCBAB(which is the longest).
53+
54+
Time complexity = O(n^2)
55+
56+
- [Longest Common Subsequence](Longest%20Common%20Subsequence.cpp)
57+
58+
Longest Common Subsequence with given two sequences, find the length of longest subsequence present in both of them.
59+
60+
Time complexity = O(2^n)
61+
62+
- [Longest Bitonic Subsequence](longest_bitonic_subsequence.cpp)
63+
64+
Longest Bitonic Subsequence, with an given array arr[0....n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Our goal is to write a function that takes an array as an argument and returns the length of the longest bitonic subsequence. A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty.
65+
66+
Time complexity = O(n^2)
67+
68+
- [Longest Palindromic SubString](LongestPalindromicSubstring.cpp)
69+
70+
Longest Palindromic Substring, given a string, find the longest substring which is palindrome. For example if input is given as "forgeeksskeegfor" output will be "geeksskeeg"
71+
72+
Time complexity = O(n^3)
73+
74+
- [Max Profit With K Transactions](Max%20Profit%20with%20K%20transactions.cpp)
75+
76+
Max Profit with K transaction which is Maximum profit by buying and selling a share at most k times. In share trading, a buyer buys shares and sells on a future date. Given the stock price of n days, the trader is allowed to make at most k transactions, where a new transaction can only start after the previous transaction is complete, find out the maximum profit that a share trader could have made.
77+
78+
Time complexity = O(k*n^2)
79+
80+
- [Minimum No of Coins(Greedy Approach)](Minimum%20no%20of%20coins(Greedy%20Approach).cpp)
81+
82+
Minimum no of coins(Greedy Approach) is to find minimum number of coins that make a given value. Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = {C1,C2,..,Cm} valued coins, we have to find the minimum number of coins to make the change.
83+
84+
Time complexity = O(mv)
85+
86+
- [Minimum Number of Jumps](Minimum%20Number%20of%20Jumps.cpp)
87+
88+
Minimum Number of Jumps, given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array(starting from the first element). If an element is 0, they cannot move through that element.
89+
90+
Time complexity = O(n^n)
91+
92+
- [Unique_Paths](Unique_Paths.cpp)
93+
94+
Unique Paths, to count all the possible paths from top left to bottom right of a m x n matrix with the constrains that from each cell you can either move only to right or down.
95+
96+
Time complexity = O(mn)
97+
98+
- [WildCard_Matching](Wildcard_matching.cpp)
99+
100+
Wildcard matching, with a given text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text(not partial text).
101+
102+
Time complexity = O(mn)
103+
104+
- [WordBreak](wordbreak.cpp)
105+
106+
wordbreak is a problem with a given input string and a dictonary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words.
107+
108+
Time complexity = O(n*s)

C++/Algorithms/Factorial/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### Factorial
2+
- [FactRecursion](factRecursion.cpp)
3+
4+
FactRecursion, which is a program for factorial of a number. Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example, we can think of factorial 5 is = 5x4x3x2x1 = 120
5+
6+
Time complexity = O(n)
7+
8+
- [FactUsual](factUsual.cpp)
9+
10+
FactUsual, which is a same problem above(which is factorial recursion), but calculating factorial of a number in an iterative way.
11+
12+
Time complexity = O(n)
Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
1-
### Graph-Algorithms
1+
### Graph-Algorithms
2+
- [A Star Search Algorithm](A_star_Search_Algorithm.cpp)
3+
4+
A_star_Search_Algorithm is one of the best and popular technique used in path-finding and graph traversals. Consider a square grid having many obstacles and we are given a starting cell and a target cell. We want to reach the target cell from the starting cell as quickly as possible.
5+
6+
Time complexity = O(E) (E is the number of edges in the graph)
7+
8+
- [Articulation Point and Bridges](ArticulationPointandBridges.cpp)
9+
10+
ArticulationPointandBridges, a vertex in an undirected connected graph is an undirected connected graph is an articulation point if and only if removing it (and edges through it) disconncets the grapth. Articulation points represent vulnerabilities in a connected network - single points whose failure would split the network into 2 or more components. For a disconnected undirected graph, an articulation point is a vertex removing which increases number of connected components.
11+
12+
Time complexity = O(V*(V+E))
13+
14+
- [Bipartite Graph](Bipartite_Graph.cpp)
15+
16+
Bipartite_Graph, which is a graph whose vertices can be divided into two independent sets, U and V such that every edge(u,v) either connects a vertex from U to V or a from V to U. A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.
17+
18+
Time complexity = O(V+E)
19+
20+
- [Breadth First Search](Breadth%20First%20Search.cpp)
21+
22+
Breadth First Search(BFS) is an algorithm for traversing or searching tree or garph data structures. It starts at the tree root(or some arbitrary node of a graph) and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
23+
24+
Time complexity = O(V+E)
25+
26+
- [Component Count](Component_Count.cpp)
27+
28+
Component_count, with a given undirected graph G, the task is to print the number of connected components in the graph. To approach, we use a variable count to store the number of connected components and to the following steps. 1. Initialize all vertices as unvisited. 2. For all the vertices check if a vertex has not been visited, then perform DFS on that vertex and increment the variable count by 1.
29+
30+
Time complexity = O(|V|+|E|)
31+
32+
- [Cycle Detection Directed Graph](Cycle_detection_directed_graph.cpp)
33+
34+
Cycle_detection_directed_graph, with a given directed graph, check whether the graph contains a cycle or not. Function we created should return true if the given graph contains at least one cycle, else return false.
35+
36+
Time complexity = O(V+E)
37+
38+
- [Depth First Search](Depth%20First%20Search.cpp)
39+
40+
Depth First Search is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores as far as possible along each branch before backtracking.
41+
42+
Time complexity = O(V+E)
43+
44+
- [Dijkstra's Algorithm](Dijkstra's%20Algorithm.cpp)
45+
46+
The shortest path tree is built up, edge by edge. We maintain two sets: a set of the vertices already included in the tree and the set of the vertices not yet included. The Greedy choice is to pick the edge that connects the two sets and is on the smallest weight path from source to the set that contains not yet included vertices.
47+
48+
Time complexity = O(|E|+|V|log|V|)
49+
50+
- [FordFulkerson](FordFlkerson.cpp)
51+
52+
FordFulkerson is a algorithm with a given graph which represents a flow network where every edge has a capacity. Also given two vertices source 's' and sink 't' in the graph, we have to find the maximum possible flow from s to t with following constraints which is Flow on an edge doesn't exceed the given capacity of the edge and incoming flow is equal to outgoing flow for every vertex except s and t.
53+
54+
Time complexity = O(max_flow*E)
55+
56+
- [Kosaraju or Kosaraju-Sharir Algorithm](Kosaraju.cpp)
57+
58+
Kosaraju or kosaraju-Sharir algorithm is a linear time algorithm to find the strongly connected components of a directed graph.
59+
60+
Time complexity = O(V+E)
61+
62+
- [Kruskal's Minimum Spanning Tree](Kruskal's.cpp)
63+
64+
In Kruskal's algorithm, we create a MST(Minimum Spanning Tree) by picking edges one by one. The Greedy Choice is to pick the smallest weight edge that doesn't cause a cycle in the MST constructed so far.
65+
66+
Time complexity = O(Elog V) (V is the number of vertices)
67+
68+
- [Number of Islands](Number_of_islands.cpp)
69+
70+
Number_of_Islands is a algorithm with an given boolean 2D matrix, find the number of islands. In this algorithm island is a group of connected 1s that forms an island. We can find value by counting the number of connected components in an undirected graph. (connected component of an undirected graph is a subgraph in which every two vertices are connected to each other by a path(s), and which is connected to no other vertices outside the subgraph).
71+
72+
Time complexity = O(ROW x COL)
73+
74+
- [Prim's Minimum Spanning Tree](prims.cpp)
75+
76+
We create a MST by picking edges one by one in Prim's algorithm. We maintatin two sets: a set of the vertices already included in MST and the set of the vertices not yet included. The Greedy choice is to pick the smallest weight edge that connects the two sets.
77+
78+
Time complexity = O((V+E)log V)
79+
80+
- [Topological Sort](Topological_sort.cpp)
81+
82+
Topological_sort is sorting algorithm for directed acyclic graph, which is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. But if graph is not a DAG, topological sorting is not possible.
83+
84+
Time complexity = O(V+E)
85+
86+
- [Transitive Closure](Transitive_Closure.cpp)
87+
88+
Transitive_Closure is a algorithm, with given a directed graph, find out if a vertex v is reachable from another vertex u for all vertex pairs (u,v) in the given graph. In this algorithm, reachable mean that there is a path from vertex u to v. The reachability matrix is called transitive closure of a graph.
89+
90+
Time complexity = O(v^2)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
// your code goes here
6+
// this program will give an intuition behind the solution for CP questions based on interval quries. Refer to the question
7+
//http://codeforces.com/gym/294377/problem/E
8+
9+
//this following two lines are used for fast input-output
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
int t;
14+
cin>>t;
15+
while(t--) //running the program for 't' test cases
16+
{
17+
int n;
18+
cin>>n; //number of students in th college
19+
20+
//declare an array of max size (based on the input constraint) to indicate the timeline of all the students
21+
int N=2*100000;
22+
int a[N]={0};
23+
24+
for(int i=0;i<n;i++)
25+
{
26+
int x,y;
27+
cin>>x>>y;
28+
// while taking the entering and leaving time of each student, increase the (x-1)th index by one and decrease the (y-1)th index by 1.
29+
a[x-1]+=1;
30+
a[y-1]-=1;// Here the student is outside the college during the yth sec, hence we subtract 1 from (y-1)th index. Else we would subtract the same from yth index
31+
}
32+
for(int i=1;i<2*100000;i++)
33+
{
34+
// Summing the value in the current index by its previous index's value will give the total number of students at a perticular instance of time on the timeline a[N]
35+
a[i]+=a[i-1];
36+
}
37+
int q;
38+
cin>>q;
39+
while(q--)// running a loop to ans all the queries
40+
{
41+
int pp;
42+
cin>>pp;// The required time to output number of students present at that particular instance
43+
44+
// We can find out the number of students present at that perticular time by accessing the value stored in the array 'a' at (pp-1) index
45+
cout<<a[pp-1]<<endl;
46+
}
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)