├── .gitignore ├── Codechef October Long 2020 ├── CHEFEZQ.cpp ├── CVDRUN.cpp ├── VILLNET.cpp ├── RANDKNAP.cpp └── SEGCOMPR.cpp ├── Codechef August Long 2020 ├── ANTS.cpp └── CHEFWARS.cpp ├── DCS ├── Day1 │ ├── B8.cpp │ ├── B7.cpp │ └── B6.cpp └── Day2 │ ├── B11.cpp │ ├── B10.cpp │ ├── B12.cpp │ └── B9.cpp ├── Codechef September Long 2020 ├── VBR.cpp ├── MOVCOIN2.cpp └── ROTATPOL.cpp ├── July LeetCode Challenge ├── Day_16.cpp ├── Day_26.cpp ├── Day_13.cpp ├── Day_14.cpp ├── Day_5.cpp ├── Day_15.cpp ├── Day_31.cpp ├── Day_12.cpp ├── Day_25.cpp ├── Day_23.cpp ├── Day_20.cpp ├── Day_7.java ├── Day_17.cpp ├── Day_11.cpp ├── Day_19.cpp ├── Day_24.cpp ├── Day_10.cpp ├── Day_6.java ├── Day_8.cpp ├── Day_29.cpp ├── Day_22.cpp ├── Day_4.cpp ├── Day_3.cpp ├── Day_28.cpp ├── Day_21.cpp └── Day_9.java ├── Codeforces ├── 122A.cpp ├── VKcupA.cpp ├── 472A.cpp ├── 645C.cpp ├── 65A.cpp ├── 645A.cpp ├── 644A.cpp ├── 1366A.cpp ├── 644B.cpp ├── 643D.cpp ├── 1359A.cpp ├── 645B.cpp ├── 1364A.cpp ├── 1352A.cpp ├── Tprimes.cpp ├── 1363B.cpp ├── 643A.cpp ├── 646A.cpp ├── 1362A.cpp ├── 1362B.cpp ├── Tprimes2.cpp ├── 645D.cpp ├── 1365A.cpp ├── 644E.cpp ├── 644C.cpp ├── 1182A.cpp ├── 1339A.cpp ├── 492A.cpp ├── 1353A.cpp ├── 644H.cpp ├── 644D.cpp ├── 1359B.cpp ├── 644G.cpp └── 1375A.cpp ├── September LeetCode Challenge ├── Day_3.cpp ├── Day_15.cpp ├── Day_1.cpp ├── Day_18.cpp ├── Day_21.cpp ├── Day_26.cpp ├── Day_7.cpp ├── Day_19.cpp ├── Day_29.cpp ├── Day_24.cpp ├── Day_23.cpp ├── Day_28.cpp ├── Day_9.cpp ├── Day_14.cpp ├── Day_4.cpp ├── Day_11.cpp ├── Day_25.cpp ├── Day_8.cpp ├── Day_12.cpp ├── Day_22.cpp ├── Day_17.cpp ├── Day_2.cpp ├── Day_30.cpp ├── Day_13.cpp ├── Day_5.cpp ├── Day_6.cpp ├── Day_10.cpp ├── Day_20.cpp ├── Day_27.cpp └── Day_16.cpp ├── August LeetCode Challenge ├── Day_4.cpp ├── Day_10.cpp ├── Day_20.cpp ├── Day_6.cpp ├── Day_1.cpp ├── Day_27.cpp ├── Day_14.cpp ├── Day_11.cpp ├── Day_16.cpp ├── Day_25.cpp ├── Day_15.cpp ├── Day_12.cpp ├── Day_3.cpp ├── Day_5.cpp ├── Day_28.cpp ├── Day_18.cpp ├── Day_24.cpp ├── Day_26.cpp ├── Day_8.cpp ├── Day_23.cpp ├── Day_2.cpp ├── Day_19.cpp ├── Day_7.cpp ├── Day_21.cpp ├── Day_13.cpp └── Day_9.cpp ├── README.md ├── CodeChef DSA Learning ├── cpptemp.cpp ├── Week 1 │ ├── ZCO14003.cpp │ ├── TEST.java │ ├── FLOW007.java │ ├── FCTRL.java │ ├── MULTHREE.java │ └── CONFLIP.java ├── Week 5 │ ├── HMAPPY2.cpp │ ├── FIBEASY.cpp │ ├── CHEFPRMS.cpp │ ├── ZACKHAN.cpp │ └── CHEFSQRS.cpp ├── Week 2 │ ├── STUPMACH.cpp │ ├── CHFQUEUE.cpp │ ├── COMPILER.cpp │ ├── ZCO15004.cpp │ ├── STFOOD.java │ ├── ZCO12001.cpp │ ├── NOTALLFL.cpp │ └── ZCO12002.cpp └── Week 3 │ ├── SAVKONO.cpp │ ├── DPAIRS.cpp │ ├── EXUNC.cpp │ └── FENCE.cpp ├── October LeetCode Challenge ├── Day_8.cpp ├── Day_3.cpp ├── Day_1.cpp ├── Day_16.cpp ├── Day_25.cpp ├── Day_15.cpp ├── Day_17.cpp ├── Day_5.cpp ├── Day_23.cpp ├── Day_14.cpp ├── Day_26.cpp ├── Day_29.cpp ├── Day_12.cpp ├── Day_19.cpp ├── Day_11.cpp ├── Day_24.cpp ├── Day_10.cpp ├── Day_4.cpp ├── Day_7.cpp ├── Day_18.cpp ├── Day_28.cpp ├── Day_6.cpp ├── Day_27.cpp ├── Day_30.cpp ├── Day_21.cpp ├── Day_20.cpp ├── Day_2.cpp ├── Day_22.cpp └── Day_31.cpp ├── November LeetCode Challenge ├── Day_3.cpp ├── Day_6.cpp ├── Day_5.cpp ├── Day_1.cpp └── Day_2.cpp ├── Codechef April Long 2020 ├── CARSELL.cpp ├── COVIDLQ.cpp ├── STRNO.cpp ├── UNITGCD.java └── ANSLEAK.cpp ├── Codechef June Long 2020 ├── XYSTR.cpp ├── PRICECON.cpp ├── EOEO.cpp ├── EVENM.cpp └── GUESSG.cpp ├── Codechef May Lunchtime 2020 ├── LOSTWKND.cpp └── WWALK.cpp ├── General Programming Problems ├── sieveofEratosthenes.cpp ├── dfs.cpp ├── sieveofEratosthenes.java ├── TDKPRIME.cpp ├── modularExponentiation.java ├── LargestRectinHistogram.cpp ├── noldbachCodeForces17A.java ├── DP │ └── 492A.cpp └── bfs.cpp ├── Codechef April CookOff 2020 ├── LIFTME.cpp └── MATBREAK.cpp ├── Google KickStart 2020 ├── C │ ├── perfectSubarray.cpp │ ├── candies_tle.cpp │ ├── countdown.cpp │ └── candies_final.cpp ├── A │ └── allocation.cpp └── B │ ├── busRoutes.java │ ├── bikeTour.java │ └── roboPathfinal.cpp ├── Codechef March Lunchtime 2020 ├── SWPDGT.cpp └── PRFXGD.cpp ├── Recode ├── RECNDSTR.cpp └── RECNDNOS.cpp ├── Codechef February Lunchtime 2020 └── POPGATES.cpp ├── Codechef March Long 2020 ├── ENGXOR.cpp ├── ADASHOP2.cpp └── CHPINTU.c ├── Codechef May Long 2020 ├── SORTVS.cpp ├── COVID19.c ├── CHANDF.java └── CORUS.java ├── Code Jam 2020 └── Qualification Round │ └── mish_rr_a-Parenting.py ├── HackerEarth May Circuits └── factorialEquations.cpp ├── Codechef May CookOff 2020 ├── CHEFSHIP.cpp ├── CHEFRECP_final.cpp ├── CHEFSHIP_hashing.cpp └── CHEFSHIP_final.cpp ├── Codechef April Lunchtime 2020 ├── SHUFFLE.cpp └── FFL.cpp ├── LICENSE ├── Code Gladiators ├── powerpuff.java └── beyblade.java ├── CSES ├── distinctNumbers.cpp ├── weirdAlgorithm.cpp └── increasingArray.cpp └── CodeVibe 1.0 └── left_rotation.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | README.md 3 | -------------------------------------------------------------------------------- /Codechef October Long 2020/CHEFEZQ.cpp: -------------------------------------------------------------------------------- 1 | Goli beta masti nahi -------------------------------------------------------------------------------- /Codechef October Long 2020/CVDRUN.cpp: -------------------------------------------------------------------------------- 1 | Goli beta masti nahi -------------------------------------------------------------------------------- /Codechef October Long 2020/VILLNET.cpp: -------------------------------------------------------------------------------- 1 | Goli beta masti nahi -------------------------------------------------------------------------------- /Codechef August Long 2020/ANTS.cpp: -------------------------------------------------------------------------------- 1 | //Sorry, you've been tricked -------------------------------------------------------------------------------- /Codechef October Long 2020/RANDKNAP.cpp: -------------------------------------------------------------------------------- 1 | Goli beta masti nahi -------------------------------------------------------------------------------- /Codechef October Long 2020/SEGCOMPR.cpp: -------------------------------------------------------------------------------- 1 | Goli beta masti nahi -------------------------------------------------------------------------------- /DCS/Day1/B8.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/kth-smallest-element/0 -------------------------------------------------------------------------------- /Codechef September Long 2020/VBR.cpp: -------------------------------------------------------------------------------- 1 | You could not live with your own failure. And where did that bring you ? Back to me. -------------------------------------------------------------------------------- /Codechef September Long 2020/MOVCOIN2.cpp: -------------------------------------------------------------------------------- 1 | You could not live with your own failure. And where did that bring you ? Back to me. -------------------------------------------------------------------------------- /Codechef September Long 2020/ROTATPOL.cpp: -------------------------------------------------------------------------------- 1 | You could not live with your own failure. And where did that bring you ? Back to me. -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_16.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | return pow(x, n); 5 | } 6 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | return num - (num - 1) / 9 * 9; 5 | } 6 | }; -------------------------------------------------------------------------------- /Codeforces/122A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n ; 4 | scanf("%d" , &n); 5 | printf((n%4 && n%7 && n%47 && n%74 && n%477)?"NO":"YES"); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool repeatedSubstringPattern(string s) { 4 | return (s + s).substr(1, 2 * s.size() - 2).find(s) != -1; 5 | } 6 | }; -------------------------------------------------------------------------------- /DCS/Day2/B11.cpp: -------------------------------------------------------------------------------- 1 | void rotate(int arr[], int n) 2 | { 3 | int left = arr[n - 1]; 4 | for (int i = n - 1; i >= 1; i--) 5 | { 6 | arr[i] = arr[i - 1]; 7 | } 8 | arr[0] = left; 9 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_4.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int num) { 3 | return num == 1 ? true : num > 0 && (num & (num - 1)) == 0 && (num % 10 == 4 || num % 10 == 6); 4 | } 5 | } -------------------------------------------------------------------------------- /DCS/Day2/B10.cpp: -------------------------------------------------------------------------------- 1 | int doUnion(int a[], int n, int b[], int m) 2 | { 3 | set val(b, b + m); 4 | for (int i = 0; i < n; i++) 5 | { 6 | val.insert(a[i]); 7 | } 8 | return val.size(); 9 | } -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_13.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSameTree(TreeNode* p, TreeNode* q) { 4 | return !p && !q || p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 5 | } 6 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double angleClock(int hour, int minutes) { 4 | return [a = abs((6.0 * (double)minutes) - (30.0 * hour + (double)minutes / 2.0))]() { return min(360.0 - a, a); }(); 5 | } 6 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_10.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int titleToNumber(string s) { 4 | int ans = 0, n = s.length() - 1; 5 | for (int i = n; i >= 0; i--) 6 | ans += pow(26, n - i) * (s[i] - 'A' + 1); 7 | return ans; 8 | } 9 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int r = x ^ y; 5 | int setBits = 0; 6 | 7 | while (r > 0) { 8 | setBits += r & 1; 9 | r >>= 1; 10 | } 11 | 12 | return setBits; 13 | } 14 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_15.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | stringstream all(s); 5 | string word, ans = ""; 6 | while (all >> word) 7 | ans = word + " " + ans; 8 | return ans.substr(0, ans.length() - 1); 9 | } 10 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_15.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | int A = 0; 5 | for (int i = s.size() - 1; i >= 0; --i) { 6 | if (s[i] != ' ') ++A; 7 | else if (A) return A; 8 | } 9 | return A; 10 | 11 | } 12 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 100DaysOfCode 2 | 3 | A small effort to document all of my coding activities
4 | This started off as a 100 day drill but now it's become a habit :shrug:
5 | -------------------------------------------------------------------------------- /DCS/Day2/B12.cpp: -------------------------------------------------------------------------------- 1 | int maxSubarraySum(int a[], int n) 2 | { 3 | int mf = INT_MIN, me = 0; 4 | 5 | for (int i = 0; i < n; i++) 6 | { 7 | me = me + a[i]; 8 | if (mf < me) 9 | mf = me; 10 | 11 | if (me < 0) 12 | me = 0; 13 | } 14 | return mf; 15 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_20.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& A) { 4 | int l = 0, r = A.size() - 1; 5 | while (l < r) { 6 | if (A[l] % 2 != 0) { 7 | swap(A[l], A[r]); 8 | --r; 9 | } else { 10 | ++l; 11 | } 12 | } 13 | return A; 14 | } 15 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/cpptemp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | ll t; 9 | cin >> t; 10 | while(t--){ 11 | // your code goes here 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /DCS/Day2/B9.cpp: -------------------------------------------------------------------------------- 1 | void sort012(int a[], int n) 2 | { 3 | int ctr[3] = {0}; 4 | for (int i = 0; i < n; i++) 5 | ctr[a[i]]++; 6 | int c = 0; 7 | for (int i = 0; i < 3; i++) 8 | { 9 | for (int j = 0; j < ctr[i]; j++) 10 | { 11 | a[c++] = i; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_31.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | int dp[46]; 5 | dp[1] = 1; 6 | dp[2] = 2; 7 | for (int i = 3; i <= 45; i++) 8 | { 9 | dp[i] = dp[i - 1] + dp[i - 2]; 10 | } 11 | return dp[n]; 12 | } 13 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_12.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | uint32_t ret = 0, power = 31; 5 | while (n != 0) { 6 | ret += (n & 1) << power; 7 | n = n >> 1; 8 | power -= 1; 9 | } 10 | return ret; 11 | } 12 | }; -------------------------------------------------------------------------------- /Codeforces/VKcupA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n, k; 4 | int T[51]; 5 | 6 | int main() 7 | { 8 | scanf("%d %d", &n, &k); 9 | int i = 1; 10 | for(; i <= n; ++i) 11 | { 12 | scanf("%d", &T[i]); 13 | if(T[i] == 0 || T[i] < T[k]) break; 14 | } 15 | printf("%d\n", i - 1); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | mapmp; 5 | for (auto i : nums) 6 | { 7 | mp[i]++; 8 | } 9 | vectorres; 10 | for (auto m : mp) 11 | { 12 | if (m.second == 2) { 13 | res.push_back(m.first); 14 | } 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /Codeforces/472A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | if(n%2==0){ 10 | cout << (n-8) << " " << 8; 11 | } 12 | else { 13 | cout << (n-9) << " " << 9; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool detectCapitalUse(string word) { 4 | int cnt = 0; 5 | int loc; 6 | for (int i = 0; i < word.length(); i++) { 7 | if (word[i] >= 'A' && word[i] <= 'Z') { 8 | cnt++; 9 | loc = i; 10 | } 11 | } 12 | return (cnt == word.length() || cnt == 0 || (cnt == 1 && loc == 0)); 13 | 14 | } 15 | }; -------------------------------------------------------------------------------- /Codeforces/645C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | ll t; 9 | cin >> t; 10 | while (t--) { 11 | ll a, b, c, d; 12 | cin >> a >> b >> c >> d; 13 | cout << (c - a)*(d - b)+1 << endl; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_25.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | int l = 0, r = nums.size() - 1, res; 5 | while (l < r) { 6 | int mid = l + (r - l) / 2; 7 | if (nums[mid] > nums[r]) { 8 | l = mid + 1; 9 | } else if (nums[mid] < nums[r]) { 10 | r = mid; 11 | } else { 12 | r--; 13 | } 14 | } 15 | return nums[l]; 16 | } 17 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_27.cpp: -------------------------------------------------------------------------------- 1 | // The rand7() API is already defined for you. 2 | // int rand7(); 3 | // @return a random integer in the range 1 to 7 4 | 5 | class Solution { 6 | public: 7 | int n, m; 8 | int rand10() { 9 | n = rand7(), m = 7; 10 | while (n > 5) { 11 | m = n - 5; 12 | n = rand7(); 13 | } 14 | while (m == 7) m = rand7(); 15 | return (m % 2 ? 5 : 0) + n; 16 | } 17 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_8.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int s = 0; 5 | 6 | int e = nums.size() - 1; 7 | 8 | while (s <= e) 9 | { 10 | int mid = s + (e - s) / 2; 11 | 12 | if (nums[mid] == target) return mid; 13 | 14 | if (target < nums[mid]) e = mid - 1; 15 | 16 | else s = mid + 1; 17 | } 18 | 19 | return -1; 20 | } 21 | }; -------------------------------------------------------------------------------- /Codeforces/65A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(0); 5 | cin.tie(0); 6 | cout.tie(0); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | string s; 11 | cin >> s; 12 | if(s.size()>10) 13 | cout << s[0]<& nums, int k) { 4 | unordered_map cnt; 5 | for (int x : nums) 6 | cnt[x] += 1; 7 | int ans = 0; 8 | for (auto [b, _] : cnt) 9 | if ((k > 0 && cnt.count(b - k)) || (k == 0 && cnt[b] >= 2)) 10 | ans += 1; 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) 4 | { 5 | vectorv(255, 0); 6 | int ans = 0; 7 | for (int i = 0; i < s.size(); i++) 8 | { 9 | v[s[i] - 'A']++; 10 | if (!(v[s[i] - 'A'] % 2)) 11 | ans += 2; 12 | } 13 | return s.size() > ans ? ans + 1 : ans; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /Codeforces/645A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | using namespace std; 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | ll t; 9 | cin >> t; 10 | while(t--){ 11 | ll a,b; 12 | cin >> a >> b; 13 | ll val =((a-1)*b)/2 + ((b-1)/2+1); 14 | 15 | cout << val << '\n'; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_11.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hIndex(vector& citations) { 4 | unordered_map m; 5 | int maxi = INT_MIN; 6 | for (int i = 0; i < citations.size(); i++) { 7 | maxi = max(maxi, citations[i]); 8 | m[citations[i]]++; 9 | } 10 | int cnt = 0; 11 | for (int i = maxi; i >= 0; i--) { 12 | if (m.count(i))cnt += m[i]; 13 | if (cnt >= i)return i; 14 | } 15 | return 0; 16 | } 17 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_1.cpp: -------------------------------------------------------------------------------- 1 | class RecentCounter { 2 | public: 3 | queue ans; 4 | RecentCounter() { 5 | 6 | } 7 | int ping(int t) { 8 | ans.push(t); 9 | while (ans.front() < t - 3000) 10 | { 11 | ans.pop(); 12 | } 13 | return ans.size(); 14 | } 15 | }; 16 | 17 | /** 18 | * Your RecentCounter object will be instantiated and called as such: 19 | * RecentCounter* obj = new RecentCounter(); 20 | * int param_1 = obj->ping(t); 21 | */ -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestTimeFromDigits(vector& A) { 4 | sort(begin(A), end(A), greater()); 5 | string hour, min; 6 | do { 7 | hour = to_string(A[0]) + to_string(A[1]); 8 | min = to_string(A[2]) + to_string(A[3]); 9 | if (hour < "24" && min < "60") return hour + ":" + min; 10 | } while (prev_permutation(begin(A), end(A))); 11 | return ""; 12 | } 13 | }; -------------------------------------------------------------------------------- /Codeforces/644A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int a,b; 11 | cin >> a >> b; 12 | if(a 3 | using namespace std; 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int a[n]; 8 | for (int i = 0; i < n; i++) 9 | { 10 | cin >> a[i]; 11 | } 12 | int mx = INT_MIN; 13 | int mn = INT_MAX; 14 | for (int i = 0; i < n; i++) 15 | { 16 | mx = max(a[i], mx); 17 | mn = min(a[i], mn); 18 | } 19 | cout << "Max element= " << mx << "\nMin element= " << mn; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_18.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | if (prices.size() < 1) return 0; 5 | int mp = INT_MIN; 6 | vector minUntil(prices.size(), 0); 7 | minUntil[0] = prices[0]; 8 | for (int i = 1; i < prices.size(); i++) { 9 | minUntil[i] = min(minUntil[i - 1], prices[i]); 10 | mp = max(mp, prices[i] - minUntil[i]); 11 | } 12 | mp = max(mp, prices.front() - minUntil.front()); 13 | return mp; 14 | } 15 | }; -------------------------------------------------------------------------------- /November LeetCode Challenge/Day_6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestDivisor(vector& nums, int threshold) { 4 | int l = 1, r = 1e6; 5 | int ans; 6 | while (l <= r) { 7 | int mid = l + (r - l) / 2; 8 | int sum = 0; 9 | for (auto num : nums) { 10 | sum += ceil((float)(num) / (float)(mid)); 11 | } 12 | if (sum <= threshold) { 13 | ans = mid; 14 | r = mid - 1; 15 | } else { 16 | l = mid + 1; 17 | } 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_16.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if (matrix.size() == 0) { 5 | return false; 6 | } 7 | int i = 0; 8 | int j = matrix[0].size() - 1; 9 | 10 | while (i < matrix.size() && j >= 0) { 11 | if (matrix[i][j] == target) { 12 | return true; 13 | } else if (target < matrix[i][j]) { 14 | j--; 15 | } else { 16 | i++; 17 | } 18 | } 19 | return false; 20 | } 21 | }; -------------------------------------------------------------------------------- /DCS/Day1/B6.cpp: -------------------------------------------------------------------------------- 1 | //https://www.geeksforgeeks.org/write-a-program-to-reverse-an-array-or-string/ 2 | #include 3 | using namespace std; 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int a[n]; 8 | for (int i = 0; i < n; i++) 9 | { 10 | cin >> a[i]; 11 | } 12 | cout << "Original array\n"; 13 | for (int i = 0; i < n; i++) 14 | cout << a[i] << " "; 15 | cout << "\nReversed array"; 16 | reverse(a, a + n); 17 | for (int i = 0; i < n; i++) 18 | cout << a[i] << " "; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_25.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | bool winnerSquareGame(int n) { 5 | vector dp(n + 1, false); 6 | for (int i = 1; i <= n; i++) 7 | { 8 | for (int j = 1; j * j <= i; j++) 9 | { 10 | if (!dp[i - j * j]) 11 | { 12 | dp[i] = true; 13 | break; 14 | } 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | }; -------------------------------------------------------------------------------- /November LeetCode Challenge/Day_5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostToMoveChips(vector& position) { 4 | short oddNumbers = 0, evenNumbers = 0; 5 | for (std::vector::iterator it = position.begin(); it != position.end(); ++it) 6 | { 7 | if (*it % 2 == 0) 8 | { 9 | ++evenNumbers; 10 | } 11 | else 12 | { 13 | ++oddNumbers; 14 | } 15 | } 16 | if (oddNumbers >= evenNumbers) 17 | { 18 | return evenNumbers; 19 | } 20 | return oddNumbers; 21 | } 22 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_15.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | k = k % nums.size(); 5 | vector temp(k); 6 | 7 | int j = 0; 8 | for (int i = nums.size() - k; i < nums.size(); i++) { 9 | temp[j++] = nums[i]; 10 | } 11 | 12 | j = nums.size() - 1; 13 | 14 | for (int i = nums.size() - k - 1; i >= 0; i--) { 15 | nums[j--] = nums[i]; 16 | } 17 | 18 | 19 | for (int i = 0; i < k; i++) { 20 | nums[i] = temp[i]; 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_16.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector distributeCandies(int candies, int num_people) { 4 | vector candiesDistributed(num_people, 0); 5 | int i = 0; 6 | while (candies > 0) 7 | { 8 | candiesDistributed[i % num_people] += i + 1; 9 | candies -= i + 1; 10 | i++; 11 | } 12 | candiesDistributed[(i - 1) % num_people] += candies; 13 | return candiesDistributed; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 1/ZCO14003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | long long int N,x,maxx; 6 | cin>>N; 7 | int A[N]; 8 | for(long long int i=0;i>A[i]; 11 | } 12 | sort(A,A+N); 13 | maxx=A[0]*(N-0); 14 | for(long long int i=1;i=maxx) 18 | { 19 | maxx=x; 20 | } 21 | } 22 | cout< singleNumber(vector& nums) { 4 | int x = 0; 5 | for (const int curr : nums) { 6 | x ^= curr; 7 | } 8 | int bit; 9 | for (int i = 0; i < 32; ++i) { 10 | if (x & (1 << i)) { 11 | bit = i; 12 | break; 13 | } 14 | } 15 | int first = 0; 16 | for (int a : nums) { 17 | if (a & (1 << bit)) { 18 | first ^= a; 19 | } 20 | } 21 | int second = first ^ x; 22 | return {first, second}; 23 | } 24 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_25.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vectorans; 5 | for (int i = 1; i <= n; i++) { 6 | if (i % 3 == 0 && i % 5 == 0) { 7 | ans.push_back("FizzBuzz"); 8 | continue; 9 | } 10 | if (i % 3 == 0) { 11 | ans.push_back("Fizz"); 12 | continue; 13 | } 14 | if (i % 5 == 0) { 15 | ans.push_back("Buzz"); 16 | continue; 17 | } 18 | ans.push_back(to_string(i)); 19 | 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_21.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool carPooling(vector>& trips, int capacity) { 4 | 5 | 6 | vector timestamp(1001, 0); 7 | 8 | for (auto trip : trips) 9 | { 10 | timestamp[trip[1]] += trip[0]; 11 | timestamp[trip[2]] -= trip[0]; 12 | } 13 | 14 | int c = 0; 15 | 16 | for (auto number : timestamp) 17 | { 18 | c += number; 19 | if (c > capacity) 20 | { 21 | return false; 22 | } 23 | } 24 | 25 | return true; 26 | 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector& timeseries, int duration) { 4 | if (timeseries.size() == 0) return 0; 5 | int count = 0; 6 | 7 | for (int i = 1; i < timeseries.size(); i++) 8 | { 9 | if (timeseries[i] - timeseries[i - 1] >= duration) 10 | { 11 | count += duration; 12 | } 13 | else 14 | { 15 | count += timeseries[i] - timeseries[i - 1]; 16 | } 17 | } 18 | count += duration; 19 | return count; 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordPattern(string pattern, string str) { 4 | vector pat_map (26, 0); 5 | unordered_map str_map; 6 | int i = 0, n = pattern.size(); 7 | istringstream ss (str); 8 | string token; 9 | 10 | for (string token; ss >> token; ++i) { 11 | if (i == n || pat_map[pattern[i] - 'a'] != str_map[token]) 12 | return false; 13 | pat_map[pattern[i] - 'a'] = str_map[token] = i + 1; 14 | } 15 | 16 | return i == n; 17 | } 18 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_15.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eraseOverlapIntervals(vector>& intervals) { 4 | if (intervals.empty()) return 0; 5 | sort(begin(intervals), end(intervals)); 6 | vectorprev = intervals[0], cur; 7 | int ans = 0; 8 | for (int i = 1 ; i < intervals.size() ; i++) 9 | { 10 | cur = intervals[i]; 11 | if (cur[0] < prev[1]) 12 | { 13 | ans++; 14 | if (cur[1] <= prev[1]) prev = cur; 15 | } 16 | else prev = cur; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | 5 | vector findRepeatedDnaSequences(string s) { 6 | 7 | vector v; 8 | 9 | unordered_map mp; 10 | 11 | if (s.length() <= 10) 12 | { 13 | return v; 14 | } 15 | 16 | for (int i = 0; i <= s.length() - 10; i++) 17 | { 18 | mp[s.substr(i, 10)]++; 19 | } 20 | 21 | for (auto x : mp) 22 | { 23 | if (x.second > 1) 24 | { 25 | v.push_back(x.first); 26 | } 27 | } 28 | 29 | return v; 30 | } 31 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_19.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sequentialDigits(int low, int high) { 4 | string s = "123456789"; 5 | vector v; 6 | for (int i = 0; i < s.length(); i++) 7 | { 8 | for (int j = i; j < s.length(); j++) 9 | { 10 | string sub = s.substr(i, j - i + 1); 11 | 12 | int ans = stoi(sub); 13 | 14 | if (ans >= low and ans <= high) 15 | { 16 | v.push_back(ans); 17 | } 18 | } 19 | } 20 | sort(v.begin(), v.end()); 21 | return v; 22 | } 23 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_12.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getRow(int rowIndex) { 4 | vector row = {1}, prev; 5 | 6 | if (rowIndex == 0) 7 | return row; 8 | prev = row; 9 | row = {1, 1}; 10 | if (rowIndex == 1) 11 | return row; 12 | for (int i = 2; i <= rowIndex; i++) { 13 | prev = row; 14 | row.clear(); 15 | row.push_back(1); 16 | for (int j = 1; j < prev.size(); j++) 17 | row.push_back(prev[j] + prev[j - 1]); 18 | row.push_back(1); 19 | } 20 | return row; 21 | } 22 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | 5 | int bitwiseComplement(int n) { 6 | 7 | if(n==0) return 1; 8 | 9 | int power=1; 10 | 11 | int ans=0; 12 | 13 | while(n>0) 14 | { 15 | int bit=n&1; 16 | 17 | if(bit==0) 18 | { 19 | ans+=(1*power); 20 | } 21 | 22 | n=n>>1; 23 | 24 | power=power*2; 25 | 26 | } 27 | 28 | return ans; 29 | 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_29.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordBreak(string s, vector& wordDict) { 4 | vector dp(s.size() + 1, false); 5 | dp[0] = true; 6 | for (int i = 1; i <= s.size(); i++) 7 | for (int j = 0; j < i; j++) 8 | if ((dp[j]) && (std::find(wordDict.begin(), wordDict.end(), s.substr(j, i - j)) != wordDict.end())) { 9 | dp[i] = true; 10 | break; 11 | } 12 | return dp.back(); 13 | } 14 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) 4 | { 5 | int l = 0; 6 | int r = s.size() - 1; 7 | while (l < r) 8 | { 9 | if (isalnum(s[l]) && isalnum(s[r])) 10 | { 11 | if (tolower(s[l]) != tolower(s[r])) 12 | return false; 13 | else 14 | { 15 | l++; r--; 16 | continue; 17 | } 18 | } 19 | else 20 | { 21 | 22 | if (!isalnum(s[l])) 23 | l++; 24 | if (!isalnum(s[r])) 25 | r--; 26 | } 27 | } 28 | return true; 29 | 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_5.cpp: -------------------------------------------------------------------------------- 1 | class WordDictionary { 2 | unordered_map>m; 3 | public: 4 | WordDictionary() {} 5 | 6 | void addWord(string word) { 7 | m[word.size()].push_back(word); 8 | } 9 | 10 | bool search(string word) { 11 | for (auto x : m[word.size()]) 12 | { 13 | bool flag = true; 14 | for (int i = 0 ; i < word.size() ; i++) 15 | { 16 | if (word[i] == '.') continue; 17 | if (word[i] != x[i]) {flag = false; break;} 18 | } 19 | if (flag) return true; 20 | } 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /Codeforces/1366A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define int long long 3 | using namespace std; 4 | signed main(){ 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | int a,b; 12 | cin >> a >> b; 13 | int maximum = max(a,b); 14 | int minimum = min (a,b); 15 | if(maximum/2>minimum) 16 | { 17 | cout << minimum << '\n'; 18 | } else{ 19 | cout << (a+b)/3 << '\n'; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | unordered_mapmp; 5 | for (int i = 0; i < s.length(); i++) 6 | { 7 | mp[s[i]]++; 8 | } 9 | char x; 10 | for (int i = 0; i < t.length(); i++) 11 | { 12 | if (!mp[t[i]]) { 13 | x = t[i]; 14 | break; 15 | } 16 | else 17 | mp[t[i]]--; 18 | } 19 | return x; 20 | } 21 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_23.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector& gas, vector& cost) { 4 | int gas_tank = 0, start_index = 0, n = gas.size(), sum = 0; 5 | 6 | for (int i = 0; i < n; i++) { 7 | 8 | sum += gas[i] - cost[i]; 9 | gas_tank += gas[i] - cost[i]; 10 | 11 | if (gas_tank < 0) { 12 | start_index = i + 1; 13 | gas_tank = 0; 14 | } 15 | 16 | } 17 | return sum < 0 ? -1 : start_index; 18 | } 19 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pancakeSort(vector& A) { 4 | vectorarr = A, res; 5 | sort(arr.begin(), arr.end()); 6 | for (int i = arr.size() - 1 ; i >= 0 ; i--) 7 | { 8 | auto it = find(A.begin(), A.end(), arr[i]) - A.begin(); 9 | reverse(A.begin(), A.begin() + it + 1); 10 | res.push_back(it + 1); 11 | reverse(A.begin(), A.begin() + i + 1); 12 | res.push_back(i + 1); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /Codechef April Long 2020/CARSELL.cpp: -------------------------------------------------------------------------------- 1 | //Question Link : https://www.codechef.com/APRIL20B/problems/CARSELL 2 | #include 3 | using namespace std; 4 | int main() { 5 | int t; 6 | cin>>t; 7 | 8 | while(t--) 9 | { 10 | long long n,sum=0; 11 | cin>>n; 12 | int *arr=new int[n]; 13 | for(int i=0;i>arr[i]; 15 | sort(arr,arr+n,greater()); 16 | for(int i=0;i0?(arr[i]-i):0; 19 | sum=sum%1000000007; 20 | } 21 | cout< 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int n; 11 | cin >> n; 12 | int a[n]; 13 | for(int i=0;i> a[i]; 15 | sort(a,a+n); 16 | int m=INT_MAX; 17 | for(int i=0;i 2 | using namespace std; 3 | #define int long long int 4 | signed main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | string s; 12 | cin >> s; 13 | int c=0; 14 | for(int i=0;i& nums) { 4 | 5 | stack stack; 6 | int second = -2147483648; 7 | for (int i = nums.size() - 1; i >= 0; i--) { 8 | if (nums [i] < second) 9 | return true; 10 | while (stack.size() > 0 && nums [i] > stack.top()) { 11 | second = stack.top (); 12 | stack.pop(); 13 | } 14 | 15 | stack.push (nums [i]); 16 | } 17 | return false; 18 | 19 | } 20 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | 5 | int numSubarrayProductLessThanK(vector& nums, int k) { 6 | 7 | if (k == 0) return 0; 8 | 9 | int start = 0; 10 | int end = 0; 11 | int p = 1; 12 | int count = 0; 13 | 14 | while (end < nums.size()) 15 | { 16 | p *= nums[end]; 17 | 18 | while (start= k) 19 | { 20 | p = p / nums[start]; 21 | start++; 22 | } 23 | 24 | if (p < k) 25 | count += end - start + 1; 26 | 27 | end++; 28 | 29 | } 30 | 31 | return count; 32 | 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /Codechef May Lunchtime 2020/LOSTWKND.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int a[5]; 11 | int p; 12 | int s=0; 13 | for(int i=0;i<5;i++) { 14 | cin >> a[i]; 15 | s+=a[i]; 16 | } 17 | cin >> p; 18 | s=s*p; 19 | if(s<=120) 20 | cout <<"No\n"; 21 | else 22 | cout <<"Yes\n"; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_20.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* removeElements(ListNode* head, int val) { 4 | if (!head)return head; 5 | while (head && head->val == val) head = head->next; 6 | ListNode* trav = head; 7 | ListNode* temp = NULL; 8 | while (trav) { 9 | temp = trav; 10 | while (trav->next && trav->next->val == val) { 11 | trav = trav->next; 12 | } 13 | temp->next = trav->next; 14 | trav = trav->next; 15 | } 16 | return head; 17 | } 18 | }; -------------------------------------------------------------------------------- /November LeetCode Challenge/Day_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | int getDecimalValue(ListNode* head) { 14 | if (!head) 15 | return res; 16 | res = res << 1; 17 | res += head->val; 18 | return getDecimalValue(head->next); 19 | } 20 | 21 | private: 22 | int res = 0; 23 | }; -------------------------------------------------------------------------------- /Codeforces/643D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t=1; 8 | while(t--){ 9 | int n, s; 10 | cin >> n >> s; 11 | if (2 * n <= s) { 12 | cout << "YES\n"; 13 | for (int i = 0; i < n - 1; i++) { 14 | cout << 2 << ' '; 15 | s -= 2; 16 | } 17 | cout << s << '\n' << 1; 18 | } else { 19 | cout << "NO"; 20 | } 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /General Programming Problems/sieveofEratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void SieveOfEratosthenes(int n) 5 | { 6 | bool prime[n+1]; 7 | memset(prime, true, sizeof(prime)); 8 | for (int p=2; p*p<=n; p++) 9 | { 10 | if (prime[p] == true) 11 | { 12 | for (int i=p*p; i<=n; i += p) 13 | prime[i] = false; 14 | } 15 | } 16 | for (int p=2; p<=n; p++) 17 | if (prime[p]) 18 | printf("%d\n",p); 19 | } 20 | int main() 21 | { 22 | int n; 23 | scanf("%d",&n); 24 | SieveOfEratosthenes(n); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_7.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int islandPerimeter(int[][] grid) { 3 | int land = 0; 4 | int edge = 0; 5 | 6 | for (int i = 0; i < grid.length; i++) { 7 | for (int j = 0; j < grid[i].length; j++) { 8 | if (grid[i][j] == 1) { 9 | land ++; 10 | if (i > 0 && grid[i - 1][j] == 1) { edge ++; } 11 | if (j > 0 && grid[i][j - 1] == 1) { edge ++; } 12 | } 13 | } 14 | } 15 | 16 | return land * 4 - edge * 2; 17 | } 18 | } -------------------------------------------------------------------------------- /Codechef April CookOff 2020/LIFTME.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/COOK117B/problems/LIFTME 2 | #include 3 | using namespace std; 4 | #define ll long long int 5 | int main() { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | ll t; 10 | cin >> t; 11 | while(t--){ 12 | ll n,q; 13 | cin >> n >> q; 14 | ll tot =0; 15 | ll curr=0; 16 | while (q--){ 17 | ll a,b; 18 | cin>>a>>b; 19 | tot +=abs(curr-a) + abs(b-a); 20 | curr=b; 21 | } 22 | cout << tot << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_9.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compareVersion(string version1, string version2) { 4 | vectorQ1, Q2; 5 | stringstream check1(version1), check2(version2); 6 | string str; 7 | while (getline(check1, str, '.'))Q1.push_back(stoi(str)); 8 | while (getline(check2, str, '.'))Q2.push_back(stoi(str)); 9 | 10 | Q1.size() > Q2.size() ? Q2.resize(Q1.size()) : Q1.resize(Q2.size()); 11 | 12 | for (int index = 0; index < Q1.size(); index++) { 13 | if (Q1[index] < Q2[index])return -1; 14 | else if (Q1[index] > Q2[index])return 1; 15 | } 16 | return 0; 17 | } 18 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 1/TEST.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | class TEST { 8 | public static void main(String[] args) throws IOException { 9 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 10 | boolean flag =true; 11 | do { 12 | int x=Integer.parseInt(br.readLine()); 13 | if(x==42) 14 | flag=false; 15 | else 16 | System.out.println(x); 17 | }while (flag==true); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 5/HMAPPY2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define int long long 3 | using namespace std; 4 | int gcd(int a,int b) 5 | { 6 | if (b == 0) 7 | return a; 8 | else 9 | return gcd(b, a % b); 10 | } 11 | signed main() 12 | { 13 | int t; 14 | cin>>t; 15 | while(t--) 16 | { 17 | int n,a,b,k; 18 | cin >> n >> a >>b >>k; 19 | int g = gcd(a, b); 20 | g = (a * b) / g; 21 | int f = n / a + n / b - 2 * (n / g); 22 | if (f < k) 23 | cout << "Lose\n"; 24 | else 25 | cout << "Win\n"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codechef June Long 2020/PRICECON.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | signed main() 5 | { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | int tt = 1; 10 | cin >>tt; 11 | while(tt--){ 12 | int n,m; 13 | cin >> n >>m; 14 | int s=0; 15 | int s1=0; 16 | for(int i=0;i>x; 19 | s1+=x; 20 | if(x>m) 21 | x=m; 22 | s+=x; 23 | } 24 | cout << s1-s <<'\n'; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) 4 | { 5 | if (nums.size() == 0) 6 | return 0; 7 | 8 | size_t evenHouses = 0; 9 | size_t oddHouses = 0; 10 | 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | if (i % 2 == 0) 14 | { 15 | evenHouses += nums[i]; 16 | evenHouses = evenHouses > oddHouses ? evenHouses : oddHouses; 17 | } 18 | else 19 | { 20 | oddHouses += nums[i]; 21 | oddHouses = evenHouses > oddHouses ? evenHouses : oddHouses; 22 | } 23 | } 24 | 25 | return evenHouses > oddHouses ? evenHouses : oddHouses; 26 | } 27 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_4.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string S) { 4 | vector v; 5 | vector last(26, 0); 6 | for (int i = 0; i < S.length(); i++) 7 | { 8 | last[S[i] - 'a'] = i; 9 | } 10 | 11 | int j = 0, anchor = 0; 12 | for (int i = 0; i < S.length(); i++) 13 | { 14 | j = max(j, last[S[i] - 'a']); 15 | if (i == j) 16 | { 17 | v.push_back(i - anchor + 1); 18 | anchor = i + 1; 19 | } 20 | } 21 | return v; 22 | } 23 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | std::map mp; 5 | for (auto x : nums) 6 | mp[x]++; 7 | vector> vp; 8 | for (auto x : mp) 9 | { 10 | vp.push_back({x.second, x.first}); 11 | } 12 | sort(vp.begin(), vp.end(), greater<>()); 13 | vector ans; 14 | for (auto x : vp) 15 | { 16 | ans.push_back(x.second); 17 | if (ans.size() == k) 18 | break; 19 | } 20 | return ans; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Codeforces/1359A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long int 3 | using namespace std; 4 | int main() 5 | { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | int t; 10 | cin >> t; 11 | while(t--){ 12 | lli n,m,k,ans=0,a,b,c; 13 | cin>>n>>m>>k; 14 | c=n/k; 15 | if(m<=c) 16 | { 17 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | void solve() { 6 | int n; 7 | cin >> n; 8 | vector arr(n); 9 | for (int &el : arr) 10 | cin >> el; 11 | sort(arr.begin(), arr.end()); 12 | for (int i = n - 1; i >= 0; i--) { 13 | if (arr[i] <= i + 1) { 14 | cout << i + 2 << '\n'; 15 | return; 16 | } 17 | } 18 | cout << 1 << '\n'; 19 | } 20 | int main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | cout.tie(nullptr); 24 | int t; 25 | cin >> t; 26 | while (t--) 27 | solve(); 28 | } -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 2/STUPMACH.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA02/submit/STUPMACH 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | int main(){ 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | int t; 10 | cin>>t; 11 | for(ll i=0;i> n; 14 | ll min = 1e18; 15 | ll ans = 0; 16 | for(ll i=0;i> x ; 18 | if(x 2 | using namespace std; 3 | 4 | int solve(vector& arr, int n) { 5 | int ans = 0; 6 | for (int i=0; i> t; 21 | int c = 1; 22 | while (t--) { 23 | int n, k; 24 | cin >> n; 25 | vector arr(n); 26 | for (int i=0; i> arr[i]; 28 | } 29 | cout << "Case #" << c++ << ": " << solve(arr, n) << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_18.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool is_vovel(char ch) { 4 | ch = tolower(ch); 5 | return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); 6 | } 7 | 8 | string toGoatLatin(string str) { 9 | stringstream ss(str); 10 | string temp, succ = "maa", ans = ""; 11 | while (!ss.eof()) { 12 | ss >> temp; 13 | if (!is_vovel(temp[0])) { 14 | char tp = temp[0]; 15 | temp.erase(temp.begin()); 16 | temp.push_back(tp); 17 | } 18 | temp += succ; 19 | succ.push_back('a'); 20 | ans += (temp + ' '); 21 | } 22 | return ans.substr(0, ans.size() - 1); 23 | } 24 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_11.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) 4 | { 5 | if (nums.size() == 0) 6 | return 0; 7 | int maxSub = nums[0]; 8 | int minSub = nums[0]; 9 | int maxProductSub = nums[0]; 10 | 11 | for (size_t i = 1; i < nums.size(); i++) 12 | { 13 | if (nums[i] < 0) 14 | swap(minSub, maxSub); 15 | maxSub = max(maxSub * nums[i], nums[i]); 16 | minSub = min(minSub * nums[i], nums[i]); 17 | maxProductSub = max(maxProductSub, maxSub); 18 | } 19 | 20 | return maxProductSub; 21 | } 22 | 23 | }; -------------------------------------------------------------------------------- /Google KickStart 2020/A/allocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, b, a[100000]; 4 | void solve() { 5 | cin >> n >> b; 6 | for(int i=0; i> a[i]; 8 | sort(a, a+n); 9 | int ans=0; 10 | for(int i=0; i=a[i]) { 12 | b-=a[i]; 13 | ++ans; 14 | } 15 | } 16 | cout << ans << "\n"; 17 | } 18 | int main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(nullptr); 21 | cout.tie(nullptr); 22 | int t, i=1; 23 | cin >> t; 24 | while(t--) { 25 | cout << "Case #" << i << ": "; 26 | solve(); 27 | ++i; 28 | } 29 | } -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int houseRobber(vector& nums) { 4 | int dp[nums.size() + 1]; 5 | dp[0] = nums[0]; 6 | dp[1] = max(nums[0], nums[1]); 7 | for (int i = 2; i < nums.size(); i++) 8 | dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]); 9 | return dp[nums.size() - 1]; 10 | } 11 | 12 | int rob(vector& nums) { 13 | 14 | if (nums.size() == 0) return 0; 15 | if (nums.size() == 1) return nums[0]; 16 | if (nums.size() == 2) return max(nums[0], nums[1]); 17 | 18 | 19 | vector v1(nums.begin(), nums.end() - 1); 20 | vector v2(nums.begin() + 1, nums.end()); 21 | return max(houseRobber(v1), houseRobber(v2)); 22 | } 23 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double champagneTower(int poured, int query_row, int query_glass) { 4 | vector> v(102, vector(102, 0)); 5 | v[0][0] = poured; 6 | for (int i = 0; i < query_row; i++) 7 | { 8 | for (int j = 0; j <= i; j++) 9 | { 10 | double flow = (v[i][j] - 1) / 2.0; 11 | 12 | if (flow > 0) 13 | { 14 | v[i + 1][j] += flow; 15 | v[i + 1][j + 1] += flow; 16 | } 17 | } 18 | } 19 | return min(1.0, v[query_row][query_glass]); 20 | } 21 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_29.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDistToClosest(vector& seats) { 4 | int longest = 0, beginning = 0, i = 0; 5 | while (seats[i] == 0) { 6 | beginning++; 7 | i++; 8 | } 9 | int count = beginning; 10 | for (; i < seats.size(); i++) { 11 | if (seats[i] == 0) 12 | count++; 13 | else { 14 | longest = std::max({count, longest}); 15 | count = 0; 16 | } 17 | } 18 | longest = longest % 2 == 0 ? longest / 2 : longest / 2 + 1; 19 | return std::max({longest, count, beginning}); 20 | } 21 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mincostTickets(vector& days, vector& cost) { 4 | int n = days.size(); 5 | int dp[n + 1]; 6 | for (int i = 0; i <= n; i++) 7 | dp[i] = INT_MAX; 8 | dp[0] = 0; 9 | dp[1] = min(cost[0], min(cost[1], cost[2])); 10 | for (int i = 2; i <= n; i++) 11 | { 12 | dp[i] = dp[i - 1] + min(cost[0], min(cost[1], cost[2])); 13 | for (int j = 1; j < i; j++) 14 | { 15 | int d = days[i - 1] - days[j - 1] + 1; 16 | if (d <= 7) 17 | dp[i] = min(dp[i], dp[j - 1] + cost[1]); 18 | if (d <= 30) 19 | dp[i] = min(dp[i], dp[j - 1] + cost[2]); 20 | 21 | } 22 | } 23 | return dp[n]; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRightInterval(vector>& intervals) { 4 | unordered_map m; 5 | vector v; 6 | vector ans; 7 | for (int i = 0 ; i < intervals.size(); i++) { 8 | m[intervals[i][0]] = i; 9 | v.push_back(intervals[i][0]); 10 | } 11 | sort(v.begin(), v.end()); 12 | int end; 13 | for (int i = 0 ; i < intervals.size(); i++) { 14 | end = intervals[i][1]; 15 | vector::iterator it = lower_bound(v.begin(), v.end(), end); 16 | if (it != v.end()) { 17 | end = *it; 18 | ans.push_back(m[end]); 19 | } else { 20 | ans.push_back(-1); 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Codeforces/1364A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | while (t--) 8 | { 9 | int n,x,sum=0,l=-1,r; 10 | scanf("%d%d",&n,&x); 11 | for (int i=0;i 3 | #define ll long long int 4 | #define M 1000000007 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(0); 10 | cout.tie(0); 11 | ll n,k; 12 | cin>>n>>k; 13 | ll *a=new ll[n]; 14 | for(ll i=0;i>a[i]; 16 | stack>s; 17 | ll ans=1; 18 | for(ll i=0;ia[i]) 21 | { 22 | pairp=s.top(); 23 | ll c=(i-(p.second)+1); 24 | ans=ans*c; 25 | ans=ans%M; 26 | s.pop(); 27 | } 28 | s.push(make_pair(a[i],i)); 29 | } 30 | cout< 3 | using namespace std; 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n; 12 | cin >> n; 13 | vector ans; 14 | int power = 1; 15 | while (n > 0) { 16 | if (n % 10 > 0) { 17 | ans.push_back((n % 10) * power); 18 | } 19 | n /= 10; 20 | power *= 10; 21 | } 22 | cout << ans.size() << endl; 23 | for (auto number : ans) cout << number << " "; 24 | cout << endl; 25 | } 26 | } -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_12.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool buddyStrings(string A, string B) { 4 | if (A.size() != B.size() || A.size() < 2) return false; 5 | string sa, sb; 6 | bitset<128> bits{0}; 7 | bool dup = false; 8 | for (int i = 0; i < A.size() && sa.size() <= 2; ++i) { 9 | if (A[i] != B[i]) { 10 | sa += A[i], sb += B[i]; 11 | } else { 12 | dup |= bits.test(A[i]); 13 | bits.set(A[i]); 14 | } 15 | } 16 | if (sa.size() == 2) { 17 | swap(sa[0], sa[1]); 18 | return sa == sb; 19 | } 20 | return sa.empty() && dup; 21 | } 22 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_11.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vectorFindSubSet(int no, vector&nums) { 4 | vectorre; 5 | int i = 0; 6 | while (no) { 7 | if (no & 1) { 8 | re.push_back(nums[i]); 9 | } 10 | i++; 11 | no >>= 1; 12 | } 13 | return re; 14 | } 15 | vector> subsets(vector& nums) { 16 | int n = nums.size(); 17 | vector>ans; 18 | int total = 1 << n; 19 | for (int i = 0; i < total; i++) { 20 | vectortemp = FindSubSet(i, nums); 21 | ans.push_back(temp); 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_19.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | int n1 = a.size() - 1; 5 | int n2 = b.size() - 1; 6 | int carry = 0; 7 | int sum = 0; 8 | string s; 9 | while (n1 >= 0 || n2 >= 0) { 10 | int c1 = n1 >= 0 ? a[n1] - '0' : 0; 11 | int c2 = n2 >= 0 ? b[n2] - '0' : 0; 12 | sum = c1 + c2 + carry; 13 | carry = 0; 14 | if (sum == 2) { 15 | sum = 0; 16 | carry = 1; 17 | } 18 | if (sum == 3) { 19 | sum = 1; 20 | carry = 1; 21 | } 22 | s.push_back(sum + '0'); 23 | n1--; 24 | n2--; 25 | } 26 | if (carry != 0) { 27 | s.push_back(carry + '0'); 28 | } 29 | reverse(s.begin(), s.end()); 30 | return s; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_25.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool check(string &s1 , string &s2) 5 | { 6 | return (s1 + s2) >= (s2 + s1); 7 | } 8 | string largestNumber(vector& nums) { 9 | int x = 0; 10 | 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | if (nums[i] == 0) 14 | { 15 | x++; 16 | } 17 | } 18 | if (x == nums.size()) return "0"; 19 | vector s(nums.size(), ""); 20 | 21 | for (int i = 0; i < nums.size(); i++) 22 | { 23 | s[i] = to_string(nums[i]); 24 | } 25 | sort(s.begin(), s.end(), check); 26 | string ans = ""; 27 | 28 | for (int i = 0; i < s.size(); i++) 29 | { 30 | ans += (s[i]); 31 | } 32 | return ans; 33 | 34 | 35 | } 36 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | static int sumRootToLeaf(const TreeNode* r, uint32_t sum = 0) { 15 | if (!r) return 0; 16 | sum |= r->val; 17 | if (!r->left && !r->right) return sum; // leaf 18 | sum <<= 1; 19 | return sumRootToLeaf(r->left, sum) + sumRootToLeaf(r->right, sum); 20 | } 21 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_19.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDominoRotations(vector& A, vector& B) { 4 | vector cntA(7), cntB(7), cntSame(7); 5 | int n = A.size(); 6 | for (int i = 0; i < n; i++) { 7 | int a = A[i], b = B[i]; 8 | cntA[a] += 1; 9 | cntB[b] += 1; 10 | if (a == b) cntSame[a] += 1; 11 | } 12 | int ans = n; 13 | for (int v = 1; v <= 6; v++) { 14 | if (cntA[v] + cntB[v] - cntSame[v] == n) { 15 | int minSwap = min(cntA[v], cntB[v]) - cntSame[v]; 16 | ans = min(ans, minSwap); 17 | } 18 | } 19 | return ans == n ? -1 : ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Codechef March Lunchtime 2020/SWPDGT.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LTIME82B/problems/SWPDGT 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | int t; 10 | cin >> t; 11 | while(t--){ 12 | int a,b; 13 | cin >> a >> b; 14 | int mx=a+b; 15 | int a1=a%10; 16 | int a2=a/10; 17 | int b1=b%10; 18 | int b2=b/10; 19 | mx=max(mx,a2*10+b1+b2*10+a1); 20 | if(b2) mx=max(mx,a2*10+b2+a1*10+b1); 21 | if(a2) mx=max(mx,b1*10+a1+b2*10+a2); 22 | if(a2 && b2) mx=max(mx,b2*10+a1+a2*10+b1); 23 | cout< visited(26, false); 6 | vector dict(26, 0); 7 | for (auto it : s) 8 | dict[it - 'a'] ++; 9 | for (int i = 0; i < s.length(); i++) { 10 | dict[s[i] - 'a'] --; 11 | if (visited[s[i] - 'a']) continue; 12 | while ( res.size() and res.back() > s[i] and dict[res.back() - 'a'] ) { 13 | visited[res.back() - 'a'] = false; 14 | res.pop_back(); 15 | } 16 | res += s[i]; 17 | visited[s[i] - 'a'] = true; 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bagOfTokensScore(vector& tokens, int P) { 4 | 5 | sort(tokens.begin(), tokens.end()); 6 | int l = 0; 7 | int r = tokens.size() - 1; 8 | int tmp = 0; 9 | int res = 0; 10 | while (l <= r) { 11 | 12 | if (tokens[l] <= P) { 13 | P -= tokens[l]; 14 | ++tmp; 15 | ++l; 16 | res = max(tmp, res); 17 | } else if (tmp) { 18 | P += tokens[r]; 19 | --tmp; 20 | --r; 21 | } else { 22 | break; 23 | } 24 | 25 | 26 | } 27 | return res; 28 | } 29 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void dfs(vector> &graph, vector> &ans, int start, int dest, vector &v) { 4 | v.push_back(start); 5 | if (start == dest) { 6 | ans.push_back(v); 7 | v.pop_back(); 8 | return; 9 | } 10 | for (int x : graph[start]) { 11 | dfs(graph, ans, x, dest, v); 12 | } 13 | v.pop_back(); 14 | return; 15 | } 16 | 17 | vector> allPathsSourceTarget(vector>& graph) { 18 | vector > ans; 19 | vector v; 20 | int n = graph.size(); 21 | dfs(graph, ans, 0, n - 1, v); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_10.cpp: -------------------------------------------------------------------------------- 1 | bool comp(pair a, pair b) { 2 | return a.second < b.second; 3 | } 4 | class Solution { 5 | public: 6 | int findMinArrowShots(vector>& points) { 7 | int n = points.size(); 8 | if (n == 0) { 9 | return 0; 10 | } 11 | vector >a; 12 | for (int i = 0; i < n; i++) { 13 | a.push_back({points[i][0], points[i][1]}); 14 | } 15 | sort(a.begin(), a.end(), comp); 16 | int arrows = 1; 17 | int fin = a[0].second; 18 | if (n == 1) { 19 | return 1; 20 | } 21 | for (int i = 1; i < n; i++) { 22 | if (a[i].first <= fin) { 23 | continue; 24 | } 25 | else { 26 | arrows++; 27 | fin = a[i].second; 28 | } 29 | } 30 | return arrows; 31 | } 32 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* prev; 7 | Node* next; 8 | Node* child; 9 | }; 10 | */ 11 | 12 | class Solution { 13 | public: 14 | Node* flatten(Node* head) { 15 | if (!head) return NULL; 16 | Node* trav = head; 17 | while (trav) { 18 | if (trav->child) { 19 | Node* next = trav->next; 20 | Node* child = flatten(trav->child); 21 | trav->child = NULL; 22 | trav->next = child; 23 | child->prev = trav; 24 | Node* lastNode = child; 25 | while (lastNode->next) lastNode = lastNode->next; 26 | lastNode->next = next; 27 | if (next) next->prev = lastNode; 28 | } 29 | trav = trav->next; 30 | } 31 | return head; 32 | } 33 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 1/FLOW007.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class FLOW007 { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(br.readLine()); 11 | while (t-->0){ 12 | int x=Integer.parseInt(br.readLine()); 13 | System.out.println(rev(x)); 14 | } 15 | } 16 | static int rev(int x){ 17 | int c=0; 18 | while (x!=0){ 19 | int q=x%10; 20 | c=c*10+q; 21 | x/=10; 22 | } 23 | return c; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Codechef April Long 2020/COVIDLQ.cpp: -------------------------------------------------------------------------------- 1 | //Question : https://www.codechef.com/APRIL20B/problems/COVIDLQ 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int t; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | int n; 13 | scanf("%d",&n); 14 | string ans="YES"; 15 | int *arr=new int[n]; 16 | for(int i=0;i>arr[i]; 19 | } 20 | 21 | int val=0; 22 | for(int i=0;i6)?6:n-i; 25 | for(int j=1;jval == sum) + DFS(root->left, sum - root->val) + DFS(root->right, sum - root->val) : 0; 16 | } 17 | int pathSum(TreeNode* root, int sum) { 18 | return root ? DFS(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum) : 0; 19 | } 20 | }; -------------------------------------------------------------------------------- /Codeforces/Tprimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | const int MAX = 1000000; 5 | int main() { 6 | ll T; 7 | cin >> T; 8 | vector all(MAX + 1, true); 9 | all[0] = all[1] = false; 10 | for (int i = 2; i * i <= MAX; ++i) 11 | if (all[i]) 12 | for (int j = i * i; j <= MAX; j += i) 13 | all[j] = false; 14 | map tprime; 15 | for (int i = 2; i < MAX; i++){ 16 | if (all[i]) { 17 | tprime[1ll * i * i] = true; 18 | } 19 | } 20 | while (T--) { 21 | ll a; 22 | cin >> a; 23 | if (tprime[a]) 24 | cout << "YES \n"; 25 | else 26 | cout << "NO \n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Recode/RECNDSTR.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/submit/RECNDSTR 2 | #include 3 | #define loop for (int i = 0; i < n; i++) 4 | using namespace std; 5 | int main() { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | int t; 10 | cin >> t; 11 | while (t--) { 12 | string s; 13 | cin >> s; 14 | int l = s.length(); 15 | if (l == 1 || l == 2) { 16 | cout << "YES" << endl; 17 | continue; 18 | } else{ 19 | string sl = s.substr(1)+s[0]; 20 | string sr = s[l-1]+s.substr(0,l-1); 21 | if(sl==sr) 22 | cout<<"YES"< 0) { 11 | int number = Integer.parseInt(br.readLine()); 12 | int count = 0; 13 | int divider = 5; 14 | 15 | while ((number / divider) > 0) { 16 | count += number / divider; 17 | divider = divider * 5; 18 | } 19 | System.out.println(count); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_23.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | private: 14 | int sum(TreeNode* n, bool isLeft) { 15 | if (n == NULL) 16 | return 0; 17 | if (isLeft && n->left == NULL && n->right == NULL) 18 | return n->val; 19 | return sum(n->left, true) + sum(n->right, false); 20 | } 21 | 22 | public: 23 | int sumOfLeftLeaves(TreeNode* root) { 24 | return sum(root, false); 25 | } 26 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_12.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void helper(vector> &ans,vector current,int n,int k,int start) 5 | { 6 | if(n==0 and k==0) 7 | { 8 | ans.push_back(current); 9 | return; 10 | } 11 | 12 | if(k==0) 13 | { 14 | return; 15 | } 16 | 17 | for(int i=start;i<=9;i++) 18 | { 19 | current.push_back(i); 20 | helper(ans,current,n-i,k-1,i+1); 21 | current.pop_back(); 22 | } 23 | 24 | return; 25 | } 26 | 27 | vector> combinationSum3(int k, int n) { 28 | 29 | 30 | vector> ans; 31 | 32 | vector current; 33 | 34 | helper(ans,current,n,k,1); 35 | 36 | return ans; 37 | 38 | } 39 | }; -------------------------------------------------------------------------------- /General Programming Problems/dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector thisisvec[1001]; 6 | 7 | bool was[10001]; 8 | int start, finish; 9 | 10 | void dfs(int start) { 11 | 12 | was[start] = true; 13 | cout << start << " "; 14 | 15 | vector::iterator i; 16 | 17 | //for (int i = 1; i <= ways[start].size(); ++i) { 18 | for (i = thisisvec[start].begin(); i != thisisvec[start].end(); ++i) { 19 | if (!was[*i]) { 20 | dfs(*i); 21 | } 22 | } 23 | } 24 | 25 | 26 | int main () { 27 | 28 | int city, ways, a, b; 29 | cin >> city >> ways; 30 | 31 | for (int i = 1; i <= ways; i++) { 32 | cin >> a >> b; 33 | thisisvec[a].push_back(b); 34 | thisisvec[b].push_back(a); 35 | } 36 | 37 | cin >> start >> finish; 38 | 39 | dfs(start); 40 | 41 | } -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_6.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] plusOne(int[] digits) { 3 | if (digits == null || digits.length == 0) 4 | return new int[0]; 5 | 6 | int carry = 1; 7 | for (int i = digits.length - 1; i >= 0; i--) { 8 | int sum = digits[i] + carry; 9 | if (sum >= 10) { 10 | carry = 1; 11 | } else { 12 | carry = 0; 13 | } 14 | digits[i] = sum % 10; 15 | } 16 | 17 | if (carry == 1) { 18 | int[] result = new int[digits.length + 1]; 19 | System.arraycopy(digits, 0, result, 1, digits.length); 20 | result[0] = 1; 21 | return result; 22 | } else { 23 | return digits; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Codechef June Long 2020/EOEO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | signed main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | int a; 12 | cin >> a; 13 | if(a<=2) 14 | { 15 | cout << "0\n"; 16 | } else if (a%2==1){ 17 | cout << a/2 <<'\n'; 18 | } else if(a%2==0){ 19 | int ct=0; 20 | while(a>2) 21 | { 22 | if(a%2==1) 23 | { 24 | ct+=a/2; 25 | break; 26 | } 27 | a=a/2; 28 | } 29 | cout << ct <<'\n'; 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_22.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector majorityElement(vector& nums) { 4 | int cand1, vote1 = 0, cand2, vote2 = 0, lmt = nums.size() / 3; 5 | for (int n : nums) { 6 | if (n == cand1) vote1++; 7 | else if (n == cand2) vote2++; 8 | else if (!vote1) { 9 | cand1 = n; 10 | vote1++; 11 | } 12 | else if (!vote2) { 13 | cand2 = n; 14 | vote2++; 15 | } 16 | else { 17 | vote1--; 18 | vote2--; 19 | } 20 | } 21 | vote1 = 0; 22 | vote2 = 0; 23 | for (int n : nums) { 24 | if (n == cand1) vote1++; 25 | if (n == cand2) vote2++; 26 | } 27 | if (vote1 < vote2) { 28 | swap(cand1, cand2); 29 | swap(vote1, vote2); 30 | } 31 | return vote1 <= lmt ? vector {} : vote2 <= lmt ? vector {cand1} : vector {cand1, cand2}; 32 | } 33 | }; -------------------------------------------------------------------------------- /Codechef February Lunchtime 2020/POPGATES.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LTIME81B/problems/POPGATES 2 | #include 3 | using namespace std; 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | int n, k; 12 | char c[100]; 13 | cin >> n >> k; 14 | for(int i=0; i> c[i]; 16 | if(c[n-k]=='H') { 17 | for(int i=0; i&A,vector&B) 2 | { 3 | if(A[0]==B[0]) 4 | { 5 | return A[1]>B[1]; 6 | } 7 | return A[0]>& intervals) { 16 | 17 | sort(intervals.begin(),intervals.end(),compare); 18 | 19 | 20 | int count=1,start=intervals[0][0],end=intervals[0][1]; 21 | 22 | for(int i=1;i=intervals[i][1]) 25 | { 26 | continue; 27 | } 28 | else 29 | { 30 | start=min(start,intervals[i][0]); 31 | end=max(end,intervals[i][1]); 32 | count++; 33 | } 34 | } 35 | return count; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /General Programming Problems/sieveofEratosthenes.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | class SieveOwn { 4 | public static void main(String[] args)throws IOException { 5 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 | int N = Integer.parseInt(br.readLine()); 7 | br.close(); 8 | boolean[] arr = new boolean[N + 1]; 9 | Arrays.fill(arr, true); 10 | arr[0] = arr[1] = false; 11 | for (int i = 2; i <= Math.sqrt(N); i++) { 12 | if (arr[i]) 13 | for (int j = i * i; j < arr.length; j += i) { 14 | arr[j] = false; 15 | } 16 | } 17 | for (int q = 2; q < arr.length; q++) { 18 | if (arr[q]){ 19 | System.out.println(q); 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_8.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | vector> output; 5 | sort(nums.begin(), nums.end()); 6 | for (int i = 0; i < nums.size(); ++i) { 7 | // Never let i refer to the same value twice to avoid duplicates. 8 | if (i != 0 && nums[i] == nums[i - 1]) continue; 9 | int j = i + 1; 10 | int k = nums.size() - 1; 11 | while (j < k) { 12 | if (nums[i] + nums[j] + nums[k] == 0) { 13 | output.push_back({nums[i], nums[j], nums[k]}); 14 | ++j; 15 | // Never let j refer to the same value twice (in an output) to avoid duplicates 16 | while (j < k && nums[j] == nums[j - 1]) ++j; 17 | } else if (nums[i] + nums[j] + nums[k] < 0) { 18 | ++j; 19 | } else { 20 | --k; 21 | } 22 | } 23 | } 24 | return output; 25 | } 26 | }; -------------------------------------------------------------------------------- /General Programming Problems/TDKPRIME.cpp: -------------------------------------------------------------------------------- 1 | //https://www.spoj.com/problems/TDKPRIME 2 | #include 3 | using namespace std; 4 | bool ar[90000001]; 5 | vector primes; 6 | void sieve(){ 7 | int maxN=90000000; 8 | ar[0]=ar[1]= true; 9 | for(int i=2;i*i<=maxN;i++){ 10 | if(!ar[i]){ 11 | for (int j = i*i; j <=maxN ; j+=i) { 12 | ar[j]=true; 13 | } 14 | } 15 | } 16 | for(int i=2;i<=maxN;i++) 17 | { 18 | if(!ar[i]) 19 | primes.push_back(i); 20 | } 21 | } 22 | int main() { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(nullptr); 25 | cout.tie(nullptr); 26 | sieve(); 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int n; 31 | cin>>n; 32 | cout << primes[n-1]< &prices, int st, int n, bool bou, vector > &dp) { 4 | if (st >= n) 5 | return 0; 6 | if (dp[st][bou] != -1) 7 | return dp[st][bou]; 8 | dp[st][bou] = max(helper(prices, st + 1 + bou, n, !bou, dp) + (bou * prices[st]) - (!bou * prices[st]), 9 | helper(prices, st + 1, n, bou, dp)); 10 | return dp[st][bou]; 11 | } 12 | int maxProfit(vector& prices) { 13 | int st = 0, n = prices.size(); 14 | if (n == 0) return 0; 15 | vector > dp(n, vector (2, -1)); 16 | while ((st < (n - 1)) && (prices[st] > prices[st + 1])) 17 | st ++; 18 | return (st == (n - 1)) ? 0 : helper(prices, st, n, 1, dp) - prices[st]; 19 | } 20 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool isRobotBounded(string instructions) { 5 | unordered_map> dir{{'N', {'W', 'E'}}, {'S', {'E', 'W'}}, {'E', {'N', 'S'}}, {'W', {'S', 'N'}}}; 6 | unordered_map> moves{{'N', {0, 1}}, {'S', {0, -1}}, {'E', {1, 0}}, {'W', { -1, 0}}}; 7 | int i = 0, j = 0; 8 | char facing = 'N'; 9 | for (auto c : instructions) 10 | { 11 | if (c == 'L') 12 | { 13 | facing = dir[facing].first; 14 | } 15 | 16 | else if (c == 'R') 17 | { 18 | facing = dir[facing].second; 19 | } 20 | 21 | else if (c == 'G') 22 | { 23 | i += moves[facing].first; 24 | j += moves[facing].second; 25 | } 26 | 27 | } 28 | 29 | if (i == 0 and j == 0) 30 | { 31 | return true; 32 | } 33 | return facing != 'N'; 34 | 35 | 36 | } 37 | }; -------------------------------------------------------------------------------- /Codechef March Lunchtime 2020/PRFXGD.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LTIME82B/problems/PRFXGD 2 | #include 3 | using namespace std; 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | string s; 12 | int k, x, n; 13 | cin >> s; n = s.size(); 14 | cin >> k >> x; 15 | int cnt[26]; 16 | memset(cnt, 0, sizeof cnt); 17 | int ans = 0 ; 18 | for(int i = 0; i < n ; i++){ 19 | int c = s[i] - 'a'; 20 | if(cnt[c] == x){ 21 | if(k==0) break ; 22 | k--; 23 | continue; 24 | } 25 | cnt[c]++ ; 26 | ans++ ; 27 | } 28 | cout << ans << "\n"; 29 | } 30 | 31 | 32 | return 0 ; 33 | } -------------------------------------------------------------------------------- /Google KickStart 2020/B/busRoutes.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | class Buses { 3 | public static void main(String args[]) throws IOException { 4 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 5 | BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out)); 6 | int T=Integer.parseInt(br.readLine()); 7 | int a=1; 8 | while(T-- != 0) 9 | { 10 | int c=0; 11 | String s[]=br.readLine().split(" "); 12 | int N=Integer.parseInt(s[0]); 13 | long D=Long.parseLong(s[1]); 14 | s=br.readLine().split(" "); 15 | for(int i=N-1;i>=0;--i) { 16 | 17 | D-=D%(Long.parseLong(s[i])); 18 | } 19 | bw.write("Case #"+a+": "+D+"\n"); 20 | bw.flush(); 21 | a++; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Codeforces/1363B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | int32_t main() 5 | { 6 | ios::sync_with_stdio(0); 7 | cin.tie(0); 8 | cout.tie(0); 9 | int t; 10 | cin >> t; 11 | while(t--) 12 | { 13 | string s; 14 | cin >> s; 15 | int suf0 = 0, suf1 = 0; 16 | for(auto &it:s) 17 | { 18 | suf0 += (it == '0'); 19 | suf1 += (it == '1'); 20 | } 21 | int ans = min(suf0, suf1); 22 | int pref0 = 0, pref1 = 0; 23 | for(auto &it:s) 24 | { 25 | pref0 += (it == '0'), suf0 -= (it == '0'); 26 | pref1 += (it == '1'), suf1 -= (it == '1'); 27 | ans = min(ans, pref1 + suf0); 28 | ans = min(ans, pref0 + suf1); 29 | } 30 | cout << ans << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Codeforces/643A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define int long long 3 | using namespace std; 4 | pair dig(int n) 5 | { 6 | int largest = 0; 7 | int smallest = 9; 8 | 9 | while (n) { 10 | int r = n % 10; 11 | largest = max(r, largest); 12 | smallest = min(r, smallest); 13 | n = n / 10; 14 | } 15 | return {smallest,largest}; 16 | } 17 | int32_t main() 18 | { 19 | ios::sync_with_stdio(false); 20 | cin.tie(nullptr); 21 | cout.tie(nullptr); 22 | int t; 23 | cin >> t; 24 | while(t--){ 25 | int a,b; 26 | cin >> a >> b; 27 | for(int i=1;i0) 30 | a+=dig(a).first*dig(a).second; 31 | else 32 | break; 33 | } 34 | cout << a << '\n'; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /General Programming Problems/modularExponentiation.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.util.Scanner; 3 | class modularExponentiation{ 4 | static long mod = (long) (1e9+7); 5 | public static void main(String args[])throws IOException{ 6 | Scanner sc = new Scanner(System.in); 7 | int t=sc.nextInt(); 8 | while (t-->0){ 9 | long a= sc.nextLong(); 10 | long b= sc.nextLong(); 11 | System.out.println(expn(a,b,mod)); 12 | } 13 | } 14 | static long expn(long x , long n , long m){ 15 | if(n==1) 16 | return (int) (x%m); 17 | else if(n==0) 18 | return 1; 19 | else if(n%2==0) { 20 | long y= expn(x, n / 2, m); 21 | return ((y*y) % m); 22 | } 23 | else 24 | return ((expn(x,1,m)*expn(x,n-1,m))%m); 25 | } 26 | } -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 3/SAVKONO.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA03/problems/SAVKONO/ 2 | #include 3 | #define endl '\n' 4 | using namespace std; 5 | int main() 6 | { 7 | ios :: sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n,z; 15 | cin>>n>>z; 16 | priority_queue pq; 17 | int a; 18 | for(int i=0;i>a; 21 | pq.push(a); 22 | } 23 | int ans=0; 24 | while(!pq.empty() && z>0) 25 | { 26 | a=pq.top(); 27 | pq.pop(); 28 | z=z-a; 29 | a=a/2; 30 | if(a>0) pq.push(a); 31 | ans++; 32 | } 33 | if(z>0) cout<<"Evacuate\n"; 34 | else cout< 2 | #define ll long long 3 | using namespace std; 4 | ll f[60] = {0}; 5 | ll fib(ll f[], ll n) 6 | { 7 | f[0] = 0; 8 | f[1] = 1; 9 | for (ll i = 2; i <= n; i++) 10 | f[i] = (f[i - 1] + f[i - 2]) % 10; 11 | 12 | return f[n]; 13 | } 14 | 15 | int findLastDigit(ll n) 16 | { 17 | if(n<0) 18 | return 0; 19 | 20 | 21 | return f[n % 60]; 22 | } 23 | int main() 24 | { 25 | ios::sync_with_stdio(false); 26 | cin.tie(nullptr); 27 | cout.tie(nullptr); 28 | int t; 29 | cin>>t; 30 | fib(f,60); 31 | while(t--) 32 | { 33 | ll n; 34 | cin>>n; 35 | ll i=1; 36 | while(i<=n) 37 | { 38 | i*=2; 39 | } 40 | if(i!=n) 41 | i/=2; 42 | 43 | cout< 3 | #define ll unsigned long int 4 | using namespace std; 5 | int main() 6 | { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | int t; 11 | cin >> t; 12 | while(t--){ 13 | ll size ,q; 14 | cin >> size >> q; 15 | ll a[size]; 16 | int c=0; 17 | for(int i=0;i>a[i]; 19 | if(__builtin_popcount(a[i])%2==0) 20 | c++; 21 | } 22 | while(q--){ 23 | int val; 24 | cin >>val; 25 | if(__builtin_popcount(val)%2==0) 26 | { 27 | cout << c << " " << size-c << '\n'; 28 | } else cout << size-c << " " << c << '\n'; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codechef May Long 2020/SORTVS.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/MAY20B/problems/SORTVS 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | long long t,n,m,q,r,flag=0,temp; 10 | cin>>t; 11 | while(t--) 12 | { 13 | cin>>n>>m; 14 | int p[n+1]; 15 | p[0]=INT_MAX; 16 | for(int i=1;i<=n;i++) 17 | cin>>p[i]; 18 | int count=0; 19 | for(int i=1;i<=n-1;i++) 20 | { 21 | for(int j=1+i;j<=n;j++) 22 | { 23 | if(i==p[j] && p[i]!=p[j]) 24 | { 25 | count++; 26 | swap(p[i],p[j]); 27 | break; 28 | } 29 | } 30 | } 31 | cout< 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int n; 11 | cin >> n; 12 | int a[n+1]; 13 | int b[n+1]; 14 | for(int i=1;i<=n;i++) 15 | cin >> a[i]; 16 | for(int i=1;i<=n;i++) 17 | cin >> b[i]; 18 | a[0]=b[0]=0; 19 | long long s1 = 0,s2=0; 20 | long long fin = 0; 21 | for(int i=0;i > zigzagLevelOrder(TreeNode* root) { 4 | if (root == NULL) { 5 | return vector > (); 6 | } 7 | vector > result; 8 | 9 | queue nodesQueue; 10 | nodesQueue.push(root); 11 | bool leftToRight = true; 12 | 13 | while ( !nodesQueue.empty()) { 14 | int size = nodesQueue.size(); 15 | vector row(size); 16 | for (int i = 0; i < size; i++) { 17 | TreeNode* node = nodesQueue.front(); 18 | nodesQueue.pop(); 19 | int index = (leftToRight) ? i : (size - 1 - i); 20 | row[index] = node->val; 21 | if (node->left) { 22 | nodesQueue.push(node->left); 23 | } 24 | if (node->right) { 25 | nodesQueue.push(node->right); 26 | } 27 | } 28 | leftToRight = !leftToRight; 29 | result.push_back(row); 30 | } 31 | return result; 32 | } 33 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { 4 | unordered_map mp; 5 | 6 | if (nums.size() < 2 || k < 1 || t < 0) return false; 7 | 8 | for (int i = 0; i < nums.size(); i++) { 9 | long long temp = ((long long)nums[i] - (long long)INT_MIN) / ((long long)t + 1); 10 | 11 | if (i > k) { 12 | long long last = ((long long)nums[i - k - 1] - (long long)INT_MIN) / ((long long)t + 1); 13 | mp.erase(last); 14 | } 15 | 16 | if (mp.count(temp) || (mp.count(temp - 1) && abs(mp[temp - 1] - nums[i]) <= t) || (mp.count(temp + 1) && abs(mp[temp + 1] - nums[i]) <= t)) { 17 | return true; 18 | } 19 | mp[temp] = nums[i]; 20 | } 21 | 22 | return false; 23 | } 24 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | if (head == NULL) return NULL; 15 | 16 | int len = 0; 17 | ListNode* node = head; 18 | ListNode* prev; 19 | 20 | while (node) prev = node, node = node->next, len = len + 1; 21 | 22 | prev->next = head; //make the list circular 23 | int leftstrt = len - k % len; //find the start from left and make prev node to start NULL 24 | node = head; 25 | 26 | while (leftstrt--) prev = node, node = node->next; 27 | 28 | prev->next = NULL; 29 | 30 | return node; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 5/CHEFPRMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool isPrime(int x) 4 | { 5 | for(int i = 2; i*i <= x; i++) 6 | if(x % i == 0) 7 | return 0; 8 | return 1; 9 | } 10 | bool isSemiPrime(int a) 11 | { 12 | for(int i = 2; i*i < a; i++) 13 | { 14 | if(a % i == 0 && isPrime(i) && isPrime(a/i)) 15 | return 1; 16 | } 17 | return 0; 18 | } 19 | int main() 20 | { 21 | int t,n; 22 | cin>>t; 23 | while(t--) 24 | { 25 | cin>>n; 26 | int f=0; 27 | for(int i = 4; i <= n/2; i++) 28 | { 29 | int j = n - i; 30 | 31 | if(isSemiPrime(i) && isSemiPrime(j)) 32 | { 33 | cout << "YES\n"; 34 | f=1; 35 | break; 36 | 37 | } 38 | } 39 | if(f==0) 40 | cout << "NO\n"; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Codeforces/646A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int n,x; 11 | cin >> n >> x; 12 | int a[n]; 13 | int odd=0; 14 | int even=0; 15 | for(int i=0;i> a[i]; 17 | if(a[i]%2==1) 18 | odd++; 19 | else 20 | even++; 21 | } 22 | if (x == n) { 23 | if (odd%2) cout << "Yes" << "\n"; 24 | else cout << "No" << "\n"; 25 | } else { 26 | if (even > 0 and odd > 0) cout << "Yes" << "\n"; 27 | else if (odd == 0) cout << "No" << "\n"; 28 | else if (x%2) cout << "Yes" << "\n"; 29 | else cout << "No" << "\n"; 30 | } 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /General Programming Problems/LargestRectinHistogram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given n non-negative integers representing the histogram's bar height where the 3 | width of each bar is 1, find the area of largest rectangle in the histogram. 4 | */ 5 | 6 | class Solution { 7 | public: 8 | int largestRectangleArea(vector& heights) { 9 | stacks; 10 | int maxa = 0; 11 | int tp = 0; 12 | int atp = 0; 13 | int i = 0; 14 | int n = heights.size(); 15 | while (i < n) { 16 | if (s.empty() || heights[s.top()] <= heights[i]) 17 | s.push(i++); 18 | else 19 | { 20 | tp = s.top(); 21 | s.pop(); 22 | 23 | 24 | atp = heights[tp] * (s.empty() ? i : i - s.top() - 1); 25 | maxa = max(maxa, atp); 26 | } 27 | } 28 | while (s.empty() == false) 29 | { 30 | tp = s.top(); 31 | s.pop(); 32 | atp = heights[tp] * (s.empty() ? i : i - s.top() - 1); 33 | maxa = max(maxa, atp); 34 | } 35 | return maxa; 36 | } 37 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_4.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthUglyNumber(int n) { 4 | unsigned ugly[n]; 5 | unsigned i2 = 0, i3 = 0, i5 = 0; 6 | unsigned next_multiple_of_2 = 2; 7 | unsigned next_multiple_of_3 = 3; 8 | unsigned next_multiple_of_5 = 5; 9 | unsigned next_ugly_no = 1; 10 | 11 | ugly[0] = 1; 12 | for (int i = 1; i < n; i++) 13 | { 14 | next_ugly_no = min(next_multiple_of_2, min(next_multiple_of_3, next_multiple_of_5)); 15 | ugly[i] = next_ugly_no; 16 | if (next_ugly_no == next_multiple_of_2) 17 | { 18 | i2 = i2 + 1; 19 | next_multiple_of_2 = ugly[i2] * 2; 20 | } 21 | if (next_ugly_no == next_multiple_of_3) 22 | { 23 | i3 = i3 + 1; 24 | next_multiple_of_3 = ugly[i3] * 3; 25 | } 26 | if (next_ugly_no == next_multiple_of_5) 27 | { 28 | i5 = i5 + 1; 29 | next_multiple_of_5 = ugly[i5] * 5; 30 | } 31 | } 32 | return next_ugly_no; 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_2.cpp: -------------------------------------------------------------------------------- 1 | struct Bucket { 2 | vector bucket = {}; 3 | void add(int val) { 4 | if (!contains(val)) bucket.push_back(val); 5 | } 6 | bool contains(int val) { 7 | return find(begin(bucket), end(bucket), val) != end(bucket); 8 | } 9 | void remove(int val) { 10 | auto pos = find(begin(bucket), end(bucket), val); 11 | if (pos != end(bucket)) bucket.erase(pos); 12 | } 13 | }; 14 | 15 | class MyHashSet { 16 | public: 17 | vector buckets; 18 | int hashSize; 19 | MyHashSet(int keySpace = 137) { 20 | hashSize = keySpace; 21 | buckets.resize(keySpace); 22 | } 23 | void add(int key) { 24 | buckets[key % hashSize].add(key); 25 | } 26 | void remove(int key) { 27 | buckets[key % hashSize].remove(key); 28 | } 29 | bool contains(int key) { 30 | return buckets[key % hashSize].contains(key); 31 | } 32 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 5/ZACKHAN.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA05/problems/ZACKHAN 2 | #include 3 | using namespace std; 4 | #define endl '\n' 5 | #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | int gcd(int a, int b) 7 | { 8 | if (a == b) 9 | return a; 10 | if (a == 0) 11 | return b; 12 | if (b == 0) 13 | return a; 14 | if (~a & 1) 15 | { 16 | if (b & 1) 17 | return gcd(a >> 1, b); 18 | else 19 | return gcd(a >> 1, b >> 1) << 1; 20 | } 21 | if (~b & 1) 22 | return gcd(a, b >> 1); 23 | if (a > b) 24 | return gcd((a - b) >> 1, b); 25 | return gcd((b - a) >> 1, a); 26 | } 27 | int main() 28 | { 29 | fastio; 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | int a,b; 35 | cin >> a >> b; 36 | cout << gcd(a,b)< 3 | using namespace std; 4 | #define endl "\n" 5 | #define ll long long 6 | #define mod 1000000007 7 | void solve(string &s) 8 | { 9 | ll i,j=0,x=-1; 10 | for(i=0;i>test; 40 | while(test--) 41 | { 42 | cin>>s; 43 | solve(s); 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Codeforces/1362A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | signed main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int a,b,c,d,e,f,i,j,k=0,l=0; 10 | cin>>a>>b; 11 | if(b>a) 12 | swap(a,b); 13 | if(a==b) 14 | cout<<0<= c_end: 13 | res_arr.append(('C', idx)) 14 | c_end = end 15 | else: 16 | res_arr.append(('J', idx)) 17 | j_end = end 18 | res = '' 19 | for c, idx in sorted(res_arr, key=lambda x: x[1]): 20 | res += c 21 | return res 22 | T = int(input()) 23 | for t in range(1, T + 1): 24 | N = int(input()) 25 | arr = [] 26 | for _ in range(N): 27 | interval = [int(s) for s in input().split(" ")] 28 | arr.append(interval) 29 | res = f(arr) 30 | print("Case #{}: {}".format(t, res)) -------------------------------------------------------------------------------- /HackerEarth May Circuits/factorialEquations.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerearth.com/challenges/competitive/may-circuits-20/algorithm/powerful-of-factorial-cb263e5b/ 2 | #include 3 | using namespace std; 4 | int modulo(int a, int b, int n){ 5 | long long x=1, y=a; 6 | while (b > 0) { 7 | if (b%2 == 1) { 8 | x = (x*y) % n; 9 | } 10 | y = (y*y) % n; 11 | b /= 2; 12 | } 13 | return x % n; 14 | } 15 | int factmod(int n, int p) { 16 | int res = 1; 17 | while (n > 1) { 18 | res = (res * ((n/p) % 2 ? p-1 : 1)) % p; 19 | for (int i = 2; i <= n%p; ++i) 20 | res = (res * i) % p; 21 | n /= p; 22 | } 23 | return res % p; 24 | } 25 | int main() { 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(nullptr); 28 | cout.tie(nullptr); 29 | int a,b; 30 | cin >> a >> b ; 31 | cout << modulo(a,factmod(b,10),10)%10 << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Codechef May CookOff 2020/CHEFSHIP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | string s; 11 | cin >> s; 12 | int c=0; 13 | if(s.length()%2==0 && s.length()>=4) { 14 | for (int i = 2; i < s.length(); i += 2) { 15 | string one = s.substr(0, i); 16 | string two = s.substr(i,s.length()); 17 | string t11 = one.substr(0, one.length() / 2); 18 | string t12 = one.substr(one.length() / 2); 19 | string t21 = two.substr(0, two.length() / 2); 20 | string t22 = two.substr(two.length() / 2); 21 | if (t11 == t12 && t21 == t22) 22 | c++; 23 | } 24 | cout << c << endl; 25 | } else cout << 0 << endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 3/DPAIRS.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA03/problems/DPAIRS 2 | #include 3 | #define ll long long int 4 | #define fr(i,n) for(i=0;i>a[i];} 6 | #define inf ((int)1e9+3) 7 | using namespace std; 8 | ll i; 9 | 10 | int main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | int n,m; 15 | cin>>n>>m; 16 | int a[n],b[m]; 17 | ain(a,n) 18 | ain(b,m) 19 | int minb = inf,pos; 20 | fr(i,m) 21 | if(minb > b[i]) 22 | { 23 | minb = b[i]; 24 | pos = i; 25 | } 26 | fr(i,n) 27 | cout< 3 | using namespace std; 4 | #define ll long long 5 | int main(){ 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | vector vrr(100003,500); 10 | vrr[100000]=0; 11 | ll n,ht,lt,ans=0; 12 | cin>>n; 13 | ll i; 14 | for(i = 0; i < n; i++){ 15 | ll x,y; 16 | cin>>x>>y; 17 | if(vrr[x] > y) 18 | vrr[x] = y; 19 | } 20 | stack st; 21 | for(i = 1; i <= 100000; i++){ 22 | while(!st.empty() && vrr[st.top()] > vrr[i]){ 23 | ht = vrr[st.top()]; 24 | st.pop(); 25 | lt = 0; 26 | if(!st.empty()) 27 | lt=st.top(); 28 | ans=max(ans,ht * (i - lt)); 29 | } 30 | st.push(i); 31 | } 32 | cout<& nums) { 6 | 7 | 8 | if (nums.size() == 0) return 1; 9 | 10 | int n = nums.size(); 11 | 12 | bool containsone = false; 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | if (nums[i] == 1) 17 | { 18 | containsone = true; 19 | break; 20 | } 21 | } 22 | 23 | if (!containsone) return 1; 24 | 25 | for (int i = 0; i < nums.size(); i++) 26 | { 27 | if (nums[i] <= 0 or nums[i] > n) 28 | { 29 | nums[i] = 1; 30 | } 31 | } 32 | 33 | for (int i = 0; i < nums.size(); i++) 34 | { 35 | int val = nums[i]; 36 | int pos = abs(val) - 1; 37 | 38 | if (nums[pos] > 0) nums[pos] = -1 * nums[pos]; 39 | 40 | 41 | } 42 | 43 | 44 | for (int i = 0; i < nums.size(); i++) 45 | { 46 | if (nums[i] > 0) 47 | { 48 | return i + 1; 49 | } 50 | } 51 | 52 | 53 | return n + 1; 54 | 55 | 56 | 57 | 58 | } 59 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 2/STFOOD.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class STFOOD { 4 | public static void main(String[]args) throws IOException { 5 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 7 | int t=Integer.parseInt(br.readLine()); 8 | 9 | while (t-->0) { 10 | int n = Integer.parseInt(br.readLine()); 11 | long m=0; 12 | while (n-- > 0) { 13 | String str[] = br.readLine().split(" "); 14 | long s = Long.parseLong(str[0]); 15 | long p = Long.parseLong(str[1]); 16 | long v = Long.parseLong(str[2]); 17 | long val = (p / (s + 1))*v; 18 | m = Math.max(m,val); 19 | } 20 | bw.write(m+"\n"); 21 | bw.flush(); 22 | } 23 | bw.close(); 24 | br.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_18.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(int k, vector& prices) { 4 | int n = prices.size(); 5 | if (n == 0 || n == 1 || k == 0) { 6 | return 0; 7 | } 8 | if (k >= n / 2) { 9 | int prof = 0; 10 | for (int i = 1; i < n; i++) { 11 | 12 | if (prices[i] > prices[i - 1]) 13 | prof += prices[i] - prices[i - 1]; 14 | } 15 | return prof; 16 | } 17 | int dp[k + 1][n]; 18 | memset(dp , 0 , sizeof(dp)); 19 | for (int i = 1; i < k + 1; i++) { 20 | int maxprofitbefore = INT_MIN; 21 | for (int j = 1; j < n; j++) { 22 | maxprofitbefore = max(maxprofitbefore , dp[i - 1][j - 1] - prices[j - 1]); 23 | dp[i][j] = max(dp[i][j - 1], prices[j] + maxprofitbefore); 24 | } 25 | } 26 | 27 | return dp[k][n - 1]; 28 | } 29 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | void reorderList(ListNode* head) { 14 | if (head == nullptr) return; 15 | std::vector nodes; 16 | ListNode* l2 = head; 17 | while (l2) { 18 | nodes.push_back(l2); 19 | l2 = l2->next; 20 | } 21 | if (nodes.size() == 2) return; 22 | int low = 1; 23 | int high = int(nodes.size()) - 1; 24 | while (low <= high) { 25 | nodes[low - 1]->next = nodes[high]; 26 | nodes[high]->next = nodes[low]; 27 | ++low; 28 | --high; 29 | } 30 | nodes[low - 1]->next = nullptr; 31 | } 32 | }; -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print_vector(vector v){ 4 | cout << "["; 5 | for(int i = 0; i prisonAfterNDays(vector& cells, int N) { 13 | map > m; 14 | if(!N) 15 | return cells; 16 | set > visited; 17 | visited.insert(cells); 18 | for(int i = 1; i<=14 ; i++ ){ 19 | vector temp(8); 20 | for(int j = 1; j < 7; j++){ 21 | temp[j]=!(cells[j - 1] ^ cells[j + 1]); 22 | } 23 | cells = temp; 24 | m[i] = temp; 25 | visited.insert(temp); 26 | } 27 | return m[N % 14 == 0? 14 : N % 14]; 28 | } 29 | }; 30 | main(){ 31 | vector v1 = {0,1,0,1,1,0,0,1}; 32 | Solution ob; 33 | print_vector(ob.prisonAfterNDays(v1, 7)); 34 | } 35 | -------------------------------------------------------------------------------- /Codeforces/1362B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | signed main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int a,b,c,e=0,i,j,l=0; 10 | cin>>a; 11 | mapn,m; 12 | vectorv,x; 13 | for(i=1;i<=a;i++) 14 | { 15 | cin>>b;m[b]++;v.push_back(b);l=max(l,b); 16 | } 17 | sort(v.begin(),v.end()); 18 | for(i=1;i<=l*2;i++) 19 | { 20 | x.clear(); 21 | n.clear(); 22 | for(j=0;jm[c]) 28 | break; 29 | } 30 | sort(x.begin(),x.end()); 31 | if(x==v){ 32 | cout<> verticalTraversal(TreeNode* root) { 4 | if (root == NULL) { 5 | return {}; 6 | } 7 | 8 | map, set> map; 9 | int maxValue = INT_MIN; 10 | int minValue = INT_MAX; 11 | dfs(map, root, 0, 0, maxValue, minValue); 12 | vector> result(maxValue - minValue + 1); 13 | for (const auto& pair : map) { 14 | int index = pair.first.first - minValue; 15 | result[index].insert(result[index].end(), pair.second.begin(), pair.second.end()); 16 | } 17 | return result; 18 | } 19 | 20 | void dfs(map, set>& map, TreeNode* root, int x, int y, int& maxValue, int& minValue) { 21 | if (root == NULL) { 22 | return; 23 | } 24 | map[ {x, y}].insert(root->val); 25 | maxValue = max(maxValue, x); 26 | minValue = min(minValue, x); 27 | dfs(map, root->left, x - 1, y + 1, maxValue, minValue); 28 | dfs(map, root->right, x + 1, y + 1, maxValue, minValue); 29 | } 30 | 31 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_13.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> insert(vector>& intervals, vector& newInterval) 4 | { 5 | vector> result; 6 | 7 | for (size_t i = 0; i < intervals.size(); i++) 8 | { 9 | if (intervals[i][1] < newInterval[0]) 10 | { 11 | result.push_back(intervals[i]); 12 | } 13 | else if (intervals[i][0] > newInterval[1]) 14 | { 15 | result.push_back(newInterval); 16 | newInterval = intervals[i]; 17 | } 18 | else if (intervals[i][1] >= newInterval[0] || intervals[i][0] <= newInterval[1]) 19 | { 20 | newInterval[0] = (min(intervals[i][0], newInterval[0])); 21 | newInterval[1] = (max(newInterval[1], intervals[i][1])); 22 | 23 | } 24 | } 25 | 26 | result.push_back(newInterval); 27 | return result; 28 | } 29 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector summaryRanges(vector& nums) { 4 | 5 | vector v; 6 | 7 | if (nums.size() == 0) 8 | { 9 | return v; 10 | } 11 | int start = nums[0]; 12 | int end = nums[0]; 13 | for (int i = 1; i < nums.size(); i++) 14 | { 15 | if (end + 1 == nums[i]) 16 | { 17 | end = nums[i]; 18 | } 19 | else 20 | { 21 | if (start == end) 22 | { 23 | string x = to_string(start); 24 | v.push_back(x); 25 | } 26 | else 27 | { 28 | string x = to_string(start); 29 | x += "->"; 30 | x += to_string(end); 31 | v.push_back(x); 32 | } 33 | start = nums[i]; 34 | end = nums[i]; 35 | } 36 | } 37 | 38 | if (start == end) 39 | { 40 | string x = to_string(start); 41 | v.push_back(x); 42 | } 43 | else 44 | { 45 | string x = to_string(start); 46 | x += "->"; 47 | x += to_string(end); 48 | v.push_back(x); 49 | } 50 | 51 | return v; 52 | } 53 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 5/CHEFSQRS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | using namespace std; 4 | #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 5 | #define loop(i,a,b) for(ll i=a;i>t; 14 | while(t--) 15 | { 16 | ll n; 17 | cin>>n; 18 | bool flag=false; 19 | ll res=-1; 20 | for(ll i = 1; i <= sqrt(n); i++) 21 | { 22 | if(n%i==0 && i!=n/i) 23 | { 24 | ll m=(n/i+i)/2; 25 | ll x=(n/i-i)/2; 26 | if(x*x+n == m*m) 27 | { 28 | if(!flag) res=x; 29 | else res=min(res, x); 30 | flag=true; 31 | } 32 | } 33 | } 34 | if(flag) res=res*res; 35 | cout< 3 | #include 4 | using namespace std; 5 | #define loopf(i,a,b) for(i=a;i a; 15 | int count = 0; 16 | int n,i; 17 | long long int k; 18 | scanf("%d",&n); 19 | cin >>k; 20 | loopw(n,2,0){ 21 | n/=2; 22 | count++; 23 | } 24 | if (count) 25 | a.push_back(count); 26 | for (long long i = 3; i <= sqrt(n); i += 2){ 27 | count = 0; 28 | loopw(n,i,0) { 29 | count++; 30 | n /=i; 31 | } 32 | if (count) 33 | a.push_back(count); 34 | } 35 | if (n > 2) 36 | a.push_back(1); 37 | long long int val=0; 38 | loopf(i,0,a.size()) 39 | { 40 | val+=a[i]; 41 | } 42 | if(val>=k) 43 | cout<<1<0){ 11 | int n=Integer.parseInt(br.readLine()); 12 | int a[]=new int[n]; 13 | String s = br.readLine(); 14 | String x[]=s.split(" "); 15 | for(int i=0;ia[i-1] && a[i]>a[i+1]) 22 | { 23 | count+=1; 24 | } 25 | } 26 | System.out.println("Case #"+c++ +": "+count); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /July LeetCode Challenge/Day_28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastInterval(vector& tasks, int n) { 4 | unordered_mapm; 5 | for (int i = 0 ; i < tasks.size(); i++) { 6 | m[tasks[i]]++; 7 | } 8 | priority_queue pq; 9 | for (auto i = m.begin(); i != m.end(); i++) { 10 | pq.push(i->second); 11 | } 12 | int ans = 0 ; 13 | while (!pq.empty()) { 14 | vector temp ; 15 | for (int i = 0 ; i <= n ; i++) { 16 | if (!pq.empty()) { 17 | temp.push_back(pq.top()); 18 | pq.pop(); 19 | } 20 | } 21 | for (int i = 0 ; i < temp.size(); i++) { 22 | temp[i]--; 23 | if (temp[i] > 0) { 24 | pq.push(temp[i]); 25 | } 26 | } 27 | 28 | ans += pq.empty() ? temp.size() : n + 1; 29 | } 30 | return ans; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /Codechef April Lunchtime 2020/SHUFFLE.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LTIME83B/problems/SHUFFLE 2 | #include 3 | using namespace std; 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n, k; 12 | cin >> n >> k; 13 | int a[n]; 14 | for (int j = 0; j < n; j++) { 15 | cin >> a[j]; 16 | } 17 | int x; 18 | for (int i = 0; i+k < n; i++) { 19 | if (a[i] > a[i + k]) { 20 | int p = a[i]; 21 | a[i] = a[i + k]; 22 | a[i + k] = p; 23 | x = i; 24 | while ((((x - k) >= 0)) && a[x - k] > a[x]) { 25 | p = a[x]; 26 | a[x] = a[x - k]; 27 | a[x - k] = p; 28 | x = x - k; 29 | } 30 | } 31 | } 32 | is_sorted(a,a+n)?cout << "yes\n":cout << "no\n"; 33 | } 34 | } -------------------------------------------------------------------------------- /Codechef March Long 2020/ADASHOP2.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/MARCH20B/problems/ADASHOP2 2 | #include 3 | using namespace std; 4 | 5 | void solve() { 6 | int r, c; 7 | cin >> r >> c; 8 | vector> moves; 9 | moves.push_back({(r+c)/2, (r+c)/2}); 10 | moves.push_back({1, 1}); 11 | for(int i=4; i<=8; i+=2) { 12 | moves.push_back({i/2, i/2}); 13 | moves.push_back({1, i-1}); 14 | moves.push_back({i-1, 1}); 15 | moves.push_back({i/2, i/2}); 16 | } 17 | for(int i=10; i<=16; i+=2) { 18 | moves.push_back({i/2, i/2}); 19 | moves.push_back({i-8, 8}); 20 | moves.push_back({8, i-8}); 21 | moves.push_back({i/2, i/2}); 22 | } 23 | cout << moves.size() << "\n"; 24 | for(array a : moves) 25 | cout << a[0] << " " << a[1] << "\n"; 26 | } 27 | 28 | int main() { 29 | ios::sync_with_stdio(0); 30 | cin.tie(0); 31 | cout.tie(0); 32 | int t; 33 | cin >> t; 34 | while(t--) 35 | solve(); 36 | } -------------------------------------------------------------------------------- /Codechef May Long 2020/COVID19.c: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/MAY20B/problems/COVID19 2 | #include 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | while(t-->0){ 8 | int n; 9 | scanf("%d",&n); 10 | int pos[n]; 11 | for(int i=0;iprs)?m1:prs; 22 | if(!m0){ 23 | m0=m1; 24 | } 25 | else{ 26 | m0=(m0prs)?m1:prs; 32 | if(!m0){ 33 | m0=m1; 34 | } 35 | else{ 36 | m0=(m0 3 | using namespace std; 4 | #define ll long long int 5 | ll m=1000000007; 6 | 7 | ll power(ll x, unsigned ll n) 8 | { 9 | ll result = 1; 10 | while (n) { 11 | if (n & 1) 12 | result = result * x % m; 13 | n = n / 2; 14 | x = x * x % m; 15 | } 16 | return result; 17 | } 18 | 19 | int main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | cout.tie(nullptr); 24 | ll t,n,a; 25 | cin>>t; 26 | while(t--) 27 | { 28 | cin>>n; 29 | cin>>a; 30 | a=a%m; 31 | ll s=0,p=a,k,b; 32 | for(ll i=1;i<=(2*n-1);i=i+2) 33 | { 34 | k=power(p,i); 35 | k=k%m; 36 | 37 | s=(s+k)%m; 38 | s=s%m; 39 | 40 | k=power(p,i+1); 41 | k=k%m; 42 | p=k; 43 | } 44 | cout< 3 | int main(){ 4 | int p; 5 | scanf("%d",&p); 6 | int z[p]; 7 | for(int i=0;ival < val) 19 | if (current->right) 20 | current = current->right; 21 | else 22 | current->right = new TreeNode(val), f = false; 23 | else if (current->left) 24 | current = current->left; 25 | else 26 | current->left = new TreeNode(val), f = false; 27 | return root; 28 | } 29 | }; -------------------------------------------------------------------------------- /Codechef April Long 2020/UNITGCD.java: -------------------------------------------------------------------------------- 1 | //Question Link : https://www.codechef.com/APRIL20B/problems/UNITGCD 2 | import java.util.*; 3 | class UnitGcd { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int t= scan.nextInt(); 7 | while(t-- >0){ 8 | int n= scan.nextInt(); 9 | StringBuffer str = new StringBuffer(); 10 | if(n==1){ 11 | str.append(1+"\n"); 12 | str.append(1+" "+1+"\n"); 13 | } 14 | else{ 15 | str.append(n/2+"\n"); 16 | if(n%2==0){ 17 | for(int i=1;i 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n,s; 11 | cin>>n>>s; 12 | int arr[n]; 13 | int brr[n]; 14 | for(int j=0;j>arr[j]; 17 | } 18 | for(int j=0;j>brr[j]; 21 | } 22 | int min1=100; 23 | int min2=100; 24 | for(int j=0;jres) 37 | { 38 | cout<<"no"< 0) { 9 | String[] s = br.readLine().split(" "); 10 | long x = Long.parseLong(s[0]); 11 | long y = Long.parseLong(s[1]); 12 | long l = Long.parseLong(s[2]); 13 | long r = Long.parseLong(s[3]); 14 | long result = 0; 15 | long val = x | y; 16 | if (Math.min(x, y) == 0) 17 | bw.write(0 + "\n"); 18 | else if (val <= r) 19 | bw.write(val + "\n"); 20 | else 21 | bw.write((int)(Math.pow(2, (int) (Math.log(Math.max(x, y)) +1)) - 1) + "\n"); 22 | bw.flush(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_27.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | 11 | public: 12 | 13 | ListNode *detectCycle(ListNode *head) { 14 | 15 | if (head == NULL or head->next == NULL) 16 | { 17 | return NULL; 18 | } 19 | 20 | ListNode* slow = head; 21 | ListNode* fast = head; 22 | int x = 1; 23 | while ((fast and fast->next and fast != slow) or x) 24 | { 25 | slow = slow->next; 26 | fast = fast->next->next; 27 | x = 0; 28 | } 29 | 30 | if (!fast or !fast->next) 31 | { 32 | return NULL; 33 | } 34 | 35 | slow = head; 36 | 37 | while (slow != fast) 38 | { 39 | slow = slow->next; 40 | fast = fast->next; 41 | } 42 | 43 | cout << slow->val; 44 | 45 | return slow; 46 | 47 | } 48 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_30.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNumberOfLIS(vector& nums) { 4 | int n = nums.size(); 5 | if (nums.size() <= 1) 6 | return nums.size(); 7 | 8 | vector dp_len(n, 1); 9 | vector dp_count(n, 1); 10 | 11 | for (int i = 1; i < n; i++) { 12 | for (int j = 0; j < i; j++) { 13 | if (nums[j] < nums[i]) { 14 | if (dp_len[i] <= dp_len[j]) { 15 | dp_len[i] = dp_len[j] + 1; 16 | dp_count[i] = dp_count[j]; 17 | } 18 | else if (dp_len[j] + 1 == dp_len[i]) 19 | dp_count[i] += dp_count[j]; 20 | } 21 | } 22 | } 23 | 24 | int max_length = *max_element(dp_len.begin(), dp_len.end()); 25 | int res = 0; 26 | for (int i = 0; i < n; i++) { 27 | if (dp_len[i] == max_length) 28 | res += dp_count[i]; 29 | } 30 | 31 | return res; 32 | } 33 | }; -------------------------------------------------------------------------------- /Codeforces/Tprimes2.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | #define LIMIT 1000000 5 | long long i, j; 6 | long long prime_flag[LIMIT]; 7 | void calculate_prime_flag(){ 8 | prime_flag[0] = prime_flag[1] = 1; 9 | for(i=2;i>total_numbers; 26 | for(i=0;i>n; 28 | if (n==4){ 29 | cout<<"YES"< & mm) { 16 | if (!root) { 17 | return; 18 | } 19 | 20 | else { 21 | mm[root->val]++; 22 | traverse(root->left, mm); 23 | traverse(root->right, mm); 24 | } 25 | 26 | } 27 | 28 | 29 | vector getAllElements(TreeNode* root1, TreeNode* root2) { 30 | vector ans; 31 | 32 | map mm; 33 | 34 | traverse(root1, mm); 35 | traverse(root2, mm); 36 | 37 | for (auto x : mm) { 38 | int cou = x.second; 39 | int ele = x.first; 40 | while (cou--) { 41 | ans.push_back(ele); 42 | } 43 | } 44 | 45 | return ans; 46 | } 47 | }; -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_21.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> rect; 4 | vector r_area; 5 | int total_area; 6 | Solution(vector> rects) { 7 | rect = rects; 8 | int total = 0; 9 | for (int i = 0; i < rects.size(); i++) { 10 | total += (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1); 11 | r_area.push_back(total); 12 | } 13 | total_area = total; 14 | } 15 | 16 | vector pick() { 17 | int random_area = rand() % total_area + 1; 18 | int i = 0; 19 | for (; i < r_area.size(); i++) { 20 | if (random_area <= r_area[i]) break; 21 | } 22 | int dis_x = rand() % (rect[i][2] - rect[i][0] + 1); 23 | int dis_y = rand() % (rect[i][3] - rect[i][1] + 1); 24 | return {rect[i][0] + dis_x, rect[i][1] + dis_y}; 25 | } 26 | }; 27 | 28 | /** 29 | * Your Solution object will be instantiated and called as such: 30 | * Solution* obj = new Solution(rects); 31 | * vector param_1 = obj->pick(); 32 | */ -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_21.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector asteroidCollision(vector& asteroids) { 4 | vector v; 5 | 6 | stack left, right; 7 | 8 | for (auto i : asteroids) { 9 | if (i <= 0 ) { 10 | if (!right.empty()) { 11 | 12 | while (!right.empty() && (-i) > right.top())right.pop(); 13 | 14 | if (!right.empty() && right.top() == (-i)) { 15 | right.pop(); 16 | continue; 17 | } 18 | 19 | } 20 | 21 | if (right.empty()) { 22 | left.push(-i); 23 | v.push_back(i); 24 | } 25 | } 26 | 27 | else right.push(i); 28 | 29 | 30 | } 31 | 32 | vectortemp; 33 | while (!right.empty()) { 34 | temp.push_back(right.top()); 35 | right.pop(); 36 | } 37 | v.insert(v.end(), temp.rbegin(), temp.rend()); 38 | return v; 39 | } 40 | }; -------------------------------------------------------------------------------- /Codeforces/645D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | int32_t main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | cout.tie(NULL); 8 | int n, x; 9 | cin >> n >> x; 10 | vector d(2 * n + 2); 11 | 12 | for (int i = 1; i <= n; i++) { 13 | cin >> d[i]; 14 | } 15 | for (int i = n + 1; i <= 2 * n + 1; i++) { 16 | d[i] = d[i - n]; 17 | } 18 | 19 | int temp = 0, val = 0; 20 | int ans = 0, idx = 0; 21 | for (int i = 1; i <= 2 * n; i++) { 22 | if (temp + d[i] <= x) { 23 | temp += d[i]; 24 | val += (d[i] * (d[i] + 1)) / 2; 25 | ans = max(ans, val); 26 | } else { 27 | int y = temp + d[i] - x; 28 | if (y > 0) 29 | ans = max(ans, val - (y * (y + 1)) / 2 + (d[i] * (d[i] + 1)) / 2); 30 | temp -= d[idx]; 31 | val -= (d[idx] * (d[idx] + 1)) / 2; 32 | idx++; 33 | i--; 34 | } 35 | } 36 | 37 | cout << ans << endl; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int count(unsigned int num) { 4 | int cnt = 0; 5 | while (num) { 6 | cnt += (num & 1); 7 | num >>= 1; 8 | } 9 | return cnt; 10 | } 11 | 12 | int largestOverlap(vector>& A, vector>& B) { 13 | int largest = 0; 14 | int n = A.size(); 15 | vector arrA(n); 16 | vector arrB(n); 17 | for (int i = 0; i < n; ++i) { 18 | for (int j = 0; j < n; ++j) { 19 | arrA[i] |= A[i][j] << (n - 1 - j); 20 | arrB[i] |= B[i][j] << (n - 1 - j); 21 | } 22 | } 23 | for (int s = 0; s < n; ++s) { 24 | for (int t = 0; t < n; ++t) { 25 | int cnt = 0; 26 | for (int i = s; i < n; ++i) { /* A slides down s, right t */ 27 | cnt += count((arrA[i - s] >> t) & arrB[i]); 28 | } 29 | largest = max(cnt, largest); 30 | cnt = 0; 31 | for (int i = 0; i < n - s; ++i) { /* A slides up s, left t */ 32 | cnt += count(arrB[i] & (arrA[i + s] << t)); 33 | } 34 | largest = max(cnt, largest); 35 | } 36 | } 37 | return largest; 38 | } 39 | }; -------------------------------------------------------------------------------- /November LeetCode Challenge/Day_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* insertionSortList(ListNode* head) { 14 | if (head == nullptr ) { 15 | return nullptr ; 16 | } 17 | vectornodes; 18 | nodes.push_back(head); 19 | ListNode*temp = head->next ; 20 | while (temp != nullptr ) { 21 | nodes.push_back(temp); 22 | for (int i = nodes.size() - 1 ; i > 0 ; i -- ) { 23 | if ( nodes[i]->val < nodes[i - 1]->val ) { 24 | swap(nodes[i - 1]->val , nodes[i]->val); 25 | } 26 | else { 27 | break; 28 | } 29 | } 30 | temp = temp->next; 31 | } 32 | return head ; 33 | } 34 | }; -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_20.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector neighbors; 7 | 8 | Node() { 9 | val = 0; 10 | neighbors = vector(); 11 | } 12 | 13 | Node(int _val) { 14 | val = _val; 15 | neighbors = vector(); 16 | } 17 | 18 | Node(int _val, vector _neighbors) { 19 | val = _val; 20 | neighbors = _neighbors; 21 | } 22 | }; 23 | */ 24 | class Solution { 25 | public: 26 | Node* rec(Node* node) { 27 | if (m_map.find(node->val) != m_map.end()) 28 | return m_map[node->val]; 29 | 30 | Node* new_node = new Node(node->val); 31 | m_map[node->val] = new_node; 32 | for (auto neighbor : node->neighbors) 33 | new_node->neighbors.push_back(rec(neighbor)); 34 | return new_node; 35 | } 36 | 37 | Node* cloneGraph(Node* node) { 38 | if (!node) 39 | return NULL; 40 | return rec(node); 41 | } 42 | 43 | private: 44 | unordered_map m_map; 45 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_10.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getHint(string secret, string guess) { 4 | 5 | string ret; 6 | int num = 0; 7 | 8 | //for bulls 9 | for (int i = 0; i < secret.size() && i < guess.size(); i++) 10 | { 11 | if (secret[i] == guess[i]) 12 | num++; 13 | } 14 | ret.append(to_string(num)); 15 | ret.push_back('A'); 16 | num = 0; 17 | 18 | //for cows 19 | vector sec(10, 0); 20 | for (int i = 0; i < secret.size(); i++) 21 | { 22 | if (i < guess.size() && (guess[i] != secret[i])) 23 | sec[secret[i] - '0']++; 24 | } 25 | 26 | for (int i = 0; i < guess.size(); i++) 27 | { 28 | if (sec[guess[i] - '0'] && guess[i] != secret[i]) 29 | { 30 | sec[guess[i] - '0']--; 31 | num++; 32 | } 33 | } 34 | 35 | ret.append(to_string(num)); 36 | ret.push_back('B'); 37 | 38 | return ret; 39 | 40 | } 41 | }; -------------------------------------------------------------------------------- /Google KickStart 2020/C/candies_tle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int solve() 4 | { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int n ,q; 9 | cin >> n >>q; 10 | vectorv(n); 11 | int sum =0; 12 | int qc=1; 13 | for(int i=0;i> v[i]; 16 | } 17 | while (qc<=q){ 18 | char c; 19 | int x,y; 20 | cin >> c >> x >>y; 21 | if(c=='Q') 22 | { 23 | int cp=1; 24 | for(int i=x-1;i> t; 44 | while(i<=t) { 45 | cout << "Case #" << i << ": "<>& grid, int count, int total) 5 | { 6 | if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size() || grid[x][y] == -1) 7 | { 8 | return; 9 | } 10 | if (grid[x][y] == 2 && total == count) 11 | { 12 | sol++; 13 | return; 14 | } 15 | if (grid[x][y] == 2 && total != count) 16 | return; 17 | if (total >= count) 18 | return; 19 | grid[x][y] = -1; 20 | dfs(x + 1, y, grid, count, total + 1); 21 | dfs(x - 1, y, grid, count, total + 1); 22 | dfs(x, y + 1, grid, count, total + 1); 23 | dfs(x, y - 1, grid, count, total + 1); 24 | grid[x][y] = 0; 25 | } 26 | int uniquePathsIII(vector>& grid) { 27 | int count = 0; 28 | int x, y; 29 | for (int i = 0; i < grid.size(); i++) 30 | { 31 | for (int j = 0; j < grid[0].size(); j++) 32 | { 33 | if (grid[i][j] == 1) 34 | { 35 | x = i; 36 | y = j; 37 | } 38 | if (grid[i][j] == 0) 39 | count++; 40 | } 41 | } 42 | dfs(x, y, grid, count + 1, 0); 43 | return sol; 44 | } 45 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Arpit Mishra 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 2/ZCO12001.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA02/submit/ZCO12001 2 | 3 | #include 4 | typedef long long ll; 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | cout.tie(NULL); 12 | int t=1; 13 | while(t--) 14 | { 15 | ll input,n,depth=0,d_pos,max=0,max_pos,count=0,pos,open=0 ; 16 | cin>>n; 17 | for(ll i=0;i>input; 20 | if(open==0) 21 | { 22 | pos=i+1; 23 | count=0; 24 | } 25 | 26 | count++; 27 | if(input==1) 28 | open++; 29 | if(input==2) 30 | open--; 31 | if(open>depth) 32 | { 33 | depth=open; 34 | d_pos=i+1; 35 | } 36 | if(max 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int T; 7 | scanf("%d",&T); 8 | while(T--){ 9 | int n, m, k, **A, *hash; 10 | scanf("%d%d%d",&n,&m,&k); 11 | A = new int*[n]; 12 | for (int i=0;imax) { max=hash[j]; ind_max=j;} 28 | } 29 | rep[i]=ind_max; 30 | delete[] hash; 31 | } 32 | for(int i = n-1;i>=0;i--) 33 | printf("%d ",rep[i]); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Codeforces/1365A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(nullptr); 6 | cout.tie(nullptr); 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n ,m; 11 | cin >> n >> m; 12 | vectorrow(m,false); 13 | vectorcol(n,false); 14 | for(int i=0;i> c; 20 | if(c=='1') 21 | { 22 | row[i]=true; 23 | col[j]=true; 24 | } 25 | } 26 | } 27 | int cc=0; 28 | for(int i=0;i>& board, int i, int j, string& word, int idx) { 3 | if (idx == word.length() - 1) return true; 4 | board[i][j] -= 65; 5 | if (i > 0 && board[i - 1][j] == word[idx + 1] && search(board, i - 1, j, word, idx + 1)) return true; 6 | if (j > 0 && board[i][j - 1] == word[idx + 1] && search(board, i, j - 1, word, idx + 1)) return true; 7 | if (i < board.size() - 1 && board[i + 1][j] == word[idx + 1] && search(board, i + 1, j, word, idx + 1)) return true; 8 | if (j < board[0].size() - 1 && board[i][j + 1] == word[idx + 1] && search(board, i, j + 1, word, idx + 1)) return true; 9 | board[i][j] += 65; 10 | return false; 11 | } 12 | 13 | public: 14 | bool exist(vector>& board, string word) { 15 | int r = board.size(); 16 | if (r == 0) return false; 17 | int c = board[0].size(); 18 | for (int i = 0; i < r; ++i) 19 | for (int j = 0; j < c; ++j) 20 | if (board[i][j] == word[0] && search(board, i, j, word, 0)) return true; 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /Codeforces/644E.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while(t--){ 11 | int n; 12 | cin >> n; 13 | char a[n][n]; 14 | for(int i=0;i> a[i][j]; 18 | } 19 | bool flag = true; 20 | for(int i=0;i> &ans,vector v,vector &candidates,int target,int idx) 6 | { 7 | // base cases 8 | 9 | if(target==0) 10 | { 11 | ans.push_back(v); 12 | return; 13 | } 14 | 15 | if(target<0) 16 | { 17 | return; 18 | } 19 | 20 | // body of recursion 21 | 22 | for(int i=idx;i> combinationSum(vector& candidates, int target) { 43 | 44 | 45 | vector> ans; 46 | 47 | vector v; 48 | 49 | sort(candidates.begin(),candidates.end()); 50 | 51 | helper(ans,v,candidates,target,0); 52 | 53 | return ans; 54 | 55 | 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /Codechef May Long 2020/CORUS.java: -------------------------------------------------------------------------------- 1 | https://www.codechef.com/MAY20B/problems/CORUS 2 | import java.lang.*; 3 | import java.io.*; 4 | class CORUS 5 | { 6 | public static void main (String[] args) throws IOException { 7 | BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 8 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 9 | int t=Integer.parseInt(br.readLine()); 10 | while(t-->0){ 11 | String[] s =br.readLine().split(" "); 12 | int q=Integer.parseInt(s[1]); 13 | String str=br.readLine(); 14 | int[] freq =new int[26]; 15 | for(int i=0;i0){ 19 | count=0; 20 | int x=Integer.parseInt(br.readLine()); 21 | for(int i =0;ix) 24 | count+=(freq[i]-x); 25 | } 26 | bw.write(count+"\n"); 27 | bw.flush(); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Google KickStart 2020/C/countdown.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /*bool check (const vector&a,int k) { 4 | for (int i : a){ 5 | if(k!=i) 6 | return false; 7 | k--; 8 | } 9 | return true; 10 | }*/ 11 | int solve(){ 12 | ios::sync_with_stdio(false); 13 | cin.tie(nullptr); 14 | cout.tie(nullptr); 15 | int n ,k; 16 | cin >> n >> k; 17 | vectorv(n); 18 | for(int i=0;i> v[i]; 20 | int c=0; 21 | vectorfin(k); 22 | for(int i=0;itemp(k); 27 | if(v[i]==k) { 28 | for (int j = 0; j < k; j++) { 29 | temp[j] = v[i + j]; 30 | } 31 | if (fin == temp) 32 | c++; 33 | } 34 | } 35 | return c; 36 | } 37 | int main() { 38 | ios::sync_with_stdio(false); 39 | cin.tie(nullptr); 40 | cout.tie(nullptr); 41 | int t, i=1; 42 | cin >> t; 43 | while(i<=t) { 44 | cout << "Case #" << i << ": "< vec; 3 | int n; 4 | vector ans; 5 | int top; 6 | public: 7 | CombinationIterator(string characters, int combinationLength) { 8 | for (auto ele : characters) vec.emplace_back(ele); 9 | n = combinationLength; 10 | top = -1; 11 | getCombinations("", n); 12 | } 13 | 14 | string next() { 15 | return ans[++top]; 16 | } 17 | 18 | bool hasNext() { 19 | return top != ans.size() - 1; 20 | } 21 | 22 | void getCombinations(string str, int &n) { 23 | if (str.size() > 1 && str[str.size() - 2] >= str[str.size() - 1]) return; 24 | if (str.size() == n) ans.push_back(str); 25 | else if (str.size() < n) { 26 | for (int i{0}; i < vec.size(); i++) { 27 | getCombinations(str + vec[i], n); 28 | } 29 | } 30 | } 31 | }; 32 | /** 33 | * Your CombinationIterator object will be instantiated and called as such: 34 | * CombinationIterator* obj = new CombinationIterator(characters, combinationLength); 35 | * string param_1 = obj->next(); 36 | * bool param_2 = obj->hasNext(); 37 | */ -------------------------------------------------------------------------------- /Code Gladiators/powerpuff.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class CandidateCode { 4 | public static void main(String args[] ) throws Exception { 5 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 | long min=0; 7 | String ing = br.readLine(); 8 | int ingnumb=Integer.parseInt(ing);//no of ingredients 9 | String lines = br.readLine(); 10 | String[] strs = lines.trim().split("\\s+"); 11 | long a[]=new long[strs.length]; 12 | for (int i = 0; i < strs.length; i++)//quantity of each to create a powerpuff girl 13 | { 14 | a[i] = Long.parseLong(strs[i]); 15 | //System.out.println(a[i]); 16 | } 17 | String quant = br.readLine(); 18 | String[] strs1 = quant.trim().split("\\s+"); 19 | long b[]=new long[strs.length]; 20 | for (int i = 0; i < strs1.length; i++)//quanitity of each available 21 | { 22 | b[i] = Long.parseLong(strs1[i]); 23 | //System.out.println(b[i]); 24 | } 25 | double tempdiv[]=new double[ingnumb]; 26 | for (int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); 7 | cout.tie(0); 8 | ll t; 9 | cin >> t; 10 | while (t--) { 11 | ll n; 12 | cin >> n; 13 | ll a[n + 1]; 14 | ll odd_c = 0, even_c = 0; 15 | for (ll i = 1; i <= n; i++) { 16 | cin >> a[i]; 17 | 18 | if (a[i] % 2 == 0) 19 | even_c++; 20 | else 21 | odd_c++; 22 | } 23 | 24 | if (even_c % 2 == 0 || odd_c % 2 == 0) { 25 | cout << "YES" << "\n"; 26 | } else { 27 | sort(a + 1, a + n + 1); 28 | ll flag = 0; 29 | for (ll i = 1; i < n; i++) { 30 | if (abs(a[i] - a[i + 1]) == 1 && ((a[i] % 2 == 0 && a[i + 1] % 2 != 0) || (a[i] % 2 != 0 && a[i + 1] % 2 == 0) )) { 31 | flag = 1; 32 | } 33 | } 34 | if (flag == 1) { 35 | cout << "YES" << "\n"; 36 | } else { 37 | cout << "NO" << "\n"; 38 | } 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 3/EXUNC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int mod=1e9 + 7; 4 | #define int long long 5 | const int inf=1e18; 6 | const int N=1e6 + 100; 7 | int n, m, arr[N]; 8 | signed main() 9 | { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(NULL); 12 | cout.tie(NULL); 13 | cin>>n>>m; 14 | arr[0]=inf; 15 | set start; 16 | for(int i=1;i<=n;i++) { 17 | cin>>arr[i]; 18 | if(arr[i]%arr[i-1]!=0) 19 | start.insert(i); 20 | } 21 | arr[n+1]=mod; 22 | while(m--) 23 | { 24 | int t;cin>>t; 25 | if(t==1) 26 | { 27 | int ind, val; 28 | cin>>ind>>val; 29 | arr[ind]=val; 30 | if(arr[ind]%arr[ind-1]!=0) 31 | start.insert(ind); 32 | else 33 | start.erase(ind); 34 | if(arr[ind+1]%arr[ind]!=0) 35 | start.insert(ind+1); 36 | else 37 | start.erase(ind+1); 38 | } 39 | else{ 40 | int ind;cin>>ind; 41 | set::iterator it=start.upper_bound(ind);it--; 42 | cout<<(*it)< 3 | using namespace std; 4 | void solve(const int * arr,int n) 5 | { 6 | unordered_map mp; 7 | set s; 8 | for(int i=0;i>t; 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int arr[n]; 50 | for(int i=0;i>arr[i]; 52 | solve(arr,n); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Codechef May CookOff 2020/CHEFSHIP_hashing.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/COOK118B/problems/CHEFSHIP 2 | #include 3 | #define ll long long 4 | #define MAX 1000000 5 | #define MOD 100000007 6 | using namespace std; 7 | ll hashF[MAX], power[MAX]; 8 | ll p=31; 9 | void calculate(string s, int n){ 10 | power[0]=1; 11 | for(int i=n-1;i>=0;i--){ 12 | hashF[i]=((hashF[i+1]*p)%MOD+(s[i]-'a'+1))%MOD; 13 | power[n-i]=(power[n-i-1]*p)%MOD; 14 | } 15 | power[n]=(power[n-1]*p)%MOD; 16 | } 17 | ll hs(int l,int r){ 18 | return (MOD-(hashF[r+1]*power[r-l+1])%MOD+hashF[l])%MOD; 19 | } 20 | int main(){ 21 | ios::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | cout.tie(nullptr); 24 | int t = 1; 25 | cin >> t; 26 | while(t--){ 27 | string s; 28 | cin >> s; 29 | int n =s.length(); 30 | int count =0; 31 | calculate(s,n); 32 | for(int i=2;i 2 | using namespace std; 3 | #define ll long long int 4 | void spiralFill(int m, int n) 5 | { 6 | int cp=m; 7 | int a[m][n]; 8 | int val = 1; 9 | int k = 0, l = 0; 10 | while (k < m && l < n) 11 | { 12 | for (int i = l; i < n; ++i) 13 | a[k][i] = val++; 14 | k++; 15 | for (int i = k; i < m; ++i) 16 | a[i][n-1] = val++; 17 | n--; 18 | if (k < m) 19 | { 20 | for (int i = n-1; i >= l; --i) 21 | a[m-1][i] = val++; 22 | m--; 23 | } 24 | if (l < n) 25 | { 26 | for (int i = m-1; i >= k; --i) 27 | a[i][l] = val++; 28 | l++; 29 | } 30 | } 31 | for(int i=0;i> t; 45 | while (t--) { 46 | int n; 47 | cin >> n; 48 | spiralFill(n,n); 49 | } 50 | } -------------------------------------------------------------------------------- /Codechef June Long 2020/GUESSG.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #define int long long int 4 | using namespace std; 5 | int n,ctr,low,high; 6 | bool isEven(int x){ 7 | return (x%2==0); 8 | } 9 | void odd() 10 | { 11 | while (true){ 12 | int mid = (int)(high + low) / 2; 13 | cout << mid << "\n"; 14 | char c; 15 | cin >> c; 16 | if (c == 'E') 17 | { 18 | break; 19 | } 20 | if (low == high) 21 | { 22 | low = 1; 23 | high = n; 24 | ctr++; 25 | break; 26 | } 27 | if (!isEven(ctr)) 28 | c=='G'?low = mid + 1:high = mid - 1; 29 | ctr++; 30 | } 31 | } 32 | void even(){ 33 | while (true) 34 | { 35 | int mid = (int)(high + low) / 2; 36 | cout << mid << "\n"; 37 | char c; 38 | cin >> c; 39 | if (c == 'E') 40 | { 41 | break; 42 | } 43 | if (isEven(ctr)) 44 | { 45 | c=='G'?low = mid + 1:high = mid - 1; 46 | } 47 | ctr++; 48 | } 49 | } 50 | signed main() 51 | { 52 | cin >> n; 53 | ctr = 1; 54 | low = 1, high = n; 55 | odd(); 56 | even(); 57 | } -------------------------------------------------------------------------------- /Codechef May CookOff 2020/CHEFSHIP_final.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/COOK118B/problems/CHEFSHIP 2 | #include 3 | using namespace std; 4 | vector < int > Z_function(string &t){ 5 | int L = 0, R = 0; 6 | vector < int > z(t.size(), 0); 7 | for(int i = 1; i < (int)t.size(); ++i){ 8 | if(i <= R)z[i] = min(R - i + 1, z[i - L]); 9 | while(i + z[i] < (int)t.size() && t[z[i]] == t[i + z[i]])z[i]++; 10 | if(i + z[i] - 1 > R){ 11 | R = i + z[i] - 1; 12 | L = i; 13 | } 14 | } 15 | reverse(t.begin(), t.end()); 16 | return z; 17 | } 18 | void solve(){ 19 | string t; 20 | cin >> t; 21 | vector < int > z1 = Z_function(t); 22 | vector < int > z2 = Z_function(t); 23 | int ans = 0; 24 | for(int i = 1; 2 * (i + 1) <= t.size(); ++i){ 25 | int length1 = i, length2 = ((int)t.size() - 2 * i) / 2; 26 | if(z1[length1] >= length1 && z2[length2] >= length2){ 27 | ans++; 28 | } 29 | } 30 | cout << ans << '\n'; 31 | } 32 | int main(){ 33 | ios::sync_with_stdio(false); 34 | cin.tie(nullptr); 35 | cout.tie(nullptr); 36 | int t = 1; 37 | cin >> t; 38 | while(t--){ 39 | solve(); 40 | } 41 | } -------------------------------------------------------------------------------- /Codeforces/1182A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | if (n % 2 == 0) 35 | printf("%d", 1 << (n / 2)); 36 | else 37 | printf("0"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/1339A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | w(t) { 33 | int n; 34 | cin >> n; 35 | cout << n << '\n'; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /October LeetCode Challenge/Day_22.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void helper(TreeNode* root, int count, int &finalcount) 15 | { 16 | if (root == NULL) 17 | { 18 | finalcount = min(finalcount, count); 19 | return; 20 | } 21 | 22 | if (root->left == NULL and root->right == NULL) 23 | { 24 | finalcount = min(finalcount, count + 1); 25 | return; 26 | } 27 | 28 | if (root->left) 29 | helper(root->left, count + 1, finalcount); 30 | 31 | if (root->right) 32 | helper(root->right, count + 1, finalcount); 33 | 34 | return; 35 | } 36 | 37 | int minDepth(TreeNode* root) { 38 | 39 | int finalcount = INT_MAX; 40 | 41 | helper(root, 0, finalcount); 42 | 43 | return finalcount; 44 | 45 | } 46 | }; -------------------------------------------------------------------------------- /Codeforces/492A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | cout << n / 100 + n % 100 / 20 + n % 20 / 10 + n % 10 / 5 + n % 5; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 1/MULTHREE.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class MULTHREE 3 | { 4 | public static void main (String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | int t=sc.nextInt(); 7 | for(;t>0;t--) 8 | { 9 | long k=sc.nextLong(); 10 | int d0=sc.nextInt(); 11 | int d1=sc.nextInt(); 12 | int s=(d0+d1); 13 | int three=s%10; 14 | if(k==2) 15 | { 16 | if(s%3==0) 17 | System.out.println("YES"); 18 | else 19 | System.out.println("NO"); 20 | } 21 | else 22 | { 23 | long freq=(k-3)/4; 24 | int rem=(int)((k-3)-freq*4); 25 | int cycle=(2*three)%10+(4*three)%10+(8*three)%10+(6*three)%10; 26 | int remsum=0,temp=2; 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | w(t) { 33 | int n, m; 34 | cin >> n >> m; 35 | cout << (min(2LL, n - 1)*m) << '\n'; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /General Programming Problems/DP/492A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | cout << n / 100 + n % 100 / 20 + n % 20 / 10 + n % 10 / 5 + n % 5; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /August LeetCode Challenge/Day_9.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int orangesRotting(vector>& grid) { 4 | int count = 0, iter = -1; 5 | vector>grid2 = grid; 6 | do 7 | { 8 | 9 | iter++; 10 | count = 0; 11 | for (int i = 0; i < grid.size(); i++) 12 | { 13 | for (int j = 0; j < grid[0].size(); j++) 14 | { 15 | if (grid[i][j] == 2) 16 | { 17 | if (i > 0 && grid[i - 1][j] == 1) 18 | { 19 | grid2[i - 1][j] = 2; 20 | count++; 21 | } 22 | if (i < grid.size() - 1 && grid[i + 1][j] == 1) 23 | { 24 | grid2[i + 1][j] = 2; 25 | count++; 26 | } 27 | 28 | if (j > 0 && grid[i][j - 1] == 1) 29 | { 30 | grid2[i][j - 1] = 2; 31 | count++; 32 | } 33 | 34 | if (j < grid[0].size() - 1 && grid[i][j + 1] == 1) 35 | { 36 | grid2[i][j + 1] = 2; 37 | count++; 38 | } 39 | 40 | } 41 | } 42 | } 43 | grid = grid2; 44 | 45 | 46 | 47 | } while (count != 0); 48 | 49 | for (int i = 0; i < grid.size(); i++) 50 | { 51 | for (int j = 0; j < grid[0].size(); j++) 52 | { 53 | if (grid[i][j] == 1) 54 | { 55 | iter = -1; 56 | break; 57 | 58 | } 59 | } 60 | } 61 | 62 | return iter; 63 | } 64 | }; -------------------------------------------------------------------------------- /General Programming Problems/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define maxNum 101 6 | using namespace std; 7 | 8 | void BFS(vector > ways, int strt) { 9 | 10 | queue holder; 11 | int line[maxNum]; 12 | memset(line, -1, sizeof line); 13 | 14 | line[strt] = 0; 15 | holder.push(strt); 16 | 17 | while (!holder.empty()) { 18 | 19 | int u = holder.front(); 20 | holder.pop(); 21 | 22 | for (int i = 0; i < ways[u].size() ; ++i) { 23 | int v = ways[u][i]; 24 | if (line[v] == -1) { 25 | cout << v << " "; 26 | line[v] = line[u] + 1; 27 | holder.push(v); 28 | } 29 | } 30 | } 31 | } 32 | 33 | int main() { 34 | 35 | vector > ways; 36 | int line[maxNum]; 37 | int n, m, a, b, strt; 38 | queue holder; 39 | 40 | cin >> n >> m; 41 | 42 | ways.resize(n); 43 | 44 | for (int i = 0 ; i < m ; ++i) { 45 | cin >> a >> b; 46 | ways[a].push_back(b); 47 | ways[b].push_back(a); 48 | } 49 | 50 | cin >> strt; 51 | 52 | memset(line, -1, sizeof(line)); 53 | 54 | BFS(line, holder, ways, strt); 55 | 56 | // We will print how far is the nodes to the start point 57 | // for (i = 0; i < n ; ++i) 58 | // cout << line[i] << " "; 59 | // cout << endl; 60 | 61 | 62 | 63 | } -------------------------------------------------------------------------------- /CSES/distinctNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | sets; 35 | for (int i = 0; i < n; i++) { 36 | int x; 37 | cin >> x; 38 | s.insert(x); 39 | } 40 | cout << s.size(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 1/CONFLIP.java: -------------------------------------------------------------------------------- 1 | import java.lang.*; 2 | import java.io.*; 3 | class CONFLIP 4 | { 5 | public static void main (String[] args) throws java.lang.Exception 6 | { 7 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 8 | int T = Integer.parseInt(br.readLine()); 9 | while(T > 0){ 10 | int G = Integer.parseInt(br.readLine()); 11 | while(G > 0){ 12 | String[] str = br.readLine().split(" "); 13 | int N = Integer.parseInt(str[1]); 14 | if(N%2==0){ 15 | System.out.println(N/2); 16 | }else{ 17 | if(str[0].equals("1") && str[2].equals("1")){ 18 | System.out.println(N/2); 19 | }else if(str[0].equals("1") && str[2].equals("2")){ 20 | System.out.println(N/2 + 1); 21 | }else if(str[0].equals("2") && str[2].equals("1")){ 22 | System.out.println(N/2 + 1); 23 | }else if(str[0].equals("2") && str[2].equals("2")){ 24 | System.out.println(N/2); 25 | } 26 | } 27 | G--; 28 | } 29 | T--; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /CodeVibe 1.0/left_rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n, d; 33 | cin >> n >> d; 34 | vi a(n); 35 | for (int i = 0; i < n; i++) 36 | cin >> a[(i + n - d) % n]; 37 | for (int p : a) 38 | cout << p << " "; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Codeforces/644H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using ld = long double; 4 | using ll = long long; 5 | void solve() { 6 | ll m, n; 7 | cin >> n >> m; 8 | vector v(n); 9 | for (int i = 0; i < n; i++) { 10 | string s; 11 | cin >> s; 12 | for (char c : s) { 13 | v[i] *= 2; 14 | v[i] += c - '0'; 15 | } 16 | } 17 | ll need = ((1ll << m) - n - 1) / 2 + 1; 18 | ll cur = (1ll << (m - 1)) - 1; 19 | while (true) { 20 | ll left = cur + 1; 21 | bool flag = false; 22 | for (int i = 0; i < n; i++) { 23 | flag |= v[i] == cur; 24 | if (v[i] <= cur) { 25 | left--; 26 | } 27 | } 28 | if (left == need && !flag) { 29 | string s; 30 | for (int i = 0; i < m; i++) { 31 | s += (char)(cur % 2 + '0'); 32 | cur /= 2; 33 | } 34 | reverse(s.begin(), s.end()); 35 | cout << s << endl; 36 | return; 37 | } else if (left < need) { 38 | cur++; 39 | } else { 40 | cur--; 41 | } 42 | } 43 | } 44 | 45 | int main() { 46 | int t; 47 | cin >> t; 48 | while (t--) { 49 | solve(); 50 | } 51 | } -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_27.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | unordered_map>> graph; 4 | 5 | pair search(string &a, string &b, unordered_set &vis, double val) { 6 | if (vis.count(a) == 0) { 7 | vis.insert(a); 8 | for (auto p : graph[a]) { 9 | double temp = val * p.second; 10 | if (p.first == b) { return make_pair(true, temp); } 11 | auto result = search(p.first, b, vis, temp); 12 | if (result.first) { return result; } 13 | } 14 | } 15 | return make_pair(false, -1.0); 16 | } 17 | 18 | public: 19 | vector calcEquation(vector> &eq, vector& val, vector> q) { 20 | vector ans; 21 | for (int i = 0; i < eq.size(); ++i) { 22 | graph[eq[i][0]].push_back(make_pair(eq[i][1], val[i])); 23 | graph[eq[i][1]].push_back(make_pair(eq[i][0], 1.0 / val[i])); 24 | } 25 | for (auto p : q) { 26 | unordered_set vis; 27 | if (p[0] == p[1] && graph.count(p[0])) ans.push_back(1.0); 28 | else ans.push_back(search(p[0], p[1], vis, 1.0).second); 29 | } 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 2/NOTALLFL.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/LRNDSA02/problems/NOTALLFL 2 | #include 3 | #include 4 | using namespace std; 5 | #define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); 6 | #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n" 7 | #define MAX 1000000 8 | #define mod 1000000007 9 | #define ll long long int 10 | #define ld long double 11 | #define ull unsigned long long 12 | #define li long int 13 | #define str string 14 | #define fr(i,n) for(ll i = 0; i=0; i--) 17 | 18 | int max (int a, int b){ 19 | if (a>b) 20 | return a; 21 | return b; 22 | } 23 | int main() { 24 | 25 | fastio; 26 | int t; 27 | cin>>t; 28 | 29 | while (t--){ 30 | int n, k, flag = 0, ans = 0, count = 0; 31 | cin>>n>>k; 32 | ll A[n+1], C[k]; 33 | ll B[k+1]; 34 | fr (i, n){ 35 | cin>>A[i]; 36 | } 37 | fr (i, k+1) 38 | B[i]=0; 39 | fr (i, n){ 40 | count = i-B[A[i]]; 41 | B[A[i]]=i+1; 42 | ans = max(ans, count); 43 | } 44 | frj (i, 1, k+1){ 45 | ans = max(ans, n-B[i]); 46 | } 47 | cout<0){ 13 | int N = sc.nextInt(); 14 | int[] arr1 = new int[N]; 15 | for(int i=0 ; i=0 && j>=0) { 26 | if(arr1[i]>arr2[j]) { 27 | answer++; 28 | i--; 29 | j--; 30 | }else { 31 | j--; 32 | } 33 | } 34 | log.write(answer +"\n"); 35 | log.flush(); 36 | 37 | T--; 38 | } 39 | sc.close(); 40 | 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /CodeChef DSA Learning/Week 2/ZCO12002.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/LRNDSA02/problems/ZCO12002 2 | #define MAX 1000002 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | cout.tie(NULL); 11 | int n, x, y, t1, t2, duration=INT_MAX; 12 | cin>>n>>x>>y; 13 | int in[MAX], out[MAX]; 14 | vector> contest; 15 | memset(in, -1, sizeof(int)*MAX); 16 | memset(out, -1, sizeof(int)*MAX); 17 | for(int i=0; i>t1>>t2; 19 | contest.push_back(make_pair(t1, t2)); 20 | } 21 | for(int i=0; i>t1; 23 | in[t1]=t1; 24 | } 25 | for(int i=0; i>t1; 27 | out[t1]=t1; 28 | } 29 | t1=-1; 30 | for(int i=1; i0; i--) { 36 | if(out[i]!=-1) t1=out[i]; 37 | else out[i]=t1; 38 | } 39 | for(auto i=contest.begin(); i!=contest.end(); i++) { 40 | if(in[i->first]!=-1 && out[i->second]!=-1 && ( out[i->second] - in[i->first]+1 < duration)) duration=out[i->second] - in[i->first]+1; 41 | } 42 | cout< 2 | using namespace std; 3 | #define ll long long 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); 7 | cout.tie(0); 8 | ll t; 9 | cin >> t; 10 | while (t--) { 11 | ll n, k; 12 | cin >> n >> k; 13 | if (n <= k) { 14 | cout << 1 << "\n"; 15 | } else if (k == 1) { 16 | cout << n << "\n"; 17 | } else { 18 | vector fact; 19 | for (ll i = 1; i <= sqrt(n); i++) { 20 | if (n % i == 0) { 21 | if (n / i == i && i <= k) 22 | fact.push_back(i); 23 | else { 24 | if (i <= k) 25 | fact.push_back(i); 26 | if (n / i <= k) 27 | fact.push_back(n / i); 28 | } 29 | } 30 | } 31 | ll ans = 2e9; 32 | for (ll i = 0; i < fact.size(); i++) { 33 | if (n % fact[i] == 0) { 34 | if (ans > (n / fact[i])) { 35 | ans = n / fact[i]; 36 | } 37 | } 38 | } 39 | cout << ans << "\n"; 40 | } 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Codeforces/1359B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll int 3 | using namespace std; 4 | char a[100][1000]; 5 | int main() 6 | { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | ll t; 11 | cin >> t; 12 | while(t--){ 13 | ll n,m,x,y; 14 | cin >> n >> m >>x >>y; 15 | for(int i=0;i> a[i][j]; 18 | } 19 | ll ans=0; 20 | for(int i=0;i=0) 40 | sum+=single*x; 41 | if(two!=0) 42 | ans+=min(c*x,sum); 43 | else 44 | ans+=(c*x); 45 | } 46 | cout < queue = new LinkedList<>(); 22 | int max = 1, leftmost = 1, rightmost = 1; 23 | root.val = 1; 24 | queue.offer(root); 25 | while (!queue.isEmpty()) { 26 | int size = queue.size(); 27 | for (int i = 0; i < size; i++) { 28 | TreeNode cur = queue.poll(); 29 | if (i == 0) { 30 | leftmost = cur.val; 31 | } 32 | if (i == size - 1) { 33 | rightmost = cur.val; 34 | } 35 | if (cur.left != null) { 36 | cur.left.val = cur.val * 2; 37 | queue.offer(cur.left); 38 | } 39 | if (cur.right != null) { 40 | cur.right.val = cur.val * 2 + 1; 41 | queue.offer(cur.right); 42 | } 43 | } 44 | max = Math.max(max, rightmost - leftmost + 1); 45 | 46 | } 47 | return max; 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /Recode/RECNDNOS.cpp: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/RC122020/problems/RECNDNOS 2 | #include 3 | using namespace std; 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | cout.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n; 12 | cin>>n; 13 | int arr[n],max=0; 14 | int p,k,l; 15 | for(int j=0;j>arr[j]; 18 | } 19 | for(int j=0;jmax) 22 | { 23 | max=arr[j]; 24 | } 25 | } 26 | int freq[max+1]; 27 | for(int j=0;jk) 44 | { 45 | k=freq[j]; 46 | l=j; 47 | } 48 | } 49 | cout< 3 | #include 4 | #include 5 | using namespace std; 6 | #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 7 | int counti(auto a[], int len) 8 | { 9 | int cnt=0; 10 | for(int i=0;i>n>>m>>k; 22 | int fencecount = k*4; 23 | int r, c; 24 | int temp = k; 25 | pair p, rp; 26 | pair arr[k], rarr[k]; 27 | while(temp--) 28 | { 29 | cin>>r>>c; 30 | p.first = r; 31 | p.second = c; 32 | rp.first = c; 33 | rp.second = r; 34 | arr[temp] = p; 35 | rarr[temp] = rp; 36 | } 37 | sort(arr,arr+k); 38 | sort(rarr, rarr + k); 39 | int rcount, dcount; 40 | rcount = counti(arr,k); 41 | dcount = counti(rarr,k); 42 | fencecount -= (rcount+dcount); 43 | cout<>t; 50 | while(t--) 51 | { 52 | solve(); 53 | } 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Codechef August Long 2020/CHEFWARS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | w(t) { 33 | int h, p; 34 | cin >> h >> p; 35 | while (h > 0 && p > 0) 36 | { 37 | h -= p; 38 | p /= 2; 39 | } 40 | cout << (p >= 0 && h <= 0) << '\n'; 41 | 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /CSES/weirdAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | while (n!=1) 35 | { 36 | cout << n << ' '; 37 | if(n%2) 38 | n=n*3+1; 39 | else 40 | n/=2; 41 | } 42 | cout <<1; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Codeforces/644G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); 7 | cout.tie(0); 8 | ll t; 9 | cin >> t; 10 | while (t--) { 11 | ll n, m, a, b; 12 | cin >> n >> m >> a >> b; 13 | if (n * a != m * b) { 14 | cout << "NO" << "\n"; 15 | } else { 16 | cout << "YES" << "\n"; 17 | ll final_array[100][100] = {0}; 18 | ll col_sum[100] = {0}; 19 | for (ll i = 1; i <= n; i++) { 20 | vector> a_columns; 21 | for (ll j = 1; j <= m; j++) { 22 | if (col_sum[j] < b) 23 | a_columns.push_back(make_pair(col_sum[j], j)); 24 | } 25 | sort(a_columns.begin(), a_columns.end()); 26 | for (ll k = 0; k < a; k++) { 27 | col_sum[a_columns[k].second]++; 28 | final_array[i][a_columns[k].second] = 1; 29 | } 30 | } 31 | for (ll i = 1; i <= n; i++) { 32 | for (ll j = 1; j <= m; j++) { 33 | cout << final_array[i][j]; 34 | } 35 | cout << "\n"; 36 | } 37 | } 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Google KickStart 2020/B/roboPathfinal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t,p=1; 7 | cin>>t; 8 | long long int mod=1e9; 9 | while(t--) 10 | { 11 | string s; 12 | cin>>s; 13 | long long int x=0,y=0,i; 14 | stackss; 15 | long long int temp=1LL; 16 | ss.push(temp); 17 | for(i=0;i='0'&&s[i]<='9') 20 | { 21 | temp=((long long int)(s[i]-'0')*temp)%mod; 22 | ss.push(temp); 23 | } 24 | else if(s[i]==')') 25 | { 26 | ss.pop(); 27 | temp=ss.top(); 28 | } 29 | else 30 | { 31 | if(s[i]=='N') 32 | x=(x-temp)%mod; 33 | else if(s[i]=='S') 34 | x=(x+temp)%mod; 35 | else if(s[i]=='E') 36 | y=(y+temp)%mod; 37 | else if(s[i]=='W') 38 | y=(y-temp)%mod; 39 | } 40 | } 41 | x=(x+mod)%mod; 42 | y=(y+mod)%mod; 43 | x++; 44 | y++; 45 | cout<<"Case #"<left, first, mid, last, prev); 20 | if (prev && root->val < prev->val) 21 | { 22 | if (!first) 23 | { 24 | first = prev; 25 | mid = root; 26 | } 27 | else last = root; 28 | } 29 | prev = root; 30 | fun(root->right, first, mid, last, prev); 31 | } 32 | 33 | void recoverTree(TreeNode* root) { 34 | if (!root) 35 | return ; 36 | TreeNode* first = NULL, *last = NULL, *mid = NULL, *prev = NULL; 37 | fun(root, first, mid, last, prev); 38 | if (last) 39 | swap(last->val, first->val); 40 | 41 | else swap(first->val, mid->val); 42 | } 43 | }; -------------------------------------------------------------------------------- /September LeetCode Challenge/Day_16.cpp: -------------------------------------------------------------------------------- 1 | class trieNode { 2 | public: 3 | trieNode *left, *right; 4 | }; 5 | 6 | void insertInTrie(trieNode *root, int num) { 7 | trieNode *curr = root; 8 | 9 | for (int i = 31; i >= 0; i--) { 10 | int bit = (num >> i) & 1; 11 | if (bit == 0) { 12 | if (!curr->left) { 13 | curr->left = new trieNode(); 14 | } 15 | curr = curr->left; 16 | } else { 17 | if (!curr->right) { 18 | curr->right = new trieNode(); 19 | } 20 | curr = curr->right; 21 | } 22 | } 23 | } 24 | 25 | class Solution { 26 | public: 27 | int findMaximumXOR(vector& nums) { 28 | trieNode *root = new trieNode(); 29 | for (int curr : nums) { 30 | insertInTrie(root, curr); 31 | } 32 | int maxXor = INT_MIN; 33 | for (int no : nums) { 34 | trieNode *curr = root; 35 | int currXor = 0, value = no; 36 | 37 | for (int i = 31; i >= 0; i--) { 38 | int bit = (value >> i) & 1; 39 | if (bit == 0) { 40 | if (curr->right) { 41 | curr = curr->right; 42 | currXor += pow(2, i); 43 | } else { 44 | curr = curr->left; 45 | } 46 | } else { 47 | if (curr->left) { 48 | curr = curr->left; 49 | currXor += pow(2, i); 50 | } else { 51 | curr = curr->right; 52 | } 53 | } 54 | } 55 | if (currXor > maxXor) 56 | maxXor = currXor; 57 | } 58 | return maxXor; 59 | } 60 | }; -------------------------------------------------------------------------------- /Codeforces/1375A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue> 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x, y) fixed << setprecision(y) << x 19 | #define mk(arr, n, type) type *arr = new type[n]; 20 | #define w(x) \ 21 | int x; \ 22 | cin >> x; \ 23 | while (x--) 24 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 25 | 26 | int32_t main() 27 | { 28 | ios_base::sync_with_stdio(0); 29 | cin.tie(0); 30 | cout.tie(0); 31 | #ifndef ONLINE_JUDGE 32 | freopen("input.txt", "r", stdin); 33 | freopen("output.txt", "w", stdout); 34 | #endif 35 | w(t) 36 | { 37 | int n, x; 38 | cin >> n; 39 | for (int i = 0; i < n; i++) 40 | { 41 | cin >> x; 42 | if (i & 1 && x > 0) 43 | x = -x; 44 | if (i % 2 == 0 && x < 0) 45 | x = -x; 46 | cout << x << " "; 47 | } 48 | cout << "\n"; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Google KickStart 2020/C/candies_final.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(vector& arr, vector& oper) { 5 | int ans = 0; 6 | if (oper[0] == 'U') { 7 | arr[(oper[1] - '0') - 1] = oper[2] - '0'; 8 | } 9 | else { 10 | int l = oper[1] - '0'; 11 | int r = oper[2] - '0'; 12 | int c = 1, sign = 1; 13 | for (int i=l-1; i> t; 27 | int c = 1; 28 | while (t--) { 29 | int ans = 0; 30 | int n, k; 31 | cin >> n >> k; 32 | vector arr(n); 33 | for (int i=0; i> arr[i]; 35 | } 36 | for (int i=0; i oper(3); 38 | for (int j=0; j<3; j++) { 39 | if (j == 0) cin >> oper[j]; 40 | else { 41 | int c; 42 | cin >> c; 43 | oper[j] = c + '0'; 44 | } 45 | } 46 | ans += solve(arr, oper); 47 | } 48 | cout << "Case #" << c++ << ": " << ans << endl; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /CSES/increasingArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ff first 5 | #define ss second 6 | #define int long long 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pii pair 10 | #define vi vector 11 | #define mii map 12 | #define pqb priority_queue 13 | #define pqs priority_queue > 14 | #define setbits(x) __builtin_popcountll(x) 15 | #define zrobits(x) __builtin_ctzll(x) 16 | #define mod 1000000007 17 | #define inf 1e18 18 | #define ps(x,y) fixed<>x; while(x--) 21 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 22 | 23 | int32_t main() 24 | { 25 | ios_base::sync_with_stdio(0); 26 | cin.tie(0); 27 | cout.tie(0); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | int n; 33 | cin >> n; 34 | int sum=0; 35 | int a, b; 36 | cin >> a; 37 | for (int i = 1; i < n; i++) 38 | { 39 | cin >> b; 40 | if (b < a) 41 | sum += a - b; 42 | else 43 | a = b; 44 | } 45 | cout << sum; 46 | return 0; 47 | } --------------------------------------------------------------------------------