Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# include <bits/stdc++.h>

using namespace std;

int main() {
int v, e, x, y;
cin>>v>>e;
list <int> ar[v], q; // store graph as adjacency list, visited nodes to be pushed into q
bool visited[v]; // boolean array to keep track of visited vertices
for (int i = 0; i < v; ++i)
visited[i] = 0; // set all vertices as unvisited
for (int i = 0; i < e; ++i) {
cin>>x>>y;
ar[x].push_back(y);
}
q.push_back(0);
visited[0] = 1; // visit first node 0
int s;
while (!q.empty()) { // BFS Code
s = q.front();
cout<<s<<" ";
q.pop_front();
for (list<int>::iterator it = ar[s].begin(); it != ar[s].end(); ++it) { // iterate through all the adjacent vertices of vertice s
if (!visited[*it]) {
q.push_back(*it); // add vertice to q if not previously visited
visited[*it] = 1;
}
}
}
return 0;
}
34 changes: 34 additions & 0 deletions 16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# include <bits/stdc++.h>

using namespace std;

int main() {
char str[100];
int N, n[3] = {0, 0, 0}; // To keep track of how many parentheses have been opened/closed
cin>>N;
for (int a = 0; a < N; ++a) {
cin>>str;
for (int i = 0; str[i] != '\0'; ++i) {
switch (str[i]) {
case '{': n[0] += 1; // Parentheses open
break;
case '(': n[1] += 1;
break;
case '[': n[2] += 1;
break;
case '}': n[0] -= 1; //Parentheses close
break;
case ')': n[1] -= 1;
break;
case ']': n[2] -= 1;
break;
}
}
if (n[0] == 0 && n[1] == 0 && n[2] == 0) // Check whether parentheses are valid
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}


}
8 changes: 8 additions & 0 deletions 17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from itertools import permutations

ar = list(map(int, str(input()).split())) # store digits in a list as integers

perms = list(permutations(ar + [ar[0]])) + list(permutations(ar + [ar[1]])) + list(permutations(ar + [ar[2]])) # generate all permutations of the pass code

for i in set(perms): # set to remove possible duplicates
print (''.join(str(j) for j in i))
26 changes: 26 additions & 0 deletions 3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# include <bits/stdc++.h>

using namespace std;

int main() {
int t, n;
cin>>t;
for (int i = 0; i < t; ++i){
cin>>n;
int ar[n], sum = 0;
for (int j = 0; j < n; ++j){
cin>>ar[j];
sum += ar[j];
}
for (int i1 = 0; i1 < n; ++i1) // starting index of sub array
for (int i2 = n - 1; i2 >= i1; --i2) { // ending index of sub array
int sub_sum = 0;
for (int k = i1; k <= i2; k++) // sum the sub array
sub_sum += ar[k];
if (sub_sum > sum)
sum = sub_sum;
}
cout<<sum<<endl;
}
return 0;
}