├── solve here ├── LeetCode │ ├── PalindromeNumber │ │ ├── function.cpp │ │ ├── nidhiparab.py │ │ ├── purbendu.cpp │ │ ├── rk-1620.py │ │ ├── croquis.cpp │ │ ├── gaut2003.cpp │ │ ├── sayak01.cpp │ │ ├── Mohit-Kukreja-2002.cpp │ │ ├── janinirami.py │ │ ├── i-see-pixels.cpp │ │ ├── Debarpan08.java │ │ ├── palindromeNumber.cpp │ │ ├── Kinshu-Learner.cpp │ │ ├── palNo.c │ │ ├── Arora-Bhumika.java │ │ ├── function.js │ │ ├── GruelingPine185.c │ │ ├── Shubhamkumar47.cpp │ │ ├── vsiddharth365.cpp │ │ ├── dipteshh.go │ │ ├── I-am-SS.cs │ │ ├── aluvaja.cs │ │ ├── iamssss.cs │ │ ├── vut321.cpp │ │ ├── 2ndOct_I-am-SS.cs │ │ └── thechangamunda.py │ ├── container with most water │ │ ├── darkNoivern.cpp │ │ └── vut321.cpp │ ├── Longest Palindromic Substring │ │ ├── README.md │ │ ├── mahadev.cpp │ │ ├── croquis.cpp │ │ └── darkNoivern.cpp │ ├── 3SUM │ │ ├── vut321.cpp │ │ ├── purbendu.cpp │ │ ├── 10thoct_iamssss.java │ │ ├── darkNoivern.cpp │ │ ├── vsiddharth365.cpp │ │ ├── techseason.py │ │ ├── wsbmun.java │ │ ├── I-am-SS.java │ │ ├── MehakGupta1103.cpp │ │ └── aluvaja.java │ ├── Regular Expression Matching │ │ ├── mahadev.cpp │ │ ├── vut321.cpp │ │ └── README.md │ ├── Roman to Integer │ │ ├── subks.java │ │ ├── darkNoivern.cpp │ │ ├── vut321.cpp │ │ └── iamssss.c │ └── Median of Two Sorted Arrays │ │ └── 4. Median_of_Two_Sorted_Arrays.cpp ├── CodeChef │ ├── BINARYSUB │ │ ├── mahadev.py │ │ └── BINARYSUB.cpp │ ├── BITTUP │ │ ├── ayushman-25.py │ │ ├── README.md │ │ ├── deteksiwarna.py │ │ ├── Dibyendu70.cpp │ │ ├── 2ndOct_I-am-SS.cpp │ │ └── ss again.cpp │ ├── DAREA │ │ ├── mahadev.cpp │ │ ├── 2ndOct_I-am-SS.cpp │ │ ├── sayak01.cpp │ │ ├── readme.md │ │ └── soln of min dual area.py │ └── SHROUTE │ │ ├── 2ndOct_I-am-SS.cpp │ │ └── README.md └── README.md ├── CONTRIBUTE.md └── README.md /solve here/LeetCode/PalindromeNumber/function.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/nidhiparab.py: -------------------------------------------------------------------------------- 1 | def isPalindrome(s): 2 | return s == s[::-1] 3 | 4 | 5 | # Driver code 6 | s = input() 7 | ans = isPalindrome(s) 8 | 9 | if ans: 10 | print("true") 11 | else: 12 | print("false") -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/purbendu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | bool isPalindrome(int x) { 5 | string s = to_string(x), revS = s; 6 | reverse(begin(revS),end(revS)); 7 | return s == revS; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/rk-1620.py: -------------------------------------------------------------------------------- 1 | def palindrome(x): 2 | num=0 3 | p=x 4 | while(x>0): 5 | r=x%10 6 | x=int(x/10) 7 | num=num*10+r 8 | if(num==p): 9 | print("true") 10 | else: 11 | print("false") 12 | #main 13 | x=int(input("x = ")) 14 | palindrome(x) -------------------------------------------------------------------------------- /solve here/CodeChef/BINARYSUB/mahadev.py: -------------------------------------------------------------------------------- 1 | mod = 998244353 2 | for _ in range(int(input())): 3 | s = input() 4 | n = len(s) 5 | dp = [1]*(n+1) 6 | for i in range(1, n): 7 | dp[i+1] = dp[i] 8 | if s[i] != s[i-1]: 9 | dp[i+1] += dp[i-1] 10 | dp[i+1] %= mod 11 | print(dp[n]) 12 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/croquis.cpp: -------------------------------------------------------------------------------- 1 | 2 | class PalindromeNumber { 3 | public: 4 | bool main(int x) { 5 | if(x<0|| (x!=0 &&x%10==0)) return false; 6 | int sum=0; 7 | while(x>sum) 8 | { 9 | sum = sum*10+x%10; 10 | x = x/10; 11 | } 12 | return (x==sum)||(x==sum/10); 13 | } 14 | }; -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/ayushman-25.py: -------------------------------------------------------------------------------- 1 | # Python Solution for BITTUP 2 | 3 | def solve(): 4 | n, m = map(int, input().split()) 5 | mod = int(1e9) + 7 6 | print(pow(pow(2, n, mod) - 1, m, mod)) 7 | 8 | 9 | def main(): 10 | t = int(input()) 11 | for _ in range(t): 12 | solve() 13 | 14 | 15 | if __name__ == "__main__": 16 | main() 17 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/gaut2003.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x<0) return false; 5 | int reverse = 0, temp = x; 6 | while(temp){ 7 | reverse = reverse*10 + (temp%10); 8 | temp /= 10; 9 | } 10 | 11 | if(reverse == x) return true; 12 | else return false; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /solve here/LeetCode/container with most water/darkNoivern.cpp: -------------------------------------------------------------------------------- 1 | int maxArea(vector& height) { 2 | int water = 0; 3 | int i = 0, j = height.size() - 1; 4 | while (i < j) { 5 | int h = min(height[i], height[j]); 6 | water = max(water, (j - i) * h); 7 | while (height[i] <= h && i < j) i++; 8 | while (height[j] <= h && i < j) j--; 9 | } 10 | return water; 11 | } 12 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/sayak01.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | bool isPalindrome(int x) 7 | { 8 | if (x < 0) 9 | return false; 10 | unsigned rx = 0; 11 | unsigned store = x; 12 | while (x) 13 | { 14 | rx = rx * 10 + x % 10; 15 | x /= 10; 16 | } 17 | return store == rx; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/Mohit-Kukreja-2002.cpp: -------------------------------------------------------------------------------- 1 | class PalindromeNumber { 2 | public: 3 | bool main(int x) { 4 | if(x<0 || (x!=0 && x%10==0){ 5 | return false; 6 | } 7 | 8 | int ans = 0; 9 | while(x > ans) { 10 | ans = ans * 10 + x % 10; 11 | x =x/ 10; 12 | } 13 | if(ans==x || ans/10 ==x){ 14 | return true; 15 | } 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/janinirami.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x:int): 3 | tmp = x 4 | reverse = 0 5 | 6 | while x>0: 7 | i = x % 10 8 | reverse = reverse * 10 + i 9 | x = x // 10 10 | 11 | return tmp == reverse 12 | 13 | 14 | if __name__ == '__main__': 15 | print (Solution().isPalindrome(121)) 16 | print (Solution().isPalindrome(-121)) 17 | print (Solution().isPalindrome(10)) -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/i-see-pixels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x < 0) return false; 5 | 6 | long int rev = 0, num = x; 7 | while(x>0){ 8 | int y = x%10; 9 | x /= 10; 10 | // if(rev*10 >= 2147483648) rev = 0; 11 | rev = rev*10 + y; 12 | } 13 | 14 | if(rev == num) return true; 15 | 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/Debarpan08.java: -------------------------------------------------------------------------------- 1 | class PalindromeExample{ 2 | public static void main(String args[]){ 3 | int r,sum=0,temp; 4 | int n=454; 5 | 6 | temp=n; 7 | while(n>0){ 8 | r=n%10; //getting remainder 9 | sum=(sum*10)+r; 10 | n=n/10; 11 | } 12 | if(temp==sum) 13 | System.out.println("palindrome number "); 14 | else 15 | System.out.println("not palindrome"); 16 | } 17 | } -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/palindromeNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | if(x<0) return false; 6 | vector s; 7 | while(x!=0){ 8 | s.push_back(x%10); 9 | x/=10; 10 | } 11 | vector n; 12 | n=s; 13 | reverse(s.begin(),s.end()); 14 | for(int i=0;i 2 | 3 | 4 | int is_palindrome(int _num) { 5 | if(_num < 10 || ((_num % 10 == 0) && _num != 0)) return 0; 6 | 7 | int rev = 0; 8 | int rem = 0; 9 | 10 | while(_num != 0) { 11 | rem = _num % 10; 12 | rev *= 10 + rem; 13 | _num /= 10; 14 | } 15 | 16 | return (_num == rev); 17 | } 18 | 19 | int main(int _argc, char** _argv) { 20 | int raw; 21 | printf("Enter an integer: "); 22 | scanf("%d", &raw); 23 | printf("%d\n", is_palindrome(raw)); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/Arora-Bhumika.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public boolean isPalindrome(int x) 4 | { 5 | int n=x; //assign number to another variable 6 | int s=0; //to store reverse of the number 7 | int r; 8 | while(n>0) //to check whether number is greater than zero 9 | { 10 | r=n%10; 11 | s=s*10+r; 12 | n=n/10; 13 | } 14 | if(x==s) // comparing the original number with reverse 15 | return true; 16 | else 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/function.js: -------------------------------------------------------------------------------- 1 | const isPalindrome = (num) => { 2 | if (num < 10) { 3 | return false; 4 | } 5 | 6 | let i = 0; 7 | const numByString = num.toString(); 8 | while (i < numByString.length - 1 - i) { 9 | if (numByString[i] !== numByString[numByString.length - 1 - i]) { 10 | return false; 11 | } 12 | i++; 13 | } 14 | 15 | return true; 16 | } 17 | 18 | console.log(isPalindrome(323)); // true 19 | console.log(isPalindrome(123)); // false 20 | console.log(isPalindrome(-123)); // false 21 | console.log(isPalindrome(3)); // false 22 | console.log(isPalindrome(1221)); // true 23 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/GruelingPine185.c: -------------------------------------------------------------------------------- 1 | // compile with: gcc Pull\ Here/LeetCode/PalindromeNumber/GruelingPine185.c 2 | // run with: ./a.out 3 | 4 | #include 5 | 6 | 7 | int is_palindrome(int _num) { 8 | if(_num < 10 || ((_num % 10 == 0) && _num != 0)) return 0; 9 | 10 | int rev = 0; 11 | int rem = 0; 12 | 13 | while(_num != 0) { 14 | rem = _num % 10; 15 | rev *= 10 + rem; 16 | _num /= 10; 17 | } 18 | 19 | return (_num == rev); 20 | } 21 | 22 | int main(int _argc, char** _argv) { 23 | int raw; 24 | printf("Enter an integer: "); 25 | scanf("%d", &raw); 26 | printf("%d\n", is_palindrome(raw)); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /solve here/LeetCode/Longest Palindromic Substring/README.md: -------------------------------------------------------------------------------- 1 | # Longest Palindromic Substring 2 | ## Link : https://leetcode.com/problems/longest-palindromic-substring/ 3 | ## Problem 4 | Given a string s, return the longest palindromic substring in s. 5 | 6 | A string is called a palindrome string if the reverse of that string is the same as the original string. 7 | 8 | ## Example 1: 9 | ``` 10 | Input: s = "babad" 11 | Output: "bab" 12 | Explanation: "aba" is also a valid answer. 13 | ``` 14 | ## Example 2: 15 | ``` 16 | Input: s = "cbbd" 17 | Output: "bb" 18 | ``` 19 | 20 | ## Constraints 21 | * 1 <= s.length <= 1000 22 | * s consist of only digits and English letters. -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/vut321.cpp: -------------------------------------------------------------------------------- 1 | vector> threeSum(vector& nums) { 2 | sort(nums.begin(), nums.end()); 3 | vector> res; 4 | for (unsigned int i=0; i0) && (nums[i]==nums[i-1])) 6 | continue; 7 | int l = i+1, r = nums.size()-1; 8 | while (l0) r--; 11 | else if (s<0) l++; 12 | else { 13 | res.push_back(vector {nums[i], nums[l], nums[r]}); 14 | while (nums[l]==nums[l+1]) l++; 15 | while (nums[r]==nums[r-1]) r--; 16 | l++; r--; 17 | } 18 | } 19 | } 20 | return res; 21 | } 22 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/Shubhamkumar47.cpp: -------------------------------------------------------------------------------- 1 | string reverse(string str){ 2 | if(str.empty()){ 3 | return str; 4 | } 5 | string ans = ""; 6 | for(int i=str.length()-1; i>=0; i--){ 7 | ans+=str[i]; 8 | } 9 | return ans; 10 | } 11 | 12 | bool isPAlindrome(string str){ 13 | 14 | string ans = reverse(str); 15 | 16 | if(ans==str){ 17 | return true; 18 | } 19 | return false; 20 | } 21 | bool isPalindrome(int x) { 22 | 23 | string str = to_string(x); 24 | 25 | if(isPAlindrome(str)){ 26 | return true; 27 | } 28 | return false; 29 | 30 | 31 | } -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/vsiddharth365.cpp: -------------------------------------------------------------------------------- 1 | // Author: Siddharth Verma 2 | #include "bits/stdc++.h" 3 | using namespace std; 4 | class Solution 5 | { 6 | public: 7 | bool isPalindrome(int x) 8 | { 9 | if(x<0) 10 | return false; 11 | int dummy = 0, original = x; 12 | while(x) 13 | { 14 | dummy=dummy*10 + x%10; 15 | x/=10; 16 | } 17 | return original==dummy; 18 | } 19 | }; 20 | int main() 21 | { 22 | Solution sol; 23 | int x; 24 | cout<<"Enter the number to check for palindrome: "; 25 | cin>>x; 26 | if(sol.isPalindrome(x)) 27 | cout< 0; palNum = palNum / 10 { 11 | remainder = palNum % 10 12 | reverse = reverse*10 + remainder 13 | } 14 | return reverse 15 | } 16 | func main() { 17 | 18 | var palNum int 19 | 20 | fmt.Print("Enter the Number to check Palindrome = ") 21 | fmt.Scanln(&palNum) 22 | 23 | reverse = revNumber(palNum) 24 | fmt.Println("The Reverse of the Given Number = ", reverse) 25 | 26 | if palNum == reverse { 27 | fmt.Println(palNum, " is a Palindrome Number") 28 | } else { 29 | fmt.Println(palNum, " is Not a Palindrome Number") 30 | } 31 | } -------------------------------------------------------------------------------- /solve here/LeetCode/Regular Expression Matching/mahadev.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int m = s.size(), n = p.size(); 5 | vector cur(n + 1, false); 6 | for (int i = 0; i <= m; i++) { 7 | bool pre = cur[0]; 8 | cur[0] = !i; 9 | for (int j = 1; j <= n; j++) { 10 | bool temp = cur[j]; 11 | if (p[j - 1] == '*') { 12 | cur[j] = cur[j - 2] || (i && cur[j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')); 13 | } else { 14 | cur[j] = i && pre && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); 15 | } 16 | pre = temp; 17 | } 18 | } 19 | return cur[n]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /solve here/LeetCode/Regular Expression Matching/vut321.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int m = s.size(), n = p.size(); 5 | vector cur(n + 1, false); 6 | for (int i = 0; i <= m; i++) { 7 | bool pre = cur[0]; 8 | cur[0] = !i; 9 | for (int j = 1; j <= n; j++) { 10 | bool temp = cur[j]; 11 | if (p[j - 1] == '*') { 12 | cur[j] = cur[j - 2] || (i && cur[j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')); 13 | } else { 14 | cur[j] = i && pre && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); 15 | } 16 | pre = temp; 17 | } 18 | } 19 | return cur[n]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /solve here/LeetCode/Longest Palindromic Substring/mahadev.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int check(string &s, int L, int R) 4 | { 5 | while(L>=0 and R ans) 26 | { 27 | ans = len; 28 | st = i-(len-1)/2; 29 | } 30 | } 31 | return s.substr(st, ans); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /solve here/LeetCode/Roman to Integer/subks.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int romanToInt(String s) { 3 | int ans = 0, num = 0; 4 | for (int i = s.length()-1; i >= 0; i--) { 5 | switch(s.charAt(i)) { 6 | case 'I': num = 1; 7 | break; 8 | case 'V': num = 5; 9 | break; 10 | case 'X': num = 10; 11 | break; 12 | case 'L': num = 50; 13 | break; 14 | case 'C': num = 100; 15 | break; 16 | case 'D': num = 500; 17 | break; 18 | case 'M': num = 1000; 19 | break; 20 | } 21 | if (4 * num < ans) 22 | ans -= num; 23 | else 24 | ans += num; 25 | } 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /solve here/LeetCode/Roman to Integer/darkNoivern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int romanToInt(String s) { 3 | if (s == null || s.length() == 0) { 4 | return 0; 5 | } 6 | HashMap map = new HashMap(); 7 | map.put('I', 1); 8 | map.put('V', 5); 9 | map.put('X', 10); 10 | map.put('L', 50); 11 | map.put('C', 100); 12 | map.put('D', 500); 13 | map.put('M', 1000); 14 | int sum = map.get(s.charAt(s.length() - 1)); 15 | for (int i = s.length() - 2; i >= 0; i--) { 16 | char first = s.charAt(i); 17 | char second = s.charAt(i + 1); 18 | if (map.get(first) < map.get(second)) { 19 | sum -= map.get(first); 20 | } else { 21 | sum += map.get(first); 22 | } 23 | } 24 | return sum; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solve here/LeetCode/Roman to Integer/vut321.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int getTranslateNum(char s) { 3 | switch(s) { 4 | case 'I': return 1; 5 | case 'V': return 5; 6 | case 'X': return 10; 7 | case 'L': return 50; 8 | case 'C': return 100; 9 | case 'D': return 500; 10 | case 'M': return 1000; 11 | default: return 0; 12 | } 13 | return 0; 14 | } 15 | public: 16 | int romanToInt(string s) { 17 | if(s.empty()) return 0; 18 | int returnValue = 0; 19 | for(unsigned int i=0; i 2 | using namespace std; 3 | long long int solve(long long int x){ 4 | long long int a[x+3]; 5 | a[0]=1; 6 | a[1]=1; 7 | a[2]=2; 8 | for(int i=3;i<=x;i++){ 9 | a[i]=((a[i-1]% 998244353)+(a[i-2]%998244353))%998244353; 10 | } 11 | return a[x]; 12 | } 13 | int main() { 14 | // your code goes here 15 | 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | string s; 20 | cin>>s; 21 | long long int ans=1; 22 | int count=1; 23 | for(int i=1;i1){ 28 | ans=((ans%998244353)*(solve(count)%998244353))%998244353; 29 | count=1; 30 | } 31 | } 32 | if(count>1){ 33 | ans=((ans%998244353)*(solve(count)%998244353))%998244353; 34 | count=1; 35 | } 36 | cout<> threeSum(vector& nums) { 4 | int n = nums.size(); 5 | if(n<3) return {}; 6 | 7 | vector> ans; 8 | sort(nums.begin(),nums.end()); 9 | for(int i = 0; i < n; i++) { 10 | int right = n-1, left = i+1, target = -nums[i]; 11 | while(right > left) { 12 | int sum = nums[left] + nums[right]; 13 | if(sum > target) right--; 14 | else if(sum < target) left++; 15 | else { 16 | vector triplet = {nums[i],nums[left],nums[right]}; 17 | while(right > left and nums[right] == triplet[2]) right--; 18 | while(right > left and nums[left] == triplet[1]) left++; 19 | ans.push_back(triplet); 20 | } 21 | } 22 | while(i+1 revertedNumber) { 14 | revertedNumber = revertedNumber * 10 + x % 10; 15 | x /= 10; 16 | } 17 | 18 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 19 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 20 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 21 | return x == revertedNumber || x == revertedNumber/10; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/aluvaja.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsPalindrome(int x) { 3 | // Special cases: 4 | // As discussed above, when x < 0, x is not a palindrome. 5 | // Also if the last digit of the number is 0, in order to be a palindrome, 6 | // the first digit of the number also needs to be 0. 7 | // Only 0 satisfy this property. 8 | if(x < 0 || (x % 10 == 0 && x != 0)) { 9 | return false; 10 | } 11 | 12 | int revertedNumber = 0; 13 | while(x > revertedNumber) { 14 | revertedNumber = revertedNumber * 10 + x % 10; 15 | x /= 10; 16 | } 17 | 18 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 19 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 20 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 21 | return x == revertedNumber || x == revertedNumber/10; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/iamssss.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsPalindrome(int x) { 3 | // Special cases: 4 | // As discussed above, when x < 0, x is not a palindrome. 5 | // Also if the last digit of the number is 0, in order to be a palindrome, 6 | // the first digit of the number also needs to be 0. 7 | // Only 0 satisfy this property. 8 | if(x < 0 || (x % 10 == 0 && x != 0)) { 9 | return false; 10 | } 11 | 12 | int revertedNumber = 0; 13 | while(x > revertedNumber) { 14 | revertedNumber = revertedNumber * 10 + x % 10; 15 | x /= 10; 16 | } 17 | 18 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 19 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 20 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 21 | return x == revertedNumber || x == revertedNumber/10; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/vut321.cpp: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsPalindrome(int x) { 3 | // Special cases: 4 | // As discussed above, when x < 0, x is not a palindrome. 5 | // Also if the last digit of the number is 0, in order to be a palindrome, 6 | // the first digit of the number also needs to be 0. 7 | // Only 0 satisfy this property. 8 | if(x < 0 || (x % 10 == 0 && x != 0)) { 9 | return false; 10 | } 11 | 12 | int revertedNumber = 0; 13 | while(x > revertedNumber) { 14 | revertedNumber = revertedNumber * 10 + x % 10; 15 | x /= 10; 16 | } 17 | 18 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 19 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 20 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 21 | return x == revertedNumber || x == revertedNumber/10; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/2ndOct_I-am-SS.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsPalindrome(int x) { 3 | // Special cases: 4 | // As discussed above, when x < 0, x is not a palindrome. 5 | // Also if the last digit of the number is 0, in order to be a palindrome, 6 | // the first digit of the number also needs to be 0. 7 | // Only 0 satisfy this property. 8 | if(x < 0 || (x % 10 == 0 && x != 0)) { 9 | return false; 10 | } 11 | 12 | int revertedNumber = 0; 13 | while(x > revertedNumber) { 14 | revertedNumber = revertedNumber * 10 + x % 10; 15 | x /= 10; 16 | } 17 | 18 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 19 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 20 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 21 | return x == revertedNumber || x == revertedNumber/10; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/README.md: -------------------------------------------------------------------------------- 1 | # Bitwise Tuples 2 | Problem Code: BITTUP 3 | ## LINK : https://www.codechef.com/JUNE21B/problems/BITTUP 4 | ## Problem: 5 | Chef has two numbers N and M. Help Chef to find number of integer N-tuples (A1,A2,…,AN) such that 0≤A1,A2,…,AN≤2^M−1 and A1&A2&…&AN=0, where & denotes the bitwise AND operator. 6 | 7 | Since the number of tuples can be large, output it modulo 10^9+7. 8 | ## Input: 9 | - The first line contains a single integer T denoting the number of test cases. The description of T test cases follows. 10 | - The first and only line of each test case contains two integers N and M 11 | ## Output: 12 | For each test case, output in a single line the answer to the problem modulo 10^9+7 13 | ## Constraints: 14 | - 1≤T≤10^5 15 | - 1≤N,M≤10^6 16 | ## Sample Input 1: 17 | ``` 18 | 4 19 | 1 2 20 | 2 2 21 | 4 2 22 | 8 4 23 | ``` 24 | ## Sample Output1: 25 | ``` 26 | 1 27 | 9 28 | 225 29 | 228250597 30 | ``` 31 | 32 | ## Explamnation: 33 | - Test Case 1 : The only possible tuple is (0). 34 | - test Case 2 : The tuples are (0,0), (0,1), (0,2), (0,3), (1,0), (2,0), (3,0), (1,2), (2,1). 35 | -------------------------------------------------------------------------------- /solve here/LeetCode/Median of Two Sorted Arrays/4. Median_of_Two_Sorted_Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& num1, vector& num2) { 4 | int size1=num1.size(); 5 | int size2=num2.size(); 6 | vector ans; 7 | int i,j; 8 | for( i=0,j=0;i s = new HashSet(); 18 | int curr_sum = sum - A[i]; 19 | for (int j = i + 1; j < arr_size; j++) 20 | { 21 | if (s.contains(curr_sum - A[j])) 22 | { 23 | System.out.printf("Triplet is %d, 24 | %d, %d", A[i], 25 | A[j], curr_sum - A[j]); 26 | return true; 27 | } 28 | s.add(A[j]); 29 | } 30 | } 31 | 32 | // If we reach here, then no triplet was found 33 | return false; 34 | } 35 | 36 | /* Driver code */ 37 | public static void main(String[] args) 38 | { 39 | int A[] = { 1, 4, 45, 6, 10, 8 }; 40 | int sum = 22; 41 | int arr_size = A.length; 42 | 43 | find3Numbers(A, arr_size, sum); 44 | } 45 | } 46 | 47 | // This code has been contributed by 29AjayKumar 48 | -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/darkNoivern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | 5 | int n = nums.size(); 6 | unordered_map mp; 7 | for(int i=0;i> s; 12 | 13 | for(int i=0;i=cnt){ 24 | vector store; 25 | store.push_back(nums[i]); 26 | store.push_back(nums[j]); 27 | store.push_back(left); 28 | sort(store.begin(),store.end()); 29 | s.insert(store); 30 | } 31 | } 32 | } 33 | 34 | 35 | vector> ans; 36 | for(auto it : s){ 37 | ans.push_back(it); 38 | } 39 | 40 | return ans; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /solve here/LeetCode/Regular Expression Matching/README.md: -------------------------------------------------------------------------------- 1 | # Regular Expression Matching 2 | 3 | ## Link : https://leetcode.com/problems/regular-expression-matching/ 4 | 5 | ## Problem 6 | Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: 7 | 8 | '.' Matches any single character.​​​​ 9 | '*' Matches zero or more of the preceding element. 10 | 11 | The matching should cover the entire input string (not partial). 12 | 13 | ## Example 1: 14 | ``` 15 | Input: s = "aa", p = "a" 16 | Output: false 17 | Explanation: "a" does not match the entire string "aa". 18 | ``` 19 | ## Example 2: 20 | ``` 21 | Input: s = "aa", p = "a*" 22 | Output: true 23 | Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". 24 | ``` 25 | ## Example 3: 26 | ``` 27 | Input: s = "ab", p = ".*" 28 | Output: true 29 | Explanation: ".*" means "zero or more (*) of any character (.)". 30 | ``` 31 | ## Constraints 32 | 33 | * 1 <= s.length <= 20 34 | * 1 <= p.length <= 30 35 | * p contains only lowercase English letters, '.', and '*'. 36 | * s contains only lowercase English letters. 37 | * It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.* -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/deteksiwarna.py: -------------------------------------------------------------------------------- 1 | from cv2 import cv2 2 | import numpy as np 3 | 4 | # Capturing Video through webcam. 5 | cap = cv2.VideoCapture(0) 6 | 7 | while(1): 8 | _, img = cap.read() 9 | 10 | # converting frame(img) from BGR (Blue-Green-Red) to HSV (hue-saturation-value) 11 | hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 12 | 13 | # defining the range of Yellow color 14 | red_lower = np.array([0, 100, 100], np.uint8) 15 | red_upper = np.array([2, 255, 255], np.uint8) 16 | red = cv2.inRange(hsv, red_lower, red_upper) 17 | 18 | # Morphological transformation, Dilation 19 | kernal = np.ones((5, 5), "uint8") 20 | res = cv2.bitwise_and(img, img, mask=red) 21 | 22 | # Tracking Colour (Yellow) 23 | (contours, hierarchy) = cv2.findContours( 24 | red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 25 | 26 | for pic, contour in enumerate(contours): 27 | area = cv2.contourArea(contour) 28 | if(area > 900): 29 | 30 | x, y, w, h = cv2.boundingRect(contour) 31 | img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 3) 32 | 33 | # show color detection result 34 | cv2.imshow("Color Tracking", img) 35 | cv2.imshow("Red", res) 36 | 37 | if cv2.waitKey(10) & 0xFF == 27: 38 | cap.release() 39 | cv2.destroyAllWindows() 40 | break 41 | -------------------------------------------------------------------------------- /solve here/README.md: -------------------------------------------------------------------------------- 1 | ## CodeChef 2 | - ### DAREA 3 | - #### Link : https://www.codechef.com/JUNE21B/problems/DAREA 4 | - #### Languages: 5 | - [X] C++ 6 | - [X] Python 7 | - [ ] Java 8 | - ### SHROUTE 9 | - #### Link : https://www.codechef.com/JUNE21B/problems/SHROUTE 10 | - #### Languages: 11 | - [X] C++ 12 | - [X] Python 13 | - [ ] Java 14 | - ### BITTUP 15 | - #### Link : https://www.codechef.com/JUNE21B/problems/BITTUP 16 | - #### Languages: 17 | - [X] C++ 18 | - [X] Python 19 | - [ ] Java 20 | 21 | 22 | ## LeetCode 23 | - ### Longest Substring Without Repeating Characters 24 | - #### Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/ 25 | - #### Languages: 26 | - [X] C++ 27 | - [X] Python 28 | - [ ] Java 29 | - [X] JS 30 | - ### Partition to K Equal Sum Subsets 31 | - #### Link : https://leetcode.com/problems/partition-to-k-equal-sum-subsets/ 32 | - #### Languages: 33 | - [X] C++ 34 | - [X] Python 35 | - [ ] Java 36 | - ### Count Complete Tree Nodes 37 | - #### Link : https://leetcode.com/problems/count-complete-tree-nodes/ 38 | - #### Languages: 39 | - [X] C++ 40 | - [X] Python 41 | - [ ] Java 42 | ## Hackerrank 43 | -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/Dibyendu70.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define ull unsigned long long int 4 | #define ll long long int 5 | ull p = pow(10,9)+7; 6 | using namespace std; 7 | int calculate(ull n,ull m){ 8 | //we want to calculate (2^n - 1)^m mod p 9 | 10 | /*Calculating : (2^n - 1)mod p*/ 11 | ull r; 12 | unsigned long long u = 1, w = 2; 13 | while (n != 0){ 14 | if ((n & 0x1) != 0){ 15 | u = (u * w) % p; /* (mul-rdx) */ 16 | } 17 | if ((n >>= 1) != 0){ 18 | w = (w * w) % p; /* (sqr-rdx) */ 19 | } 20 | } 21 | r = (unsigned long) u; 22 | r = (r == 0) ? (p - 1) : (r - 1); 23 | //r = (2^n - 1)mod p 24 | 25 | 26 | /*Now calculating final result = (2^n - 1)^m mod p*/ 27 | ull res = 1; // Initialize result 28 | if (r == 0) return 0; // In case x is divisible bm p; 29 | while (m > 0){ 30 | // If m is odd, multiplm x with result 31 | if (m & 1) 32 | res = (res*r) % p; 33 | // m must be even now 34 | m = m>>1; // m = y/2 35 | r = (r*r) % p; 36 | } 37 | return res; 38 | } 39 | int main(){ 40 | ull t; 41 | cin >> t; 42 | for(;t>0;t--){ 43 | ull n,m; 44 | cin >> n >>m; 45 | cout << calculate(n,m) << endl; 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/vsiddharth365.cpp: -------------------------------------------------------------------------------- 1 | // Author: Siddharth Verma 2 | #include "bits/stdc++.h" 3 | using namespace std; 4 | class Solution { 5 | public: 6 | vector> threeSum(vector& nums) { 7 | vector>v; 8 | sort(nums.begin(),nums.end()); 9 | int left,right,sum; 10 | for(int i=0;i0 && nums[i]==nums[i-1]) 13 | continue; 14 | left=i+1; 15 | right=nums.size()-1; 16 | sum=nums[i]; 17 | while(left0) 20 | right--; 21 | else if(sum+nums[left]+nums[right]<0) 22 | left++; 23 | else 24 | { 25 | v.push_back(vector{nums[i],nums[left],nums[right]}); 26 | left++; 27 | while(nums[left]==nums[left-1] and leftv{-1, 0, 1, 2, -1, -4}; 39 | vector>ans=sol.threeSum(v); 40 | for(int i=0;i List[List[int]]: 2 | 3 | res = set() 4 | 5 | #1. Split nums into three lists: negative numbers, positive numbers, and zeros 6 | n, p, z = [], [], [] 7 | for num in nums: 8 | if num > 0: 9 | p.append(num) 10 | elif num < 0: 11 | n.append(num) 12 | else: 13 | z.append(num) 14 | 15 | #2. Create a separate set for negatives and positives for O(1) look-up times 16 | N, P = set(n), set(p) 17 | 18 | #3. If there is at least 1 zero in the list, add all cases where -num exists in N and num exists in P 19 | # i.e. (-3, 0, 3) = 0 20 | if z: 21 | for num in P: 22 | if -1*num in N: 23 | res.add((-1*num, 0, num)) 24 | 25 | #3. If there are at least 3 zeros in the list then also include (0, 0, 0) = 0 26 | if len(z) >= 3: 27 | res.add((0,0,0)) 28 | 29 | #4. For all pairs of negative numbers (-3, -1), check to see if their complement (4) 30 | # exists in the positive number set 31 | for i in range(len(n)): 32 | for j in range(i+1,len(n)): 33 | target = -1*(n[i]+n[j]) 34 | if target in P: 35 | res.add(tuple(sorted([n[i],n[j],target]))) 36 | 37 | #5. For all pairs of positive numbers (1, 1), check to see if their complement (-2) 38 | # exists in the negative number set 39 | for i in range(len(p)): 40 | for j in range(i+1,len(p)): 41 | target = -1*(p[i]+p[j]) 42 | if target in N: 43 | res.add(tuple(sorted([p[i],p[j],target]))) 44 | 45 | return res 46 | -------------------------------------------------------------------------------- /solve here/LeetCode/Longest Palindromic Substring/croquis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class LongestPalindrome { 3 | public: 4 | string main(string s) { 5 | int n=s.length(); 6 | 7 | int maxi=1; 8 | 9 | int start=0; 10 | int end=0; 11 | 12 | int l,r; 13 | 14 | for(int i=0;i=0 && rmaxi){ 22 | maxi=r-l+1; 23 | start=l; 24 | end=r; 25 | } 26 | l--; 27 | r++; 28 | }else{ 29 | break; 30 | } 31 | } 32 | 33 | l=i-1; 34 | r=i; 35 | 36 | while(l>=0 && rmaxi){ 39 | maxi=r-l+1; 40 | start=l; 41 | end=r; 42 | } 43 | l--; 44 | r++; 45 | }else{ 46 | break; 47 | } 48 | } 49 | } 50 | 51 | string ans=""; 52 | for(int i=start;i<=end;i++){ 53 | ans+=s[i]; 54 | } 55 | return ans; 56 | } 57 | }; -------------------------------------------------------------------------------- /solve here/LeetCode/Roman to Integer/iamssss.c: -------------------------------------------------------------------------------- 1 | char * intToRoman(int num){ 2 | char *ret=malloc(30*sizeof(char)); 3 | 4 | int cnt=0; int i=0; 5 | if(num/1000>0){ 6 | for(i=0;i0){ 11 | ret[cnt++]='C'; ret[cnt++]='M'; num=num%900; 12 | }else{ 13 | if(num/500>0){ 14 | ret[cnt++]='D'; num=num-500; 15 | }else if(num/400>0){ 16 | ret[cnt++]='C'; ret[cnt++]='D'; num=num-400; 17 | } 18 | if(num/100>0){ 19 | for(i=0;i0){ 25 | ret[cnt++]='X'; ret[cnt++]='C'; num=num%90; 26 | }else{ 27 | if(num/50>0){ 28 | ret[cnt++]='L'; num=num-50; 29 | }else if(num/40>0){ 30 | ret[cnt++]='X'; ret[cnt++]='L'; num=num-40; 31 | } 32 | if(num/10>0){ 33 | for(i=0;i0){ 39 | ret[cnt++]='I'; ret[cnt++]='X'; 40 | }else{ 41 | if(num/5>0){ 42 | ret[cnt++]='V'; num=num-5; 43 | }else if(num/4>0){ 44 | ret[cnt++]='I'; ret[cnt++]='V'; num=num-4; 45 | } 46 | if(num>0){ 47 | for(i=0;i here . 6 | 7 | # Rule for Contrinution 8 | * Visit issues and select an issue to contribute 9 | * Go to folder specified in Issues 10 | * Give file name according to specified in Issue. (only files with proper filename will be accepted) 11 | * Then make yout Pull Request and it will be accepted soon 12 | 13 | # :thumbsup: Awesome! How and What can I Contribute? 14 | It's very easy. Follow the below steps you need to create your -(maybe)- EXAMPLE first pull request. 15 | 16 | * Fork this repository by click the Fork button in the top right of this page or simply click here. 17 | * Visit issues and select any issue to contribute (details will be there). 18 | * Create a new file and add a new Program code(like C++ program to implement Binary Search, etc.) in any programming language like C++, Java, Python, etc. (Note: Program must not be exist already in this repository) 19 | * After adding the code, Commit your changes. 20 | * Create a new pull request from your forked repository (Button located at the top of your repo) 21 | * Star this repository! :blush: -------------------------------------------------------------------------------- /solve here/LeetCode/Longest Palindromic Substring/darkNoivern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rec(int i,int j,vector> &dp,string &s){ 4 | if(i==j){ 5 | return dp[i][i] = 1; 6 | } 7 | if(j==(i+1)){ 8 | if(s[i]==s[j]){ 9 | return dp[i][j] = 2; 10 | } 11 | else{ 12 | return dp[i][j] = 0; 13 | } 14 | } 15 | if(i>j){ 16 | return 0; 17 | } 18 | 19 | auto &ans = dp[i][j]; 20 | if(ans!=(-1)){ 21 | return ans; 22 | } 23 | 24 | if(s[i]==s[j]){ 25 | int x = rec(i+1,j-1,dp,s); 26 | if(x==0){ 27 | return ans = 0; 28 | } 29 | else{ 30 | return ans = x+2; 31 | } 32 | } 33 | else{ 34 | int x = rec(i+1,j,dp,s); 35 | int y = rec(i,j-1,dp,s); 36 | return ans = 0; 37 | } 38 | } 39 | 40 | string longestPalindrome(string s) { 41 | 42 | int n = s.length(); 43 | vector> dp(n,vector(n,-1)); 44 | 45 | // rec(0,n-1,dp,s); 46 | 47 | int val = 1; 48 | int x = 0; 49 | int y = 0; 50 | 51 | for(int i=0;ival){ 57 | val = dp[i][j]; 58 | x = i; 59 | y = j; 60 | } 61 | } 62 | } 63 | 64 | string ans = ""; 65 | for(int i=x;i<(y+1);i++){ 66 | ans+=s[i]; 67 | } 68 | 69 | return ans; 70 | } 71 | }; 72 | -------------------------------------------------------------------------------- /solve here/CodeChef/DAREA/mahadev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // commrnting in this line 3 | using namespace std; 4 | #define int long long 5 | #define MOD 1000000007 6 | 7 | 8 | void solve(){ 9 | int i; 10 | int n,m; cin>>n>>m; 11 | int a[n]; 12 | int b[m]; 13 | 14 | for (i=0;i>a[i];} 16 | for(i=0;i>b[i];} 18 | 19 | int max=1e9; 20 | mapmp; 21 | 22 | for(i=0;i=0;i--) { 41 | if(a[i]==2) left=i; 42 | if(left!=-1) { 43 | if(a[i]==0) 44 | mp[i] =min(mp[i],left-i); 45 | 46 | } 47 | } 48 | 49 | for(i=0;i>t; 65 | // t = 1; 66 | int _=1; 67 | while(t--){ 68 | // cout<<"Case #"<<_++<<": "; 69 | solve(); 70 | } 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /solve here/LeetCode/container with most water/vut321.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int i=0; int j=height.size()-1; 5 | int ans = 0; 6 | while(i < j){ 7 | ans = max(ans,min(height[i],height[j])*(j-i)); 8 | if(height[i]height[j])j--; 10 | else { 11 | i++; 12 | j--; 13 | } 14 | } 15 | return ans; 16 | } 17 | }; 18 | class Solution { 19 | public: 20 | int maxArea(vector& height) { 21 | int i=0; int j=height.size()-1; 22 | int ans = 0; 23 | while(i < j){ 24 | ans = max(ans,min(height[i],height[j])*(j-i)); 25 | if(height[i]height[j])j--; 27 | else { 28 | i++; 29 | j--; 30 | } 31 | } 32 | return ans; 33 | } 34 | }; 35 | class Solution { 36 | public: 37 | int maxArea(vector& height) { 38 | int i=0; int j=height.size()-1; 39 | int ans = 0; 40 | while(i < j){ 41 | ans = max(ans,min(height[i],height[j])*(j-i)); 42 | if(height[i]height[j])j--; 44 | else { 45 | i++; 46 | j--; 47 | } 48 | } 49 | return ans; 50 | } 51 | }; 52 | class Solution { 53 | public: 54 | int maxArea(vector& height) { 55 | int i=0; int j=height.size()-1; 56 | int ans = 0; 57 | while(i < j){ 58 | ans = max(ans,min(height[i],height[j])*(j-i)); 59 | if(height[i]height[j])j--; 61 | else { 62 | i++; 63 | j--; 64 | } 65 | } 66 | return ans; 67 | } 68 | }; 69 | -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/2ndOct_I-am-SS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define int long long 5 | #define MOD 1000000008-1 6 | 7 | 8 | void solve(){ 9 | int i; 10 | int n,m; cin>>n>>m; 11 | int a[n]; 12 | int b[m]; 13 | 14 | for (i=0;i>a[i];} 16 | for(i=0;i>b[i];} 18 | 19 | int max=1e9; 20 | mapmp; 21 | 22 | for(i=0;i=0;i--) { 41 | if(a[i]==2) left=i; 42 | if(left!=-1) { 43 | if(a[i]==0) 44 | mp[i] =min(mp[i],left-i); 45 | 46 | } 47 | } 48 | 49 | for(i=0;i>t; 65 | // t = 1; 66 | int _=1; 67 | while(t--){ 68 | // cout<<"Case #"<<_++<<": "; 69 | solve(); 70 | } 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /solve here/CodeChef/DAREA/2ndOct_I-am-SS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // commrnting in this line 3 | using namespace std; 4 | #define int long long 5 | #define MOD 1000000007 6 | 7 | 8 | void solve(){ 9 | int i; 10 | int n,m; cin>>n>>m; 11 | int a[n]; 12 | int b[m]; 13 | 14 | for (i=0;i>a[i];} 16 | for(i=0;i>b[i];} 18 | 19 | int max=1e9; 20 | mapmp; 21 | 22 | for(i=0;i=0;i--) { 41 | if(a[i]==2) left=i; 42 | if(left!=-1) { 43 | if(a[i]==0) 44 | mp[i] =min(mp[i],left-i); 45 | 46 | } 47 | } 48 | 49 | for(i=0;i>t; 65 | // t = 1; 66 | int _=1; 67 | while(t--){ 68 | // cout<<"Case #"<<_++<<": "; 69 | solve(); 70 | } 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /solve here/CodeChef/SHROUTE/2ndOct_I-am-SS.cpp: -------------------------------------------------------------------------------- 1 | // happy pijo vai 2 | #include 3 | 4 | using namespace std; 5 | #define int long long 6 | #define MOD 1000000007 7 | 8 | 9 | void solve(){ 10 | int i; 11 | int n,m; cin>>n>>m; 12 | int a[n]; 13 | int b[m]; 14 | 15 | for (i=0;i>a[i];} 17 | for(i=0;i>b[i];} 19 | 20 | int max=1e9; 21 | mapmp; 22 | 23 | for(i=0;i=0;i--) { 42 | if(a[i]==2) left=i; 43 | if(left!=-1) { 44 | if(a[i]==0) 45 | mp[i] =min(mp[i],left-i); 46 | 47 | } 48 | } 49 | 50 | for(i=0;i>t; 66 | // t = 1; 67 | int _=1; 68 | while(t--){ 69 | // cout<<"Case #"<<_++<<": "; 70 | solve(); 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competetive Pogramming 2 | 3 | Here you will find Data Structures Algorithms and CompetetiveProgramming Related Problems in Multiple Languages (C , C++ , Python etc) 4 | 5 | # HacktoberFest 2022 6 | ![image](https://user-images.githubusercontent.com/64575577/193452407-791d65ac-1b5c-4971-a09b-e155b57e3997.png) 7 | 8 | ## HacktoberFest - Contribute to Open Source 9 | 10 | * Hacktoberfest is DigitalOcean’s annual event that encourages people to contribute to open source throughout October. 11 | * Create your first Pull Request :fire: and help contributing to open-source and help everyone with this repository. 12 | * If you want to know more about Hacktober Fest visit here . 13 | 14 | ## Rule for Contrinution 15 | * Visit issues and select an issue to contribute 16 | * Go to folder specified in Issues 17 | * Give file name according to specified in Issue. (only files with proper filename will be accepted) 18 | * Then make yout Pull Request and it will be accepted soon 19 | 20 | ## :thumbsup: Awesome! How and What can I Contribute? 21 | It's very easy. Follow the below steps you need to create your -(maybe)- EXAMPLE first pull request. 22 | 23 | * Fork this repository by click the Fork button in the top right of this page or simply click here. 24 | * Visit issues and select any issue to contribute (details will be there). 25 | * Create a new file and add a new Program code(like C++ program to implement Binary Search, etc.) in any programming language like C++, Java, Python, etc. (Note: Program must not be exist already in this repository) 26 | * After adding the code, Commit your changes. 27 | * Create a new pull request from your forked repository (Button located at the top of your repo) 28 | * Star this repository! :blush: 29 | -------------------------------------------------------------------------------- /solve here/LeetCode/PalindromeNumber/thechangamunda.py: -------------------------------------------------------------------------------- 1 | """ 2 | Problem Statement: 3 | Given an integer x, return true if x is palindrome integer. 4 | An integer is a palindrome when it reads the same backward as forward. 5 | 6 | For example, 121 is a palindrome while 123 is not. 7 | 8 | Example 1: 9 | Input: x = 121 10 | Output: true 11 | Explanation: 121 reads as 121 from left to right and from right to left. 12 | 13 | Example 2: 14 | Input: x = -121 15 | Output: false 16 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 17 | 18 | Example 3: 19 | Input: x = 10 20 | Output: false 21 | Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 22 | 23 | Constraints: 24 | -231 <= x <= 231 - 1 25 | 26 | Follow up: Could you solve it without converting the integer to a string? 27 | """ 28 | '------------------------------------------------------------------------------------------------------------' 29 | "Solution:" 30 | 31 | n=int(input("Enter number:")) 32 | temp=n 33 | rev=0 34 | while(n>0): 35 | num=n%10 36 | rev=rev*10+num 37 | n=n//10 38 | if(temp==rev): 39 | print("The number is a palindrome.") 40 | else: 41 | print("The number isn't a palindrome.") 42 | 43 | """ 44 | Program Explanation 45 | 1. User must first enter the value of the integer and store it in a variable(n). 46 | 2. The value of the integer is then stored in another temporary variable(temp). 47 | 3. The while loop is used and the last digit of the number is obtained by using the modulus operator(num=n%10). 48 | 4. The last digit is then stored at the ones place, second last at the tens place and so on. 49 | 5. The last digit is then removed by truly dividing the number with 10. 50 | 6. This loop terminates when the value of the number is 0. 51 | 7. The reverse of the number is then compared with the integer value stored in the temporary variable 52 | (if(temp==rev)). 53 | 8. If both are equal, the number is a palindrome. 54 | 9. If both arent equal, the number isnt a palindrome. 55 | 10. The final result is then printed. 56 | 57 | """ 58 | -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/wsbmun.java: -------------------------------------------------------------------------------- 1 | public class FourSum { 2 | 3 | private static List> fourSum(int[] nums, int target) { 4 | // Resultant list 5 | List> quadruplets = new ArrayList<>(); 6 | // Base condition 7 | if (nums == null || nums.length < 4) { 8 | return quadruplets; 9 | } 10 | // Sort the array 11 | Arrays.sort(nums); 12 | // Length of the array 13 | int n = nums.length; 14 | // Loop for each element in the array 15 | for (int i = 0; i < n - 3; i++) { 16 | // Check for skipping duplicates 17 | if (i > 0 && nums[i] == nums[i - 1]) { 18 | continue; 19 | } 20 | // Reducing problem to 3Sum problem 21 | for (int j = i + 1; j < n - 2; j++) { 22 | // Check for skipping duplicates 23 | if (j != i + 1 && nums[j] == nums[j - 1]) { 24 | continue; 25 | } 26 | // Left and right pointers 27 | int k = j + 1; 28 | int l = n - 1; 29 | // Reducing to two sum problem 30 | while (k < l) { 31 | int currentSum = nums[i] + nums[j] + nums[k] + nums[l]; 32 | if (currentSum < target) { 33 | k++; 34 | } else if (currentSum > target) { 35 | l--; 36 | } else { 37 | quadruplets.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); 38 | k++; 39 | l--; 40 | // Check for skipping duplicates 41 | while (k < l && nums[k] == nums[k - 1]) { 42 | k++; 43 | } 44 | while (k < l && nums[l] == nums[l + 1]) { 45 | l--; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | return quadruplets; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/I-am-SS.java: -------------------------------------------------------------------------------- 1 | public class FourSum { 2 | 3 | private static List> fourSum(int[] nums, int target) { 4 | // Resultant list 5 | List> quadruplets = new ArrayList<>(); 6 | // Base condition 7 | if (nums == null || nums.length < 4) { 8 | return quadruplets; 9 | } 10 | // Sort the array 11 | Arrays.sort(nums); 12 | // Length of the array 13 | int n = nums.length; 14 | // Loop for each element in the array 15 | for (int i = 0; i < n - 3; i++) { 16 | // Check for skipping duplicates 17 | if (i > 0 && nums[i] == nums[i - 1]) { 18 | continue; 19 | } 20 | // Reducing problem to 3Sum problem 21 | for (int j = i + 1; j < n - 2; j++) { 22 | // Check for skipping duplicates 23 | if (j != i + 1 && nums[j] == nums[j - 1]) { 24 | continue; 25 | } 26 | // Left and right pointers 27 | int k = j + 1; 28 | int l = n - 1; 29 | // Reducing to two sum problem 30 | while (k < l) { 31 | int currentSum = nums[i] + nums[j] + nums[k] + nums[l]; 32 | if (currentSum < target) { 33 | k++; 34 | } else if (currentSum > target) { 35 | l--; 36 | } else { 37 | quadruplets.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); 38 | k++; 39 | l--; 40 | // Check for skipping duplicates 41 | while (k < l && nums[k] == nums[k - 1]) { 42 | k++; 43 | } 44 | while (k < l && nums[l] == nums[l + 1]) { 45 | l--; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | return quadruplets; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /solve here/CodeChef/DAREA/sayak01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | 5 | void solve() 6 | { 7 | 8 | } 9 | 10 | int main() 11 | { 12 | // your code goes here 13 | ll t; 14 | cin >> t; 15 | for (;t>0;t--){ 16 | ll n; 17 | cin >> n; 18 | vector> x; 19 | vector> y; 20 | multiset X; 21 | multiset Y; 22 | for (ll i = 0; i < n; ++i){ 23 | ll a, b; 24 | cin >> a >> b; 25 | x.push_back({a, b}); 26 | y.push_back({b, a}); 27 | X.insert(a); 28 | Y.insert(b); 29 | } 30 | sort(x.begin(), x.end()); 31 | sort(y.begin(), y.end()); 32 | ll height1 = 0; 33 | ll height2 = 0; 34 | ll h1Max = 0; 35 | ll h1Min = LONG_MAX; 36 | ll area = LONG_MAX; 37 | for (ll i = 0; i < n - 1; ++i){ 38 | h1Max = max(h1Max, x[i].second); 39 | h1Min = min(h1Min, x[i].second); 40 | height1 = h1Max - h1Min; 41 | auto it = Y.find(x[i].second); 42 | Y.erase(it); 43 | height2 = *Y.rbegin() - *Y.begin(); 44 | ll newArea = (x[i].first - x[0].first) * height1 + 45 | (x[n - 1].first - x[i + 1].first) * height2; 46 | area = min(area, newArea); 47 | } 48 | ll width1 = 0; 49 | ll width2 = 0; 50 | ll w1Max = 0; 51 | ll w1Min = LONG_MAX; 52 | for (ll i = 0; i < n - 1; ++i){ 53 | w1Max = max(w1Max, y[i].second); 54 | w1Min = min(w1Min, y[i].second); 55 | width1 = w1Max - w1Min; 56 | auto it = X.find(y[i].second); 57 | X.erase(it); 58 | width2 = *X.rbegin() - *X.begin(); 59 | ll newArea = (y[i].first - y[0].first) * width1 + (y[n - 1].first - y[i + 1].first) * width2; 60 | area = min(area, newArea); 61 | } 62 | if (area == LONG_MAX) 63 | area = 0; 64 | cout << area << endl; 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /solve here/CodeChef/DAREA/readme.md: -------------------------------------------------------------------------------- 1 | # Minimum Dual Area 2 | Problem Code: DAREA 3 | ## Problem Link: https://www.codechef.com/JUNE21B/problems/DAREA 4 | ## What to Do: 5 | - Go to folder: "./CP/CodeChef/DAREA" 6 | - Take your GitHUB username id like "hrithik339", "hacker-boy", etc or anything which you have. 7 | - File extension = ".cpp" in this issue. 8 | ## Problem: 9 | Given N points in a 2D space, find the minimum sum of areas of rectangles required to cover all the points given that we can use at most 2 non-overlapping rectangles whose sides can touch. The rectangles must be axis-aligned, meaning the sides are vertical and horizontal. 10 | 11 | ## Input: 12 | - The first line contains an integer T, the number of test cases. Then the test cases follow. 13 | - Each test case contains N+1 lines of input. 14 | - The first line contains a single integer N, the number of points. 15 | - The next N lines each contain two integers xi, yi, the coordinates of the i-th point. 16 | #### Note: Points need not be distinct. 17 | 18 | ## Output 19 | - For each test case, output in a single line the answer to the problem. 20 | 21 | ## Constraints 22 | - 1≤T≤105 23 | - 1≤N≤105 24 | - 0≤xi,yi≤109 25 | - The sum of N over all test cases is at most 105. 26 | 27 | ## Sample Input 1 : 28 | ``` 29 | 3 30 | 1 31 | 100 100 32 | 4 33 | 1 100 34 | 100 100 35 | 100 1 36 | 1 1 37 | 5 38 | 1 100 39 | 100 100 40 | 100 1 41 | 1 1 42 | 50 50 43 | ``` 44 | ## Sample Output 1 : 45 | ``` 46 | 0 47 | 0 48 | 4851 49 | ``` 50 | 51 | ## Explanation : 52 | - Case 1: Since there is only one point, the minimum area of a rectangle to cover this point is 0. 53 | - Case 2: We can have rectangles as 2 opposite sides of the square. Since the width of the rectangles is 0, the total area is also 0. 54 | ![image](https://user-images.githubusercontent.com/64575577/135620742-930afa64-bd0f-470e-8f7d-1739b29d51c9.png) 55 | - Case 3: The optimal solution is with the rectangles having endpoints {(1,1),(100,1),(1,50),(100,50)} and {(1,100),(1,100),(100,100),(100,100)}. Therefore the total area is 49⋅99+0⋅99=4851 56 | ![image](https://user-images.githubusercontent.com/64575577/135620800-176b5941-b4ad-4dc8-91c5-b59bed4df2f1.png) -------------------------------------------------------------------------------- /solve here/CodeChef/BITTUP/ss again.cpp: -------------------------------------------------------------------------------- 1 | // https://github.com/sayak2k1maruti/competetivePogramming/ 2 | 3 | #include 4 | using namespace std; 5 | #define ll long long int 6 | 7 | void solve() 8 | { 9 | 10 | } 11 | 12 | int main() 13 | { 14 | // your code goes here 15 | ll t; 16 | cin >> t; 17 | for (;t>0;t--){ 18 | ll n; 19 | cin >> n; 20 | vector> x; 21 | vector> y; 22 | multiset X; 23 | multiset Y; 24 | for (ll i = 0; i < n; ++i){ 25 | ll a, b; 26 | cin >> a >> b; 27 | x.push_back({a, b}); 28 | y.push_back({b, a}); 29 | X.insert(a); 30 | Y.insert(b); 31 | } 32 | sort(x.begin(), x.end()); 33 | sort(y.begin(), y.end()); 34 | ll height1 = 0; 35 | ll height2 = 0; 36 | ll h1Max = 0; 37 | ll h1Min = LONG_MAX; 38 | ll area = LONG_MAX; 39 | for (ll i = 0; i < n - 1; ++i){ 40 | h1Max = max(h1Max, x[i].second); 41 | h1Min = min(h1Min, x[i].second); 42 | height1 = h1Max - h1Min; 43 | auto it = Y.find(x[i].second); 44 | Y.erase(it); 45 | height2 = *Y.rbegin() - *Y.begin(); 46 | ll newArea = (x[i].first - x[0].first) * height1 + 47 | (x[n - 1].first - x[i + 1].first) * height2; 48 | area = min(area, newArea); 49 | } 50 | ll width1 = 0; 51 | ll width2 = 0; 52 | ll w1Max = 0; 53 | ll w1Min = LONG_MAX; 54 | for (ll i = 0; i < n - 1; ++i){ 55 | w1Max = max(w1Max, y[i].second); 56 | w1Min = min(w1Min, y[i].second); 57 | width1 = w1Max - w1Min; 58 | auto it = X.find(y[i].second); 59 | X.erase(it); 60 | width2 = *X.rbegin() - *X.begin(); 61 | ll newArea = (y[i].first - y[0].first) * width1 + (y[n - 1].first - y[i + 1].first) * width2; 62 | area = min(area, newArea); 63 | } 64 | if (area == LONG_MAX) 65 | area = 0; 66 | cout << area << endl; 67 | } 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /solve here/CodeChef/SHROUTE/README.md: -------------------------------------------------------------------------------- 1 | # Shortest Route 2 | Problem Code: SHROUTE 3 | ## Problem Link: https://www.codechef.com/JUNE21B/problems/SHROUTE 4 | ## What to Do: 5 | - Go to folder: ".\CP\CodeChef\SHROUTE" 6 | - Take your GitHUB username id like "hrithik339", "hacker-boy", etc or anything which you have. 7 | ## Problem: 8 | There are N cities in Chefland numbered from 1 to N and every city has a railway station. Some cities have a train and each city has at most one train originating from it. The trains are represented by an array A, where Ai=0 means the i-th city doesn't have any train originating from it, Ai=1 means the train originating from the i-th city is moving right (to a higher numbered city), and Ai=2 means the train originating from the i-th city is moving left (to a lower numbered city). 9 | 10 | Each train keeps on going forever in its direction and takes 1 minute to travel from the current station to the next one. There is a special station at city 1 which lets travellers instantaneously teleport to any other station that currently has a train. Teleportation and getting on a train once in the city both take 0 minutes and all trains start at time 0. 11 | 12 | There are M travellers at city 1, and the i-th traveller has destination city Bi. They ask Chef to guide them to teleport to a particular station from which they can catch a train to go to their destination in the least time possible. In case it's not possible for a person to reach his destination, print −1. 13 | 14 | #### Note: The input and output of this problem are large, so prefer using fast input/output methods. 15 | 16 | ## Input: 17 | - The first line contains an integer T, the number of test cases. Then the test cases follow. 18 | - Each test case contains three lines of input. 19 | - The first line contains two integers N, M 20 | - The second line contains N integers A1,A2,…,AN. 21 | - The third line contains M integers B1,B2,…,BM 22 | 23 | 24 | ## Output 25 | - For each test case, output M space-separated integers C1,C2,…,CM, where Ci is the minimum time required by the i-th traveller to reach his destination and if the i-th traveller can't reach his destination, Ci=−1. 26 | 27 | ## Constraints 28 | - 1≤N,M≤10^5 29 | - 0≤Ai≤2 30 | - 1≤Bi≤N 31 | - The sum of N over all test cases is at most 10^6 32 | - The sum of M over all test cases is at most 10^6 33 | 34 | ## Sample Input 1 : 35 | ``` 36 | 3 37 | 5 1 38 | 1 0 0 0 0 39 | 5 40 | 5 1 41 | 1 0 0 0 2 42 | 4 43 | 5 2 44 | 2 0 0 0 1 45 | 3 1 46 | 47 | ``` 48 | ## Sample Output 1 : 49 | ``` 50 | 4 51 | 1 52 | -1 0 53 | 54 | ``` 55 | 56 | ## Explanation : 57 | - Case 1: The only person takes the train from station 1 and hence takes |5−1|=4 minutes to reach his destination. 58 | - Case 2: The only person takes the train from station 5 and hence takes |5−4|=1 minute to reach his destination. 59 | - Case 3: Since no train passes station 3, it's impossible for the first person to reach his destination and since the second person is already at station 1, it takes him 0 minutes to reach his destination. 60 | -------------------------------------------------------------------------------- /solve here/CodeChef/DAREA/soln of min dual area.py: -------------------------------------------------------------------------------- 1 | def area(rectangle): 2 | [A,B] = rectangle 3 | return abs(B[0] - A[0]) * abs(B[1] - A[1]) 4 | def solve(points): 5 | if len(points)<=2: 6 | return 0 7 | points_x = sorted(points, key = lambda x: x[0]) 8 | ymx = {} 9 | for p in points_x: 10 | if p[0] not in ymx: 11 | ymx[p[0]] = [p[1], p[1]] 12 | continue 13 | if p[1] < ymx[p[0]][0]: 14 | ymx[p[0]][0] = p[1] 15 | elif p[1] > ymx[p[0]][1]: 16 | ymx[p[0]][1] = p[1] 17 | 18 | y_right = {points_x[-1][0]: ymx[points_x[-1][0]]} 19 | for i in range(N-2,-1, -1): 20 | y_right[points_x[i][0]] = [min(y_right[points_x[i+1][0]][0], ymx[points_x[i][0]][0]), 21 | max(y_right[points_x[i+1][0]][1], ymx[points_x[i][0]][1])] 22 | r1_x = [points_x[0][0],points_x[0][0]] 23 | r2_x = [points_x[-1][0], points_x[-1][0]] 24 | r1_y = [ymx[points_x[0][0]][0],ymx[points_x[0][0]][1]] 25 | r2_y = [y_right[points_x[-1][0]][0],y_right[points_x[-1][0]][1]] 26 | area_max = float('inf') 27 | for i in range(N-1): 28 | r1_x[1] = points_x[i][0] 29 | r2_x[0] = points_x[i+1][0] 30 | if r1_y[0] > ymx[points_x[i][0]][0]: 31 | r1_y[0] = ymx[points_x[i][0]][0] 32 | if r1_y[1] < ymx[points_x[i][0]][1]: 33 | r1_y[1] = ymx[points_x[i][0]][1] 34 | r2_y = y_right[points_x[i+1][0]] 35 | new_area = area([[r1_x[0],r1_y[0]],[r1_x[1],r1_y[1]]]) + area([[r2_x[0],r2_y[0]],[r2_x[1],r2_y[1]]]) 36 | if new_area < area_max: 37 | area_max = new_area 38 | points = [[y,x] for [x,y] in points] 39 | points_x = sorted(points, key = lambda x: x[0]) 40 | ymx = {} 41 | for p in points_x: 42 | if p[0] not in ymx: 43 | ymx[p[0]] = [p[1], p[1]] 44 | continue 45 | if p[1] < ymx[p[0]][0]: 46 | ymx[p[0]][0] = p[1] 47 | elif p[1] > ymx[p[0]][1]: 48 | ymx[p[0]][1] = p[1] 49 | 50 | y_right = {points_x[-1][0]: ymx[points_x[-1][0]]} 51 | for i in range(N-2,-1, -1): 52 | y_right[points_x[i][0]] = [min(y_right[points_x[i+1][0]][0], ymx[points_x[i][0]][0]), 53 | max(y_right[points_x[i+1][0]][1], ymx[points_x[i][0]][1])] 54 | r1_x = [points_x[0][0],points_x[0][0]] 55 | r2_x = [points_x[-1][0], points_x[-1][0]] 56 | r1_y = [ymx[points_x[0][0]][0],ymx[points_x[0][0]][1]] 57 | r2_y = [y_right[points_x[-1][0]][0],y_right[points_x[-1][0]][1]] 58 | full_rect = [[points_x[0][0], y_right[points[0][0]][0]], [points_x[-1][0],y_right[points[0][0]][1]]] 59 | for i in range(N-1): 60 | r1_x[1] = points_x[i][0] 61 | r2_x[0] = points_x[i+1][0] 62 | if r1_y[0] > ymx[points_x[i][0]][0]: 63 | r1_y[0] = ymx[points_x[i][0]][0] 64 | if r1_y[1] < ymx[points_x[i][0]][1]: 65 | r1_y[1] = ymx[points_x[i][0]][1] 66 | r2_y = y_right[points_x[i+1][0]] 67 | new_area = area([[r1_x[0],r1_y[0]],[r1_x[1],r1_y[1]]]) + area([[r2_x[0],r2_y[0]],[r2_x[1],r2_y[1]]]) 68 | if new_area < area_max: 69 | area_max = new_area 70 | return area_max 71 | for _ in range(int(input())): 72 | N = int(input()) 73 | points = [[int(x) for x in input().split()] for i in range(N)] 74 | print(solve(points)) 75 | -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/MehakGupta1103.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | 5 | vector> ans; 6 | 7 | //if all are greater than 0 8 | int f1 = 1, f2 = 1, f3 = 1; 9 | int ze = 0; 10 | int n = nums.size(); 11 | //f1 --> all positive 12 | //f2 all negetive 13 | for(int i = 0; i < n; i++){ 14 | 15 | if(nums[i]==0) ze++; 16 | 17 | if(nums[i] < 0){ 18 | f2 = 0; 19 | f3 = 0; 20 | } 21 | if(nums[i] > 0){ 22 | f1 = 0; 23 | f3 = 0; 24 | } 25 | } 26 | 27 | 28 | if(f3){ 29 | cout << "chala" << endl; 30 | vector temp; 31 | for(int i = 0; i < 3; i++){ 32 | temp.push_back(nums[i]); 33 | } 34 | ans.push_back(temp); 35 | return ans; 36 | } 37 | if(f1) return ans; 38 | if(f2) return ans; 39 | 40 | map m; 41 | for(int i = 0; i < n; i++){ 42 | m[nums[i]]++; 43 | } 44 | 45 | vector odd; 46 | 47 | vector even; 48 | 49 | vector temp; 50 | 51 | set> s; 52 | 53 | int flag = 0; 54 | for(int i = 0; i < n; i++){ 55 | if(nums[i]<0) odd.push_back(nums[i]); 56 | else if(nums[i] != 0) even.push_back(nums[i]); 57 | else flag = 1; 58 | } 59 | 60 | sort(odd.begin(), odd.end()); 61 | sort(even.begin(), even.end()); 62 | 63 | //case 1 -- (o, o, e) where o+o = e 64 | int o = odd.size(); 65 | for(int i = 0; i < o; i++){ 66 | for(int j = i+1; j < o; j++){ 67 | int l = odd[i]+odd[j]; 68 | if(m[(-1)*l]>0){ 69 | temp.push_back(odd[i]); 70 | temp.push_back(odd[j]); 71 | temp.push_back(l*(-1)); 72 | } 73 | if(!temp.empty()) s.insert(temp); 74 | temp.clear(); 75 | } 76 | } 77 | 78 | //case 2 -- (e, e, 0) where e+e = o 79 | int e = even.size(); 80 | for(int i = 0; i < e; i++){ 81 | for(int j = i+1; j < e; j++){ 82 | int l = even[i]+even[j]; 83 | if(m[(-1)*l]>0){ 84 | temp.push_back(even[i]); 85 | temp.push_back(even[j]); 86 | temp.push_back(l*(-1)); 87 | } 88 | if(!temp.empty()) s.insert(temp); 89 | temp.clear(); 90 | } 91 | } 92 | 93 | //case 3 --> zero exist (o, 0, e) 94 | if(ze){ 95 | for(int i = 0; i < o; i++){ 96 | if(m[(-1)*odd[i]] > 0){ 97 | temp.push_back(odd[i]); 98 | temp.push_back(0); 99 | temp.push_back((-1)*odd[i]); 100 | if(!temp.empty()) s.insert(temp); 101 | temp.clear(); 102 | } 103 | } 104 | } 105 | 106 | 107 | cout << "all the tuples" << endl; 108 | for(auto &it: s){ 109 | // cout << it << endl;\ 110 | for(auto &p: it) cout << p << " "; 111 | cout << endl; 112 | ans.push_back(it); 113 | } 114 | temp.clear(); 115 | if(ze>=3){ 116 | temp.push_back(0); 117 | temp.push_back(0); 118 | temp.push_back(0); 119 | ans.push_back(temp); 120 | } 121 | return ans; 122 | } 123 | }; -------------------------------------------------------------------------------- /solve here/LeetCode/3SUM/aluvaja.java: -------------------------------------------------------------------------------- 1 | public class FourSum { 2 | 3 | private static List> fourSum(int[] nums, int target) { 4 | // Resultant list 5 | List> quadruplets = new ArrayList<>(); 6 | // Base condition 7 | if (nums == null || nums.length < 4) { 8 | return quadruplets; 9 | } 10 | // Sort the array 11 | //solved using java 12 | Arrays.sort(nums); 13 | // Length of the array 14 | int n = nums.length; 15 | // Loop for each element in the array 16 | for (int i = 0; i < n - 3; i++) { 17 | // Check for skipping duplicates 18 | if (i > 0 && nums[i] == nums[i - 1]) { 19 | continue; 20 | } 21 | // Reducing problem to 3Sum problem 22 | for (int j = i + 1; j < n - 2; j++) { 23 | // Check for skipping duplicates 24 | if (j != i + 1 && nums[j] == nums[j - 1]) { 25 | continue; 26 | } 27 | // Left and right pointers 28 | int k = j + 1; 29 | int l = n - 1; 30 | // Reducing to two sum problem 31 | while (k < l) { 32 | int currentSum = nums[i] + nums[j] + nums[k] + nums[l]; 33 | if (currentSum < target) { 34 | k++; 35 | } else if (currentSum > target) { 36 | l--; 37 | } else { 38 | quadruplets.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); 39 | k++; 40 | l--; 41 | // Check for skipping duplicates 42 | while (k < l && nums[k] == nums[k - 1]) { 43 | k++; 44 | } 45 | while (k < l && nums[l] == nums[l + 1]) { 46 | l--; 47 | } 48 | } 49 | } 50 | } 51 | } 52 | return quadruplets; 53 | } 54 | } 55 | 56 | public class FourSum { 57 | 58 | private static List> fourSum(int[] nums, int target) { 59 | // Resultant list 60 | List> quadruplets = new ArrayList<>(); 61 | // Base condition 62 | if (nums == null || nums.length < 4) { 63 | return quadruplets; 64 | } 65 | // Sort the array 66 | Arrays.sort(nums); 67 | // Length of the array 68 | int n = nums.length; 69 | // Loop for each element in the array 70 | for (int i = 0; i < n - 3; i++) { 71 | // Check for skipping duplicates 72 | if (i > 0 && nums[i] == nums[i - 1]) { 73 | continue; 74 | } 75 | // Reducing problem to 3Sum problem 76 | for (int j = i + 1; j < n - 2; j++) { 77 | // Check for skipping duplicates 78 | if (j != i + 1 && nums[j] == nums[j - 1]) { 79 | continue; 80 | } 81 | // Left and right pointers 82 | int k = j + 1; 83 | int l = n - 1; 84 | // Reducing to two sum problem 85 | while (k < l) { 86 | int currentSum = nums[i] + nums[j] + nums[k] + nums[l]; 87 | if (currentSum < target) { 88 | k++; 89 | } else if (currentSum > target) { 90 | l--; 91 | } else { 92 | quadruplets.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); 93 | k++; 94 | l--; 95 | // Check for skipping duplicates 96 | while (k < l && nums[k] == nums[k - 1]) { 97 | k++; 98 | } 99 | while (k < l && nums[l] == nums[l + 1]) { 100 | l--; 101 | } 102 | } 103 | } 104 | } 105 | } 106 | return quadruplets; 107 | } 108 | } 109 | public class FourSum { 110 | 111 | private static List> fourSum(int[] nums, int target) { 112 | // Resultant list 113 | List> quadruplets = new ArrayList<>(); 114 | // Base condition 115 | if (nums == null || nums.length < 4) { 116 | return quadruplets; 117 | } 118 | // Sort the array 119 | Arrays.sort(nums); 120 | // Length of the array 121 | int n = nums.length; 122 | // Loop for each element in the array 123 | for (int i = 0; i < n - 3; i++) { 124 | // Check for skipping duplicates 125 | if (i > 0 && nums[i] == nums[i - 1]) { 126 | continue; 127 | } 128 | // Reducing problem to 3Sum problem 129 | for (int j = i + 1; j < n - 2; j++) { 130 | // Check for skipping duplicates 131 | if (j != i + 1 && nums[j] == nums[j - 1]) { 132 | continue; 133 | } 134 | // Left and right pointers 135 | int k = j + 1; 136 | int l = n - 1; 137 | // Reducing to two sum problem 138 | while (k < l) { 139 | int currentSum = nums[i] + nums[j] + nums[k] + nums[l]; 140 | if (currentSum < target) { 141 | k++; 142 | } else if (currentSum > target) { 143 | l--; 144 | } else { 145 | quadruplets.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); 146 | k++; 147 | l--; 148 | // Check for skipping duplicates 149 | while (k < l && nums[k] == nums[k - 1]) { 150 | k++; 151 | } 152 | while (k < l && nums[l] == nums[l + 1]) { 153 | l--; 154 | } 155 | } 156 | } 157 | } 158 | } 159 | return quadruplets; 160 | } 161 | } 162 | --------------------------------------------------------------------------------