diff --git a/.cph/.arrayrecovery.cpp_7b3b9871d14eed2f4ce9e9619c6c4e79.prob b/.cph/.arrayrecovery.cpp_7b3b9871d14eed2f4ce9e9619c6c4e79.prob new file mode 100644 index 0000000..f8f73be --- /dev/null +++ b/.cph/.arrayrecovery.cpp_7b3b9871d14eed2f4ce9e9619c6c4e79.prob @@ -0,0 +1 @@ +{"name":"Local: arrayrecovery","url":"c:\\Users\\CC\\Desktop\\git-anurag\\git-akpro\\leetcode-solutions-1\\arrayrecovery.cpp","tests":[{"id":1666365548858,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\CC\\Desktop\\git-anurag\\git-akpro\\leetcode-solutions-1\\arrayrecovery.cpp","group":"local","local":true} \ No newline at end of file diff --git a/1-Two Sum b/1-Two Sum new file mode 100644 index 0000000..286b5d2 --- /dev/null +++ b/1-Two Sum @@ -0,0 +1,37 @@ +//Question- 1 +Link: https://leetcode.com/problems/two-sum/ +class Solution { + public int[] twoSum(int[] nums, int target) { + int c=0; + int[] b; + b= new int [2]; + for(int i=0;i hm = new HashMap<>(); + for(int i=0;i hm = new HashMap<>(); + for(int we = 0 ; we < nums.length ; we++){ + // if(nums[we] == 0) count++; + if(nums[we]==0) hm.put(nums[we],hm.getOrDefault(nums[we],0)+1); + while(nums[we]==0 && hm.get(nums[we]) > k){ + if(nums[ws] == 0){ + hm.put(nums[ws],hm.get(nums[ws])-1); + } + ws++; + } + max=Math.max(max,we-ws+1); + } + return max; + } +} diff --git a/112. Path sum b/112. Path sum index 958939c..a0068df 100644 --- a/112. Path sum +++ b/112. Path sum @@ -10,10 +10,10 @@ if(root == null) return false; currSum += root.val; if(targetSum == currSum) { return true; - } + } else{ return false; - } + } } diff --git a/1206. Design Skiplist (Hard) b/1206. Design Skiplist (Hard) new file mode 100644 index 0000000..61ad7dd --- /dev/null +++ b/1206. Design Skiplist (Hard) @@ -0,0 +1,39 @@ +class Skiplist { +public: + + maps; + + Skiplist() { + + } + + bool search(int target) { + if(s.find(target)!=s.end()) + return true; + else + return false; + } + + void add(int num) { + s[num]++; + } + + bool erase(int num) { + if(s.find(num)==s.end()) + return false; + else{ + s[num]--; + if(s[num]==0) + s.erase(num); + return true; + } + } +}; + +/** + * Your Skiplist object will be instantiated and called as such: + * Skiplist* obj = new Skiplist(); + * bool param_1 = obj->search(target); + * obj->add(num); + * bool param_3 = obj->erase(num); + */ diff --git a/1372-RichestCustomerWealth.py b/1372-RichestCustomerWealth.py new file mode 100644 index 0000000..3df8c13 --- /dev/null +++ b/1372-RichestCustomerWealth.py @@ -0,0 +1,13 @@ +class Solution: + def maximumWealth(self, accounts: List[List[int]]) -> int: + maxSum = 0 + for row in range(len(accounts)): + runningSum = 0 + for col in range(len(accounts[row])): + runningSum =runningSum+ accounts[row][col] + + if runningSum>maxSum: + maxSum = runningSum + return(maxSum) + + \ No newline at end of file diff --git a/146. LRU Cache/LRU_Cache.cpp b/146. LRU Cache/LRU_Cache.cpp new file mode 100644 index 0000000..4165420 --- /dev/null +++ b/146. LRU Cache/LRU_Cache.cpp @@ -0,0 +1,51 @@ +// Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. + +struct Node { + int key; + int value; + Node(int key, int value) : key(key), value(value) {} +}; + + +class LRUCache { + public: + LRUCache(int capacity) : capacity(capacity) {} + + int get(int key) { + if (!keyToIterator.count(key)) + return -1; + + const auto& it = keyToIterator[key]; + // move it to the front + cache.splice(begin(cache), cache, it); + return it->value; + } + + void put(int key, int value) { + // no capacity issue, just update the value + if (keyToIterator.count(key)) { + const auto& it = keyToIterator[key]; + // move it to the front + cache.splice(begin(cache), cache, it); + it->value = value; + return; + } + + // check the capacity + if (cache.size() == capacity) { + const auto& lastNode = cache.back(); + // that's why we store `key` in `Node` + keyToIterator.erase(lastNode.key); + cache.pop_back(); + } + + cache.emplace_front(key, value); + keyToIterator[key] = begin(cache); + } + + private: + const int capacity; + list cache; + unordered_map::iterator> keyToIterator; +}; + diff --git a/16. 3Sum Closest b/16. 3Sum Closest new file mode 100644 index 0000000..a8f9d1e --- /dev/null +++ b/16. 3Sum Closest @@ -0,0 +1,49 @@ +class Solution { + public int threeSumClosest(int[] nums, int target) { + int n = nums.length; + int res = 0; + int m = Integer.MAX_VALUE; + Arrays.sort(nums); + for (int i = 0; i < n - 2; i++){ + if (i > 0 && nums[i-1] == nums[i]){ + continue; + } + if (nums[i] * 3 >= target){ + int s = nums[i] + nums[i+1] + nums[i+2]; + if (s - target < m){ + return s; + } + break; + } + int t = nums[i] + nums[n-1] + nums[n-2]; + if (t == target){ + return t; + } + if (t < target){ + if (t - target < m){ + m = target - t; + res = t; + } + continue; + } + int a = i + 1; + int b = n - 1; + while (a < b){ + t = nums[i] + nums[a] + nums[b]; + if (t == target){ + return t; + } + if (Math.abs(t - target) < m){ + res = t; + m = Math.abs(t - target); + } + if (t > target){ + b--; + } else { + a++; + } + } + } + return res; + } +} diff --git a/160-intersection-of-two-linked-lists.cpp b/160-intersection-of-two-linked-lists.cpp new file mode 100644 index 0000000..5252b66 --- /dev/null +++ b/160-intersection-of-two-linked-lists.cpp @@ -0,0 +1,26 @@ +// Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. +// If the two linked lists have no intersection at all, return null. + +// Time: O(m + n) +// Space: O(1) + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ + +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode *curA = headA, *curB = headB; + while (curA != curB) { + curA = curA ? curA->next : headB; + curB = curB ? curB->next : headA; + } + return curA; + } +}; diff --git a/1672-richestcustomerwealth.java b/1672-richestcustomerwealth.java new file mode 100644 index 0000000..3706b0e --- /dev/null +++ b/1672-richestcustomerwealth.java @@ -0,0 +1,20 @@ +class Solution { + public int maximumWealth(int[][] acc) { + int max=0; + for (int i=0;imax) + { + max=sum; + } + } + + return max; + } +} diff --git a/20-Valid Parentheses b/20-Valid Parentheses new file mode 100644 index 0000000..3e234bb --- /dev/null +++ b/20-Valid Parentheses @@ -0,0 +1,51 @@ +//https://leetcode.com/problems/valid-parentheses/ +//0ms +class Solution { + public boolean isValid(String s) { + if(s.length()==1) return false; + Stack stack = new Stack<>(); + for(int i = 0;i < s.length();i++){ + char ch=s.charAt(i); + if(ch=='(' || ch=='{' || ch=='['){ + stack.push(ch); + } + if(ch==')'){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='('){ + stack.pop(); + } + else { + return false; + } + } + if(ch==']' ){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='['){ + stack.pop(); + } + else{ + return false; + } + } + if(ch=='}'){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='{'){ + stack.pop(); + } + else{ + return false; + } + } + } + if(stack.isEmpty()){ + return true; + } + + } +} diff --git a/204_count_primes.java b/204_count_primes.java new file mode 100644 index 0000000..4f557b7 --- /dev/null +++ b/204_count_primes.java @@ -0,0 +1,23 @@ +class Solution{ +public int countPrimes(int n) { + if(n <=1 ) return 0; + + boolean[] notPrime = new boolean[n]; + notPrime[0] = true; + notPrime[1] = true; + + for(int i = 2; i < Math.sqrt(n); i++){ + if(!notPrime[i]){ + for(int j = 2; j*i < n; j++){ + notPrime[i*j] = true; + } + } + } + + int count = 0; + for(int i = 2; i< notPrime.length; i++){ + if(!notPrime[i]) count++; + } + return count; +} +} diff --git a/25. Reverse Nodes in k-Group.cpp b/25. Reverse Nodes in k-Group.cpp new file mode 100644 index 0000000..6246b33 --- /dev/null +++ b/25. Reverse Nodes in k-Group.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + void reverse(ListNode *start, ListNode *end) + { + ListNode *prev = NULL; + ListNode *curr = start; + ListNode *nexty = start->next; + + while(prev!=end) + { + curr->next = prev; + prev = curr; + curr = nexty; + + if(nexty!=NULL) + { + nexty = nexty->next; + } + } + + } + + ListNode* reverseKGroup(ListNode* head, int k) { + + if(head==NULL || head->next == NULL || k==1) + return head; + + ListNode *dummy = new ListNode(-1); + dummy->next = head; + ListNode *beforeStart = dummy, *end = head; + int i=0; + while(end!=NULL) + { + i++; + if(i%k==0) + { + //reversal + ListNode *start = beforeStart->next, *temp = end->next; + reverse(start,end); + beforeStart->next = end; + start->next = temp; + beforeStart = start; + end = temp; + } + else + { + end = end->next; + } + + } + return dummy->next; + } +}; diff --git a/268.Missing_Number.java b/268.Missing_Number.java index 14035b8..9f0428e 100644 --- a/268.Missing_Number.java +++ b/268.Missing_Number.java @@ -3,7 +3,7 @@ public int missingNumber(int[] nums) { int n = nums.length; int sum = (n * (n+1)) / 2; for(int i=0; i int: + l=0 + res=0 + charset=set() + for r in range(len(s)): + while(s[r] in charset): + charset.remove(s[l]) + l+=1 + charset.add(s[r]) + res=max(res,r-l+1) + return (res) diff --git a/334. Increasing Triplet Subsequence b/334. Increasing Triplet Subsequence new file mode 100644 index 0000000..34e3d44 --- /dev/null +++ b/334. Increasing Triplet Subsequence @@ -0,0 +1,14 @@ +class Solution: + def increasingTriplet(self, nums: List[int]) -> bool: + first = math.inf + second = math.inf + + for num in nums: + if num <= first: + first = num + elif num <= second: # First < num <= second + second = num + else: + return True # First < second < num (third) + + return False diff --git a/34. Find First and Last Position of Element in Sorted Array.cpp b/34. Find First and Last Position of Element in Sorted Array.cpp new file mode 100644 index 0000000..6449954 --- /dev/null +++ b/34. Find First and Last Position of Element in Sorted Array.cpp @@ -0,0 +1,10 @@ +class Solution { + public: + vector searchRange(vector& nums, int target) { + const int l = lower_bound(begin(nums), end(nums), target) - begin(nums); + if (l == nums.size() || nums[l] != target) + return {-1, -1}; + const int r = upper_bound(begin(nums), end(nums), target) - begin(nums) - 1; + return {l, r}; + } +}; diff --git a/43. Multiply Strings.cpp b/43. Multiply Strings.cpp new file mode 100644 index 0000000..1159873 --- /dev/null +++ b/43. Multiply Strings.cpp @@ -0,0 +1,17 @@ +class Solution { + public: + string multiply(string num1, string num2) { + string s(num1.length() + num2.length(), '0'); + + for (int i = num1.length() - 1; i >= 0; --i) + for (int j = num2.length() - 1; j >= 0; --j) { + const int mult = (num1[i] - '0') * (num2[j] - '0'); + const int sum = mult + (s[i + j + 1] - '0'); + s[i + j] += sum / 10; + s[i + j + 1] = '0' + sum % 10; + } + + const int i = s.find_first_not_of('0'); + return i == -1 ? "0" : s.substr(i); + } +}; diff --git a/5.Longest_Palindrome_Substring.java b/5.Longest_Palindrome_Substring.java index fd4e0aa..b6274b1 100644 --- a/5.Longest_Palindrome_Substring.java +++ b/5.Longest_Palindrome_Substring.java @@ -5,7 +5,7 @@ public String longestPalindrome(String s) { // aba int len1 = expandAroundCenter(s, i, i); // bb - int len2 = expandAroundCenter(s, i, i + 1); + int len2 =expandAroundCenter(s, i, i + 1); int len = Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; @@ -26,4 +26,4 @@ private int expandAroundCenter(String s, int left, int right) { - \ No newline at end of file + diff --git a/61. Rotate List b/61. Rotate List index 6857e3f..8642aa7 100644 --- a/61. Rotate List +++ b/61. Rotate List @@ -17,10 +17,10 @@ public: while(head == NULL || head -> next == NULL || k==0) return head; while(curr -> next != NULL) - { - len++; + { + len++; curr = curr -> next; - } + } curr -> next = head; k = k % len; k = len - k; diff --git a/61. Rotate List.cpp b/61. Rotate List.cpp new file mode 100644 index 0000000..e531875 --- /dev/null +++ b/61. Rotate List.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + ListNode* rotateRight(ListNode* head, int k) { + if (!head || !head->next || k == 0) + return head; + + ListNode* tail; + int length = 1; + for (tail = head; tail->next; tail = tail->next) + ++length; + tail->next = head; // Circle the list + + const int t = length - k % length; + for (int i = 0; i < t; ++i) + tail = tail->next; + ListNode* newHead = tail->next; + tail->next = nullptr; + + return newHead; + } +}; diff --git a/692. Top K Frequent Words/k_freq_words.cpp b/692. Top K Frequent Words/k_freq_words.cpp new file mode 100644 index 0000000..8a2695a --- /dev/null +++ b/692. Top K Frequent Words/k_freq_words.cpp @@ -0,0 +1,35 @@ + +class Solution { +public: + vector topKFrequent(vector& words, int k) { + unordered_map hashmap; + for(string& word : words) { + hashmap[word] += 1; + } + priority_queue, vector>, MyComp> pq; + for(auto it = hashmap.begin(); it != hashmap.end(); ++it) { + pq.push(make_pair(it->second, it->first)); + if(pq.size() > k) pq.pop(); + } + vector res; + while(!pq.empty()) { + // res.insert(res.begin(), pq.top().second); + res.push_back(pq.top().second); // push the results in increasing, and reverse later + pq.pop(); + } + reverse(res.begin(), res.end()); + return res; + } +private: + struct MyComp { + bool operator() (const pair& a, const pair& b) { + if(a.first != b.first) { + return a.first > b.first; + } + else { + return a.second < b.second; + } + } + }; +}; + diff --git a/7.reverseInteger.cpp b/7.reverseInteger.cpp index f93234a..e188630 100644 --- a/7.reverseInteger.cpp +++ b/7.reverseInteger.cpp @@ -13,9 +13,9 @@ class Solution { num = num + (x % 10); x = x / 10; } - } + } else{ - x = -1 * x; + x = -1 * x; while(x > 0){ num = num * 10; if( num >= 2147483647 || num <= -2147483648) @@ -27,4 +27,4 @@ class Solution { } return int(num); } -}; \ No newline at end of file +}; diff --git a/707. Design Linked List.cpp b/707. Design Linked List.cpp new file mode 100644 index 0000000..745af7a --- /dev/null +++ b/707. Design Linked List.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +class MyLinkedList { + +private: + class Node{ + public: + int val; + Node* prev; + Node* next; + + Node(int val, Node* prev, Node* next): val(val), prev(prev), next(next){} + Node(int val): Node(val, NULL, NULL){} + }; + + Node* dummyHead; + +public: + /** Initialize your data structure here. */ + MyLinkedList() { + dummyHead = new Node(-1); + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + int get(int index) { + + Node* cur = dummyHead->next; + for(int i = 0; i < index && cur; i ++) + cur = cur->next; + + if(!cur) return -1; + return cur->val; + } + + /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ + void addAtHead(int val) { + dummyHead->next = new Node(val, dummyHead, dummyHead->next); + } + + /** Append a node of value val to the last element of the linked list. */ + void addAtTail(int val) { + + Node* pre = dummyHead; + while(pre->next) + pre = pre->next; + pre->next = new Node(val, pre, NULL); + } + + /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ + void addAtIndex(int index, int val) { + + Node* pre = dummyHead; + for(int i = 0; i < index && pre; i ++) + pre = pre->next; + + if(pre) + pre->next = new Node(val, pre, pre->next); + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + void deleteAtIndex(int index) { + + Node* pre = dummyHead; + for(int i = 0; i < index && pre; i ++) + pre = pre->next; + + if(pre && pre->next){ + Node* delNode = pre->next; + pre->next = delNode->next; + if(pre->next) + pre->next->prev = pre; + delete delNode; + } + } +}; + + +int main() { + + return 0; +} diff --git a/73-setmatrixzero.java b/73-setmatrixzero.java new file mode 100644 index 0000000..572d835 --- /dev/null +++ b/73-setmatrixzero.java @@ -0,0 +1,65 @@ +class Solution { + public void setZeroes(int[][] matrix) { + int row=matrix.length; + int col=matrix[0].length; + int count=0; + + for(int i=0;i=0;f--) + { + if(matrix[f][j]!=0) + matrix[f][j]=-1; + } + for(int f=i+1;f=0;f--) + {if(matrix[i][f]!=0) + matrix[i][f]=-1; + } + for(int f=j+1;f + +using namespace std; + +int search(int mat[4][4], int n, int x) +{ + if (n == 0) + return -1; + + int smallest = mat[0][0], largest = mat[n - 1][n - 1]; + if (x < smallest || x > largest) + return -1; + + int i = 0, j = n - 1; + while (i < n && j >= 0) + { + if (mat[i][j] == x) + { + cout << "n Found at " + << i << ", " << j; + return 1; + } + if (mat[i][j] > x) + j--; + + else + i++; + } + + cout << "n Element not found"; + return 0; +} + +// Driver code +int main() +{ + int mat[4][4] = { { 10, 20, 30, 40 }, + { 15, 25, 35, 45 }, + { 27, 29, 37, 48 }, + { 32, 33, 39, 50 } }; + search(mat, 4, 29); + + return 0; +} diff --git a/74_search_element_in_row_column_sorted_matrix.cpp b/74_search_element_in_row_column_sorted_matrix.cpp new file mode 100644 index 0000000..30a72aa --- /dev/null +++ b/74_search_element_in_row_column_sorted_matrix.cpp @@ -0,0 +1,46 @@ +// C++ program to search an element in row-wise +// and column-wise sorted matrix +#include + +using namespace std; + +int search(int mat[4][4], int n, int x) +{ + if (n == 0) + return -1; + + int smallest = mat[0][0], largest = mat[n - 1][n - 1]; + if (x < smallest || x > largest) + return -1; + + int i = 0, j = n - 1; + while (i < n && j >= 0) + { + if (mat[i][j] == x) + { + cout << "n Found at " + << i << ", " << j; + return 1; + } + if (mat[i][j] > x) + j--; + + else + i++; + } + + cout << "n Element not found"; + return 0; +} + +// Driver code +int main() +{ + int mat[4][4] = { { 10, 20, 30, 40 }, + { 15, 25, 35, 45 }, + { 27, 29, 37, 48 }, + { 32, 33, 39, 50 } }; + search(mat, 4, 29); + + return 0; +} diff --git a/95. Unique Binary Search Trees II.cpp b/95. Unique Binary Search Trees II.cpp index 6838b5a..0f58099 100644 --- a/95. Unique Binary Search Trees II.cpp +++ b/95. Unique Binary Search Trees II.cpp @@ -13,7 +13,7 @@ class Solution { public: // Copied solution - vector generateTrees(int n, int s = 1) + vector generateTrees(int n, int s = 1) { vector ans; if (n < s) @@ -36,4 +36,4 @@ class Solution } return ans; } -}; \ No newline at end of file +}; diff --git a/C++/Binary_search.cpp b/C++/Binary_search.cpp new file mode 100644 index 0000000..d39c299 --- /dev/null +++ b/C++/Binary_search.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +// recursive function for binary search +/* if match found then return index of search key + else return -1 */ +int binarySearch(int arr[], int low, int high, int key) { + if (high >= low) { + // find middle index + int mid = low + (high - low) / 2; + + // find middle term and compare + if (arr[mid] == key) return mid; // key found + + // If key is smaller than middle term, then + // it can only be present in left subarray + if (arr[mid] > key) + return binarySearch(arr, low, mid - 1, key); + + // Else the key can only be present + // in right subarray + return binarySearch(arr, mid + 1, high, key); + } + + // key not found + return -1; +} + +// main function +int main() +{ + int array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + int key = 0; + + // take input for the search key + cout << "Enter Search Element: "; + cin >> key; + + // find the size array + int size = sizeof(array)/sizeof(array[0]); + + // search key + int index = binarySearch(array, 0, size, key); + + // display result + if(index == -1) + cout << key << " Not Found" << endl; + else + cout << key << " Found at Index = " << index << endl; + + return 0; +} diff --git a/C++/Factorial_recursion.cpp b/C++/Factorial_recursion.cpp new file mode 100644 index 0000000..e7dd570 --- /dev/null +++ b/C++/Factorial_recursion.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int factorial(int); + +int main() { + int n, result; + + cout << "Enter a non-negative number: "; + cin >> n; + + result = factorial(n); + cout << "Factorial of " << n << " = " << result; + return 0; +} + +int factorial(int n) { + if (n > 1) { + return n * factorial(n - 1); + } else { + return 1; + } +} diff --git a/Convert_Sorted_Array_To_BST.cpp b/Convert_Sorted_Array_To_BST.cpp new file mode 100644 index 0000000..40dbfbe --- /dev/null +++ b/Convert_Sorted_Array_To_BST.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + TreeNode* solve(vector &nums, int start, int end){ + //base case + if(start > end){ + return NULL; + } + + int mid = (start + end)/2; + TreeNode* temp = new TreeNode(nums[mid]); + + temp -> left = solve(nums, start, mid-1); + temp -> right = solve(nums, mid+1, end); + return temp; + } + + TreeNode* sortedArrayToBST(vector& nums) { + return solve(nums, 0, nums.size()-1); + } +}; \ No newline at end of file diff --git a/Intersection_of_Two_LL.cpp b/Intersection_of_Two_LL.cpp new file mode 100644 index 0000000..0336875 --- /dev/null +++ b/Intersection_of_Two_LL.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* a = headA, *b = headB; + while(a != b){ + if(a == NULL){ + a = headB; + } + else{ + a = a -> next; + } + + if(b == NULL){ + b = headA; + } + else{ + b = b -> next; + } + } + return a; + } +}; \ No newline at end of file diff --git a/JAVA-20-Valid_Parentheses b/JAVA-20-Valid_Parentheses new file mode 100644 index 0000000..e276305 --- /dev/null +++ b/JAVA-20-Valid_Parentheses @@ -0,0 +1,51 @@ +//https://leetcode.com/problems/valid-parentheses/ +//0ms +class Solution { + public boolean isValid(String s) { + if(s.length()==1) return false; + Stack stack = new Stack<>(); + for(int i = 0;i < s.length();i++){ + char ch=s.charAt(i); + if(ch=='(' || ch=='{' || ch=='['){ + stack.push(ch); + } + if(ch==')'){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='('){ + stack.pop(); + } + else { + return false; + } + } + if(ch==']' ){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='['){ + stack.pop(); + } + else{ + return false; + } + } + if(ch=='}'){ + if(stack.isEmpty()){ + return false; + } + else if(stack.peek()=='{'){ + stack.pop(); + } + else{ + return false; + } + } + } + if(stack.isEmpty()){ + return true; + } + + } +} diff --git a/Kth_Smallest_Element_in_BST.cpp b/Kth_Smallest_Element_in_BST.cpp new file mode 100644 index 0000000..04b72e2 --- /dev/null +++ b/Kth_Smallest_Element_in_BST.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int solve(TreeNode* root, int &i, int k){ + //base case + if(root == NULL){ + return -1; + } + + int left = solve(root -> left, i, k); + if(left != -1){ + return left; + } + + i++; + if(i == k){ + return root -> val; + } + return solve(root -> right, i, k); + } + + int kthSmallest(TreeNode* root, int k) { + int i = 0; + int ans = solve(root, i, k); + return ans; + } +}; \ No newline at end of file diff --git a/Letter_Combinations_of_a_Phone_Number.cpp b/Letter_Combinations_of_a_Phone_Number.cpp new file mode 100644 index 0000000..ccbc92a --- /dev/null +++ b/Letter_Combinations_of_a_Phone_Number.cpp @@ -0,0 +1,32 @@ +class Solution { +private: + void solve(string digit, string output, int index, vector& ans, string mapping[]){ + //base case + if(index >= digit.length()){ + ans.push_back(output); + return; + } + + int number = digit[index] - '0'; + string value = mapping[number]; + + for(int i=0; i letterCombinations(string digits) { + vector ans; + if(digits.length()==0){ + return ans; + } + string output = ""; + int index = 0; + string mapping[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + solve(digits, output, index, ans, mapping); + return ans; + } +}; \ No newline at end of file diff --git a/LinearSearch.cpp b/LinearSearch.cpp new file mode 100644 index 0000000..4f7427a --- /dev/null +++ b/LinearSearch.cpp @@ -0,0 +1,30 @@ +#include +#include +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) /* If required element is found */ + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} \ No newline at end of file diff --git a/Matrix.c b/Matrix.c new file mode 100644 index 0000000..d187a39 --- /dev/null +++ b/Matrix.c @@ -0,0 +1,65 @@ +#include +#include +int main(){ + int a[10][10],b[10][10],c[10][10],d[10][10], row, col,i,j; + + printf("Enter the rows number of both matrices\n"); + scanf("%d",&row); + printf("Enter the columns number of both matrices\n"); + scanf("%d",&col); + + + printf("Enter the elements of matrix A\n"); + for(i=0;i> solveNQueens(int n) { + vector> ans; + dfs(n, 0, vector(n), vector(2 * n - 1), vector(2 * n - 1), + vector(n, string(n, '.')), ans); + return ans; + } + + private: + void dfs(int n, int i, vector&& cols, vector&& diag1, + vector&& diag2, vector&& board, + vector>& ans) { + if (i == n) { + ans.push_back(board); + return; + } + + for (int j = 0; j < n; ++j) { + if (cols[j] || diag1[i + j] || diag2[j - i + n - 1]) + continue; + board[i][j] = 'Q'; + cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true; + dfs(n, i + 1, move(cols), move(diag1), move(diag2), move(board), ans); + cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false; + board[i][j] = '.'; + } + } +}; diff --git a/N_Queen.cpp b/N_Queen.cpp new file mode 100644 index 0000000..9dec166 --- /dev/null +++ b/N_Queen.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + void solve(int col, vector &board, vector> &ans, vector &leftRow, vector &upperDiagonal, vector &lowerDiagonal, int n){ + //base case + if(col == n){ + ans.push_back(board); + return; + } + + for(int row=0; row> solveNQueens(int n) { + vector> ans; + vector board(n); + string s(n, '.'); + for(int i=0; i leftRow(n, 0), upperDiagonal(2 * n - 1, 0), lowerDiagonal(2 * n - 1, 0); + solve(0, board, ans, leftRow, upperDiagonal, lowerDiagonal, n); + return ans; + } +}; \ No newline at end of file diff --git a/QueueUsingTwoStacks.cpp b/QueueUsingTwoStacks.cpp new file mode 100644 index 0000000..7db8250 --- /dev/null +++ b/QueueUsingTwoStacks.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +class que{ + stack s1; + stack s2; + + public: + void push(int x){ + s1.push(x); + } + + int pop(){ + if(s1.empty() and s2.empty()){ + cout<<"Queue is empty"< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for (int i = n - 1; i >= 0; i--) +#define rep(i, n) for (int i = 0; i < n; i++) +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + +int main() +{ + + int t; + cin >> t; + while (t--) + { + string a, b; + cin >> a >> b; + int n1 = a.length(); + int n2 = b.length(); + char x = a.at(n1 - 1); + char y = b.at(n2 - 1); + if (n1 == n2) + { + if (x == y) + { + cout << "=" << endl; + } + if ((x == 'M') && (y == 'S')) + { + cout << ">" << endl; + } + if ((x == 'S') && (y == 'M')) + { + cout << "<" << endl; + } + if ((x == 'L') && (y == 'S')) + { + cout << ">" << endl; + } + if ((x == 'S') && (y == 'L')) + { + cout << "<" << endl; + } + if ((x == 'L') && (y == 'M')) + { + cout << ">" << endl; + } + if ((x == 'M') && (y == 'L')) + { + cout << "<" << endl; + } + } + if (n1 != n2) + { + if (x == y) + { + if ((x == 'L') && (n1 > n2)) + { + cout << ">" << endl; + } + if ((x == 'L') && (n2 > n1)) + { + cout << "<" << endl; + } + if ((x == 'S') && (n1 > n2)) + { + cout << "<" << endl; + } + if ((x == 'S') && (n2 > n1)) + { + cout << ">" << endl; + } + } + if (x != y) + { + if ((x == 'M') && (y == 'S')) + { + cout << ">" << endl; + } + if ((x == 'S') && (y == 'M')) + { + cout << "<" << endl; + } + if ((x == 'L') && (y == 'S')) + { + cout << ">" << endl; + } + if ((x == 'S') && (y == 'L')) + { + cout << "<" << endl; + } + if ((x == 'L') && (y == 'M')) + { + cout << ">" << endl; + } + if ((x == 'M') && (y == 'L')) + { + cout << "<" << endl; + } + } + } + } + return 0; +} diff --git a/Thickness.cpp b/Thickness.cpp new file mode 100644 index 0000000..024cceb --- /dev/null +++ b/Thickness.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +int main() +{ + + int x1; + cin >> x1; + while (x1--) + { + int x; + cin >> x; + if (x == 3) + cout << "-1" << endl; + else + { + if (x % 2) + { + int kas = (x / 2) + 1; + for (int j = x; j > kas; j--) + { + cout << i << " "; + } + for (int j = 1; j <= kas; j++) + { + cout << j << " "; + } + cout << endl; + } + else + { + for (int k = x; k >= 1; k--) + { + cout << k << " "; + } + cout << endl; + } + } + } + return 0; +} \ No newline at end of file diff --git a/TrappingRainWater.cpp b/TrappingRainWater.cpp new file mode 100644 index 0000000..7720932 --- /dev/null +++ b/TrappingRainWater.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + int trap(vector& height) { + const int n = height.size(); + int ans = 0; + vector l(n); // l[i] := max(height[0..i]) + vector r(n); // r[i] := max(height[i..n)) + + for (int i = 0; i < n; ++i) + l[i] = i == 0 ? height[i] : max(height[i], l[i - 1]); + + for (int i = n - 1; i >= 0; --i) + r[i] = i == n - 1 ? height[i] : max(height[i], r[i + 1]); + + for (int i = 0; i < n; ++i) + ans += min(l[i], r[i]) - height[i]; + + return ans; + } +}; + diff --git a/arrayrecovery.cpp b/arrayrecovery.cpp new file mode 100644 index 0000000..d0649ae --- /dev/null +++ b/arrayrecovery.cpp @@ -0,0 +1,66 @@ +// shree ganeshay namah +#include +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for (int i = n - 1; i >= 0; i--) +#define rep(i, n) for (int i = 0; i < n; i++) +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + int c = 0; + vector a(n); + vector b(n); + int flag = 0; + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + b[0] = a[0]; + for (int i = 1; i < n; i++) + { + if(a[i]==0){ + b[i] = b[i-1] + a[i]; + continue; + } + if (b[i-1] - a[i] >= 0) + { + flag = 1; + break; + } + else{ + b[i] = a[i] + b[i - 1]; + } + } + for (int i = 0; i < n; i++) + { + if (flag == 0) + { + cout << b[i] << " "; + } + else{ + cout << "-1"; + break; + } + } + cout< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i>t; + while(t--) + { + int n, cv = 0, qw = 0, po = 0; + cin >> n; + std::vector a(n); + for (int i = 0; i < n; i++) + cin >> a[i]; + for (int j = 0; j < n; j++) + { + for (int i = 0; i < n; i++) + { + if (a[j] >= a[i]){ + cv++; + } + else{ + qw++; + } + + } + if (cv > qw) + po++; + cv = 0; + qw = 0; + } + cout << po << endl; +} + return 0; +} diff --git a/balancedParenthesis.cpp b/balancedParenthesis.cpp new file mode 100644 index 0000000..bd4dc48 --- /dev/null +++ b/balancedParenthesis.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +bool isValid(string s){ + int n = s.size(); + + stack st; + bool ans = true; + for(int i=0; i& prices) { + int maxPro = 0; + int minPrice = INT_MAX; + for(int i=0; i +#include +int main() +{ + int n, key,l,h,mid,x; + printf("Enter the size of Array"); + scanf("%d", &n); + int a[n]; + printf("Enter the key"); + scanf("%d", &key); + printf("Enter the elements of Array"); + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + l = 0; + h = n - 1; + mid = ((l + h)/2); + x=mid; + while (l <= h) + { + if (key == a[x]) + { + printf("position of the key in array\n"); + printf("%d",x); + break; + } + if (a[x] > key) + { + h = x - 1; + } + if (a[x] < key) + { + l = x + 1; + } + } + return 0; +} \ No newline at end of file diff --git a/cancelTrains.cpp b/cancelTrains.cpp new file mode 100644 index 0000000..d24b6dd --- /dev/null +++ b/cancelTrains.cpp @@ -0,0 +1,52 @@ +//shree ganeshay namah +#include +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for (int i = n - 1; i >= 0; i--) +#define rep(i, n) for (int i = 0; i < n; i++) +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + +int main() +{ + + int t, n, m; + cin >> t; + while (t--) + { + cin >> n >> m; + int a[n]; + int b[m]; + int c = 0; + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + for (int j = 0; j < m; j++) + { + cin >> b[j]; + } + for (int k = 0; k < n; k++) + { + for (int p = 0; p < m; p++) + { + if (a[k] == b[p]) + { + c++; + } + } + } + cout << c << endl; + } + return 0; +} diff --git a/confusingconcatenations.cpp b/confusingconcatenations.cpp new file mode 100644 index 0000000..7808138 --- /dev/null +++ b/confusingconcatenations.cpp @@ -0,0 +1,45 @@ +//shree ganeshay namah +#include +using namespace std; +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + +int main() +{ + + + int tt; + cin>>tt; + while(tt--) + { + int n; + cin>>n; + + int a[n]; + for (int i = 0; i < n; i++) + { + cin>>a[i]; + } + int max1 = 0; + for (int i = 0; i < n; i++) + { + if(a[i]>a[max1]) + max1 = i; + } + if(max1 == 0){ + cout<<-1< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i>t; + while(t--) + { + int n,m; + cin>>n>>m; + vector> ma(m , vector (2)); + unordered_map mp; + unordered_map mq; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < 2; j++) + { + cin>>ma[i][j]; + mp[ma[i][0]]++; + mq[ma[i][1]]++; + } + } + if(n == m) + cout<<"NO"< lookup = new Dictionary(); + for (var i = 0; i < nums.Length; i++) { + if (lookup.ContainsKey(target - nums[i])) { + return new int [] { lookup[target - nums[i]], i }; + } + lookup[nums[i]] = i; + } + return new int[] { }; + } +} diff --git a/decodeways.cpp b/decodeways.cpp new file mode 100644 index 0000000..8305ecf --- /dev/null +++ b/decodeways.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + int numDecodings(string s) { + /*int n = s.size(); + int n1=0,n2=1,n3,ans; + for(int i=1;i<=n;i++){ + n3=n1+n2; + n1=n2; + n2=n3; + } + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + +int main() +{ + + + int ttt; + cin>>ttt; + while(ttt--) + { + int n1; + cin>>n1; + if(n1==3) + cout<<"-1"< anurag; i--) + { + cout<= 1; i--) + { + cout< +typedef struct{ + char *sym; + int val; +}numeral; +int maxNume(numeral *nu, int num){ + int i, index; + for(i = 0; i<15; i++){//15 numerals in array + if(nu[i].val <= num) + index = i; + } + //gretest value numeral index, not greater than number + return index; +} +void decToRoman(numeral *nu, int num){ + int max; + if(num != 0){ + max = maxNume(nu, num); + printf("%s", nu[max].sym); + num -= nu[max].val;//decrease number + decToRoman(nu, num);//recursively print numerals + } +} +main(){ + int number; + numeral nume[15] = {{"I",1},{"IV",4},{"V",5},{"IX",9}, {"X",10},{"XL",40},{"L",50},{"XC",90}, +{"C",100},{"CD",400},{"D",500},{"CM",900},{"M",1000},{"MMMM",4000},{"V'",5000}}; + printf("Enter a decimal number: "); + scanf("%d", &number); + if(number >0 && number <= 5000){//checking input number + printf("The Roman equivalent of %d is ", number); + decToRoman(nume, number); + } + else{ + printf("Invalid Input"); + } + printf("\n"); +} diff --git a/leetcode_contest_314_solutions/1.cpp b/leetcode_contest_314_solutions/1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/leetcode_contest_314_solutions/2.cpp b/leetcode_contest_314_solutions/2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/leetcode_contest_314_solutions/3.cpp b/leetcode_contest_314_solutions/3.cpp new file mode 100644 index 0000000..e69de29 diff --git a/mergeTwoSortedll.cpp b/mergeTwoSortedll.cpp new file mode 100644 index 0000000..dd0fa80 --- /dev/null +++ b/mergeTwoSortedll.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* next; + node* prev; + + node(int val){ + data = val; + next = NULL; + prev = NULL; + } +}; + +void insertAtHead(node* &head, int val){ + node* n = new node(val); + n->next = head; + if(head!=NULL){ + head->prev = n; + } + + head = n; +} + +void insertAtTail(node* &head, int val){ + if(head==NULL){ + insertAtHead(head, val); + return; + } + + node* n = new node(val); + node* temp = head; + + while(temp->next!=NULL){ + temp = temp->next; + } + + temp->next = n; + n->prev = temp; +} + +node* merge(node* &head1, node* &head2){ + node* p1 = head1; + node* p2 = head2; + node* dummyNode = new node(-1); + node* p3 = dummyNode; + + while(p1!=NULL && p2!=NULL){ + if(p1->datadata){ + p3->next = p1; + p1 = p1->next; + } + else{ + p3->next = p2; + p2 = p2->next; + } + p3 = p3->next; + } + + while(p1!=NULL){ + p3->next = p1; + p1 = p1->next; + p3 = p3->next; + } + + while(p2!=NULL){ + p3->next = p2; + p2 = p2->next; + p3 = p3->next; + } + return dummyNode->next; +} + +void display(node *head){ + node* temp = head; + while(temp!=NULL){ + cout<data<<"->"; + temp = temp->next; + } + cout<<"NULL"< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i>t; + while(t--) + { + int n; + cin>>n; + string as; + cin>>as; + int c1=0,c0=0; + for (int i = 0; i < n; i++) + { + if(as[i] == '1') + c1++; + else + c0++; + } + if(n%2==0){ + if(c1%2 && c0%2) + cout<<"NO"< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i>t; + while(t--) + { + ll n; + cin>>n; + string as; + cin>>as; + if(as[0]=='0'){ + for (int i = 0; i < 2*n; i++) + { + if(as[i]=='1'){ + as[i]='0'; + } + else{ + as[i]='1'; + } + } + + } + vector vc; + ll cc; + ll flag = 0; + ll an; + for (int i = 0; i < 2*n; i++) + { + if(as[i]=='0'){ + vc.push_back(i); + } + if(as[i] != as[2*n-i-1]){ + flag=1; + break; + } + } + if(flag){ + cout<<"1\n"; + cout<<2*n<=1; i--){ + if(vc[i]-vc[i-1]-1!=cc){ + flag=1; + cout<<"2\n"; + cout< +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i>t; + while(t--) + { + int ak,akg; + cin>>ak>>akg; + cout<<(ak/akg)*(ak/akg)< +using namespace std; + +void pypart2(int n) +{ + int i = 0, j = 0, k = 0; + while (i < n) { + + while (k <= n - i - 2) { + cout << " "; + k++; + } + k = 0; + + while (j < 2 * i - 1) { + cout << "*"; + j++; + } + j = 0; + i++; + cout << endl; + } +} + +int main() +{ + int n = 5; + + pypart2(n); + return 0; +} diff --git a/stripes.cpp b/stripes.cpp new file mode 100644 index 0000000..a5e378c --- /dev/null +++ b/stripes.cpp @@ -0,0 +1,67 @@ + +#include +using namespace std; +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ff first +#define ss second +#define all(c) c.begin(), c.end() +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for (int i = n - 1; i >= 0; i--) +#define rep(i, n) for (int i = 0; i < n; i++) +#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + +int main() +{ + int t; + cin >> t; + while (t--) + { + int red = 0, ak = 0; + string a[8]; + for (int i = 0; i < 8; i++) + { + cin >> a[i]; + } + int flag = 0; + for (int i = 0; i < 8; i++) + { + red = 0; + for (int j = 0; j < 8; j++) + { + if (a[i][j] == 'R') + red++; + } + if (red == 8) + { + cout << "R" << endl; + flag = 1; + break; + } + } + if (!flag) + { + for (int i = 0; i < 8; i++) + { + ak = 0; + for (int j = 0; j < 8; j++) + { + if (a[j][i] == 'B') + ak++; + } + if (ak == 8) + { + cout << "B" << endl; + break; + } + } + } + } + return 0; +} diff --git a/subsequence.cpp b/subsequence.cpp new file mode 100644 index 0000000..a643529 --- /dev/null +++ b/subsequence.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +#define ll long long +#define pi (3.141592653589) +#define mod 1000000007 +#define ll long long +#define float double +#define pb push_back +#define mp make_pair +#define ss second +#define min3(a, b, c) min(c, min(a, b)) +#define min4(a, b, c, d) min(d, min(c, min(a, b))) +#define rrep(i, n) for(int i=n-1;i>=0;i--) +#define rep(i,n) for(int i=0;i vi; +typedef vector> vpi; +typedef vector vl; +#define all(aa) aa.begin(), aa.end() +#define pb push_back + +#define ff \ + ios_base::sync_with_stdio(false); \ + cin.tie(NULL); \ + cout.tie(NULL); + +#define t \ + int test_cases; \ + cin >> test_cases; \ + while (test_cases--) + + +int main() +{ + ff + + t{ + ll n, anurag = 0; + cin >> n; + vl aa, aa1, mohit; + for (ll i = 0; i < n; i++) + { + ll a; + cin >> a; + + if (i % 2 == 0){ + aa.pb(a); + } + else{ + aa1.pb(a); + } + + } + + sort(all(aa), greater()); + sort(all(aa1)); + + ll a = 0, b = 0; + for (ll i = 0; i < n; i++) + { + if (i % 2 == 0) + { + mohit.pb(aa[a]); + a++; + } + else + { + mohit.pb(aa1[b]); + b++; + } + } + + if (aa1.size() > 1) + for (ll i = aa1.size() - 2; i >= 0; i--) + { + aa1[i] += aa1[i + 1]; + } + + for (ll i = 0; i < aa.size() && i < aa1.size(); i++) + { + anurag += aa[i] * aa1[i]; + } + + for (auto i : mohit) + cout << i << " "; + + cout << "\n" << anurag << "\n"; + } + return 0; +} \ No newline at end of file diff --git a/sum-with-multiplicity b/sum-with-multiplicity new file mode 100644 index 0000000..d41813d --- /dev/null +++ b/sum-with-multiplicity @@ -0,0 +1,25 @@ +# Time: O(n^2), n is the number of disctinct A[i] +# Space: O(n) + +import collections +import itertools + + +class Solution(object): + def threeSumMulti(self, A, target): + """ + :type A: List[int] + :type target: int + :rtype: int + """ + count = collections.Counter(A) + result = 0 + for i, j in itertools.combinations_with_replacement(count, 2): + k = target - i - j + if i == j == k: + result += count[i] * (count[i]-1) * (count[i]-2) // 6 + elif i == j != k: + result += count[i] * (count[i]-1) // 2 * count[k] + elif max(i, j) < k: + result += count[i] * count[j] * count[k] + return result % (10**9 + 7) diff --git a/wildcard-matching.java b/wildcard-matching.java new file mode 100644 index 0000000..c76d579 --- /dev/null +++ b/wildcard-matching.java @@ -0,0 +1,39 @@ +public class WildcardMatching { + + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + System.out.println(new WildcardMatching().isMatch("abebd", "?be******e")); + } + + public boolean isMatch(String s, String p) { + int starIdx = -1; + int starPosAtStr = -1; + int j = 0; + for (int i = 0, l = s.length(); i < l; ) { + if (j < p.length()) { + if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') { + i++; + j++; + } else if (p.charAt(j) == '*') { + starIdx = j; + starPosAtStr = i; + j++; // increment only pattern index. This is because '*' can match also empty string. + } else if (starIdx != -1) { + i = ++starPosAtStr; + j = starIdx + 1; + } else return false; + } else if (starIdx != -1) { + i = ++starPosAtStr; + j = starIdx + 1; + } else return false; + } + while (j < p.length()) { + if (p.charAt(j) == '*') { + j++; + } else break; + } + return j == p.length(); + } +} \ No newline at end of file