-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,035 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"compilerPath": "C:/MinGW/bin/g++.exe", | ||
"cStandard": "c17", | ||
"cppStandard": "c++17", | ||
"intelliSenseMode": "windows-gcc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Add Edges | ||
Difficulty: MEDIUM | ||
Contributed By | ||
Vishal R S | ||
| | ||
Level 2 | ||
Avg. time to solve | ||
15 min | ||
Success Rate | ||
85% | ||
Problem Statement | ||
A bipartite graph is a graph where each node is either coloured black or white and that for any edge, the endpoints of the edge are in different colours. | ||
You are given an undirected tree of ‘N’ nodes. You can keep adding edges to it as long as the graph remains bipartite. What is the maximum number of edges you can add? | ||
Note - If there already exists an edge between two nodes, you cannot add another. You also cannot add an edge from any node to itself. However, cycles are permitted. | ||
For example: | ||
If there is a tree of 4 nodes, with edges (1,2), (2,3) and (1,4), you can add an edge between nodes 3 and 4 and the graph still remains bipartite. | ||
Input Format: | ||
The first line contains 'T', denoting the number of test cases. | ||
For each Test : | ||
The first line contains a single integer ‘N’, representing the number of elements. | ||
The next N-1 lines contain two integers ‘X’ and ‘Y’, indicating that there is an edge between node ‘X’ and ‘Y’. | ||
Output Format: | ||
For each test case, print one integer, the maximum number of edges you can add. | ||
Note: | ||
You are not required to print the expected output. It has already been taken care of. Just implement the function. | ||
Constraints - | ||
1 <= ‘T’ <= 10 | ||
2 <= ‘N’ <= 10^5 | ||
1 <= ‘X’,’Y’ <= N, X != Y | ||
Note: It is guaranteed that the sum of N across all test cases will be at most 10^5. | ||
Time Limit: 1 sec | ||
Sample Input 1: | ||
1 | ||
5 | ||
1 2 | ||
1 3 | ||
2 4 | ||
2 5 | ||
Sample Output 1 | ||
2 | ||
Explanation For Sample Input 1: | ||
For test case 1: | ||
We can add two edges (3,4) and (3,5) and the graph still remains bipartite. | ||
Sample Input 2: | ||
1 | ||
3 | ||
1 2 | ||
1 3 | ||
Sample Output 2: | ||
0 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int dfs(int node, int color, int p, vector<vector<int>>&adj){//will count no. of blacks | ||
int black= color;//initially 1 black | ||
//for particular node | ||
for(auto it: adj[node]){//traversing neigbours of node 1 | ||
if(it!=p){//p is parent | ||
black+=dfs(it, !color,node,adj);//oppo colo and new parent is node itself | ||
} | ||
} | ||
return black; | ||
} | ||
long long maxEdges(int n, vector<pair<int,int>> edges) | ||
{ | ||
// Write your code here. | ||
//any graph having odd length cycle can't be bipartite | ||
|
||
//creating a graph | ||
vector<vector<int>>adj(n+1); | ||
for(auto it:edges){//traversing edges vector | ||
adj[it.first].push_back(it.second); | ||
adj[it.second].push_back(it.first); | ||
} | ||
//color the graph into b and w | ||
//assuming 1 to be black, 0 to be white | ||
//1st node is black and parent in -1 in func call | ||
int b = dfs(1,1,-1,adj);//no of blk nodes | ||
int w = n-b;//whites | ||
//b*w - (n-1) | ||
//3 b and 2 w so 3*2=6 {6-4}=2 | ||
long long ans = 1LL * (b*w)-(n-1); | ||
return ans; | ||
|
||
} | ||
int main() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Count The People | ||
Difficulty: MEDIUM | ||
Contributed By | ||
Nihal Srivastava | ||
| | ||
Level 5 | ||
Avg. time to solve | ||
17 min | ||
Success Rate | ||
82% | ||
Problem Statement | ||
You and your friend recently watched a Mr Beast’s video; now you want to give away food just like him. There are infinite people arranged in a 2-D grid of an infinite length and breadth, each cell having one person. You and your friend are starting at the same cell of the grid. You also have a string ‘S’ of length ‘N’ consisting of the instructions you and your friend have to follow. You will follow the first instruction; your friend will follow the second instruction; you will follow the third instruction, and so on. | ||
The instructions are as follow: | ||
1) ‘U’ means to move one cell UP. | ||
2) ‘D’ means to move one cell DOWN. | ||
3) ‘L’ means to move one cell LEFT. | ||
4) ‘R’ means to move one cell RIGHT. | ||
Once you or your friend reach a cell (including the starting position), the person present in the cell will receive the food. Now you wonder how many distinct people got the food. | ||
For example: | ||
If the instruction is 'RU', you move one cell right and then you friend moves one cell up. Hence, in total 3 cells are visited. | ||
Input Format- | ||
First-line contains ‘T’, denoting the number of Test cases. | ||
For each Test case: | ||
The first line contains an integer, ‘N’, denoting the length of the string ‘S’. | ||
The following contains the string ‘S’ consisting only of the character {‘U’, ‘D’, ‘R’, ‘L’}. | ||
Output Format- | ||
For each test case, you have to print an integer denoting the number of distinct people that got the food. | ||
Note : | ||
You don’t need to print anything. It has already been taken care of. Just implement the given function. | ||
Constraints - | ||
1 <= ‘T’ <= 10 | ||
1 <= ‘N’ <= 10^5 | ||
Note- Sum of ‘N’ over all test cases does not exceed 10^5. | ||
Time Limit: 1 sec | ||
Sample Input-1 | ||
2 | ||
2 | ||
UL | ||
4 | ||
ULUL | ||
Sample Output-1 | ||
3 | ||
5 | ||
Explanation For Sample Input 1: | ||
For test case 1: | ||
You and your friend gave food to the person at the starting position. Then you went up by one cell and gave food to the person in that cell. Your friend went left by one cell and gave food to the person in that cell. Hence a total of three distinct people got the food. | ||
Sample Input -2 | ||
2 | ||
4 | ||
ULRD | ||
6 | ||
DRDRDR | ||
Sample Output -2 | ||
5 | ||
7 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int count(int n, string s) | ||
{ | ||
// Write your code here. | ||
int x1=0, y1=0;//my coordinates, x is for rows and y is for cols | ||
int x2=0, y2=0;//friend's coordinates | ||
unordered_set<int>st;//pos is visited or not | ||
st.insert(0*1e6+0);//at initial | ||
for(int i=0; i<n; ++i){ | ||
if(i%2==0){//me taking term | ||
if(s[i]=='U'){ | ||
x1--;//row number will reduce | ||
} | ||
else if(s[i]=='D'){ | ||
x1++;//row number incre | ||
} | ||
else if(s[i]=='L'){ | ||
y1--;//col reduces | ||
} | ||
else{ | ||
y1++;//col incre | ||
} | ||
//at end we will be standing somewhere | ||
st.insert(x1*1e6+y1); | ||
} | ||
else{//friend taking term | ||
if(s[i]=='U'){ | ||
x2--;//row number will reduce | ||
} | ||
else if(s[i]=='D'){ | ||
x2++;//row number incre | ||
} | ||
else if(s[i]=='L'){ | ||
y2--;//col reduces | ||
} | ||
else{ | ||
y2++;//col incre | ||
} | ||
st.insert(x2*1e6+y2); | ||
} | ||
} | ||
int cnt=st.size(); | ||
return cnt; | ||
} | ||
int main() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Minimize Bill | ||
Difficulty: EASY | ||
Contributed By | ||
Himanshu Airan | ||
| | ||
Level 4 | ||
Avg. time to solve | ||
10 min | ||
Success Rate | ||
80% | ||
Problem Statement | ||
You are buying some items from a grocery store and now it’s time to pay the bill. The bill is represented in the form of an integer array 'A' consisting of 'N' integers. The integers in array 'A' denotes the cost of items you purchased. | ||
You get a chance to make a change in the bill. In one change, you can select any index 'i' in array 'A' and multiply element 'A[i]' by -1. | ||
Your task is to perform exactly one change in the bill such that the sum of elements in array 'A', i.e., total amount you need to pay is the minimum possible. Return the minimum possible sum after making a change. | ||
Note : | ||
Cost of items (elements in 'A') and total amount of bill is allowed to be negative. | ||
Example : | ||
N = 4 | ||
A = [2, 3, 5, 1] | ||
You are supposed to perform exactly one change. The optimal way is performing change on A[2] = 5. Minimum sum of array (after change) becomes 2+3+(-5)+1 = 1. | ||
Input Format : | ||
The first line contains an integer 'T', denoting the number of test cases. | ||
Then the test cases follow: | ||
The first line of each test case contains an integer 'N', denoting the size of the array 'A'. | ||
The second line contains 'N' space-separated integers, denoting the array 'A' elements. | ||
Output Format : | ||
For each test case, print an integer denoting the minimum sum of array 'A' (amount you need to pay) after exactly one change is done. | ||
Print the output of each test case in a new line. | ||
Note : | ||
You are not required to print the expected output. It has already been taken care of. Just implement the function. | ||
Constraints - | ||
1 <= 'T' <= 10 | ||
1 <= 'N' <= 10^5 | ||
-10^6 <= 'A[i]' <= 10^6 | ||
Sum of 'N' over all test cases doesn’t exceed 10^5. | ||
Time Limit: 1 sec | ||
Sample Input 1 : | ||
2 | ||
5 | ||
-2 0 5 2 -1 | ||
4 | ||
5 8 7 9 | ||
Sample Output 1 : | ||
-6 | ||
11 | ||
Explanation For Sample Input 1 : | ||
For test case 1: | ||
The optimal way is to make change at A[2]. Minimum sum of array (after change) becomes -2+0+(-5)+2-1 = -6. | ||
For test case 2: | ||
Make change at A[3]. Minimum sum becomes 5+8+7+(-9) = 11. | ||
Sample Input 2 : | ||
3 | ||
4 | ||
3 -1 0 2 | ||
2 | ||
0 -1 | ||
5 | ||
5 5 5 5 5 | ||
Sample Output 2 : | ||
-2 | ||
-1 | ||
15 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
long long totalBill(int n, vector<int>& a) | ||
{ | ||
// Write your code here. | ||
|
||
int sum =0; | ||
int idx; | ||
int maxi = INT_MIN; | ||
for(int i=0; i<n; ++i){ | ||
if(a[i]>maxi){ | ||
maxi=a[i]; | ||
idx=i; | ||
} | ||
} | ||
a[idx]*=-1; | ||
for(int i=0; i<n; ++i){ | ||
sum+=a[i]; | ||
} | ||
return sum; | ||
} | ||
int main() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"compilerPath": "C:/MinGW/bin/g++.exe", | ||
"cStandard": "c17", | ||
"cppStandard": "c++17", | ||
"intelliSenseMode": "windows-gcc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
Oops, something went wrong.