Skip to content

Commit 301a0ae

Browse files
committed
Bellman ford algo from gfg
1 parent 4740387 commit 301a0ae

11 files changed

+510
-0
lines changed

Diff for: GFG/Negative_weight_cycle.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// { Driver Code Starts
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// } Driver Code Ends
6+
class Solution {
7+
public:
8+
int isNegativeWeightCycle(int n, vector<vector<int>>edges){
9+
// Code here
10+
vector<int> vertex(n,INT_MAX);
11+
vertex[0]=0;
12+
int V=edges.size();
13+
for(int i=1;i<n;i++){
14+
for(auto x:edges){
15+
int u=x[0],v=x[1],w=x[2];
16+
if(vertex[u]!=INT_MAX&&vertex[v]>vertex[u]+w){
17+
vertex[v]=vertex[u]+w;
18+
}
19+
}
20+
}
21+
for(auto x:edges){
22+
int u=x[0],v=x[1],w=x[2];
23+
if(vertex[u]!=INT_MAX&&vertex[u]+w<vertex[v]){
24+
return 1;
25+
}
26+
}
27+
return 0;
28+
}
29+
};
30+
31+
// { Driver Code Starts.
32+
int main(){
33+
int tc;
34+
cin >> tc;
35+
while(tc--){
36+
int n, m;
37+
cin >> n >> m;
38+
vector<vector<int>>edges;
39+
for(int i = 0; i < m; i++){
40+
int x, y, z;
41+
cin >> x >> y >> z;
42+
edges.push_back({x,y,z});
43+
}
44+
Solution obj;
45+
int ans = obj.isNegativeWeightCycle(n, edges);
46+
cout << ans <<"\n";
47+
}
48+
return 0;
49+
} // } Driver Code Ends

Diff for: Hackerearth/Practice/colorful_floors.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int N,K;
7+
cin>>N>>K;
8+
if(k<=N)
9+
{
10+
cout<<0<<endl;
11+
}
12+
else
13+
{
14+
15+
}
16+
}

Diff for: Hackerearth/Practice/distribute_chocolates.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int T;
7+
// yanh pe agar ans and k ko else ke andar declare karo
8+
// ya int declare kiye to wrong dega.
9+
long int c,n,ans,k;
10+
cin>>T;
11+
for(int i=0;i<T;i++)
12+
{
13+
cin>>c>>n;
14+
if(c<(n*(n+1))/2)
15+
{
16+
cout<<c<<endl;
17+
}
18+
else
19+
{
20+
k=(c-(n*(n-1))/2)/n;
21+
ans =c-n*k-(n*(n-1))/2;
22+
cout<<ans<<endl;
23+
}
24+
}
25+
}

Diff for: Hackerearth/Practice/equal_subarray.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int N,K;
7+
cin>>N>>K;
8+
int *arr=new int[N];
9+
for(int i=0;i<N;i++)
10+
{
11+
cin>>arr[i];
12+
}
13+
cout<<"array is ";
14+
for(int i=0;i<N;i++)
15+
{
16+
cout<<arr[i];
17+
}
18+
}

Diff for: Hackerearth/Practice/fredo_and_array_update.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int N,sum=0,d;
7+
cin>>N;
8+
int *arr = new int[N];
9+
for(int i=0;i<N;i++)
10+
{
11+
cin>>arr[i];
12+
sum = sum+arr[i];
13+
}
14+
if(sum%N==0)
15+
d=sum/N;
16+
else
17+
d=(sum/N);
18+
cout<<d+1;
19+
}

Diff for: Hackerearth/Practice/little_shino_common_factor.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<iostream>
2+
#include<math.h>
3+
using namespace std;
4+
long int hcf(long int a,long int b);
5+
6+
int main()
7+
{
8+
long int a,b,count=0,temp;
9+
cin>>a>>b;
10+
if(a>b)
11+
{
12+
temp=a;
13+
a=b;
14+
b=temp;
15+
}
16+
temp=hcf(a,b);
17+
for(long int i=1;i<=sqrt(temp)+1;i++)
18+
{
19+
if(a%i==0 && b%i==0)
20+
{
21+
count ++;
22+
}
23+
}
24+
cout<<count;
25+
}
26+
27+
long int hcf(long int a,long int b)
28+
{
29+
if(b%a==0)
30+
return b;
31+
else
32+
return hcf(b,b%a);
33+
}

Diff for: Hackerearth/Practice/set_numbers.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
using namespace std;
3+
#include<math.h>
4+
5+
6+
int main()
7+
{
8+
int t,N,z=0;
9+
int *arr=new int[31];
10+
for(int i=0;i<=30;i++)
11+
{
12+
arr[i]=(pow(2,i)-1);
13+
}
14+
cin>>t;
15+
for(int i=0;i<t;i++)
16+
{
17+
cin>>N;
18+
int previous;
19+
for(int j=0;j<30;j++)
20+
{
21+
previous=arr[j];
22+
if(N<arr[j+1])
23+
{
24+
cout<<previous<<endl;
25+
break ;
26+
}
27+
}
28+
}
29+
}

Diff for: Hackerearth/Practice/zoos.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
#include<string.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int len=0,c_z=0,c_o=0;
9+
string str;
10+
cin>>str;
11+
len = str.length();
12+
for(int i=0;i<len;i++)
13+
{
14+
if(str[i]=='z')
15+
c_z++;
16+
else
17+
c_o++;
18+
}
19+
if(c_o==2*c_z)
20+
cout<<"Yes"<<endl;
21+
else
22+
cout<<"No"<<endl;
23+
}

Diff for: LeetCode/Practice/113.Path_Sum_II.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
typedef long long ll ;
7+
typedef unsigned long long ull;
8+
typedef vector<int> vl;
9+
typedef vector<vector<int>> vvl;
10+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
11+
/* Abbrevations */
12+
#define ff first
13+
#define ss second
14+
#define mp make_pair
15+
#define pb push_back
16+
// loops
17+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
18+
// Some print
19+
#define no cout<<"NO"<<endl;
20+
#define yes cout<<"YES"<<endl;
21+
// sort
22+
#define all(V) (V).begin(),(V).end()
23+
#define srt(V) sort(all(V))
24+
#define srtGreat(V) sort(all(V),greater<ll>())
25+
// some extra
26+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} cout<<endl;
27+
#define precision(x) cout<<fixed<<setprecision(x);
28+
#define sz(V) ll(V.size())
29+
30+
31+
/* ascii value
32+
A=65,Z=90,a=97,z=122
33+
*/
34+
35+
/* Some syntax
36+
//Syntax to create a min heap for priority queue
37+
//priority_queue <int, vector<int>, greater<int>>pq;
38+
*/
39+
/* --------------------MAIN PROGRAM----------------------------*/
40+
// to run ctrl+b
41+
const ll INF=1e18;
42+
const ll mod1=1e9+7;
43+
const ll mod2=998244353;
44+
// Techniques :
45+
// divide into cases, brute force, pattern finding
46+
// sort, greedy, binary search, two pointer
47+
// transform into graph
48+
49+
// Experience :
50+
// Cp is nothing but only observation and mathematics.
51+
52+
53+
//Add main code here
54+
55+
/**
56+
* Definition for a binary tree node.
57+
* struct TreeNode {
58+
* int val;
59+
* TreeNode *left;
60+
* TreeNode *right;
61+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
62+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
63+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
64+
* };
65+
*/
66+
class Solution {
67+
public:
68+
vector<int> path;
69+
vector<vector<int>> all_path;
70+
71+
void dfs(TreeNode* root,int targetSum,int& sum){
72+
if(root==nullptr){
73+
return ;
74+
}
75+
sum+=root->val;
76+
path.pb(root->val);
77+
if(root->left==nullptr&&root->right==nullptr&&sum==targetSum){
78+
all_path.pb(path);
79+
}
80+
dfs(root->left,targetSum,sum);
81+
dfs(root->right,targetSum,sum);
82+
sum-=root->val;
83+
path.pop_back();
84+
return ;
85+
}
86+
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
87+
int sum=0;
88+
dfs(root,targetSum,sum);
89+
return all_path;
90+
}
91+
};
92+
93+
94+
95+
/* -----------------END OF PROGRAM --------------------*/
96+
/*
97+
* stuff you should look before submission
98+
* constraint and time limit
99+
* int overflow
100+
* special test case (n=0||n=1||n=2)
101+
* don't get stuck on one approach if you get wrong answer
102+
*/

0 commit comments

Comments
 (0)