diff --git a/CodeChef_problems/ChefinaAndSwaps.py b/CodeChef_problems/ChefinaAndSwaps.py new file mode 100644 index 00000000..dcc33ebe --- /dev/null +++ b/CodeChef_problems/ChefinaAndSwaps.py @@ -0,0 +1,54 @@ +''' +Problem Link - https://www.codechef.com/JULY20B/problems/CHFNSWPS +''' + +from sys import * +from math import * +from collections import * + +mod = 1000000007 + +def get_array(): return list(map(int, stdin.readline().split())) + +def get_ints(): return map(int, stdin.readline().split()) + +def get_int(): return int(stdin.readline()) + +def get_input(): return stdin.readline().strip() + +def main(): + tc = get_int() + while(tc): + N = get_int();arr = get_array();arr1 = get_array() + mini = min(min(arr),min(arr1)) + counter = Counter(arr+arr1) + set,dic,dic1 = 0,{},{} + for i in counter: + if counter[i]&1:set = 1;break + else: + dic[i] = counter[i]/2 + if set:stdout.write("-1\n"); + else: + dic1,list1,list2 = dic,[],[] + for i in range(N): + if dic[arr[i]]: + dic[arr[i]] -= 1 + else: + list1.append(arr[i]) + for i in range(N): + if dic1[arr1[i]]: + dic1[arr1[i]] -= 1 + else: + list2.append(arr1[i]) + list1.sort();list2.sort(reverse=True) + length = len(list1) + if length==0:stdout.write('0\n') + else: + ans = 0 + for i in range(length): + ans += min(2*mini,min(list1[i],list2[i])) + stdout.write(str(ans) + '\n') + tc-=1 + +if __name__ == "__main__": + main() diff --git a/CodeChef_problems/Long_contest_and_cook_off_overlaps.py b/CodeChef_problems/Long_contest_and_cook_off_overlaps.py new file mode 100644 index 00000000..9209e700 --- /dev/null +++ b/CodeChef_problems/Long_contest_and_cook_off_overlaps.py @@ -0,0 +1,13 @@ +t = int(input()) +year = [0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0] + +for _ in range(t): + m1,y1 = map(int,input().split()) + m2,y2 = map(int,input().split()) + if(m1<=2): + y1-=1 + if(m2<2): + y2-=1 + value1 = (y1//400)*101 + sum(year[:y1%400]) + value2 = (y2//400)*101 + sum(year[:y2%400]) + print(value2 - value1) \ No newline at end of file diff --git a/CodeChef_problems/expected_change.py b/CodeChef_problems/expected_change.py new file mode 100644 index 00000000..d1a08319 --- /dev/null +++ b/CodeChef_problems/expected_change.py @@ -0,0 +1,28 @@ +def fun(s): + ans = 0 + o = 0 + c = 0 + for i in s: + if(i=="("): + o+=1 + elif(i==")"): + c+=1 + if(c>o): + c-=1 + o+=1 + ans+=1 + return ans + +t = int(input()) + +for _ in range(t): + n = int(input()) + s = input() + ans = 0 + N = 10**9+7 + value = ((n+1)*n)//2 + value = pow(value,N-2,N) + print(value) + for i in range(n): + ans+=fun(s[i:]) + print((ans*value)%N) \ No newline at end of file diff --git a/CodeChef_problems/the_theatre_problem.py b/CodeChef_problems/the_theatre_problem.py new file mode 100644 index 00000000..68e4f79b --- /dev/null +++ b/CodeChef_problems/the_theatre_problem.py @@ -0,0 +1,72 @@ +# def fun(mymatrix,col,row,xvalue): +# ans = 0 +# maximum = 0 +# #itration 1 +# for i in range(4): +# if(row[i]==1): +# for j in range(4): +# if(col[j]==1): +# if(mymatrix[i][j]>maximum): +# maximum = mymatrix[i][j] +# if(maximum==0): +# ans-=(5-xvalue)*100 +# return ans +# ans+=maximum*(5*xvalue)*25 +# value = [] +# for i in range(4): +# flag = 0 +# if(row[i]==1): +# for j in range(4): +# if(col[j]==1): +# if(mymatrix[i][j]==maximum): +# col[j] = 0 +# row[i] = 0 +# value.append(fun(mymatrix,col,row,xvalue+1)) +# col[j] = 1 +# row[i] = 1 + +# return ans + max(value) + +t = int(input()) +myans = 0 +for _ in range(t): + n = int(input()) + # col = [1 for i in range(4)] + # row = [1 for i in range(4)] + mymatrix = [[0 for i in range(4)] for i in range(4)] + for i in range(n): + [a,b] = input().split() + if(a=='A'): + mymatrix[0][(int(b)//3)%4]+=1 + elif(a=='B'): + mymatrix[1][(int(b)//3)%4]+=1 + elif(a=='C'): + mymatrix[2][(int(b)//3)%4]+=1 + else: + mymatrix[3][(int(b)//3)%4]+=1 + value = [] + for a in range(4): + for b in range(4): + for c in range(4): + for d in range(4): + if(a!=b and a!=c and a!=d and b!=c and b!=d and c!=d): + mylist = [mymatrix[0][a],mymatrix[1][b],mymatrix[2][c],mymatrix[3][d]] + mylist.sort() + if(mylist[0]!=0): + value.append(mylist[0]*25 + mylist[1]*50 + mylist[2]*75 + mylist[3]*100) + elif(mylist[1]!=0): + value.append(mylist[3]*100 + mylist[1]*50 + mylist[2]*75 -100) + elif(mylist[2]!=0): + value.append(mylist[2]*75 + mylist[3]*100 -200) + elif(mylist[3]!=0): + value.append(mylist[3]*100 - 300) + else: + value.append(-400) + + value = max(value) + myans += value + print(value) +print(myans) + + + diff --git a/Codeforces_Contests/7_Round_674_Div_3/F.cpp b/Codeforces_Contests/7_Round_674_Div_3/F.cpp new file mode 100644 index 00000000..3a541b7b --- /dev/null +++ b/Codeforces_Contests/7_Round_674_Div_3/F.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +#define int long long +#define ll long long +#define pb push_back +#define mp make_pair +#define f(i,a,n) for(int i=a ; i=a ; i--) +#define F first +#define S second +#define all(c) (c).begin(),(c).end() +#define sz(v) (int)(v).size() +#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) + +typedef long double ld; +typedef vector vi; +typedef pair pii; +typedef pair ppi; +typedef vector vpi; + +const int inf = 1e9; +const int inf64 = 1e18; +const int MOD = inf + 7; + +int32_t main() { + fast; + int n; + cin >> n; + + string s; + cin >> s; + + vector> dp(n + 1, vector(3, 0)); + + int cur = 1; + + for (int i = 1 ; i <= n ; i++) { + if (s[i - 1] == 'a' || s[i - 1] == '?') { + dp[i][0] = dp[i - 1][0] + cur; + dp[i][1] = dp[i - 1][1]; + dp[i][2] = dp[i - 1][2]; + } + if (s[i - 1] == 'b' || s[i - 1] == '?') { + dp[i][0] += dp[i - 1][0]; + dp[i][1] += dp[i - 1][1] + dp[i - 1][0]; + dp[i][2] += dp[i - 1][2]; + } + if (s[i - 1] == 'c' || s[i - 1] == '?') { + dp[i][0] += dp[i - 1][0]; + dp[i][1] += dp[i - 1][1]; + dp[i][2] += dp[i - 1][2] + dp[i - 1][1]; + } + if (s[i - 1] == '?') { + cur = (cur * 3) % MOD; + } + dp[i][0] %= MOD; + dp[i][1] %= MOD; + dp[i][2] %= MOD; + } + cout << dp[n][2] << "\n"; +} diff --git a/Codeforces_Contests/9_Round_667_Div_3/A.cpp b/Codeforces_Contests/9_Round_667_Div_3/A.cpp new file mode 100644 index 00000000..e2911296 --- /dev/null +++ b/Codeforces_Contests/9_Round_667_Div_3/A.cpp @@ -0,0 +1,32 @@ +#include +#include + +#include +#include + +using namespace std; + +int main() + +{ + int noOfTestCase{}; + cin>>noOfTestCase; + long long a[noOfTestCase]; + long long b[noOfTestCase]; + for(int i=0;i>a[i]; + cin>>b[i]; + } + for(int i=0;i +#define int long long +#define pb emplace_back +#define mp make_pair +#define mt make_tuple +#define all(v) v.begin(),v.end() +#define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++) +#define revrep(i,n) for(long long i=n-1;i>=0;i--) +#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +#define osit ostream_iterator output (cout," ") +#define endl '\n' +#define f first +#define s second +#define PI 3.141592653589793 +#define MOD 1000000007 +#define set0(x) memset(x,0,sizeof(x)) +#define set1(x) memset(x,1,sizeof(x)) +using namespace std; +typedef vector vi; +typedef list li; +typedef pair ii; +typedef vector vii; +typedef tuple iii; +template T gcd(T a,T b){if(a==0) return b; return gcd(b%a,a);} +template P dectobin(P a){if(a==0)return 0; else return(a%2 + 10*dectobin(a/2));} +template Y bintodec(Y a){int ans=0,b=1,t=a; while (t){int ld=t%10; t/=10;ans+=ld* b;b=b*2;} return ans; } +template H modExp(H x, H n){ int r=1; while(n>0){if(n%2==1){r=(r*x)%MOD;}x=(x*x)%MOD;n/=2;}return r;} +template T isPowerOfTwo(T x){return x && (!(x & (x - 1)));} +template T lcm(T a,T b) {return a*b/gcd(a,b);} +//template T extendedEuclid(T A,T B){if(B==0){d=A; x=1;y=0;}else{ extendedEuclid(B,A%B);int temp=x; x=y; y=temp-(A +//B)*y;}} +int32_t main(){ + boost;osit; + int t; + cin>>t; + while(t--){ + int n; + cin>>n; + bool ok=false; + if(n%3==0) cout< +#define int long long +#define pb emplace_back +#define mp make_pair +#define mt make_tuple +#define all(v) v.begin(),v.end() +#define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++) +#define revrep(i,n) for(long long i=n-1;i>=0;i--) +#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +#define osit ostream_iterator output (cout," ") +#define endl '\n' +#define f first +#define s second +#define PI 3.141592653589793 +#define MOD 1000000007 +#define set0(x) memset(x,0,sizeof(x)) +#define set1(x) memset(x,1,sizeof(x)) +using namespace std; +typedef vector vi; +typedef list li; +typedef pair ii; +typedef vector vii; +typedef tuple iii; +template T gcd(T a,T b){if(a==0) return b; return gcd(b%a,a);} +template P dectobin(P a){if(a==0)return 0; else return(a%2 + 10*dectobin(a/2));} +template Y bintodec(Y a){int ans=0,b=1,t=a; while (t){int ld=t%10; t/=10;ans+=ld* b;b=b*2;} return ans; } +template H modExp(H x, H n){ int r=1; while(n>0){if(n%2==1){r=(r*x)%MOD;}x=(x*x)%MOD;n/=2;}return r;} +template T isPowerOfTwo(T x){return x && (!(x & (x - 1)));} +template T lcm(T a,T b) {return a*b/gcd(a,b);} +//template T extendedEuclid(T A,T B){if(B==0){d=A; x=1;y=0;}else{ extendedEuclid(B,A%B);int temp=x; x=y; y=temp-(A +//B)*y;}} +int32_t main(){ + boost;osit; + int t; + cin>>t; + while(t--){ + int n,k; + cin>>n>>k; + int arr[n],sum=0; + rep(i,0,n) cin>>arr[i]; + sort(arr,arr+n); + for(int i=n-1;i>=(n-k-1);i--) sum+=arr[i]; + cout< +#define int long long +#define pb emplace_back +#define mp make_pair +#define mt make_tuple +#define all(v) v.begin(),v.end() +#define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++) +#define revrep(i,n) for(long long i=n-1;i>=0;i--) +#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +#define osit ostream_iterator output (cout," ") +#define endl '\n' +#define f first +#define s second +#define PI 3.141592653589793 +#define MOD 1000000007 +#define set0(x) memset(x,0,sizeof(x)) +#define set1(x) memset(x,1,sizeof(x)) +using namespace std; +typedef vector vi; +typedef list li; +typedef pair ii; +typedef vector vii; +typedef tuple iii; +template T gcd(T a,T b){if(a==0) return b; return gcd(b%a,a);} +template P dectobin(P a){if(a==0)return 0; else return(a%2 + 10*dectobin(a/2));} +template Y bintodec(Y a){int ans=0,b=1,t=a; while (t){int ld=t%10; t/=10;ans+=ld* b;b=b*2;} return ans; } +template H modExp(H x, H n){ int r=1; while(n>0){if(n%2==1){r=(r*x)%MOD;}x=(x*x)%MOD;n/=2;}return r;} +template T isPowerOfTwo(T x){return x && (!(x & (x - 1)));} +template T lcm(T a,T b) {return a*b/gcd(a,b);} +//template T extendedEuclid(T A,T B){if(B==0){d=A; x=1;y=0;}else{ extendedEuclid(B,A%B);int temp=x; x=y; y=temp-(A +//B)*y;}} +int32_t main(){ + boost;osit; + int t; + cin>>t; + while(t--){ + int n; + cin>>n; + int a=n-1,b=n; + vii v; + rep(i,0,n-1){ + v.pb(mp(a,b)); + b=(a+b+1)/2; + a--; + } + cout<t): + isProduct = False + for i in range(n): + for j in range(i+1,n): + temp = product - matrix[original[i][0]][original[i][1]] - matrix[original[j][0]][original[j][1]]+ matrix[original[i][0]][original[j][1]]+matrix[original[j][0]][original[i][1]] + if(tempmpt): + mpt = temp + mi = i + mj = j + isProduct = True + elif(temp>t and tempmp and isProduct == False): + mp = temp + mi = i + mj = j + elif(temp == t): + mi = i + mj = j + isFound = True + break + if(isFound): + break + else: + for i in range(n): + for j in range(i+1,n): + temp = product - matrix[original[i][0]][original[i][1]] - matrix[original[j][0]][original[j][1]]+ matrix[original[i][0]][original[j][1]]+matrix[original[j][0]][original[i][1]] + if(tempmpt): + mpt = temp + mi = i + mj = j + elif(temp == t): + mi = i + mj = j + isFound = True + break + if(isFound): + break + + if(mj==-1 and mi == -1): + isPossible = False + else: + product = product - matrix[original[mi][0]][original[mi][1]] - matrix[original[mj][0]][original[mj][1]]+ matrix[original[mi][0]][original[mj][1]]+matrix[original[mj][0]][original[mi][1]] + temp = original[mi][1] + original[mi][1] = original[mj][1] + original[mj][1] = temp + +for i in range(n): + print(a[original[i][0]],end=" ") +print("") +for i in range(n): + print(b[original[i][1]],end = "") diff --git a/Hacker Earth/README.md b/Hacker Earth/README.md new file mode 100644 index 00000000..0a89c647 --- /dev/null +++ b/Hacker Earth/README.md @@ -0,0 +1 @@ +This Folder Consists of solutions for HackerEarth problems diff --git a/Hackerrank/c++/QHEAP1.cpp b/Hackerrank/c++/QHEAP1.cpp new file mode 100644 index 00000000..dfb55dab --- /dev/null +++ b/Hackerrank/c++/QHEAP1.cpp @@ -0,0 +1,120 @@ +//Min heap implementation. +// This question is submitted on Hackerrank to "QHEAP1". So, it is according to it. +#include +#include +#include // Not used in this. +#include +#include // Used for heap STL. Not used in this. +#include // It is used to get maximum and minimum limits of different data types. +using namespace std; + +void swap(int *x, int *y){ //Swap function to swap the elements of array. + int temp=*x; + *x=*y; + *y=temp; +} + +class MinHeap // Class MINHEAP +{ + int size; + int capacity; + int *harr; // Pointer to array to be made into Min heap. +public: + MinHeap(int cap); // Constrcutor which takes capacity. + void insertkey(int k); + void deletekey(int k); + int getmin(); // returns minimum element or the top element from the heap. + void Minheapify(int i); // Recreates the heap which was disturbed due to deletion or insertion of element. + + int parent(int i){ // Returns parent node index of i. Similarly left and right functions do their works. + return (i-1)/2; + } + + int left(int i){ + return (2*i)+1; + } + + int right(int i){ + return (2*i)+2; + } +}; + +MinHeap::MinHeap(int cap){ // Constructor for MinHeap. + size=0; + capacity=cap; + harr=new int[cap]; // Creates array of size cap=q. new is used to get address of array in harr. +} + +void MinHeap::insertkey(int k){ // Insert key in MinHeap. + if(size==capacity){ + cout<<"Heap size is full. Couldn't insert."; + return; + } + + size++; + int i=size-1; + harr[i]=k; + while(i!=0 && harr[parent(i)]>harr[i]){ + swap(&harr[parent(i)],&harr[i]); + i=parent(i); + } +} + +void MinHeap::deletekey(int k){ // Find the value(k) and delete it from the heap. + int i,j; + for(j=0;jharr[l] && lharr[r] && r>q; + MinHeap h(q); // h is object of types MinHeap. + while(q--){ + int operation, k; + cin>>operation; + if(operation==1){ + cin>>k; + h.insertkey(k); + } + else if(operation==2){ + cin>>k; + h.deletekey(k); + } + else if(operation==3){ + cout< Solution::plusOne(vector &A) { + vector result; + if (A.empty()) + return result; + + int z = 0; int size = A.size(); + while (z < size && A[z] == 0) + ++z; + if (z == size) + return vector {1}; + + int carry = 0; + for (int end = size-1; end>=z; --end) + { + if (end == size-1 && A[end] != 9) + { + ++A[end]; + break; + } + + if (A[end] == 9) + { + A[end] = 0; + carry = 1; + } + else + { + ++A[end]; + carry = 0; + break; + } + } + + if (carry) + { + result.emplace_back(carry); + vector temp(A.cbegin() + z, A.cend()); + for (auto& t : temp) + result.emplace_back(t); + } + else + result = vector(A.cbegin() + z, A.cend()); + return result; +} diff --git a/InterviewBit/Anti Diagonals.cpp b/InterviewBit/Anti Diagonals.cpp new file mode 100644 index 00000000..12fa4996 --- /dev/null +++ b/InterviewBit/Anti Diagonals.cpp @@ -0,0 +1,38 @@ +vector > Solution::diagonal(vector > &A) { + int n = A.size(); + vector > result; + int l = -1; + int i = 0; + int c = 0; + + int r = 0; + int col = 0; + for (auto j = 0; j0) + { + --c; + result[l].emplace_back(A[i+1][c]); + ++i; + } + } + for (auto x = 1; x=math.sqrt(i)): +# break +# if(temp%primeNum[j]==0): +# temp//=primeNum[j] +# else: +# j+=1 +# if(j==len(primeNum)): +# break +# if(temp==1): +# count+=1 +# print(i) # printing value of smooth sqrt numbers + +#Adding 1 because of the number 1 is a smooth sqrt number + +# print("\n\t**Value of smooth square root number below",n,"=",count+1,"**") + + +#Optimised code +x = 0 +for i in range(1,n+1): + if(primeNum[x] + +using namespace std; + +//Bucket sort is an effiicent way of sorting numbers between 0.0 and 1.0. + +//Algorithm: +void bucketSort(float array[], int n){ + vector bucket[n]; + int k = 0; + + for(int i = 0; i < n; i++){ + int x = n*array[i]; + bucket[x].push_back(array[i]); + } + + for(int i = 0; i < n; i++){ + sort(bucket[i].begin(), bucket[i].end()); + + } + + for(int i = 0; i < n; i++){ + for (int j = 0; j < bucket[i].size(); j++){ + array[k] = bucket[i][j]; + k++; + } + } +} + +//Test: +int main(){ + int n; + float array[] = {0.182, 0.429, 0.18, 0.02, 0.912, 0.586, 0.129, 0.911}; + n = sizeof(array) / sizeof(array[0]); + cout << "Elements before bucket sort:\n"; + for (int i = 0; i < n; i++){ + cout << array[i] << " "; + } + cout << "\n"; + + bucketSort(array, n); + + cout << "Elements after bucket sort:\n"; + for(int i = 0; i < n; i++){ + cout << array[i] << " "; + } + cout << "\n"; +} \ No newline at end of file diff --git a/classical_algorithms/c++/dijkstra_implement..cpp b/classical_algorithms/c++/dijkstra_implement..cpp new file mode 100644 index 00000000..502e7175 --- /dev/null +++ b/classical_algorithms/c++/dijkstra_implement..cpp @@ -0,0 +1,113 @@ +// Dijkstra implementation to find the shortest path and print it........ + +#include +using namespace std; +typedef long long int ll; +using ii= pair; +#define F first +#define S second +#define MP make_pair + +vector v[100]; +ll dist[100]; +ll vst[100]; +vector parent; + +class prioritize{ // comparator design of priority queue... + public: bool operator()(ii &p1,ii &p2){ + return p1.S>p2.S; // S -> second element of the pair ii... p-> (node,weight) + } +}; + +void dijkstra(ll s,ll n) +{ + for(ll i=0;i,prioritize > pq; // use a priority queue if a pair...its internally wrapped in a vector..hence the + pq.push(MP(s,0)); // vector in the expression.....// push the source node ...... + + while(!pq.empty()) // loop untill the pq is empty... + { + ii h=pq.top(); // pop the top most element in the ascending order of the weights... + pq.pop(); + ll node=h.F; + + if(vst[node])continue; // if the node is already visited , we need not do anything....else... + else + { + vst[node]=1; // mark it visited..... + for(auto u: v[node]) // loop on all its neighbours in its adjacency list..... + { + ll neigh=u.F; // let neigh be a node in its adjacency list and wt be its distance from the parent node 'node' + ll wt=u.S; + if(dist[neigh]>dist[node]+wt) // if the distance of neigh from souce is > distance of it via the 'node', update the distance.... + { + dist[neigh]=dist[node]+wt; // min distance updated... + parent[neigh]=node; // push node as the parent of neigh as it get its shortst distance when accessed through 'node '... + pq.push(make_pair(neigh,dist[neigh])); // now push the neigh into the pq, so that its neighbours can be analysed... + } + + } + } + } +} + + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin>>t; + while(t--) + { + ll n; + cin>>n; + + for(ll i=0;i>x; + // wt[i][j]=x; + if(x!=0) + { + v[i].push_back(MP(j,x)); // creating our adjacency list.we push a pair of (node,weight) in the adjac. list of another node + } + } + } + + ll s,g; // s-> source g-> destination.... + cin>>s>>g; + parent.resize(n,-1); // must resize the parent vector otherwise u may get runtime (t)error.... + dijkstra(s,n); // calling dijkstra.... + ll j=dist[g]; // j-> shortest path of g from s...accesing it using the distance array + cout< path; // vector to store the shortest path nodes..... + ll dest=g; + while(dest!=-1) // in the end.. only the source will have a parent as -1, + { + path.push_back(dest); + dest=parent[dest]; // -1->0->1->4->8->9->7 + } + reverse(path.begin(),path.end()); // reverse the path vector.... + for(auto no: path) // loop on the path vector and print the node elements..... + { + cout< array = [10, -4, 7, 3, 1, 0, 2, 6, 99]; + BubbleSort(array); + + for (int i = 0; i < array.length - 1; i++) print(array[i]); +} + +List BubbleSort(List list) { + print("Bubble Sort: "); + for (int i = 0; i < list.length; i++) { + for (int j = 0; j < list.length - 1; j++) { + if (list[j] > list[j + 1]) { + int num = list[j]; + list[j] = list[j + 1]; + list[j + 1] = num; + } + } + } + return list; +} diff --git a/classical_algorithms/java/BinarySearch.java b/classical_algorithms/java/BinarySearch.java new file mode 100644 index 00000000..16189bdc --- /dev/null +++ b/classical_algorithms/java/BinarySearch.java @@ -0,0 +1,45 @@ +import java.util.Scanner; +public class Main { + private static int binarySearch(int arr[], int x) { + int l = 0, r = arr.length - 1; + while (l <= r) { + int m = l + (r - l) / 2; + // Check if x is present at mid + if (arr[m] == x) + return m; + // If x greater, ignore left half + if (arr[m] < x) + l = m + 1; + // If x is smaller, ignore right half + else + r = m - 1; + } + // if we reach here, then element was + // not present + return -1; + } + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + try { + int i,n,x, last; + System.out.println("How many elements do you want in the array?"); + n=scan.nextInt(); + int a[]= new int[n]; + System.out.println("Enter the array elements one by one"); + for(i=0;i> permute(int[] nums) { + List> permutations = new ArrayList<>(); + boolean[] seen = new boolean[nums.length]; + if(nums == null || nums.length == 0) { + return permutations; + } + findPermutations(nums, permutations, new ArrayList<>(), seen); + return permutations; + } + + private void findPermutations (int[] nums, List> permutations, List current, boolean[] seen) { + if(current.size() > nums.length) { + return; + } + + if(current.size() == nums.length) { + permutations.add(new ArrayList<>(current)); + return; + } + + for(int i = 0; i < nums.length; i++) { + if(seen[i] == true) { + continue; + } + current.add(nums[i]); + seen[i] = true; + findPermutations(nums, permutations, current, seen); + current.remove(current.size() - 1); + seen[i] = false; + } + + return; + } + } +} diff --git a/leetcode/Java/Product_of_Array_Except_Self.java b/leetcode/Java/Product_of_Array_Except_Self.java new file mode 100644 index 00000000..1c47f81e --- /dev/null +++ b/leetcode/Java/Product_of_Array_Except_Self.java @@ -0,0 +1,20 @@ +// Problem Link: https://leetcode.com/problems/product-of-array-except-self/ + +class Solution { + public int[] productExceptSelf(int[] nums) { + int [] ans = new int[nums.length]; + ans[0] = 1; + for(int i=1; i=0; i--) + { + ans[i] = ans[i] * R; + R *= nums[i]; + } + + return ans; + } +} diff --git a/leetcode/cpp/Array/190.cpp b/leetcode/cpp/Array/190.cpp new file mode 100644 index 00000000..6e55ceed --- /dev/null +++ b/leetcode/cpp/Array/190.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + uint32_t res = 0; + uint32_t mask = 1; + for (int i=0;i<32;i++){ + if (n&mask) res = res + 1; + if (i!=31) res <<= 1; + mask <<= 1; + } + return res; + } +}; diff --git a/leetcode/python/strings/count_and_say.py b/leetcode/python/strings/count_and_say.py new file mode 100644 index 00000000..6b99f272 --- /dev/null +++ b/leetcode/python/strings/count_and_say.py @@ -0,0 +1,33 @@ +# Solves LeetCode question 38. Count and Say + +class Solution(object): + def countAndSay(self, n): + """ + :type n: int + :rtype: str + """ + s = "1" + if n == 1: + return s + for i in range(2,n+1): + j = 0 + temp = "" + curr = "" + count = 0 + while j