Skip to content

Commit 0e4e07b

Browse files
Add files via upload
1 parent a070d70 commit 0e4e07b

9 files changed

+506
-0
lines changed

BestLine.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// Best Line
2+
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int pointsOnLine(int n, vector < vector < int >> & arr) {
8+
sort(arr.begin(),arr.end());
9+
int counter=0;
10+
for(int i=0;i<n;i++){
11+
unordered_map<double,int> mp;
12+
int yAxis=0;
13+
for(int j=i+1;j<n;j++){
14+
int dy=arr[j][1]-arr[i][1];
15+
int dx=arr[j][0]-arr[i][0];
16+
if(dx==0){
17+
yAxis++;
18+
}else{
19+
double slope=(double)dy/dx;
20+
mp[slope]++;
21+
}
22+
}
23+
for(auto it : mp){
24+
counter=max(counter,it.second);
25+
}
26+
counter=max(counter,yAxis);
27+
}
28+
return counter+1;
29+
}

BreakTheBoard.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// Breaak The Board :
2+
3+
#include <bits/stdc++.h>
4+
int minimumCost(vector<int> row, vector<int> column, int l, int w) {
5+
sort(row.begin(),row.end());
6+
sort(column.begin(),column.end());
7+
int result=0, h=1, v=1;
8+
int i=l-2, j=w-2;
9+
while(i>=0 and j>=0){
10+
if(row[i]>column[j]){
11+
result+=(row[i]*v);
12+
i--;
13+
h++;
14+
}else{
15+
result+=(column[j]*h);
16+
j--;
17+
v++;
18+
}
19+
}
20+
while(i>=0){
21+
result+=(row[i]*v);
22+
i--;
23+
h++;
24+
}
25+
while(j>=0){
26+
result+=(column[j]*h);
27+
j--;
28+
v++;
29+
}
30+
return result;
31+
}

CarPooling.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
3+
You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.
4+
Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
5+
6+
Example 1:
7+
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
8+
Output: false
9+
10+
Example 2:
11+
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
12+
Output: true
13+
*/
14+
15+
16+
class Solution {
17+
public:
18+
bool carPooling(vector<vector<int>>& trips, int capacity) {
19+
// Breaking trips into 2 parts each : {from,enter,cap} and {to,leave,cap}
20+
// Sort on start points then {maintain 0/1 for leave/enter};
21+
vector<vector<int>> nums;
22+
for(int i=0;i<trips.size();i++){
23+
int cap=trips[i][0], low=trips[i][1], high=trips[i][2];
24+
nums.push_back({low,1,cap});
25+
nums.push_back({high,0,cap});
26+
}
27+
sort(nums.begin(),nums.end());
28+
int currCap=capacity;
29+
for(int i=0;i<nums.size();i++){
30+
int choice=nums[i][1], cap=nums[i][2];
31+
if(choice==1){
32+
currCap-=cap;
33+
if(currCap<0) return false;
34+
}else{
35+
currCap+=cap;
36+
}
37+
}
38+
return true;
39+
}
40+
};

CourseSchedule3.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
There are n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [durationi, lastDayi] indicate that the ith course should be taken continuously for durationi days and must be finished before or on lastDayi.
3+
You will start on the 1st day and you cannot take two or more courses simultaneously.
4+
Return the maximum number of courses that you can take.
5+
6+
Example 1:
7+
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
8+
Output: 3
9+
Explanation:
10+
There are totally 4 courses, but you can take 3 courses at most:
11+
First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.
12+
Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day.
13+
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
14+
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
15+
16+
Example 2:
17+
Input: courses = [[1,2]]
18+
Output: 1
19+
20+
Example 3:
21+
Input: courses = [[3,2],[4,3]]
22+
Output: 0
23+
*/
24+
25+
class Solution {
26+
public:
27+
int scheduleCourse(vector<vector<int>>& courses) {
28+
sort(courses.begin(),courses.end(),[&](vector<int> &a, vector<int> &b){
29+
if(a[1]==b[1]) return a[0]<b[0];
30+
return a[1]<b[1];
31+
});
32+
priority_queue<int> pq;
33+
int timeLine=0;
34+
for(auto it : courses){
35+
if(it[0]>it[1]) continue;
36+
if(pq.empty() or timeLine+it[0]<=it[1]){
37+
pq.push(it[0]);
38+
timeLine+=it[0];
39+
}else if(pq.top()>it[0]){
40+
timeLine-=pq.top();
41+
timeLine+=it[0];
42+
pq.pop();
43+
pq.push(it[0]);
44+
}
45+
}
46+
return pq.size();
47+
}
48+
};

DividesChocolates.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Divides Chocolates
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
bool isValid(vector<int> &arr, int k, int n, int mid){
7+
int pieces=0, currSum=0;
8+
for(int i=0;i<n;i++){
9+
if(currSum+arr[i]>=mid){
10+
pieces++;
11+
currSum=0;
12+
}else{
13+
currSum+=arr[i];
14+
}
15+
}
16+
17+
return pieces>=k;
18+
}
19+
20+
int getMaximumSweetness(vector<int> &arr, int k)
21+
{
22+
if(k+1>arr.size()) return 0;
23+
// max of min
24+
int n=arr.size();
25+
int low=*min_element(arr.begin(),arr.end());
26+
int high=accumulate(arr.begin(),arr.end(),0);
27+
int result=low;
28+
while(low<=high){
29+
int mid=low+(high-low)/2;
30+
if(isValid(arr,k+1,n,mid)){
31+
result=mid;
32+
low=mid+1;
33+
}else {
34+
high=mid-1;
35+
}
36+
}
37+
return result;
38+
}
39+

GraphOnAnArray.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/// Graph On an array : Codechef
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
class DSU{
7+
vector<int> parent,rank,size;
8+
public:
9+
DSU(int n){
10+
rank.resize(n+1,0);
11+
parent.resize(n+1);
12+
size.resize(n+1);
13+
for(int i=1;i<=n;i++){
14+
parent[i]=i;
15+
size[i]=1;
16+
}
17+
}
18+
int findParent(int node){
19+
if(node==parent[node]) return node;
20+
return parent[node]=findParent(parent[node]);
21+
}
22+
void unionByRank(int u, int v){
23+
int ulp_u=findParent(u);
24+
int ulp_v=findParent(v);
25+
if(ulp_u==ulp_v) return;
26+
if(rank[ulp_u]<rank[ulp_v]) parent[ulp_u]=ulp_v;
27+
else if(rank[ulp_u]>rank[ulp_v]) parent[ulp_v]=ulp_u;
28+
else{
29+
parent[ulp_v]=ulp_u;
30+
rank[ulp_u]++;
31+
}
32+
}
33+
void unionBySize(int u, int v){
34+
int ulp_u=findParent(u);
35+
int ulp_v=findParent(v);
36+
if(ulp_u==ulp_v) return;
37+
if(size[ulp_u]<size[ulp_v]){
38+
parent[ulp_u]=ulp_v;
39+
size[ulp_v]+=size[ulp_u];
40+
}
41+
else{
42+
parent[ulp_v]=ulp_u;
43+
size[ulp_u]+=size[ulp_v];
44+
}
45+
}
46+
};
47+
48+
vector<int> prime{29, 31, 37, 41, 43, 47};
49+
50+
void solve(){
51+
map<int,int> mp;
52+
int n;
53+
cin>>n;
54+
vector<int> arr(n+1);
55+
for(int i=1;i<=n;i++){
56+
cin>>arr[i];
57+
mp[arr[i]]++;
58+
}
59+
DSU ds(n);
60+
for(int i=1;i<n;i++){
61+
for(int j=i+1;j<=n;j++){
62+
if(__gcd(arr[i],arr[j])==1){
63+
ds.unionBySize(i,j);
64+
}
65+
}
66+
}
67+
bool flag=false;
68+
int x=ds.findParent(1);
69+
for(int i=2;i<=n;i++){
70+
int ax1=ds.findParent(i);
71+
if(ax1!=x){
72+
flag=true;
73+
break;
74+
}
75+
}
76+
if(flag){
77+
for(auto it : prime){
78+
if(mp[it]==0){
79+
arr[1]=it;
80+
break;
81+
}
82+
}
83+
cout<<1<<endl;
84+
}else{
85+
cout<<0<<endl;
86+
}
87+
for(int i=1;i<=n;i++){
88+
cout<<arr[i]<<" \n"[i==n];
89+
}
90+
}
91+
92+
93+
int main() {
94+
ios_base::sync_with_stdio(0);
95+
cin.tie(0);
96+
cout.tie(0);
97+
98+
int t;
99+
cin>>t;
100+
while(t--){
101+
solve();
102+
}
103+
return 0;
104+
}

0 commit comments

Comments
 (0)