├── .cph └── .arrayrecovery.cpp_7b3b9871d14eed2f4ce9e9619c6c4e79.prob ├── 1-Two Sum ├── 1.Two-sum.java ├── 1004. Max Consecutive Ones III ├── 11. Container With Most Water ├── 11.contain-most-water.py ├── 112. Path sum ├── 112. PathSum.cpp ├── 12. Integer to Roman.cpp ├── 12.Integer to Roman.java ├── 1206. Design Skiplist (Hard) ├── 132-pattern.cpp ├── 1372-RichestCustomerWealth.py ├── 146. LRU Cache └── LRU_Cache.cpp ├── 16. 3Sum Closest ├── 160-intersection-of-two-linked-lists.cpp ├── 1672-richestcustomerwealth.java ├── 1823.find-the-winner-of-the-circular-game.cpp ├── 19.remove-nth-from-end-of-list.py ├── 2.Add-Two-Numbers.java ├── 2.add-two-numbers.cpp ├── 20-Valid Parentheses ├── 204_count_primes.java ├── 2149. rearrange array elements by sign.cpp ├── 238.odd-even-linked-list.cpp ├── 25. Reverse Nodes in k-Group.cpp ├── 268.Missing_Number.java ├── 3-Longest Substring Without Repeating Characters ├── 3.Longest_Substring.java ├── 3.longest-substring-without-repeating-characters.cpp ├── 30.Substring-with-Concatenation-of-All-Words.cpp ├── 334. Increasing Triplet Subsequence ├── 34. Find First and Last Position of Element in Sorted Array.cpp ├── 37.Sudoku Solver.cpp ├── 39. Combination Sum ├── 392.is-subsequence.cpp ├── 4.Median_of_two_Sorted_array.java ├── 43. Multiply Strings.cpp ├── 48.RotateImage.cpp ├── 5.Longest_Palindrome_Substring.java ├── 53. Maximum Subarray ├── 567. Permutation in String └── code.cpp ├── 61. Rotate List ├── 61. Rotate List.cpp ├── 659-split-array-into-consecutive-subsequences ├── 659-split-array-into-consecutive-subsequences.cpp └── README.md ├── 69.sqrtx.cpp ├── 692. Top K Frequent Words └── k_freq_words.cpp ├── 7.Reverse.java ├── 7.reverseInteger.cpp ├── 704.BinarySearch └── .cpp ├── 707. Design Linked List.cpp ├── 73-setmatrixzero.java ├── 74. search_element_in_row_column_sorted_matrix.cpp ├── 74_search_element_in_row_column_sorted_matrix.cpp ├── 8.String_to_Integer.java ├── 832.Flipping_an_Image.java ├── 9.Palindrome-Number.java ├── 95. Unique Binary Search Trees II.cpp ├── 994. Rotting_Oranges.cpp ├── All Nodes Distance K in Binary Tree ├── BANKERS-ALGO.cpp ├── C++ ├── Binary_search.cpp └── Factorial_recursion.cpp ├── CONTRIBUTING.md ├── CircularDoubleLinkedList.cpp ├── CombinationSum.java ├── Convert_Sorted_Array_To_BST.cpp ├── Custom-Sort-String.cpp ├── Intersection_of_Two_LL.cpp ├── JAVA-20-Valid_Parentheses ├── Kth_Smallest_Element_in_BST.cpp ├── Letter_Combinations_of_a_Phone_Number.cpp ├── LinearSearch.cpp ├── Matrix.c ├── Minimum time taken to BURN the Binary Tree from a Node ├── Mirror Reflection.java ├── N QUEENS ├── N_Queen.cpp ├── Next-Perm ├── Peak Index of an Mountain Array.cpp ├── PeakIndexinaMountainArray.cpp ├── PigLatin in Python ├── QueueUsingTwoStacks.cpp ├── README.md ├── Remove Linked List Elements ├── Reverse_Vowels_Of_String.cpp ├── Rotate_Matrix_by_90_degrees.java ├── Search-Insert-Position.py ├── ShirtSize.cpp ├── Sieve of Eratosthenes.java ├── Thickness.cpp ├── TrappingRainWater.cpp ├── WordSearch.java ├── arrayrecovery.cpp ├── arrayrecovery.exe ├── averageflex.cpp ├── balancedParenthesis.cpp ├── best_time_to_buy_and_sell_stock.cpp ├── binarySearch.c ├── cancelTrains.cpp ├── confusingconcatenations.cpp ├── cowardly rooks.cpp ├── create two-sum.cs ├── decodeways.cpp ├── funny ├── funnypermutation.cpp ├── hack.cpp ├── integertoroman.c ├── josh-solution.cpp ├── leetcode_contest_314_solutions ├── 1.cpp ├── 2.cpp └── 3.cpp ├── level_order_tree_traversal.cpp ├── maximal square.java ├── mergeTwoSortedll.cpp ├── number_of_1_bits.cpp ├── palindromeflipping.cpp ├── palindromepartition.cpp ├── palindromepartition.exe ├── papercutting.cpp ├── pattern.cpp ├── push-dominoes.cpp ├── richest-wealth.cpp ├── singly linked list ├── singly_linked_list ├── spiralMatrix.cpp ├── stacks.cpp ├── stripes.cpp ├── subsequence.cpp ├── sum-with-multiplicity ├── sum_triangle.cpp ├── unit_converter.cpp └── wildcard-matching.java /.cph/.arrayrecovery.cpp_7b3b9871d14eed2f4ce9e9619c6c4e79.prob: -------------------------------------------------------------------------------- 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} -------------------------------------------------------------------------------- /1-Two Sum: -------------------------------------------------------------------------------- 1 | //Question- 1 2 | Link: https://leetcode.com/problems/two-sum/ 3 | class Solution { 4 | public int[] twoSum(int[] nums, int target) { 5 | int c=0; 6 | int[] b; 7 | b= new int [2]; 8 | for(int i=0;i hm = new HashMap<>(); 27 | for(int i=0;i map = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | int x = nums[i]; 6 | if (map.containsKey(target - x)) { 7 | return new int[]{map.get(target - x), i}; 8 | } 9 | map.put(x, i); 10 | } 11 | throw new IllegalArgumentException("No two sum solution"); 12 | } 13 | } -------------------------------------------------------------------------------- /1004. Max Consecutive Ones III: -------------------------------------------------------------------------------- 1 | Link: https://leetcode.com/problems/max-consecutive-ones-iii/ 2 | 3 | //JAVA Solution 4 | //O(n) Optimize 5 | class Solution { 6 | public int longestOnes(int[] nums, int k) { 7 | int ws = 0 , max = 0 , count = 0; 8 | HashMap hm = new HashMap<>(); 9 | for(int we = 0 ; we < nums.length ; we++){ 10 | // if(nums[we] == 0) count++; 11 | if(nums[we]==0) hm.put(nums[we],hm.getOrDefault(nums[we],0)+1); 12 | while(nums[we]==0 && hm.get(nums[we]) > k){ 13 | if(nums[ws] == 0){ 14 | hm.put(nums[ws],hm.get(nums[ws])-1); 15 | } 16 | ws++; 17 | } 18 | max=Math.max(max,we-ws+1); 19 | } 20 | return max; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /11. Container With Most Water: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int maxstored = INT_MIN; 5 | int waterfilled; 6 | int s = 0; 7 | int e = height.size()-1; 8 | while(s<=e) 9 | { 10 | waterfilled = (e-s)*(min(height[s], height[e])); 11 | maxstored = max(waterfilled, maxstored); 12 | if(height[s] int: 2 | area = 0 3 | n = len(height) -1 4 | j = 0 5 | 6 | while j < n: 7 | w = n-j 8 | area = max(area, min(height[j], height[n])*w) 9 | if height[j] <= height[n]: 10 | j+=1 11 | else: 12 | n-=1 13 | return area 14 | -------------------------------------------------------------------------------- /112. Path sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasPathSum(TreeNode root, int targetSum) { 3 | return solve(root,targetSum,0); 4 | } 5 | public boolean solve(TreeNode root, int targetSum,int currSum) { 6 | if(root == null) return false; 7 | 8 | //leaf node 9 | if(root.left == null && root.right == null) { 10 | currSum += root.val; 11 | if(targetSum == currSum) { 12 | return true; 13 | } 14 | else{ 15 | return false; 16 | } 17 | } 18 | 19 | 20 | boolean left = solve(root.left,targetSum,currSum + root.val); 21 | boolean right = solve(root.right,targetSum,currSum + root.val); 22 | 23 | return left || right; 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /112. PathSum.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/path-sum 2 | // 98% efficient solution 3 | 4 | class Solution { 5 | public: 6 | int pathSum = 0; 7 | bool pathFound = false; 8 | 9 | bool hasPathSum(TreeNode* root, int targetSum) 10 | { 11 | if(root != NULL) 12 | { 13 | pathSum += root->val; 14 | 15 | if(root->left == NULL && root->right == NULL) 16 | { 17 | if(pathSum == targetSum) 18 | pathFound = true; 19 | } 20 | else 21 | { 22 | hasPathSum(root->left, targetSum); 23 | hasPathSum(root->right, targetSum); 24 | } 25 | 26 | pathSum -= root->val; 27 | } 28 | 29 | return (pathFound == true) ? true : false; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /12. Integer to Roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | // Copied solution 5 | string intToRoman(int num) 6 | { 7 | string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; 8 | string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; 9 | string hrns[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; 10 | string ths[] = {"", "M", "MM", "MMM"}; 11 | 12 | return ths[num / 1000] + hrns[(num % 1000) / 100] + tens[(num % 100) / 10] + ones[num % 10]; 13 | } 14 | }; -------------------------------------------------------------------------------- /12.Integer to Roman.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String intToRoman(int num) { 3 | Map map = new HashMap(); 4 | map.put(1, "I"); map.put(5, "V"); map.put(10, "X"); 5 | map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M"); 6 | map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC"); 7 | map.put(400, "CD"); map.put(900, "CM"); 8 | 9 | int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 10 | 11 | StringBuffer sb = new StringBuffer(); 12 | for (int i = 0; i= base) { 16 | sb.append(map.get(base)); 17 | num -= base; 18 | } 19 | } 20 | 21 | return sb.toString(); 22 | } 23 | } -------------------------------------------------------------------------------- /1206. Design Skiplist (Hard): -------------------------------------------------------------------------------- 1 | class Skiplist { 2 | public: 3 | 4 | maps; 5 | 6 | Skiplist() { 7 | 8 | } 9 | 10 | bool search(int target) { 11 | if(s.find(target)!=s.end()) 12 | return true; 13 | else 14 | return false; 15 | } 16 | 17 | void add(int num) { 18 | s[num]++; 19 | } 20 | 21 | bool erase(int num) { 22 | if(s.find(num)==s.end()) 23 | return false; 24 | else{ 25 | s[num]--; 26 | if(s[num]==0) 27 | s.erase(num); 28 | return true; 29 | } 30 | } 31 | }; 32 | 33 | /** 34 | * Your Skiplist object will be instantiated and called as such: 35 | * Skiplist* obj = new Skiplist(); 36 | * bool param_1 = obj->search(target); 37 | * obj->add(num); 38 | * bool param_3 = obj->erase(num); 39 | */ 40 | -------------------------------------------------------------------------------- /132-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | int ak = numeric_limits::min(); 5 | stack st; 6 | for (int i = nums.size() - 1; i >= 0; --i) { 7 | if (nums[i] < ak) { 8 | return true; 9 | } else { 10 | while (!st.empty() && nums[i] > st.top()) { 11 | ak = st.top(), st.pop(); 12 | } 13 | } 14 | st.emplace(nums[i]); 15 | } 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1372-RichestCustomerWealth.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumWealth(self, accounts: List[List[int]]) -> int: 3 | maxSum = 0 4 | for row in range(len(accounts)): 5 | runningSum = 0 6 | for col in range(len(accounts[row])): 7 | runningSum =runningSum+ accounts[row][col] 8 | 9 | if runningSum>maxSum: 10 | maxSum = runningSum 11 | return(maxSum) 12 | 13 | -------------------------------------------------------------------------------- /146. LRU Cache/LRU_Cache.cpp: -------------------------------------------------------------------------------- 1 | // Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. 2 | 3 | struct Node { 4 | int key; 5 | int value; 6 | Node(int key, int value) : key(key), value(value) {} 7 | }; 8 | 9 | 10 | class LRUCache { 11 | public: 12 | LRUCache(int capacity) : capacity(capacity) {} 13 | 14 | int get(int key) { 15 | if (!keyToIterator.count(key)) 16 | return -1; 17 | 18 | const auto& it = keyToIterator[key]; 19 | // move it to the front 20 | cache.splice(begin(cache), cache, it); 21 | return it->value; 22 | } 23 | 24 | void put(int key, int value) { 25 | // no capacity issue, just update the value 26 | if (keyToIterator.count(key)) { 27 | const auto& it = keyToIterator[key]; 28 | // move it to the front 29 | cache.splice(begin(cache), cache, it); 30 | it->value = value; 31 | return; 32 | } 33 | 34 | // check the capacity 35 | if (cache.size() == capacity) { 36 | const auto& lastNode = cache.back(); 37 | // that's why we store `key` in `Node` 38 | keyToIterator.erase(lastNode.key); 39 | cache.pop_back(); 40 | } 41 | 42 | cache.emplace_front(key, value); 43 | keyToIterator[key] = begin(cache); 44 | } 45 | 46 | private: 47 | const int capacity; 48 | list cache; 49 | unordered_map::iterator> keyToIterator; 50 | }; 51 | 52 | -------------------------------------------------------------------------------- /16. 3Sum Closest: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int threeSumClosest(int[] nums, int target) { 3 | int n = nums.length; 4 | int res = 0; 5 | int m = Integer.MAX_VALUE; 6 | Arrays.sort(nums); 7 | for (int i = 0; i < n - 2; i++){ 8 | if (i > 0 && nums[i-1] == nums[i]){ 9 | continue; 10 | } 11 | if (nums[i] * 3 >= target){ 12 | int s = nums[i] + nums[i+1] + nums[i+2]; 13 | if (s - target < m){ 14 | return s; 15 | } 16 | break; 17 | } 18 | int t = nums[i] + nums[n-1] + nums[n-2]; 19 | if (t == target){ 20 | return t; 21 | } 22 | if (t < target){ 23 | if (t - target < m){ 24 | m = target - t; 25 | res = t; 26 | } 27 | continue; 28 | } 29 | int a = i + 1; 30 | int b = n - 1; 31 | while (a < b){ 32 | t = nums[i] + nums[a] + nums[b]; 33 | if (t == target){ 34 | return t; 35 | } 36 | if (Math.abs(t - target) < m){ 37 | res = t; 38 | m = Math.abs(t - target); 39 | } 40 | if (t > target){ 41 | b--; 42 | } else { 43 | a++; 44 | } 45 | } 46 | } 47 | return res; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /160-intersection-of-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | // Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. 2 | // If the two linked lists have no intersection at all, return null. 3 | 4 | // Time: O(m + n) 5 | // Space: O(1) 6 | 7 | /** 8 | * Definition for singly-linked list. 9 | * struct ListNode { 10 | * int val; 11 | * ListNode *next; 12 | * ListNode(int x) : val(x), next(NULL) {} 13 | * }; 14 | */ 15 | 16 | class Solution { 17 | public: 18 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 19 | ListNode *curA = headA, *curB = headB; 20 | while (curA != curB) { 21 | curA = curA ? curA->next : headB; 22 | curB = curB ? curB->next : headA; 23 | } 24 | return curA; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1672-richestcustomerwealth.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumWealth(int[][] acc) { 3 | int max=0; 4 | for (int i=0;imax) 13 | { 14 | max=sum; 15 | } 16 | } 17 | 18 | return max; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1823.find-the-winner-of-the-circular-game.cpp: -------------------------------------------------------------------------------- 1 | // 1823. Find the Winner of the Circular Game 2 | // Josephus Problem 3 | #include 4 | using namespace std; 5 | 6 | void solve(vector &arr, int index, int k){ 7 | if(arr.size() == 1) 8 | return; 9 | 10 | index = (index + k) % arr.size(); 11 | 12 | arr.erase(arr.begin() + index); 13 | index--; 14 | 15 | solve(arr, index, k); 16 | } 17 | 18 | int findTheWinner(int n, int k) { 19 | vector arr; 20 | for(int i = 1; i <= n; i++){ 21 | arr.push_back(i); 22 | } 23 | int index = -1; 24 | solve(arr, index, k); 25 | return arr[0]; 26 | } 27 | 28 | int main(){ 29 | // Input: n = 5, k = 2 30 | // Output: 3 31 | // Explanation: Here are the steps of the game: 32 | // 1) Start at friend 1. 33 | // 2) Count 2 friends clockwise, which are friends 1 and 2. 34 | // 3) Friend 2 leaves the circle. Next start is friend 3. 35 | // 4) Count 2 friends clockwise, which are friends 3 and 4. 36 | // 5) Friend 4 leaves the circle. Next start is friend 5. 37 | // 6) Count 2 friends clockwise, which are friends 5 and 1. 38 | // 7) Friend 1 leaves the circle. Next start is friend 3. 39 | // 8) Count 2 friends clockwise, which are friends 3 and 5. 40 | // 9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner. 41 | int n = 4, k = 2; 42 | cout << findTheWinner(n,k); 43 | return 0; 44 | } 45 | 46 | // for(int i = 0; i < arr.size(); i++){ 47 | // cout << arr[i] << ' '; 48 | // } 49 | // cout< ListNode: 12 | dummy = ListNode(0, head) 13 | current = dummy 14 | second = head 15 | 16 | while n > 0 and second: 17 | second = second.next 18 | n-=1 19 | 20 | while second: 21 | second = second.next 22 | current = current.next 23 | current.next = current.next.next 24 | 25 | return dummy.next -------------------------------------------------------------------------------- /2.Add-Two-Numbers.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 4 | ListNode dummyHead = new ListNode(0); 5 | ListNode p = l1, q= l2, curr = dummyHead; 6 | int carry = 0; 7 | while (p != null || q!= null) { 8 | int x = (p != null) ? p.val : 0; 9 | int y = (q != null) ? q.val : 0; 10 | int digit = carry + x + y; 11 | carry = digit / 10; 12 | curr.next = new ListNode(digit % 10); 13 | curr = curr.next; 14 | if (p != null) p = p.next; 15 | if (q != null) q = q.next; 16 | } 17 | if (carry > 0) { 18 | curr.next = new ListNode(carry); 19 | } 20 | return dummyHead.next; 21 | } 22 | } -------------------------------------------------------------------------------- /2.add-two-numbers.cpp: -------------------------------------------------------------------------------- 1 | // 2. Add Two Numbers 2 | #include 3 | using namespace std; 4 | 5 | class ListNode { 6 | public: 7 | int data; 8 | ListNode* next; 9 | 10 | public: 11 | ListNode(int data){ 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | void insertAtTail(ListNode* &tail, int data){ 18 | ListNode* temp = new ListNode(data); 19 | tail->next = temp; 20 | tail = tail->next; 21 | } 22 | 23 | void print(ListNode* &head){ 24 | ListNode* temp = head; 25 | while(temp != NULL){ 26 | cout << temp->data << " "; 27 | temp = temp->next; 28 | } 29 | cout << endl; 30 | } 31 | 32 | ListNode* addTwoNumbers(ListNode* head1, ListNode* head2) { 33 | ListNode* dummy = new ListNode(-1); 34 | ListNode* temp = dummy; 35 | // int sum = 0; 36 | int carry = 0; 37 | 38 | while(head1 != NULL && head2 != NULL){ 39 | 40 | int sum = head1->data + head2->data + carry; 41 | 42 | if(sum >= 10){ 43 | carry = 1; 44 | sum = sum - 10; 45 | ListNode* nextNode = new ListNode(sum); 46 | temp->next = nextNode; 47 | temp = temp->next; 48 | head1 = head1->next; 49 | head2 = head2->next; 50 | }else{ 51 | carry = 0; 52 | ListNode* nextNode = new ListNode(sum); 53 | temp->next = nextNode; 54 | temp = temp->next; 55 | head1 = head1->next; 56 | head2 = head2->next; 57 | } 58 | } 59 | 60 | while(head1 != NULL){ 61 | int sum = head1->data + carry; 62 | if(sum >= 10){ 63 | carry = 1; 64 | sum = sum - 10; 65 | ListNode* nextNode = new ListNode(sum); 66 | temp->next = nextNode; 67 | temp = temp->next; 68 | head1 = head1->next; 69 | }else{ 70 | carry = 0; 71 | ListNode* nextNode = new ListNode(sum); 72 | temp->next = nextNode; 73 | temp = temp->next; 74 | head1 = head1->next; 75 | } 76 | } 77 | 78 | while(head2 != NULL){ 79 | int sum = head2->data + carry; 80 | if(sum >= 10){ 81 | carry = 1; 82 | sum = sum - 10; 83 | ListNode* nextNode = new ListNode(sum); 84 | temp->next = nextNode; 85 | temp = temp->next; 86 | head2 = head2->next; 87 | }else{ 88 | carry = 0; 89 | ListNode* nextNode = new ListNode(sum); 90 | temp->next = nextNode; 91 | temp = temp->next; 92 | head2 = head2->next; 93 | } 94 | } 95 | 96 | if(carry != 0){ 97 | ListNode* nextNode = new ListNode(carry); 98 | temp->next = nextNode; 99 | temp = temp->next; 100 | } 101 | 102 | // delete dummy 103 | ListNode* prev = dummy; 104 | dummy = dummy->next; 105 | prev->next = NULL; 106 | delete prev; 107 | 108 | return dummy; 109 | } 110 | 111 | int main(){ 112 | ListNode* node1 = new ListNode(7); 113 | 114 | ListNode* head1 = node1; 115 | ListNode* tail1 = node1; 116 | 117 | insertAtTail(tail1, 5); 118 | insertAtTail(tail1, 9); 119 | insertAtTail(tail1, 4); 120 | insertAtTail(tail1, 6); 121 | 122 | print(head1); 123 | 124 | ListNode* node2 = new ListNode(8); 125 | 126 | ListNode* head2 = node2; 127 | ListNode* tail2 = node2; 128 | 129 | insertAtTail(tail2, 4); 130 | 131 | print(head2); 132 | 133 | head1 = addTwoNumbers(head1, head2); 134 | 135 | print(head1); 136 | 137 | 138 | return 0; 139 | } -------------------------------------------------------------------------------- /20-Valid Parentheses: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/valid-parentheses/ 2 | //0ms 3 | class Solution { 4 | public boolean isValid(String s) { 5 | if(s.length()==1) return false; 6 | Stack stack = new Stack<>(); 7 | for(int i = 0;i < s.length();i++){ 8 | char ch=s.charAt(i); 9 | if(ch=='(' || ch=='{' || ch=='['){ 10 | stack.push(ch); 11 | } 12 | if(ch==')'){ 13 | if(stack.isEmpty()){ 14 | return false; 15 | } 16 | else if(stack.peek()=='('){ 17 | stack.pop(); 18 | } 19 | else { 20 | return false; 21 | } 22 | } 23 | if(ch==']' ){ 24 | if(stack.isEmpty()){ 25 | return false; 26 | } 27 | else if(stack.peek()=='['){ 28 | stack.pop(); 29 | } 30 | else{ 31 | return false; 32 | } 33 | } 34 | if(ch=='}'){ 35 | if(stack.isEmpty()){ 36 | return false; 37 | } 38 | else if(stack.peek()=='{'){ 39 | stack.pop(); 40 | } 41 | else{ 42 | return false; 43 | } 44 | } 45 | } 46 | if(stack.isEmpty()){ 47 | return true; 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /204_count_primes.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public int countPrimes(int n) { 3 | if(n <=1 ) return 0; 4 | 5 | boolean[] notPrime = new boolean[n]; 6 | notPrime[0] = true; 7 | notPrime[1] = true; 8 | 9 | for(int i = 2; i < Math.sqrt(n); i++){ 10 | if(!notPrime[i]){ 11 | for(int j = 2; j*i < n; j++){ 12 | notPrime[i*j] = true; 13 | } 14 | } 15 | } 16 | 17 | int count = 0; 18 | for(int i = 2; i< notPrime.length; i++){ 19 | if(!notPrime[i]) count++; 20 | } 21 | return count; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /2149. rearrange array elements by sign.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector rearrangeArray(vector& nums) { 4 | int x= nums.size(); 5 | vector a(x); 6 | int i=0,j=1; 7 | for(int k=0;k0){ 9 | a[i]=nums[k]; 10 | i+=2; 11 | } 12 | else{ 13 | a[j]=nums[k]; 14 | j+=2; 15 | } 16 | } 17 | return a; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /238.odd-even-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // 328. Odd Even Linked List 2 | #include 3 | using namespace std; 4 | 5 | class ListNode { 6 | public: 7 | int data; 8 | ListNode* next; 9 | 10 | public: 11 | ListNode(int data){ 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | ListNode* oddEvenList(ListNode* head) { 18 | 19 | if(head == NULL || head->next == NULL || head->next->next == NULL) 20 | return head; 21 | 22 | ListNode* end = head; 23 | int count = 0; 24 | 25 | while(end->next != NULL) { 26 | end = end->next; 27 | count++; 28 | } 29 | 30 | int counter = (count & 1) ? (count/2 + 1) : (count/2); 31 | ListNode* temp = head; 32 | 33 | while(counter--) { 34 | 35 | end->next = temp->next; 36 | temp->next = temp->next->next; 37 | end->next->next = NULL; 38 | temp = temp->next; 39 | end = end->next; 40 | } 41 | 42 | return head; 43 | } -------------------------------------------------------------------------------- /25. Reverse Nodes in k-Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverse(ListNode *start, ListNode *end) 4 | { 5 | ListNode *prev = NULL; 6 | ListNode *curr = start; 7 | ListNode *nexty = start->next; 8 | 9 | while(prev!=end) 10 | { 11 | curr->next = prev; 12 | prev = curr; 13 | curr = nexty; 14 | 15 | if(nexty!=NULL) 16 | { 17 | nexty = nexty->next; 18 | } 19 | } 20 | 21 | } 22 | 23 | ListNode* reverseKGroup(ListNode* head, int k) { 24 | 25 | if(head==NULL || head->next == NULL || k==1) 26 | return head; 27 | 28 | ListNode *dummy = new ListNode(-1); 29 | dummy->next = head; 30 | ListNode *beforeStart = dummy, *end = head; 31 | int i=0; 32 | while(end!=NULL) 33 | { 34 | i++; 35 | if(i%k==0) 36 | { 37 | //reversal 38 | ListNode *start = beforeStart->next, *temp = end->next; 39 | reverse(start,end); 40 | beforeStart->next = end; 41 | start->next = temp; 42 | beforeStart = start; 43 | end = temp; 44 | } 45 | else 46 | { 47 | end = end->next; 48 | } 49 | 50 | } 51 | return dummy->next; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /268.Missing_Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] nums) { 3 | int n = nums.length; 4 | int sum = (n * (n+1)) / 2; 5 | for(int i=0; i int: 3 | l=0 4 | res=0 5 | charset=set() 6 | for r in range(len(s)): 7 | while(s[r] in charset): 8 | charset.remove(s[l]) 9 | l+=1 10 | charset.add(s[r]) 11 | res=max(res,r-l+1) 12 | return (res) 13 | -------------------------------------------------------------------------------- /3.Longest_Substring.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | int[] charMap = new int[256]; 4 | Arrays.fill(charMap, -1); 5 | int i = 0, maxLen = 0; 6 | for (int j = 0; j < s.length(); j++) { 7 | if (charMap[s.charAt(j)] >= i) { 8 | i = charMap[s.charAt(j)] + 1; 9 | } 10 | charMap[s.charAt(j)] = j; 11 | maxLen = Math.max(j - i + 1, maxLen); 12 | } 13 | return maxLen; 14 | } 15 | } -------------------------------------------------------------------------------- /3.longest-substring-without-repeating-characters.cpp: -------------------------------------------------------------------------------- 1 | // 3. Longest Substring Without Repeating Characters 2 | #include 3 | using namespace std; 4 | 5 | // Approach 1 O(2n) 6 | int uniqueSubstrings1(string s){ 7 | // int ans = 0; 8 | // int maxi = 0; 9 | int i = 0, j = 0, n = s.size(), ans = 0; 10 | set charSet; 11 | 12 | while(i map; 32 | 33 | // while(left mp,string s,int el){ 4 | int i = st; 5 | while(i<=en){ 6 | string t = s.substr(i,el); 7 | //cout< findSubstring(string s, vector& w) { 22 | map mp; 23 | int m = w.size(); 24 | int el = w[0].size(); 25 | int n = s.size(); 26 | int i = 0; 27 | vector res; 28 | for(auto x:w){ 29 | mp[x]++; 30 | } 31 | while(i=n)break; 35 | if(check(st,en,0,mp,s,el)){ 36 | res.push_back(i); 37 | i++; 38 | }else{ 39 | i++; 40 | } 41 | } 42 | return res; 43 | } 44 | }; -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def increasingTriplet(self, nums: List[int]) -> bool: 3 | first = math.inf 4 | second = math.inf 5 | 6 | for num in nums: 7 | if num <= first: 8 | first = num 9 | elif num <= second: # First < num <= second 10 | second = num 11 | else: 12 | return True # First < second < num (third) 13 | 14 | return False 15 | -------------------------------------------------------------------------------- /34. Find First and Last Position of Element in Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | const int l = lower_bound(begin(nums), end(nums), target) - begin(nums); 5 | if (l == nums.size() || nums[l] != target) 6 | return {-1, -1}; 7 | const int r = upper_bound(begin(nums), end(nums), target) - begin(nums) - 1; 8 | return {l, r}; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /37.Sudoku Solver.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool check(int i, int j, int k, vector> &board) 5 | { 6 | for (int p = 0; p < 9; p++) 7 | { 8 | if (board[i][p] - '0' == k) 9 | return false; 10 | if (board[p][j] - '0' == k) 11 | return false; 12 | } 13 | int x = i / 3 * 3, y = j / 3 * 3; 14 | for (int p = x; p < x + 3; p++) 15 | { 16 | for (int r = y; r < y + 3; r++) 17 | { 18 | if (board[p][r] - '0' == k) 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | bool solve(vector> &board) 25 | { 26 | for (int i = 0; i < 9; i++) 27 | { 28 | for (int j = 0; j < 9; j++) 29 | { 30 | if (board[i][j] == '.') 31 | { 32 | for (int k = 1; k <= 9; k++) 33 | { 34 | if (check(i, j, k, board)) 35 | { 36 | board[i][j] = '0' + k; 37 | if (solve(board)) 38 | return true; 39 | board[i][j] = '.'; 40 | } 41 | } 42 | return false; 43 | } 44 | } 45 | } 46 | return true; 47 | } 48 | void solveSudoku(vector> &board) 49 | { 50 | solve(board); 51 | } 52 | }; -------------------------------------------------------------------------------- /39. Combination Sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void helper(vector& candidates, int target, int sumtillnow, vector&subset, vector>&ans, int i) 4 | { 5 | if(sumtillnow==target) 6 | { 7 | ans.push_back(subset); 8 | return; 9 | } 10 | if(sumtillnow>target) return; 11 | if(i>=candidates.size()) return; 12 | helper(candidates, target, sumtillnow, subset, ans, i+1); 13 | //pick 14 | sumtillnow+=candidates[i]; 15 | subset.push_back(candidates[i]); 16 | helper(candidates, target, sumtillnow, subset, ans, i); 17 | sumtillnow-=candidates[i]; 18 | subset.pop_back(); 19 | 20 | } 21 | vector> combinationSum(vector& candidates, int target) { 22 | vectorsubset; 23 | vector>ans; 24 | int sumtillnow=0; 25 | sort(candidates.begin(), candidates.end()); 26 | helper(candidates, target, sumtillnow, subset, ans, 0); 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /392.is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | // 392. Is Subsequence 2 | #include 3 | using namespace std; 4 | 5 | void solve(string &t, vector &ans, int index, string &temp) { 6 | 7 | if(index == t.size()) { 8 | ans.push_back(temp); 9 | return; 10 | } 11 | 12 | // exclude 13 | solve(t, ans, index+1, temp); 14 | 15 | // inclusion 16 | temp.push_back(t[index]); 17 | solve(t, ans, index+1, temp); 18 | temp.pop_back(); 19 | } 20 | 21 | bool isSubsequence(string s, string t) { 22 | 23 | vector ans; 24 | string temp = ""; 25 | 26 | solve(t, ans, 0, temp); 27 | 28 | for(int i=0; i= s.size()) 40 | return true; 41 | 42 | if(i < s.size() && j >= t.size()) 43 | return false; 44 | 45 | if(s[i] == t[j]) 46 | return solve(s, t, i+1, j+1); 47 | 48 | else 49 | return solve(s, t, i, j+1); 50 | } 51 | 52 | bool isSubsequence1(string s, string t) { 53 | return solve2(s, t, 0, 0); 54 | } 55 | int main() { 56 | 57 | string s = "abc"; 58 | string t = "ahbgdc"; 59 | 60 | cout << isSubsequence(s, t); 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /4.Median_of_two_Sorted_array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 3 | int p1 = 0, p2 = 0, pos = 0; 4 | int ls1 = nums1.length, ls2 = nums2.length; 5 | int[] all_nums = new int[ls1+ls2]; 6 | double median = 0.0; 7 | while (p1 < ls1 && p2 < ls2){ 8 | if (nums1[p1] <= nums2[p2]) 9 | all_nums[pos++] = nums1[p1++]; 10 | else 11 | all_nums[pos++] = nums2[p2++]; 12 | } 13 | while (p1 < ls1) 14 | all_nums[pos++] = nums1[p1++]; 15 | while (p2 < ls2) 16 | all_nums[pos++] = nums2[p2++]; 17 | if ((ls1 + ls2) % 2 == 1) 18 | median = all_nums[(ls1 + ls2) / 2]; 19 | else 20 | median = (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2.0; 21 | return median; 22 | } 23 | } -------------------------------------------------------------------------------- /43. Multiply Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string multiply(string num1, string num2) { 4 | string s(num1.length() + num2.length(), '0'); 5 | 6 | for (int i = num1.length() - 1; i >= 0; --i) 7 | for (int j = num2.length() - 1; j >= 0; --j) { 8 | const int mult = (num1[i] - '0') * (num2[j] - '0'); 9 | const int sum = mult + (s[i + j + 1] - '0'); 10 | s[i + j] += sum / 10; 11 | s[i + j + 1] = '0' + sum % 10; 12 | } 13 | 14 | const int i = s.find_first_not_of('0'); 15 | return i == -1 ? "0" : s.substr(i); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /48.RotateImage.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | for(int i=0;i end - start) { 11 | start = i - (len - 1) / 2; 12 | end = i + len / 2; 13 | } 14 | } 15 | return s.substring(start, end + 1); 16 | } 17 | 18 | private int expandAroundCenter(String s, int left, int right) { 19 | int L = left, R = right; 20 | while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { 21 | L--; 22 | R++; 23 | } 24 | return R - L - 1; 25 | } 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /53. Maximum Subarray: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int sum = 0; 5 | int maxi = INT_MIN; 6 | for(int i=0; i next == NULL || k==0) 18 | return head; 19 | while(curr -> next != NULL) 20 | { 21 | len++; 22 | curr = curr -> next; 23 | } 24 | curr -> next = head; 25 | k = k % len; 26 | k = len - k; 27 | while(k-- > 0) 28 | { 29 | curr = curr -> next; 30 | } 31 | head = curr -> next; 32 | curr -> next = NULL; 33 | 34 | return head; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /61. Rotate List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* rotateRight(ListNode* head, int k) { 4 | if (!head || !head->next || k == 0) 5 | return head; 6 | 7 | ListNode* tail; 8 | int length = 1; 9 | for (tail = head; tail->next; tail = tail->next) 10 | ++length; 11 | tail->next = head; // Circle the list 12 | 13 | const int t = length - k % length; 14 | for (int i = 0; i < t; ++i) 15 | tail = tail->next; 16 | ListNode* newHead = tail->next; 17 | tail->next = nullptr; 18 | 19 | return newHead; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /659-split-array-into-consecutive-subsequences/659-split-array-into-consecutive-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | typedef long long ll; 3 | 4 | public: 5 | bool isPossible(vector &nums) 6 | { 7 | 8 | map mp, mp2; 9 | for (ll i = 0; i < nums.size(); i++) 10 | { 11 | mp[nums[i]] = mp[nums[i]] + 1; 12 | } 13 | 14 | for (ll i = 0; i < nums.size(); i++) 15 | { 16 | // cout << nums[i] << " "; 17 | if (mp2[nums[i]] && mp[nums[i]]) 18 | { 19 | mp[nums[i]] = mp[nums[i]] - 1; 20 | mp2[nums[i]] = mp2[nums[i]] - 1; 21 | mp2[nums[i] + 1] = mp2[nums[i] + 1] + 1; 22 | } 23 | else 24 | { 25 | if (mp[nums[i]]) 26 | { 27 | mp[nums[i]] = mp[nums[i]] - 1; 28 | if (mp[nums[i] + 1]) 29 | mp[nums[i] + 1] = mp[nums[i] + 1] - 1; 30 | else 31 | return false; 32 | if (mp[nums[i] + 2]) 33 | mp[nums[i] + 2] = mp[nums[i] + 2] - 1; 34 | else 35 | return false; 36 | mp2[nums[i] + 3] = mp2[nums[i] + 3] + 1; 37 | } 38 | } 39 | // for (ll i = 0; i < nums.size(); i++) 40 | // { 41 | // cout << mp[nums[i]] << " "; 42 | // // if(mp[nums[i]])return false; 43 | // } 44 | } 45 | for (ll i = 0; i < nums.size(); i++) 46 | { 47 | // cout<659. Split Array into Consecutive Subsequences

Medium


You are given an integer array nums that is sorted in non-decreasing order.

2 | 3 |

Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:

4 | 5 |
    6 |
  • Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
  • 7 |
  • All subsequences have a length of 3 or more.
  • 8 |
9 | 10 |

Return true if you can split nums according to the above conditions, or false otherwise.

11 | 12 |

A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).

13 | 14 |

 

15 |

Example 1:

16 | 17 |
Input: nums = [1,2,3,3,4,5]
18 | Output: true
19 | Explanation: nums can be split into the following subsequences:
20 | [1,2,3,3,4,5] --> 1, 2, 3
21 | [1,2,3,3,4,5] --> 3, 4, 5
22 | 
23 | 24 |

Example 2:

25 | 26 |
Input: nums = [1,2,3,3,4,4,5,5]
27 | Output: true
28 | Explanation: nums can be split into the following subsequences:
29 | [1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
30 | [1,2,3,3,4,4,5,5] --> 3, 4, 5
31 | 
32 | 33 |

Example 3:

34 | 35 |
Input: nums = [1,2,3,4,4,5]
36 | Output: false
37 | Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
38 | 
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • 1 <= nums.length <= 104
  • 45 |
  • -1000 <= nums[i] <= 1000
  • 46 |
  • nums is sorted in non-decreasing order.
  • 47 |
48 |
49 | -------------------------------------------------------------------------------- /69.sqrtx.cpp: -------------------------------------------------------------------------------- 1 | // 69. Sqrt(x) 2 | #include 3 | using namespace std; 4 | 5 | int mySqrt(int x) { 6 | 7 | int start = 1; 8 | int end = x; 9 | int ans; 10 | int mid = start + (end - start)/2; 11 | 12 | if (x == 0) 13 | return 0; 14 | 15 | while(start <= end) { 16 | 17 | if(mid*mid == x) 18 | return mid; 19 | 20 | if(mid*mid < x){ 21 | start = mid + 1; 22 | ans=mid; 23 | } 24 | 25 | else 26 | end = mid - 1; 27 | } 28 | return ans; 29 | } 30 | 31 | int main(){ 32 | // Input: x = 8 33 | // Output: 2 34 | cout< topKFrequent(vector& words, int k) { 5 | unordered_map hashmap; 6 | for(string& word : words) { 7 | hashmap[word] += 1; 8 | } 9 | priority_queue, vector>, MyComp> pq; 10 | for(auto it = hashmap.begin(); it != hashmap.end(); ++it) { 11 | pq.push(make_pair(it->second, it->first)); 12 | if(pq.size() > k) pq.pop(); 13 | } 14 | vector res; 15 | while(!pq.empty()) { 16 | // res.insert(res.begin(), pq.top().second); 17 | res.push_back(pq.top().second); // push the results in increasing, and reverse later 18 | pq.pop(); 19 | } 20 | reverse(res.begin(), res.end()); 21 | return res; 22 | } 23 | private: 24 | struct MyComp { 25 | bool operator() (const pair& a, const pair& b) { 26 | if(a.first != b.first) { 27 | return a.first > b.first; 28 | } 29 | else { 30 | return a.second < b.second; 31 | } 32 | } 33 | }; 34 | }; 35 | 36 | -------------------------------------------------------------------------------- /7.Reverse.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | if (x == 0) return 0; 4 | long res = 0; 5 | while (x != 0) { 6 | res = res * 10 + x % 10; 7 | if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) 8 | return 0; 9 | x /= 10; 10 | } 11 | return (int) res; 12 | } 13 | } -------------------------------------------------------------------------------- /7.reverseInteger.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long int num = 0; 5 | //2147483647 6 | if( x >= 2147483647 || x <= -2147483648) 7 | return 0; 8 | if(x > 0){ 9 | while(x > 0){ 10 | num = num * 10; 11 | if( num >= 2147483647 || num <= -2147483648) 12 | return 0; 13 | num = num + (x % 10); 14 | x = x / 10; 15 | } 16 | } 17 | else{ 18 | x = -1 * x; 19 | while(x > 0){ 20 | num = num * 10; 21 | if( num >= 2147483647 || num <= -2147483648) 22 | return 0; 23 | num = num + (x % 10); 24 | x = x / 10; 25 | } 26 | num = -1 * num; 27 | } 28 | return int(num); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /704.BinarySearch/.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& a, int k) { 4 | int low = 0, high = a.size() - 1, mid; 5 | while(low <= high){ 6 | mid = (low + high) / 2; 7 | if(a[mid] == k)return mid; 8 | else if(a[mid] > k)high = mid - 1; 9 | else low = mid + 1; 10 | } 11 | return -1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /707. Design Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class MyLinkedList { 6 | 7 | private: 8 | class Node{ 9 | public: 10 | int val; 11 | Node* prev; 12 | Node* next; 13 | 14 | Node(int val, Node* prev, Node* next): val(val), prev(prev), next(next){} 15 | Node(int val): Node(val, NULL, NULL){} 16 | }; 17 | 18 | Node* dummyHead; 19 | 20 | public: 21 | /** Initialize your data structure here. */ 22 | MyLinkedList() { 23 | dummyHead = new Node(-1); 24 | } 25 | 26 | /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ 27 | int get(int index) { 28 | 29 | Node* cur = dummyHead->next; 30 | for(int i = 0; i < index && cur; i ++) 31 | cur = cur->next; 32 | 33 | if(!cur) return -1; 34 | return cur->val; 35 | } 36 | 37 | /** 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. */ 38 | void addAtHead(int val) { 39 | dummyHead->next = new Node(val, dummyHead, dummyHead->next); 40 | } 41 | 42 | /** Append a node of value val to the last element of the linked list. */ 43 | void addAtTail(int val) { 44 | 45 | Node* pre = dummyHead; 46 | while(pre->next) 47 | pre = pre->next; 48 | pre->next = new Node(val, pre, NULL); 49 | } 50 | 51 | /** 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. */ 52 | void addAtIndex(int index, int val) { 53 | 54 | Node* pre = dummyHead; 55 | for(int i = 0; i < index && pre; i ++) 56 | pre = pre->next; 57 | 58 | if(pre) 59 | pre->next = new Node(val, pre, pre->next); 60 | } 61 | 62 | /** Delete the index-th node in the linked list, if the index is valid. */ 63 | void deleteAtIndex(int index) { 64 | 65 | Node* pre = dummyHead; 66 | for(int i = 0; i < index && pre; i ++) 67 | pre = pre->next; 68 | 69 | if(pre && pre->next){ 70 | Node* delNode = pre->next; 71 | pre->next = delNode->next; 72 | if(pre->next) 73 | pre->next->prev = pre; 74 | delete delNode; 75 | } 76 | } 77 | }; 78 | 79 | 80 | int main() { 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /73-setmatrixzero.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void setZeroes(int[][] matrix) { 3 | int row=matrix.length; 4 | int col=matrix[0].length; 5 | int count=0; 6 | 7 | for(int i=0;i=0;f--) 30 | { 31 | if(matrix[f][j]!=0) 32 | matrix[f][j]=-1; 33 | } 34 | for(int f=i+1;f=0;f--) 40 | {if(matrix[i][f]!=0) 41 | matrix[i][f]=-1; 42 | } 43 | for(int f=j+1;f 4 | 5 | using namespace std; 6 | 7 | int search(int mat[4][4], int n, int x) 8 | { 9 | if (n == 0) 10 | return -1; 11 | 12 | int smallest = mat[0][0], largest = mat[n - 1][n - 1]; 13 | if (x < smallest || x > largest) 14 | return -1; 15 | 16 | int i = 0, j = n - 1; 17 | while (i < n && j >= 0) 18 | { 19 | if (mat[i][j] == x) 20 | { 21 | cout << "n Found at " 22 | << i << ", " << j; 23 | return 1; 24 | } 25 | if (mat[i][j] > x) 26 | j--; 27 | 28 | else 29 | i++; 30 | } 31 | 32 | cout << "n Element not found"; 33 | return 0; 34 | } 35 | 36 | // Driver code 37 | int main() 38 | { 39 | int mat[4][4] = { { 10, 20, 30, 40 }, 40 | { 15, 25, 35, 45 }, 41 | { 27, 29, 37, 48 }, 42 | { 32, 33, 39, 50 } }; 43 | search(mat, 4, 29); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /74_search_element_in_row_column_sorted_matrix.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to search an element in row-wise 2 | // and column-wise sorted matrix 3 | #include 4 | 5 | using namespace std; 6 | 7 | int search(int mat[4][4], int n, int x) 8 | { 9 | if (n == 0) 10 | return -1; 11 | 12 | int smallest = mat[0][0], largest = mat[n - 1][n - 1]; 13 | if (x < smallest || x > largest) 14 | return -1; 15 | 16 | int i = 0, j = n - 1; 17 | while (i < n && j >= 0) 18 | { 19 | if (mat[i][j] == x) 20 | { 21 | cout << "n Found at " 22 | << i << ", " << j; 23 | return 1; 24 | } 25 | if (mat[i][j] > x) 26 | j--; 27 | 28 | else 29 | i++; 30 | } 31 | 32 | cout << "n Element not found"; 33 | return 0; 34 | } 35 | 36 | // Driver code 37 | int main() 38 | { 39 | int mat[4][4] = { { 10, 20, 30, 40 }, 40 | { 15, 25, 35, 45 }, 41 | { 27, 29, 37, 48 }, 42 | { 32, 33, 39, 50 } }; 43 | search(mat, 4, 29); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /8.String_to_Integer.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private static final int maxDiv10 = Integer.MAX_VALUE / 10; 3 | public int myAtoi(String str) { 4 | int i = 0, n = str.length(); 5 | while (i < n && Character.isWhitespace(str.charAt(i))) 6 | i++; 7 | int sign = 1; 8 | if (i < n && str.charAt(i) == '+') 9 | i++; 10 | else if (i < n && str.charAt(i) == '-') { 11 | sign = -1; 12 | i++; 13 | } 14 | int num = 0; 15 | while (i < n && Character.isDigit(str.charAt(i))) { 16 | int digit = Character.getNumericValue(str.charAt(i)); 17 | if (num > maxDiv10 || num == maxDiv10 && digit >= 8) 18 | return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; 19 | num = num * 10 + digit; 20 | i++; 21 | } 22 | return sign * num; 23 | } 24 | } -------------------------------------------------------------------------------- /832.Flipping_an_Image.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] flipAndInvertImage(int[][] image) { 3 | for(int i=0;i= 10) { 30 | div *= 10; 31 | } 32 | while (x !=0) { 33 | int l = x / div; 34 | int r = x % 10; 35 | if (l != r) return false; 36 | // Remove left and right number 37 | x = (x % div) / 10; 38 | div /= 100; 39 | } 40 | return true; 41 | } 42 | } 43 | 44 | 45 | 46 | 47 | 48 | class Solution { 49 | public boolean isPalindrome(int x) { 50 | int r,s=0,number=x; 51 | if(number<0){ 52 | return false; 53 | } 54 | while (number!=0){ 55 | r=number%10; 56 | s= s*10 +r; 57 | number/=10; 58 | } 59 | if (s==x){ 60 | return true; 61 | } 62 | else { 63 | return false; 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /95. Unique Binary Search Trees II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution 13 | { 14 | public: 15 | // Copied solution 16 | vector generateTrees(int n, int s = 1) 17 | { 18 | vector ans; 19 | if (n < s) 20 | return {nullptr}; 21 | 22 | // Consider every number in range [s,n] as root 23 | for (int i = s; i <= n; i++) 24 | { 25 | 26 | // generate all possible trees in range [s,i) 27 | for (auto left : generateTrees(i - 1, s)) 28 | { 29 | 30 | // generate all possible trees in range (i,e] 31 | for (auto right : generateTrees(n, i + 1)) 32 | 33 | // make new trees with i as the root 34 | ans.push_back(new TreeNode(i, left, right)); 35 | } 36 | } 37 | return ans; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /994. Rotting_Oranges.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int orangesRotting(vector>& grid) { 4 | const int m = grid.size(); 5 | const int n = grid[0].size(); 6 | const vector dirs{0, 1, 0, -1, 0}; 7 | 8 | auto isNeighborRotten = [&](int i, int j, const vector>& grid) { 9 | for (int k = 0; k < 4; ++k) { 10 | const int r = i + dirs[k]; 11 | const int c = j + dirs[k + 1]; 12 | if (r < 0 || r == m || c < 0 || c == n) 13 | continue; 14 | if (grid[r][c] == 2) 15 | return true; 16 | } 17 | return false; 18 | }; 19 | 20 | int ans = 0; 21 | 22 | while (true) { 23 | vector> nextGrid(m, vector(n)); 24 | // Calculate `nextGrid` based on `grid` 25 | for (int i = 0; i < m; ++i) 26 | for (int j = 0; j < n; ++j) 27 | if (grid[i][j] == 1) { // Fresh 28 | if (isNeighborRotten( 29 | i, j, grid)) // Any of 4-directionally oranges is rotten 30 | nextGrid[i][j] = 2; 31 | else 32 | nextGrid[i][j] = 1; 33 | } else if (grid[i][j] == 2) { // Rotten 34 | nextGrid[i][j] = 2; // Keep rotten 35 | } 36 | if (nextGrid == grid) 37 | break; 38 | grid = nextGrid; 39 | ++ans; 40 | } 41 | 42 | return any_of( 43 | begin(grid), end(grid), 44 | [&](vector& row) { 45 | return any_of(begin(row), end(row), 46 | [&](int orange) { return orange == 1; }); 47 | }) 48 | ? -1 49 | : ans; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /All Nodes Distance K in Binary Tree: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | private: 12 | void markParents(TreeNode* root, unordered_map &mp){ 13 | queue q; 14 | q.push(root); 15 | while(!q.empty()){ 16 | TreeNode* node = q.front(); 17 | q.pop(); 18 | if(node->left){ 19 | mp[node->left] = node; 20 | q.push(node->left); 21 | } 22 | if(node->right){ 23 | mp[node->right] = node; 24 | q.push(node->right); 25 | } 26 | } 27 | } 28 | 29 | public: 30 | vector distanceK(TreeNode* root, TreeNode* target, int k) { 31 | unordered_map parent_track; 32 | markParents(root, parent_track); 33 | 34 | unordered_map visited; 35 | queue q; 36 | q.push(target); 37 | visited[target] = true; 38 | int des = 0; 39 | while(!q.empty()){ 40 | int size = q.size(); 41 | if(des++ == k) break; 42 | for(int i=0; ileft && !visited[node->left]){ 46 | q.push(node->left); 47 | visited[node->left] = true; 48 | } 49 | if(node->right && !visited[node->right]){ 50 | q.push(node->right); 51 | visited[node->right] = true; 52 | } 53 | if(parent_track[node] && !visited[parent_track[node]]){ 54 | q.push(parent_track[node]); 55 | visited[parent_track[node]] = true; 56 | } 57 | } 58 | } 59 | vector ans; 60 | while(!q.empty()){ 61 | TreeNode* node = q.front(); 62 | q.pop(); 63 | ans.push_back(node->val); 64 | } 65 | return ans; 66 | } 67 | 68 | }; 69 | -------------------------------------------------------------------------------- /BANKERS-ALGO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int P = 5; 5 | 6 | const int R = 3; 7 | 8 | void calculateNeed(int need[P][R], int maxm[P][R], 9 | int allot[P][R]) 10 | { 11 | for (int i = 0 ; i < P ; i++) 12 | for (int j = 0 ; j < R ; j++) 13 | need[i][j] = maxm[i][j] - allot[i][j]; 14 | } 15 | 16 | bool isSafe(int processes[], int avail[], int maxm[][R], 17 | int allot[][R]) 18 | { 19 | int need[P][R]; 20 | calculateNeed(need, maxm, allot); 21 | bool finish[P] = {0}; 22 | int safeSeq[P]; 23 | int work[R]; 24 | for (int i = 0; i < R ; i++) 25 | work[i] = avail[i]; 26 | 27 | int count = 0; 28 | while (count < P) 29 | { 30 | bool found = false; 31 | for (int p = 0; p < P; p++) 32 | { 33 | if (finish[p] == 0) 34 | { 35 | int j; 36 | for (j = 0; j < R; j++) 37 | if (need[p][j] > work[j]) 38 | break; 39 | if (j == R) 40 | { 41 | for (int k = 0 ; k < R ; k++) 42 | work[k] += allot[p][k]; 43 | safeSeq[count++] = p; 44 | finish[p] = 1; 45 | found = true; 46 | } 47 | } 48 | } 49 | if (found == false) 50 | { 51 | cout << "System is not in safe state"; 52 | return false; 53 | } 54 | } 55 | cout << "System is in safe state.\nSafe" 56 | " sequence is: "; 57 | for (int i = 0; i < P ; i++) 58 | cout << safeSeq[i] << " "; 59 | 60 | return true; 61 | } 62 | int main() 63 | { 64 | int processes[] = {0, 1, 2, 3, 4}; 65 | 66 | int avail[] = {3, 3, 2}; 67 | 68 | int maxm[][R] = {{7, 5, 3}, 69 | {3, 2, 2}, 70 | {9, 0, 2}, 71 | {2, 2, 2}, 72 | {4, 3, 3}}; 73 | 74 | int allot[][R] = {{0, 1, 0}, 75 | {2, 0, 0}, 76 | {3, 0, 2}, 77 | {2, 1, 1}, 78 | {0, 0, 2}}; 79 | 80 | isSafe(processes, avail, maxm, allot); 81 | return 0; 82 | } -------------------------------------------------------------------------------- /C++/Binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // recursive function for binary search 4 | /* if match found then return index of search key 5 | else return -1 */ 6 | int binarySearch(int arr[], int low, int high, int key) { 7 | if (high >= low) { 8 | // find middle index 9 | int mid = low + (high - low) / 2; 10 | 11 | // find middle term and compare 12 | if (arr[mid] == key) return mid; // key found 13 | 14 | // If key is smaller than middle term, then 15 | // it can only be present in left subarray 16 | if (arr[mid] > key) 17 | return binarySearch(arr, low, mid - 1, key); 18 | 19 | // Else the key can only be present 20 | // in right subarray 21 | return binarySearch(arr, mid + 1, high, key); 22 | } 23 | 24 | // key not found 25 | return -1; 26 | } 27 | 28 | // main function 29 | int main() 30 | { 31 | int array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 32 | int key = 0; 33 | 34 | // take input for the search key 35 | cout << "Enter Search Element: "; 36 | cin >> key; 37 | 38 | // find the size array 39 | int size = sizeof(array)/sizeof(array[0]); 40 | 41 | // search key 42 | int index = binarySearch(array, 0, size, key); 43 | 44 | // display result 45 | if(index == -1) 46 | cout << key << " Not Found" << endl; 47 | else 48 | cout << key << " Found at Index = " << index << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /C++/Factorial_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int); 5 | 6 | int main() { 7 | int n, result; 8 | 9 | cout << "Enter a non-negative number: "; 10 | cin >> n; 11 | 12 | result = factorial(n); 13 | cout << "Factorial of " << n << " = " << result; 14 | return 0; 15 | } 16 | 17 | int factorial(int n) { 18 | if (n > 1) { 19 | return n * factorial(n - 1); 20 | } else { 21 | return 1; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## How to contribute 2 | - Fork the repository from the bottom right corner and visit to your forked repository. 3 | - Clone the repository in your terminal using command 4 | git clone 5 | . 6 | - After cloning, create a branch 7 | git checkout -b 8 | . 9 | - Make contribution to the repo and add all changes using 10 | git add . 11 | or 12 | git add 13 | . 14 | - Create a commit with a commit message 15 | git commit -m 16 | . 17 | - After commiting, push the changes to origin using command 18 | git push origin 19 | . 20 | - Create a pull request and wait for your pull request to be merged. 21 | 22 | ## Things to keep in mind while contributing 23 | - Make sure the algorithm you are contributing is not already there in the repo otherwise your PR will not be merged. 24 | - ALso include problem statement in your algorithm file. 25 | - Contribute in the correct folder only. 26 | ## How to contribute 27 | - Fork the repository from the bottom right corner and visit to your forked repository. 28 | - Clone the repository in your terminal using command 29 | git clone 30 | - After cloning, create a branch 31 | git checkout -b 32 | - Make contribution to the repo and add all changes using 33 | git add . 34 | or 35 | git add 36 | - Create a commit with a commit message 37 | git commit -m 38 | - After commiting, push the changes to origin using command 39 | git push origin 40 | - Create a pull request and wait for your pull request to be merged. 41 | 42 | ## Things to keep in mind while contributing 43 | - Make sure the algorithm you are contributing is not already there in the repo otherwise your PR will not be merged. 44 | - ALso include problem statement in your algorithm file. 45 | - Contribute in the correct folder only. 46 | -------------------------------------------------------------------------------- /CircularDoubleLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class DList; 7 | 8 | /* 9 | DNode class containing information and pointer to the next node. 10 | */ 11 | 12 | template 13 | class DNode 14 | { 15 | public: 16 | friend DList; 17 | T info; 18 | DNode* next; 19 | DNode* prev; 20 | 21 | DNode(T inInfo, DNode* nPtr = NULL, DNode* pPtr = NULL) 22 | { 23 | info = inInfo; 24 | next = nPtr; 25 | prev = pPtr; 26 | } 27 | }; 28 | 29 | /* 30 | Double Linked List class: 31 | A Double linked list is a linear dynamic data structure to store data items. 32 | Every node is mainly divided into three parts, one part holds the data and the other two parts 33 | is connected to a different node. One next and one to the previous 34 | We can add, find and delete nodes. 35 | */ 36 | 37 | template 38 | class DList 39 | { 40 | private: 41 | DNode * head; 42 | DNode * tail; 43 | public: 44 | DList(); 45 | DList(const DList&); 46 | void setHead(DNode* head); 47 | void setTail(DNode* tail); 48 | DNode* getHead(); 49 | DNode* getTail(); 50 | bool isEmpty(); 51 | void insertAtHead(T); 52 | void insertAtTail(T); 53 | bool deleteAtHead(); 54 | bool deleteAtTail(); 55 | void printList(); 56 | DNode* getNode(unsigned int n); 57 | DNode* search(T); 58 | bool insertAfter(T, T); 59 | bool insertBefore(T, T); 60 | bool deleteBefore(T); 61 | bool deleteAfter(T); 62 | int getLength(); 63 | ~DList(); 64 | 65 | 66 | }; 67 | 68 | 69 | 70 | template 71 | DList::DList() :head(NULL), tail(NULL) //Empty --- NULL 72 | { 73 | } 74 | 75 | template 76 | DList::DList(const DList& L) 77 | { 78 | DNode* temp = L.head; 79 | while (temp != tail) 80 | { 81 | insertAtTail(temp->info); 82 | temp = temp->next; 83 | } 84 | insertAtTail(temp->info); 85 | } 86 | 87 | template 88 | void DList::setHead(DNode* head) 89 | { 90 | if (isEmpty()) //NewNode is head & tail 91 | { 92 | this->head = this->tail = head; 93 | tail->next = head; 94 | } 95 | } 96 | 97 | template 98 | void DList::setTail(DNode* tail) 99 | { 100 | if (isEmpty()) //New node is head & tail 101 | { 102 | this->head = this->tail = tail; 103 | tail->next = head; 104 | } 105 | 106 | else 107 | { 108 | tail->prev = this->tail; 109 | this->tail->next = tail; 110 | this->tail = tail; 111 | this->tail->next = head; 112 | } 113 | } 114 | 115 | template 116 | DNode* DList::getHead() 117 | { 118 | return head; 119 | } 120 | 121 | template 122 | DNode* DList::getTail() 123 | { 124 | return tail; 125 | } 126 | 127 | template 128 | bool DList::isEmpty() 129 | { 130 | return head == NULL; 131 | } 132 | 133 | template 134 | void DList::insertAtHead(T value) 135 | { 136 | /* 137 | This function inserts a node in the start of the list. 138 | */ 139 | 140 | DNode* newNode = new DNode(value); 141 | 142 | if (isEmpty()) //NewNode is head & tail 143 | { 144 | this->head = this->tail = newNode; 145 | tail->next = head; 146 | } 147 | else 148 | { 149 | this->head->prev = newNode; 150 | newNode->next = this->head; 151 | this->head = newNode; 152 | tail->next = head; 153 | } 154 | } 155 | 156 | template 157 | void DList::insertAtTail(T value) 158 | { 159 | /* 160 | This function inserts a node at the end of the list. 161 | */ 162 | 163 | DNode* temp = new DNode(value); 164 | 165 | if (isEmpty()) //New node is head & tail 166 | { 167 | head = tail = temp; 168 | tail->next = head; 169 | } 170 | 171 | else 172 | { 173 | temp->prev = tail; 174 | tail->next = temp; 175 | tail = temp; 176 | tail->next = head; 177 | } 178 | } 179 | 180 | template 181 | bool DList::deleteAtHead() 182 | { 183 | 184 | /* 185 | This function delets a node from the start of the list. 186 | */ 187 | 188 | if (!isEmpty()) 189 | { 190 | DNode* temp = head; 191 | head = head->next; 192 | delete temp; 193 | if (isEmpty()) { 194 | tail = NULL; 195 | head = NULL; 196 | tail->next = head; 197 | 198 | } 199 | else { 200 | head->prev = NULL; 201 | tail->next = head; 202 | } 203 | return true; 204 | } 205 | 206 | else 207 | return false; 208 | } 209 | 210 | template 211 | bool DList::deleteAtTail() 212 | { 213 | /* 214 | This function deletes a node from the end of the list. 215 | */ 216 | 217 | if (!isEmpty()) { 218 | DNode* temp = tail; //To be pointed at tail 219 | DNode* temp1 = tail->prev; //To be pointed 1 step behind the tail 220 | 221 | tail = temp1; 222 | 223 | if (tail) { 224 | tail->next = head; 225 | } 226 | delete temp; 227 | 228 | if (tail == NULL) { 229 | head = NULL; 230 | tail->next = NULL; 231 | } 232 | return true; 233 | } 234 | 235 | else 236 | return false; 237 | } 238 | 239 | template 240 | void DList::printList() 241 | { 242 | /* 243 | This function prints the list. 244 | */ 245 | 246 | DNode* temp = head; 247 | while (temp != tail) 248 | { 249 | cout << temp->info << " --> "; 250 | temp = temp->next; 251 | } 252 | cout << temp->info << endl; 253 | } 254 | 255 | template 256 | DNode* DList::getNode(unsigned int n) 257 | { 258 | /* 259 | This function returns the nth index's node. Where n can be 0 to N. 0 is the first node and so on. 260 | */ 261 | 262 | if (!isEmpty()) { 263 | int size = getLength(); 264 | if (n >= size) 265 | return tail; 266 | 267 | DNode* temp = head; 268 | for (int i = 0; i < n; i++) { 269 | temp = temp->next; 270 | } 271 | return temp; 272 | } 273 | 274 | else 275 | return nullptr; 276 | } 277 | 278 | template 279 | bool DList::insertAfter(T value, T key) 280 | { 281 | /* 282 | This function inserts a node after the founded value. 283 | */ 284 | 285 | DNode* found = search(key); //founded node 286 | if (found) 287 | { 288 | 289 | if (found == tail) { 290 | insertAtTail(value); 291 | } 292 | 293 | else { 294 | DNode* newNode = new DNode(value); 295 | newNode->next = found->next; 296 | found->next->prev = newNode; 297 | found->next = newNode; 298 | newNode->prev = found; 299 | } 300 | 301 | return true; 302 | } 303 | 304 | else 305 | return false; 306 | } 307 | 308 | template 309 | bool DList::insertBefore(T value, T key) 310 | { 311 | /* 312 | This function inserts a node before the founded value. 313 | */ 314 | 315 | DNode* found = search(key); //founded node 316 | 317 | if (found) 318 | { 319 | if (found == head) 320 | insertAtHead(value); 321 | 322 | else { 323 | DNode* temp = found->prev; //one behind founded Node 324 | DNode* newNode = new DNode(value); 325 | newNode->next = found; 326 | found->prev = newNode; 327 | temp->next = newNode; 328 | newNode->prev = temp; 329 | } 330 | 331 | return true; 332 | } 333 | 334 | else 335 | return false; 336 | } 337 | 338 | template 339 | bool DList::deleteBefore(T key) 340 | { 341 | /* 342 | This function deletes a node before the founded value. 343 | */ 344 | DNode* found = search(key); //founded node 345 | if (found) { 346 | DNode* temp = found->prev; //1 behind finded node/node to be deleted 347 | 348 | if (temp == head) { 349 | deleteAtHead(); 350 | return true; 351 | } 352 | 353 | else if (temp) { 354 | DNode* temp1 = temp->prev; //2 behind finded node 355 | temp1->next = found; 356 | found->prev = temp1; 357 | delete temp; 358 | return true; 359 | } 360 | 361 | return false; 362 | } 363 | 364 | else 365 | return false; 366 | } 367 | 368 | template 369 | bool DList::deleteAfter(T key) 370 | { 371 | /* 372 | This function deletes a node after the founded value. 373 | */ 374 | 375 | DNode* found = search(key); 376 | if (found) 377 | { 378 | DNode* temp = found->next; 379 | if (found == tail) 380 | return false; 381 | 382 | else if (temp) { 383 | if (temp == tail) 384 | deleteAtTail(); 385 | 386 | else 387 | { 388 | found->next = temp->next; 389 | temp->next->prev = found; 390 | delete temp; 391 | } 392 | 393 | return true; 394 | } 395 | 396 | else 397 | return false; 398 | } 399 | 400 | else 401 | return false; 402 | } 403 | 404 | template 405 | DNode* DList::search(T key) 406 | { 407 | /* 408 | This function returns the founded value's node. 409 | */ 410 | 411 | DNode* temp = head; 412 | while (temp != tail) 413 | { 414 | if (temp->info == key) 415 | return temp; 416 | else 417 | temp = temp->next; 418 | } 419 | 420 | if (temp->info == key) 421 | return temp; 422 | 423 | return NULL; 424 | } 425 | 426 | template 427 | int DList::getLength() 428 | { 429 | /* 430 | This function returns the length of list. 431 | */ 432 | 433 | int count = 0; 434 | if (!isEmpty()) { 435 | DNode* temp = head; 436 | while (temp != tail) { 437 | temp = temp->next; 438 | count++; 439 | } 440 | return ++count; 441 | } 442 | 443 | else 444 | return count; 445 | } 446 | 447 | template 448 | DList::~DList() 449 | { 450 | //deletes the list. 451 | DNode* temp = head; 452 | while (temp != tail) 453 | { 454 | head = head->next; 455 | delete temp; 456 | temp = head; 457 | } 458 | delete temp; 459 | 460 | } 461 | -------------------------------------------------------------------------------- /CombinationSum.java: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | // https://leetcode.com/problems/combination-sum/ 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class CombinationSum { 8 | public static void main(String[] args) { 9 | int[] candidates = {2,3,6,7}; 10 | List> sum = combinationSum(candidates, 7); 11 | System.out.println(sum); 12 | } 13 | public static List> combinationSum(int[] candidates, int target) { 14 | List> ans = new ArrayList<>(); 15 | findCombination(0, candidates, target, ans, new ArrayList<>()); 16 | return ans; 17 | } 18 | public static void findCombination(int index, int[] arr, int target, List> ans, List ds){ 19 | if(index == arr.length){ 20 | if(target == 0){ 21 | ans.add(new ArrayList<>(ds)); 22 | } 23 | return; 24 | } 25 | 26 | if(arr[index] <= target){ 27 | ds.add(arr[index]); 28 | findCombination(index, arr, target - arr[index], ans, ds); 29 | ds.remove(ds.size()-1); 30 | } 31 | findCombination(index+1, arr, target, ans, ds); 32 | } 33 | } 34 | ======= 35 | // https://leetcode.com/problems/combination-sum/ 36 | import java.util.ArrayList; 37 | import java.util.Arrays; 38 | import java.util.List; 39 | 40 | public class CombinationSum { 41 | public static void main(String[] args) { 42 | int[] candidates = {2,3,6,7}; 43 | List> sum = combinationSum(candidates, 7); 44 | System.out.println(sum); 45 | } 46 | public static List> combinationSum(int[] candidates, int target) { 47 | List> ans = new ArrayList<>(); 48 | findCombination(0, candidates, target, ans, new ArrayList<>()); 49 | return ans; 50 | } 51 | public static void findCombination(int index, int[] arr, int target, List> ans, List ds){ 52 | if(index == arr.length){ 53 | if(target == 0){ 54 | ans.add(new ArrayList<>(ds)); 55 | } 56 | return; 57 | } 58 | 59 | if(arr[index] <= target){ 60 | ds.add(arr[index]); 61 | findCombination(index, arr, target - arr[index], ans, ds); 62 | ds.remove(ds.size()-1); 63 | } 64 | findCombination(index+1, arr, target, ans, ds); 65 | } 66 | } 67 | >>>>>>> 67aafb10a70e470ee512334062930bef20542fa0 68 | -------------------------------------------------------------------------------- /Convert_Sorted_Array_To_BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* solve(vector &nums, int start, int end){ 4 | //base case 5 | if(start > end){ 6 | return NULL; 7 | } 8 | 9 | int mid = (start + end)/2; 10 | TreeNode* temp = new TreeNode(nums[mid]); 11 | 12 | temp -> left = solve(nums, start, mid-1); 13 | temp -> right = solve(nums, mid+1, end); 14 | return temp; 15 | } 16 | 17 | TreeNode* sortedArrayToBST(vector& nums) { 18 | return solve(nums, 0, nums.size()-1); 19 | } 20 | }; -------------------------------------------------------------------------------- /Custom-Sort-String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string customSortString(string order, string s) 4 | { 5 | unordered_mapmp; 6 | for(auto it:s) 7 | mp[it]++; 8 | 9 | string ans=""; 10 | for(int i=0;i next; 11 | } 12 | 13 | if(b == NULL){ 14 | b = headA; 15 | } 16 | else{ 17 | b = b -> next; 18 | } 19 | } 20 | return a; 21 | } 22 | }; -------------------------------------------------------------------------------- /JAVA-20-Valid_Parentheses: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/valid-parentheses/ 2 | //0ms 3 | class Solution { 4 | public boolean isValid(String s) { 5 | if(s.length()==1) return false; 6 | Stack stack = new Stack<>(); 7 | for(int i = 0;i < s.length();i++){ 8 | char ch=s.charAt(i); 9 | if(ch=='(' || ch=='{' || ch=='['){ 10 | stack.push(ch); 11 | } 12 | if(ch==')'){ 13 | if(stack.isEmpty()){ 14 | return false; 15 | } 16 | else if(stack.peek()=='('){ 17 | stack.pop(); 18 | } 19 | else { 20 | return false; 21 | } 22 | } 23 | if(ch==']' ){ 24 | if(stack.isEmpty()){ 25 | return false; 26 | } 27 | else if(stack.peek()=='['){ 28 | stack.pop(); 29 | } 30 | else{ 31 | return false; 32 | } 33 | } 34 | if(ch=='}'){ 35 | if(stack.isEmpty()){ 36 | return false; 37 | } 38 | else if(stack.peek()=='{'){ 39 | stack.pop(); 40 | } 41 | else{ 42 | return false; 43 | } 44 | } 45 | } 46 | if(stack.isEmpty()){ 47 | return true; 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Kth_Smallest_Element_in_BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(TreeNode* root, int &i, int k){ 4 | //base case 5 | if(root == NULL){ 6 | return -1; 7 | } 8 | 9 | int left = solve(root -> left, i, k); 10 | if(left != -1){ 11 | return left; 12 | } 13 | 14 | i++; 15 | if(i == k){ 16 | return root -> val; 17 | } 18 | return solve(root -> right, i, k); 19 | } 20 | 21 | int kthSmallest(TreeNode* root, int k) { 22 | int i = 0; 23 | int ans = solve(root, i, k); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Letter_Combinations_of_a_Phone_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | void solve(string digit, string output, int index, vector& ans, string mapping[]){ 4 | //base case 5 | if(index >= digit.length()){ 6 | ans.push_back(output); 7 | return; 8 | } 9 | 10 | int number = digit[index] - '0'; 11 | string value = mapping[number]; 12 | 13 | for(int i=0; i letterCombinations(string digits) { 22 | vector ans; 23 | if(digits.length()==0){ 24 | return ans; 25 | } 26 | string output = ""; 27 | int index = 0; 28 | string mapping[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 29 | solve(digits, output, index, ans, mapping); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int array[100], search, c, n; 6 | 7 | printf("Enter number of elements in array\n"); 8 | scanf("%d", &n); 9 | 10 | printf("Enter %d integer(s)\n", n); 11 | 12 | for (c = 0; c < n; c++) 13 | scanf("%d", &array[c]); 14 | 15 | printf("Enter a number to search\n"); 16 | scanf("%d", &search); 17 | 18 | for (c = 0; c < n; c++) 19 | { 20 | if (array[c] == search) /* If required element is found */ 21 | { 22 | printf("%d is present at location %d.\n", search, c+1); 23 | break; 24 | } 25 | } 26 | if (c == n) 27 | printf("%d isn't present in the array.\n", search); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int a[10][10],b[10][10],c[10][10],d[10][10], row, col,i,j; 5 | 6 | printf("Enter the rows number of both matrices\n"); 7 | scanf("%d",&row); 8 | printf("Enter the columns number of both matrices\n"); 9 | scanf("%d",&col); 10 | 11 | 12 | printf("Enter the elements of matrix A\n"); 13 | for(i=0;i 2 | 3 | int findMaxDistance(map*, BinaryTreeNode*> &mpp, BinaryTreeNode* target) { 4 | queue*> q; 5 | q.push(target); 6 | map*,int> vis; 7 | vis[target] = 1; 8 | int maxi = 0; 9 | while(!q.empty()) { 10 | int sz = q.size(); 11 | int fl = 0; 12 | for(int i = 0;ileft && !vis[node->left]) { 16 | fl = 1; 17 | vis[node->left] = 1; 18 | q.push(node->left); 19 | } 20 | if(node->right && !vis[node->right]) { 21 | fl = 1; 22 | vis[node->right] = 1; 23 | q.push(node->right); 24 | } 25 | 26 | if(mpp[node] && !vis[mpp[node]]) { 27 | fl = 1; 28 | vis[mpp[node]] = 1; 29 | q.push(mpp[node]); 30 | } 31 | } 32 | if(fl) maxi++; 33 | } 34 | return maxi; 35 | } 36 | BinaryTreeNode* bfsToMapParents(BinaryTreeNode* root, 37 | map*, BinaryTreeNode*> &mpp, int start) { 38 | queue*> q; 39 | q.push(root); 40 | BinaryTreeNode* res; 41 | while(!q.empty()) { 42 | BinaryTreeNode* node = q.front(); 43 | if(node->data == start) res = node; 44 | q.pop(); 45 | if(node->left) { 46 | mpp[node->left] = node; 47 | q.push(node->left); 48 | } 49 | if(node->right) { 50 | mpp[node->right] = node; 51 | q.push(node->right); 52 | } 53 | } 54 | return res; 55 | } 56 | int timeToBurnTree(BinaryTreeNode* root, int start) 57 | { 58 | map*, BinaryTreeNode*> mpp; 59 | BinaryTreeNode* target = bfsToMapParents(root, mpp, start); 60 | int maxi = findMaxDistance(mpp, target); 61 | return maxi; 62 | } 63 | -------------------------------------------------------------------------------- /Mirror Reflection.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mirrorReflection(int p, int q) { 3 | 4 | while(p%2==0 && q%2==0){ 5 | p=p/2; 6 | q=q/2; 7 | } 8 | 9 | if(p%2==0 && q%2!=0){ 10 | return 2; 11 | } 12 | else if(p%2!=0 && q%2==0){ 13 | return 0; 14 | } 15 | else{ 16 | return 1; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /N QUEENS: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> solveNQueens(int n) { 4 | vector> ans; 5 | dfs(n, 0, vector(n), vector(2 * n - 1), vector(2 * n - 1), 6 | vector(n, string(n, '.')), ans); 7 | return ans; 8 | } 9 | 10 | private: 11 | void dfs(int n, int i, vector&& cols, vector&& diag1, 12 | vector&& diag2, vector&& board, 13 | vector>& ans) { 14 | if (i == n) { 15 | ans.push_back(board); 16 | return; 17 | } 18 | 19 | for (int j = 0; j < n; ++j) { 20 | if (cols[j] || diag1[i + j] || diag2[j - i + n - 1]) 21 | continue; 22 | board[i][j] = 'Q'; 23 | cols[j] = diag1[i + j] = diag2[j - i + n - 1] = true; 24 | dfs(n, i + 1, move(cols), move(diag1), move(diag2), move(board), ans); 25 | cols[j] = diag1[i + j] = diag2[j - i + n - 1] = false; 26 | board[i][j] = '.'; 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /N_Queen.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(int col, vector &board, vector> &ans, vector &leftRow, vector &upperDiagonal, vector &lowerDiagonal, int n){ 4 | //base case 5 | if(col == n){ 6 | ans.push_back(board); 7 | return; 8 | } 9 | 10 | for(int row=0; row> solveNQueens(int n) { 26 | vector> ans; 27 | vector board(n); 28 | string s(n, '.'); 29 | for(int i=0; i leftRow(n, 0), upperDiagonal(2 * n - 1, 0), lowerDiagonal(2 * n - 1, 0); 33 | solve(0, board, ans, leftRow, upperDiagonal, lowerDiagonal, n); 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Next-Perm: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int n = nums.size(), k, l; 5 | for (k = n - 2; k >= 0; k--) { 6 | if (nums[k] < nums[k + 1]) { 7 | break; 8 | } 9 | } 10 | if (k < 0) { 11 | reverse(nums.begin(), nums.end()); 12 | } else { 13 | for (l = n - 1; l > k; l--) { 14 | if (nums[l] > nums[k]) { 15 | break; 16 | } 17 | } 18 | swap(nums[k], nums[l]); 19 | reverse(nums.begin() + k + 1, nums.end()); 20 | } 21 | 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Peak Index of an Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | 5 | 6 | 7 | 8 | int start = 0; 9 | int end = arr.size()-1; 10 | int mid = start + (end-start)/2; 11 | while(start& arr) { 4 | int ans=0; 5 | int mid=0; 6 | int s=0; 7 | int e=arr.size()-1; 8 | while(s<=e){ 9 | mid=s+(e-s)/2; 10 | if(mid==0){ 11 | mid=1; 12 | } 13 | if(arr[mid]>arr[mid-1] && arr[mid]>arr[mid+1]){ 14 | ans=mid; 15 | break; 16 | } 17 | else if(arr[mid]arr[mid+1]){ 18 | e=mid-1; 19 | } 20 | else if(arr[mid]>arr[mid-1] && arr[mid]0 and name.isalpha()) : 16 | pyg='ay' 17 | first=name[0] 18 | new_word= name + first + pyg 19 | new_word= new_word[1:len(new_word)] 20 | print("PIG LATIN TRANSTATION OF THIS WORD IS : "+new_word) 21 | 22 | else: 23 | print('Enter a valid String') 24 | 25 | /** 26 | 27 | Input 1 28 | Enter the nameRIYA 29 | The Initial word is :RIYA 30 | PIG LATIN TRANSTATION OF THIS WORD IS : iyaray 31 | 32 | Input2 33 | Enter the namedfgr345@ 34 | The Initial word is :dfgr345@ 35 | Enter a valid String 36 | 37 | Input3 38 | Enter the name 39 | The Initial word is : 40 | Enter a valid String 41 | 42 | **/ 43 | -------------------------------------------------------------------------------- /QueueUsingTwoStacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class que{ 6 | stack s1; 7 | stack s2; 8 | 9 | public: 10 | void push(int x){ 11 | s1.push(x); 12 | } 13 | 14 | int pop(){ 15 | if(s1.empty() and s2.empty()){ 16 | cout<<"Queue is empty"<val == val){ 23 | temp = head ->next; 24 | delete head; 25 | head = temp; 26 | } 27 | 28 | // base condition 29 | if(head == NULL) return head; 30 | 31 | ListNode* current = head->next; 32 | ListNode* previous = head; 33 | 34 | while(current != NULL){ 35 | if(current->val == val){ 36 | previous->next = current->next; 37 | delete current; 38 | current = previous->next; 39 | continue; 40 | } 41 | previous = current; 42 | current = current->next; 43 | } 44 | 45 | return head; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Reverse_Vowels_Of_String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isVowel(char c){ 4 | return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); 5 | } 6 | 7 | string reverseVowels(string s) { 8 | int n = s.length(); 9 | int st = 0; 10 | int e = n-1; 11 | while(st int: 3 | l = 0 4 | r = len(nums) 5 | 6 | while l < r: 7 | m = (l + r) // 2 8 | if nums[m] == target: 9 | return m 10 | if nums[m] < target: 11 | l = m + 1 12 | else: 13 | r = m 14 | 15 | return l 16 | -------------------------------------------------------------------------------- /ShirtSize.cpp: -------------------------------------------------------------------------------- 1 | // shree ganeshay namah 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for (int i = n - 1; i >= 0; i--) 17 | #define rep(i, n) for (int i = 0; i < n; i++) 18 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 19 | 20 | int main() 21 | { 22 | 23 | int t; 24 | cin >> t; 25 | while (t--) 26 | { 27 | string a, b; 28 | cin >> a >> b; 29 | int n1 = a.length(); 30 | int n2 = b.length(); 31 | char x = a.at(n1 - 1); 32 | char y = b.at(n2 - 1); 33 | if (n1 == n2) 34 | { 35 | if (x == y) 36 | { 37 | cout << "=" << endl; 38 | } 39 | if ((x == 'M') && (y == 'S')) 40 | { 41 | cout << ">" << endl; 42 | } 43 | if ((x == 'S') && (y == 'M')) 44 | { 45 | cout << "<" << endl; 46 | } 47 | if ((x == 'L') && (y == 'S')) 48 | { 49 | cout << ">" << endl; 50 | } 51 | if ((x == 'S') && (y == 'L')) 52 | { 53 | cout << "<" << endl; 54 | } 55 | if ((x == 'L') && (y == 'M')) 56 | { 57 | cout << ">" << endl; 58 | } 59 | if ((x == 'M') && (y == 'L')) 60 | { 61 | cout << "<" << endl; 62 | } 63 | } 64 | if (n1 != n2) 65 | { 66 | if (x == y) 67 | { 68 | if ((x == 'L') && (n1 > n2)) 69 | { 70 | cout << ">" << endl; 71 | } 72 | if ((x == 'L') && (n2 > n1)) 73 | { 74 | cout << "<" << endl; 75 | } 76 | if ((x == 'S') && (n1 > n2)) 77 | { 78 | cout << "<" << endl; 79 | } 80 | if ((x == 'S') && (n2 > n1)) 81 | { 82 | cout << ">" << endl; 83 | } 84 | } 85 | if (x != y) 86 | { 87 | if ((x == 'M') && (y == 'S')) 88 | { 89 | cout << ">" << endl; 90 | } 91 | if ((x == 'S') && (y == 'M')) 92 | { 93 | cout << "<" << endl; 94 | } 95 | if ((x == 'L') && (y == 'S')) 96 | { 97 | cout << ">" << endl; 98 | } 99 | if ((x == 'S') && (y == 'L')) 100 | { 101 | cout << "<" << endl; 102 | } 103 | if ((x == 'L') && (y == 'M')) 104 | { 105 | cout << ">" << endl; 106 | } 107 | if ((x == 'M') && (y == 'L')) 108 | { 109 | cout << "<" << endl; 110 | } 111 | } 112 | } 113 | } 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /Sieve of Eratosthenes.java: -------------------------------------------------------------------------------- 1 | /*Sieve of Eratosthenes 2 | This method is used to find whether the number 3 | from 1 to N is Prime or not 4 | */ 5 | 6 | import java.util.*; 7 | 8 | 9 | 10 | public class Main 11 | { 12 | static boolean[] sieve (int n){ 13 | boolean isPrime[] = new boolean[n + 1]; 14 | Arrays.fill(isPrime, true); 15 | isPrime[0] = false; 16 | isPrime[1] = false; 17 | for(int i=2; i*i<=n; i++){ 18 | for(int j=2*i; j<=n; j+=i){ 19 | isPrime[j] = false; 20 | } 21 | } 22 | return isPrime; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | int n = sc.nextInt(); 28 | boolean isPrime[] = sieve(n); 29 | for(int i=0; i<=n; i++){ 30 | System.out.println(i + " " + isPrime[i]); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Thickness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int x1; 8 | cin >> x1; 9 | while (x1--) 10 | { 11 | int x; 12 | cin >> x; 13 | if (x == 3) 14 | cout << "-1" << endl; 15 | else 16 | { 17 | if (x % 2) 18 | { 19 | int kas = (x / 2) + 1; 20 | for (int j = x; j > kas; j--) 21 | { 22 | cout << i << " "; 23 | } 24 | for (int j = 1; j <= kas; j++) 25 | { 26 | cout << j << " "; 27 | } 28 | cout << endl; 29 | } 30 | else 31 | { 32 | for (int k = x; k >= 1; k--) 33 | { 34 | cout << k << " "; 35 | } 36 | cout << endl; 37 | } 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /TrappingRainWater.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | const int n = height.size(); 5 | int ans = 0; 6 | vector l(n); // l[i] := max(height[0..i]) 7 | vector r(n); // r[i] := max(height[i..n)) 8 | 9 | for (int i = 0; i < n; ++i) 10 | l[i] = i == 0 ? height[i] : max(height[i], l[i - 1]); 11 | 12 | for (int i = n - 1; i >= 0; --i) 13 | r[i] = i == n - 1 ? height[i] : max(height[i], r[i + 1]); 14 | 15 | for (int i = 0; i < n; ++i) 16 | ans += min(l[i], r[i]) - height[i]; 17 | 18 | return ans; 19 | } 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /WordSearch.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/word-search/ 2 | 3 | public class WordSearch { 4 | public static void main(String[] args) { 5 | char[][] board = { 6 | {'A','B','C','E'}, 7 | {'S','F','C','S'}, 8 | {'A','D','E','E'} 9 | }; 10 | boolean ans = exist(board, "ABCCED"); 11 | System.out.println(ans); 12 | } 13 | public static boolean exist(char[][] board, String word) { 14 | 15 | for(int i = 0; i < board.length; i++){ 16 | for(int j = 0; j < board[0].length; j++){ 17 | if(board[i][j] == word.charAt(0)){ 18 | char temp = board[i][j]; 19 | board[i][j] = '0'; 20 | if(isValid(board, i, j, 1, word)){ 21 | return true; 22 | } 23 | board[i][j] = temp; 24 | } 25 | } 26 | } 27 | 28 | return false; 29 | } 30 | public static boolean isValid(char[][] board, int row, int col, int index, String word){ 31 | if(index == word.length()){ 32 | return true; 33 | } 34 | char temp; 35 | if(col != 0 && board[row][col - 1] == word.charAt(index)){ 36 | temp = board[row][col - 1]; 37 | board[row][col - 1] = '0'; 38 | if(isValid(board, row, col-1, index+1, word)){ 39 | return true; 40 | } 41 | board[row][col-1] = temp; 42 | } 43 | if(col <= board[0].length-2 && board[row][col + 1] == word.charAt(index)){ 44 | temp = board[row][col + 1]; 45 | board[row][col + 1] = '0'; 46 | if(isValid(board, row, col+1, index+1, word)){ 47 | return true; 48 | } 49 | board[row][col+1] = temp; 50 | } 51 | if(row != 0 && board[row - 1][col] == word.charAt(index)){ 52 | temp = board[row - 1][col]; 53 | board[row - 1][col] = '0'; 54 | if(isValid(board, row - 1, col, index+1, word)){ 55 | return true; 56 | } 57 | board[row - 1][col] = temp; 58 | } 59 | if(row <= board.length - 2 && board[row + 1][col] == word.charAt(index)){ 60 | temp = board[row + 1][col]; 61 | board[row + 1][col] = '0'; 62 | if(isValid(board, row + 1, col, index+1, word)){ 63 | return true; 64 | } 65 | board[row + 1][col] = temp; 66 | } 67 | 68 | return false; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /arrayrecovery.cpp: -------------------------------------------------------------------------------- 1 | // shree ganeshay namah 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for (int i = n - 1; i >= 0; i--) 17 | #define rep(i, n) for (int i = 0; i < n; i++) 18 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 19 | 20 | int main() 21 | { 22 | int t; 23 | cin >> t; 24 | while (t--) 25 | { 26 | int n; 27 | cin >> n; 28 | int c = 0; 29 | vector a(n); 30 | vector b(n); 31 | int flag = 0; 32 | for (int i = 0; i < n; i++) 33 | { 34 | cin >> a[i]; 35 | } 36 | b[0] = a[0]; 37 | for (int i = 1; i < n; i++) 38 | { 39 | if(a[i]==0){ 40 | b[i] = b[i-1] + a[i]; 41 | continue; 42 | } 43 | if (b[i-1] - a[i] >= 0) 44 | { 45 | flag = 1; 46 | break; 47 | } 48 | else{ 49 | b[i] = a[i] + b[i - 1]; 50 | } 51 | } 52 | for (int i = 0; i < n; i++) 53 | { 54 | if (flag == 0) 55 | { 56 | cout << b[i] << " "; 57 | } 58 | else{ 59 | cout << "-1"; 60 | break; 61 | } 62 | } 63 | cout< 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 17 | #define rep(i,n) for(int i=0;i>t; 27 | while(t--) 28 | { 29 | int n, cv = 0, qw = 0, po = 0; 30 | cin >> n; 31 | std::vector a(n); 32 | for (int i = 0; i < n; i++) 33 | cin >> a[i]; 34 | for (int j = 0; j < n; j++) 35 | { 36 | for (int i = 0; i < n; i++) 37 | { 38 | if (a[j] >= a[i]){ 39 | cv++; 40 | } 41 | else{ 42 | qw++; 43 | } 44 | 45 | } 46 | if (cv > qw) 47 | po++; 48 | cv = 0; 49 | qw = 0; 50 | } 51 | cout << po << endl; 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /balancedParenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isValid(string s){ 6 | int n = s.size(); 7 | 8 | stack st; 9 | bool ans = true; 10 | for(int i=0; i& prices) { 4 | int maxPro = 0; 5 | int minPrice = INT_MAX; 6 | for(int i=0; i 2 | #include 3 | int main() 4 | { 5 | int n, key,l,h,mid,x; 6 | printf("Enter the size of Array"); 7 | scanf("%d", &n); 8 | int a[n]; 9 | printf("Enter the key"); 10 | scanf("%d", &key); 11 | printf("Enter the elements of Array"); 12 | for (int i = 0; i < n; i++) 13 | { 14 | scanf("%d", &a[i]); 15 | } 16 | l = 0; 17 | h = n - 1; 18 | mid = ((l + h)/2); 19 | x=mid; 20 | while (l <= h) 21 | { 22 | if (key == a[x]) 23 | { 24 | printf("position of the key in array\n"); 25 | printf("%d",x); 26 | break; 27 | } 28 | if (a[x] > key) 29 | { 30 | h = x - 1; 31 | } 32 | if (a[x] < key) 33 | { 34 | l = x + 1; 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /cancelTrains.cpp: -------------------------------------------------------------------------------- 1 | //shree ganeshay namah 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for (int i = n - 1; i >= 0; i--) 17 | #define rep(i, n) for (int i = 0; i < n; i++) 18 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 19 | 20 | int main() 21 | { 22 | 23 | int t, n, m; 24 | cin >> t; 25 | while (t--) 26 | { 27 | cin >> n >> m; 28 | int a[n]; 29 | int b[m]; 30 | int c = 0; 31 | for (int i = 0; i < n; i++) 32 | { 33 | cin >> a[i]; 34 | } 35 | for (int j = 0; j < m; j++) 36 | { 37 | cin >> b[j]; 38 | } 39 | for (int k = 0; k < n; k++) 40 | { 41 | for (int p = 0; p < m; p++) 42 | { 43 | if (a[k] == b[p]) 44 | { 45 | c++; 46 | } 47 | } 48 | } 49 | cout << c << endl; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /confusingconcatenations.cpp: -------------------------------------------------------------------------------- 1 | //shree ganeshay namah 2 | #include 3 | using namespace std; 4 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 5 | 6 | 7 | int main() 8 | { 9 | 10 | 11 | int tt; 12 | cin>>tt; 13 | while(tt--) 14 | { 15 | int n; 16 | cin>>n; 17 | 18 | int a[n]; 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin>>a[i]; 22 | } 23 | int max1 = 0; 24 | for (int i = 0; i < n; i++) 25 | { 26 | if(a[i]>a[max1]) 27 | max1 = i; 28 | } 29 | if(max1 == 0){ 30 | cout<<-1< 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 17 | #define rep(i,n) for(int i=0;i>t; 25 | while(t--) 26 | { 27 | int n,m; 28 | cin>>n>>m; 29 | vector> ma(m , vector (2)); 30 | unordered_map mp; 31 | unordered_map mq; 32 | for (int i = 0; i < m; i++) 33 | { 34 | for (int j = 0; j < 2; j++) 35 | { 36 | cin>>ma[i][j]; 37 | mp[ma[i][0]]++; 38 | mq[ma[i][1]]++; 39 | } 40 | } 41 | if(n == m) 42 | cout<<"NO"< lookup = new Dictionary(); 4 | for (var i = 0; i < nums.Length; i++) { 5 | if (lookup.ContainsKey(target - nums[i])) { 6 | return new int [] { lookup[target - nums[i]], i }; 7 | } 8 | lookup[nums[i]] = i; 9 | } 10 | return new int[] { }; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /decodeways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | /*int n = s.size(); 5 | int n1=0,n2=1,n3,ans; 6 | for(int i=1;i<=n;i++){ 7 | n3=n1+n2; 8 | n1=n2; 9 | n2=n3; 10 | } 11 | cout< 2 | using namespace std; 3 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 4 | 5 | 6 | int main() 7 | { 8 | 9 | 10 | int ttt; 11 | cin>>ttt; 12 | while(ttt--) 13 | { 14 | int n1; 15 | cin>>n1; 16 | if(n1==3) 17 | cout<<"-1"< anurag; i--) 22 | { 23 | cout<= 1; i--) 34 | { 35 | cout< 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | // Hex String variable 9 | string hex_s = "653cae8da8edb426052"; 10 | 11 | // Plain text variable 12 | string plain = ""; 13 | 14 | // variable to store the XOR 15 | // of previous digits 16 | int x = 0; 17 | 18 | int l = hex_s.length(); 19 | 20 | // Loop for loop from the end to 21 | // the mid section of the string 22 | for (int i = l - 1; i > (l / 2) - 1; i--) { 23 | string digit = ""; 24 | digit += hex_s[i]; 25 | 26 | // calculation of the plaintext digit 27 | unsigned int y = x ^ stoul(digit, nullptr, 16); 28 | 29 | // calculation of XOR chain 30 | x = x ^ y; 31 | stringstream sstream; 32 | sstream << hex << y; 33 | string z = sstream.str(); 34 | plain = z[z.length() - 1] + plain; 35 | } 36 | 37 | cout << plain; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /integertoroman.c: -------------------------------------------------------------------------------- 1 | #include 2 | typedef struct{ 3 | char *sym; 4 | int val; 5 | }numeral; 6 | int maxNume(numeral *nu, int num){ 7 | int i, index; 8 | for(i = 0; i<15; i++){//15 numerals in array 9 | if(nu[i].val <= num) 10 | index = i; 11 | } 12 | //gretest value numeral index, not greater than number 13 | return index; 14 | } 15 | void decToRoman(numeral *nu, int num){ 16 | int max; 17 | if(num != 0){ 18 | max = maxNume(nu, num); 19 | printf("%s", nu[max].sym); 20 | num -= nu[max].val;//decrease number 21 | decToRoman(nu, num);//recursively print numerals 22 | } 23 | } 24 | main(){ 25 | int number; 26 | numeral nume[15] = {{"I",1},{"IV",4},{"V",5},{"IX",9}, {"X",10},{"XL",40},{"L",50},{"XC",90}, 27 | {"C",100},{"CD",400},{"D",500},{"CM",900},{"M",1000},{"MMMM",4000},{"V'",5000}}; 28 | printf("Enter a decimal number: "); 29 | scanf("%d", &number); 30 | if(number >0 && number <= 5000){//checking input number 31 | printf("The Roman equivalent of %d is ", number); 32 | decToRoman(nume, number); 33 | } 34 | else{ 35 | printf("Invalid Input"); 36 | } 37 | printf("\n"); 38 | } 39 | -------------------------------------------------------------------------------- /josh-solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void Josh(vector person, int k, int index) 6 | { 7 | // Base case , when only one person is left 8 | if (person.size() == 1) { 9 | cout << person[0] << endl; 10 | return; 11 | } 12 | 13 | // find the index of first person which will die 14 | index = ((index + k) % person.size()); 15 | 16 | // remove the first person which is going to be killed 17 | person.erase(person.begin() + index); 18 | 19 | // recursive call for n-1 persons 20 | Josh(person, k, index); 21 | } 22 | 23 | int main() 24 | { 25 | int n = 14; // specific n and k values for original 26 | // josephus problem 27 | int k = 2; 28 | k--; // (k-1)th person will be killed 29 | int index 30 | = 0; // The index where the person which will die 31 | 32 | vector person; 33 | // fill the person vector 34 | for (int i = 1; i <= n; i++) { 35 | person.push_back(i); 36 | } 37 | 38 | Josh(person, k, index); 39 | } 40 | -------------------------------------------------------------------------------- /leetcode_contest_314_solutions/1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/leetcode-solutions-1/7b53a6e5f13dae95fdb19b4305596749ccc7771d/leetcode_contest_314_solutions/1.cpp -------------------------------------------------------------------------------- /leetcode_contest_314_solutions/2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/leetcode-solutions-1/7b53a6e5f13dae95fdb19b4305596749ccc7771d/leetcode_contest_314_solutions/2.cpp -------------------------------------------------------------------------------- /leetcode_contest_314_solutions/3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kishanrajput23/leetcode-solutions-1/7b53a6e5f13dae95fdb19b4305596749ccc7771d/leetcode_contest_314_solutions/3.cpp -------------------------------------------------------------------------------- /level_order_tree_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* A binary tree node has data, 5 | pointer to left child 6 | and a pointer to right child */ 7 | class node { 8 | public: 9 | int data; 10 | node *left, *right; 11 | }; 12 | 13 | /* Function prototypes */ 14 | void printCurrentLevel(node* root, int level); 15 | int height(node* node); 16 | node* newNode(int data); 17 | 18 | /* Function to print level 19 | order traversal a tree*/ 20 | void printLevelOrder(node* root) 21 | { 22 | int h = height(root); 23 | int i; 24 | for (i = 1; i <= h; i++) 25 | printCurrentLevel(root, i); 26 | } 27 | 28 | /* Print nodes at a current level */ 29 | void printCurrentLevel(node* root, int level) 30 | { 31 | if (root == NULL) 32 | return; 33 | if (level == 1) 34 | cout << root->data << " "; 35 | else if (level > 1) { 36 | printCurrentLevel(root->left, level - 1); 37 | printCurrentLevel(root->right, level - 1); 38 | } 39 | } 40 | 41 | /* Compute the "height" of a tree -- the number of 42 | nodes along the longest path from the root node 43 | down to the farthest leaf node.*/ 44 | int height(node* node) 45 | { 46 | if (node == NULL) 47 | return 0; 48 | else { 49 | /* compute the height of each subtree */ 50 | int lheight = height(node->left); 51 | int rheight = height(node->right); 52 | 53 | /* use the larger one */ 54 | if (lheight > rheight) { 55 | return (lheight + 1); 56 | } 57 | else { 58 | return (rheight + 1); 59 | } 60 | } 61 | } 62 | 63 | /* Helper function that allocates 64 | a new node with the given data and 65 | NULL left and right pointers. */ 66 | node* newNode(int data) 67 | { 68 | node* Node = new node(); 69 | Node->data = data; 70 | Node->left = NULL; 71 | Node->right = NULL; 72 | 73 | return (Node); 74 | } 75 | 76 | /* Driver code*/ 77 | int main() 78 | { 79 | node* root = newNode(1); 80 | root->left = newNode(2); 81 | root->right = newNode(3); 82 | root->left->left = newNode(4); 83 | root->left->right = newNode(5); 84 | 85 | cout << "Level Order traversal of binary tree is \n"; 86 | printLevelOrder(root); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /maximal square.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximalSquare(char[][] matrix) { 3 | int rows = matrix.length; 4 | int cols = matrix[0].length; 5 | int maxSquare = 0; 6 | int[][] maxSideLength = new int[rows+1][cols+1]; 7 | 8 | for(int i=rows-1;i>=0;i--){ 9 | for(int j=cols-1;j>=0;j--){ 10 | if(matrix[i][j] == '1'){ 11 | // maximum sideLength of square is minimum of length covered by either of row, column, diagonal 12 | maxSideLength[i][j] = Math.min(maxSideLength[i+1][j],Math.min(maxSideLength[i][j+1],maxSideLength[i+1][j+1]))+1; 13 | maxSquare = Math.max(maxSquare, maxSideLength[i][j]); 14 | }else{ 15 | maxSideLength[i][j] = 0; 16 | } 17 | } 18 | } 19 | return maxSquare*maxSquare; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /mergeTwoSortedll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* next; 8 | node* prev; 9 | 10 | node(int val){ 11 | data = val; 12 | next = NULL; 13 | prev = NULL; 14 | } 15 | }; 16 | 17 | void insertAtHead(node* &head, int val){ 18 | node* n = new node(val); 19 | n->next = head; 20 | if(head!=NULL){ 21 | head->prev = n; 22 | } 23 | 24 | head = n; 25 | } 26 | 27 | void insertAtTail(node* &head, int val){ 28 | if(head==NULL){ 29 | insertAtHead(head, val); 30 | return; 31 | } 32 | 33 | node* n = new node(val); 34 | node* temp = head; 35 | 36 | while(temp->next!=NULL){ 37 | temp = temp->next; 38 | } 39 | 40 | temp->next = n; 41 | n->prev = temp; 42 | } 43 | 44 | node* merge(node* &head1, node* &head2){ 45 | node* p1 = head1; 46 | node* p2 = head2; 47 | node* dummyNode = new node(-1); 48 | node* p3 = dummyNode; 49 | 50 | while(p1!=NULL && p2!=NULL){ 51 | if(p1->datadata){ 52 | p3->next = p1; 53 | p1 = p1->next; 54 | } 55 | else{ 56 | p3->next = p2; 57 | p2 = p2->next; 58 | } 59 | p3 = p3->next; 60 | } 61 | 62 | while(p1!=NULL){ 63 | p3->next = p1; 64 | p1 = p1->next; 65 | p3 = p3->next; 66 | } 67 | 68 | while(p2!=NULL){ 69 | p3->next = p2; 70 | p2 = p2->next; 71 | p3 = p3->next; 72 | } 73 | return dummyNode->next; 74 | } 75 | 76 | void display(node *head){ 77 | node* temp = head; 78 | while(temp!=NULL){ 79 | cout<data<<"->"; 80 | temp = temp->next; 81 | } 82 | cout<<"NULL"<>= 1; 12 | } 13 | return count; 14 | } 15 | }; 16 | 17 | // author:Chinmay Lohani 18 | // date:9-3-2022 19 | // problemName:A. Deletions of Two Adjacent Letters 20 | // problemLink:https://codeforces.com/contest/1650/problem/A 21 | // memoryLimit: 256 22 | // timeLimit:2000 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | #define mod 998244353 29 | #define lli long long int 30 | #define ll long long 31 | #define li long int 32 | #define FastIO \ 33 | ios::sync_with_stdio(0); \ 34 | cin.tie(0) 35 | 36 | uint32_t solve(); 37 | int main() 38 | { 39 | FastIO; 40 | ll tc; 41 | cin >> tc; 42 | while (tc--) 43 | { 44 | cout << solve(); 45 | } 46 | return 0; 47 | } 48 | uint32_t solve() 49 | { 50 | uint32_t n; 51 | cin >> n; 52 | uint32_t temp = n; 53 | unsigned int count = 0; 54 | while (n) 55 | { 56 | count += (n & 1); 57 | n >>= 1; 58 | } 59 | return count; 60 | } -------------------------------------------------------------------------------- /palindromeflipping.cpp: -------------------------------------------------------------------------------- 1 | //shree ganeshay namah 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 17 | #define rep(i,n) for(int i=0;i>t; 27 | while(t--) 28 | { 29 | int n; 30 | cin>>n; 31 | string as; 32 | cin>>as; 33 | int c1=0,c0=0; 34 | for (int i = 0; i < n; i++) 35 | { 36 | if(as[i] == '1') 37 | c1++; 38 | else 39 | c0++; 40 | } 41 | if(n%2==0){ 42 | if(c1%2 && c0%2) 43 | cout<<"NO"< 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 17 | #define rep(i,n) for(int i=0;i>t; 25 | while(t--) 26 | { 27 | ll n; 28 | cin>>n; 29 | string as; 30 | cin>>as; 31 | if(as[0]=='0'){ 32 | for (int i = 0; i < 2*n; i++) 33 | { 34 | if(as[i]=='1'){ 35 | as[i]='0'; 36 | } 37 | else{ 38 | as[i]='1'; 39 | } 40 | } 41 | 42 | } 43 | vector vc; 44 | ll cc; 45 | ll flag = 0; 46 | ll an; 47 | for (int i = 0; i < 2*n; i++) 48 | { 49 | if(as[i]=='0'){ 50 | vc.push_back(i); 51 | } 52 | if(as[i] != as[2*n-i-1]){ 53 | flag=1; 54 | break; 55 | } 56 | } 57 | if(flag){ 58 | cout<<"1\n"; 59 | cout<<2*n<=1; i--){ 68 | if(vc[i]-vc[i-1]-1!=cc){ 69 | flag=1; 70 | cout<<"2\n"; 71 | cout< 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 17 | #define rep(i,n) for(int i=0;i>t; 27 | while(t--) 28 | { 29 | int ak,akg; 30 | cin>>ak>>akg; 31 | cout<<(ak/akg)*(ak/akg)< 2 | using namespace std; 3 | 4 | void pypart2(int n) 5 | { 6 | int i = 0, j = 0, k = 0; 7 | while (i < n) { 8 | 9 | while (k <= n - i - 2) { 10 | cout << " "; 11 | k++; 12 | } 13 | k = 0; 14 | 15 | while (j < 2 * i - 1) { 16 | cout << "*"; 17 | j++; 18 | } 19 | j = 0; 20 | i++; 21 | cout << endl; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int n = 5; 28 | 29 | pypart2(n); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /push-dominoes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | // using 2-pointer 7 | string pushDominoes(string dominoes) { 8 | int l = 0, n = dominoes.size(); 9 | for (int r = 0; r < n; r++) { 10 | if (dominoes[r] == '.') { 11 | continue; 12 | } else if ((dominoes[r] == dominoes[l]) || (dominoes[l] == '.' && dominoes[r] == 'L')) { 13 | for (int k = l; k < r; k++) dominoes[k] = dominoes[r]; 14 | } else if (dominoes[l] == 'L' && dominoes[r] == 'R') { 15 | 16 | } else if (dominoes[l] == 'R' && dominoes[r] == 'L') { 17 | 18 | int m = (r - l - 1) / 2; 19 | for (int k = 1; k <= m; k++) dominoes[r - k] = 'L', dominoes[l + k] = 'R'; 20 | } 21 | l = r; 22 | dominoes[l] = dominoes[r]; 23 | } 24 | if (dominoes[l] == 'R') for (int k = l; k < n; k++) dominoes[k] = 'R'; 25 | return dominoes; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /richest-wealth.cpp: -------------------------------------------------------------------------------- 1 | // Question Link : https://leetcode.com/problems/richest-customer-wealth/ 2 | 3 | class Solution { 4 | public: 5 | int maximumWealth(vector>& accounts) { 6 | int val = INT_MIN; 7 | int sum ; 8 | int row = accounts.size(); //row 9 | int col = accounts[0].size(); //col 10 | for(int i=0;i 2 | #include 3 | struct list{ 4 | int key; 5 | struct list *next; 6 | }; 7 | struct list *head,*tail=NULL; 8 | void add_node(int key){ 9 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 10 | newnode->key=key; 11 | newnode->next=NULL; 12 | if(head==NULL){ 13 | head=newnode; 14 | tail=newnode; 15 | } 16 | else{ 17 | tail->next=newnode; 18 | tail=newnode; 19 | } 20 | } 21 | void add_node_beg(int key){ 22 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 23 | newnode->key=key; 24 | newnode->next=head; 25 | head=newnode; 26 | } 27 | void add_node_end(int key){ 28 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 29 | newnode->key=key; 30 | newnode->next=NULL; 31 | tail=newnode; 32 | } 33 | void add_node_mid(int key,int index){ 34 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 35 | newnode->key=key; 36 | newnode->next=NULL; 37 | if(head==NULL){ 38 | head=newnode; 39 | tail=newnode; 40 | } 41 | else{ 42 | struct list *temp,*current; 43 | temp=head; 44 | current=NULL; 45 | for(int i=0;inext; 48 | } 49 | current->next=newnode; 50 | newnode->next=temp; 51 | } 52 | } 53 | void delete_beg(){ 54 | if(head==NULL){ 55 | printf("\nList is empty"); 56 | } 57 | else{ 58 | struct list *temp; 59 | temp=head; 60 | head=head->next; 61 | free(temp); 62 | } 63 | } 64 | void delete_mid(int index){ 65 | if(head==NULL){ 66 | printf("\nList is empty"); 67 | } 68 | else{ 69 | struct list *temp,*current; 70 | temp=head; 71 | current=NULL; 72 | for(int i=0;inext; 74 | 75 | } 76 | current=temp->next; 77 | temp->next=current->next; 78 | free(current); 79 | } 80 | } 81 | void isempty(){ 82 | if(head==NULL){ 83 | printf("\nList is empty"); 84 | } 85 | else{ 86 | printf("\nList is not empty"); 87 | } 88 | } 89 | void lastpos(){ 90 | printf("\nLast value of the list is:\n"); 91 | printf("%d\n",tail->key); 92 | } 93 | void display(){ 94 | struct list *current = head; 95 | if(head==NULL){ 96 | printf("List is empty\n"); 97 | return; 98 | } 99 | printf("Nodes of singly linked list:\n"); 100 | while(current!=NULL){ 101 | printf("%d\n",current->key); 102 | current=current->next; 103 | } 104 | } 105 | 106 | int main(){ 107 | add_node(1); 108 | add_node(2); 109 | add_node(3); 110 | add_node(4); 111 | display(); 112 | isempty(); 113 | lastpos(); 114 | add_node_beg(0); 115 | display(); 116 | add_node_end(5); 117 | display(); 118 | add_node_mid(7,3); 119 | display(); 120 | delete_beg(); 121 | display(); 122 | delete_mid(4); 123 | display(); 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /singly_linked_list: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct list{ 4 | int key; 5 | struct list *next; 6 | }; 7 | struct list *head,*tail=NULL; 8 | void add_node(int key){ 9 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 10 | newnode->key=key; 11 | newnode->next=NULL; 12 | if(head==NULL){ 13 | head=newnode; 14 | tail=newnode; 15 | } 16 | else{ 17 | tail->next=newnode; 18 | tail=newnode; 19 | } 20 | } 21 | void add_node_beg(int key){ 22 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 23 | newnode->key=key; 24 | newnode->next=head; 25 | head=newnode; 26 | } 27 | void add_node_end(int key){ 28 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 29 | newnode->key=key; 30 | newnode->next=NULL; 31 | tail=newnode; 32 | } 33 | void add_node_mid(int key,int index){ 34 | struct list *newnode=(struct list*)malloc(sizeof(struct list)); 35 | newnode->key=key; 36 | newnode->next=NULL; 37 | if(head==NULL){ 38 | head=newnode; 39 | tail=newnode; 40 | } 41 | else{ 42 | struct list *temp,*current; 43 | temp=head; 44 | current=NULL; 45 | for(int i=0;inext; 48 | } 49 | current->next=newnode; 50 | newnode->next=temp; 51 | } 52 | } 53 | void delete_beg(){ 54 | if(head==NULL){ 55 | printf("\nList is empty"); 56 | } 57 | else{ 58 | struct list *temp; 59 | temp=head; 60 | head=head->next; 61 | free(temp); 62 | } 63 | } 64 | void delete_mid(int index){ 65 | if(head==NULL){ 66 | printf("\nList is empty"); 67 | } 68 | else{ 69 | struct list *temp,*current; 70 | temp=head; 71 | current=NULL; 72 | for(int i=0;inext; 74 | 75 | } 76 | current=temp->next; 77 | temp->next=current->next; 78 | free(current); 79 | } 80 | } 81 | void isempty(){ 82 | if(head==NULL){ 83 | printf("\nList is empty"); 84 | } 85 | else{ 86 | printf("\nList is not empty"); 87 | } 88 | } 89 | void lastpos(){ 90 | printf("\nLast value of the list is:\n"); 91 | printf("%d\n",tail->key); 92 | } 93 | void display(){ 94 | struct list *current = head; 95 | if(head==NULL){ 96 | printf("List is empty\n"); 97 | return; 98 | } 99 | printf("Nodes of singly linked list:\n"); 100 | while(current!=NULL){ 101 | printf("%d\n",current->key); 102 | current=current->next; 103 | } 104 | } 105 | 106 | int main(){ 107 | add_node(1); 108 | add_node(2); 109 | add_node(3); 110 | add_node(4); 111 | display(); 112 | isempty(); 113 | lastpos(); 114 | add_node_beg(0); 115 | display(); 116 | add_node_end(5); 117 | display(); 118 | add_node_mid(7,3); 119 | display(); 120 | delete_beg(); 121 | display(); 122 | delete_mid(4); 123 | display(); 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /spiralMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, m; 7 | // n are rows & m are coloumns 8 | cin >> n >> m; 9 | 10 | int a[n][m]; 11 | // Input the matrix 12 | for (int i = 0; i < n; i++) 13 | { 14 | for (int j = 0; j < m; j++) 15 | { 16 | cin >> a[i][j]; 17 | } 18 | } 19 | int row_start = 0, row_end = n - 1, col_start = 0, col_end = m - 1; 20 | 21 | while (row_start <= row_end && col_start <= col_end) 22 | { 23 | // for row_start 24 | for (int col = col_start; col <= col_end; col++) 25 | { 26 | cout << a[row_start][col] << " "; 27 | } 28 | row_start++; 29 | 30 | // for col_end 31 | for (int row = row_start; row <= row_end; row++) 32 | { 33 | cout << a[row][col_end]<<" "; 34 | } 35 | col_end--; 36 | 37 | // for row_end 38 | for (int col = col_end; col >= col_start; col--) 39 | { 40 | cout << a[row_end][col] << " "; 41 | } 42 | row_end--; 43 | 44 | // for col_start 45 | for (int row = row_end; row >= row_start; row--) 46 | { 47 | cout << a[row][col_start]<<" "; 48 | } 49 | col_start++; 50 | } 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /stacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Stack Has the Following Operations 7 | // Push - And the given element to the Stack 8 | // Pop - Remove the Last Added Element 9 | // isEmpty - Check whether Stack is Empty 10 | // isFull - Check whether Stack is Full 11 | 12 | // To implement all these operations using Linked List Follow this logic. 13 | // Have the two variables max and count 14 | // max - Max Size of the Stack 15 | // count - Size of the Stack at given moment 16 | // Implement a constructure which takes max as input and initialize count as 0 17 | // 18 | // isEmpty - if count is 0 then the Stack is empty 19 | // 20 | // isFull - if count is equal to max then the Stack is full 21 | // 22 | // Push - push the given element at the begining of the Linked List and 23 | // 24 | // increament count 25 | // 26 | // Pop - Remove the first element by changing the head pointer to head->next and 27 | // decrementing count. 28 | 29 | class Node { 30 | public: 31 | int data; 32 | Node *next; 33 | 34 | Node() { 35 | this->data = 0; 36 | this->next = nullptr; 37 | } 38 | 39 | Node(int val) { 40 | this->data = val; 41 | this->next = nullptr; 42 | } 43 | }; 44 | 45 | class Stack { 46 | private: 47 | Node *head; 48 | int max; 49 | int count; 50 | 51 | public: 52 | Stack(int max) { 53 | this->head = nullptr; 54 | this->max = max; 55 | this->count = 0; 56 | } 57 | 58 | bool isEmpty() { 59 | if (this->count == 0) { 60 | return true; 61 | } 62 | return false; 63 | } 64 | 65 | bool isFull() { 66 | if (this->count == this->max) { 67 | return true; 68 | } 69 | return false; 70 | } 71 | 72 | int top() { 73 | if (this->isEmpty()) { 74 | return INT32_MIN; 75 | } 76 | return this->head->data; 77 | } 78 | 79 | void push(int val) { 80 | if (this->isFull()) { 81 | cout << "The Stack is Already Full" << endl; 82 | return; 83 | } 84 | 85 | Node *newNode = new Node(val); 86 | newNode->next = this->head; 87 | this->head = newNode; 88 | this->count++; 89 | } 90 | 91 | int pop() { 92 | if (this->isEmpty()) { 93 | return INT32_MIN; 94 | } 95 | int ret = this->head->data; 96 | Node *temp = this->head; 97 | this->head = this->head->next; 98 | free(temp); 99 | this->count--; 100 | return ret; 101 | } 102 | 103 | void traverse() { 104 | Node *temp = this->head; 105 | while (temp != nullptr) { 106 | cout << temp->data << " "; 107 | temp = temp->next; 108 | } 109 | cout << endl; 110 | } 111 | }; 112 | 113 | int main(int argc, char *argv[]) { 114 | Stack st = Stack(10); 115 | 116 | // Is the Stack Empty 117 | if (st.isEmpty()) { 118 | cout << "The Stack is Empty" << endl; 119 | } else { 120 | cout << "The Stack is Not Empty" << endl; 121 | } 122 | 123 | st.push(1); 124 | st.push(2); 125 | st.push(3); 126 | st.push(4); 127 | st.push(5); 128 | st.push(6); 129 | st.push(7); 130 | st.push(8); 131 | st.push(9); 132 | st.push(10); 133 | st.push(11); 134 | st.push(12); 135 | 136 | st.traverse(); 137 | 138 | // Is the Stack Empty 139 | if (st.isEmpty()) { 140 | cout << "The Stack is Empty" << endl; 141 | } else { 142 | cout << "The Stack is Not Empty" << endl; 143 | } 144 | 145 | cout << st.pop() << endl; 146 | 147 | st.traverse(); 148 | 149 | cout << st.top() << endl; 150 | 151 | st.traverse(); 152 | 153 | cout << st.pop() << endl; 154 | cout << st.pop() << endl; 155 | cout << st.pop() << endl; 156 | cout << st.pop() << endl; 157 | cout << st.pop() << endl; 158 | cout << st.pop() << endl; 159 | cout << st.pop() << endl; 160 | 161 | // Is the Stack Empty 162 | if (st.isEmpty()) { 163 | cout << "The Stack is Empty" << endl; 164 | } else { 165 | cout << "The Stack is Not Empty" << endl; 166 | } 167 | 168 | return 0; 169 | } 170 | -------------------------------------------------------------------------------- /stripes.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ff first 12 | #define ss second 13 | #define all(c) c.begin(), c.end() 14 | #define min3(a, b, c) min(c, min(a, b)) 15 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 16 | #define rrep(i, n) for (int i = n - 1; i >= 0; i--) 17 | #define rep(i, n) for (int i = 0; i < n; i++) 18 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 19 | 20 | int main() 21 | { 22 | int t; 23 | cin >> t; 24 | while (t--) 25 | { 26 | int red = 0, ak = 0; 27 | string a[8]; 28 | for (int i = 0; i < 8; i++) 29 | { 30 | cin >> a[i]; 31 | } 32 | int flag = 0; 33 | for (int i = 0; i < 8; i++) 34 | { 35 | red = 0; 36 | for (int j = 0; j < 8; j++) 37 | { 38 | if (a[i][j] == 'R') 39 | red++; 40 | } 41 | if (red == 8) 42 | { 43 | cout << "R" << endl; 44 | flag = 1; 45 | break; 46 | } 47 | } 48 | if (!flag) 49 | { 50 | for (int i = 0; i < 8; i++) 51 | { 52 | ak = 0; 53 | for (int j = 0; j < 8; j++) 54 | { 55 | if (a[j][i] == 'B') 56 | ak++; 57 | } 58 | if (ak == 8) 59 | { 60 | cout << "B" << endl; 61 | break; 62 | } 63 | } 64 | } 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | #define pi (3.141592653589) 6 | #define mod 1000000007 7 | #define ll long long 8 | #define float double 9 | #define pb push_back 10 | #define mp make_pair 11 | #define ss second 12 | #define min3(a, b, c) min(c, min(a, b)) 13 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 14 | #define rrep(i, n) for(int i=n-1;i>=0;i--) 15 | #define rep(i,n) for(int i=0;i vi; 19 | typedef vector> vpi; 20 | typedef vector vl; 21 | #define all(aa) aa.begin(), aa.end() 22 | #define pb push_back 23 | 24 | #define ff \ 25 | ios_base::sync_with_stdio(false); \ 26 | cin.tie(NULL); \ 27 | cout.tie(NULL); 28 | 29 | #define t \ 30 | int test_cases; \ 31 | cin >> test_cases; \ 32 | while (test_cases--) 33 | 34 | 35 | int main() 36 | { 37 | ff 38 | 39 | t{ 40 | ll n, anurag = 0; 41 | cin >> n; 42 | vl aa, aa1, mohit; 43 | for (ll i = 0; i < n; i++) 44 | { 45 | ll a; 46 | cin >> a; 47 | 48 | if (i % 2 == 0){ 49 | aa.pb(a); 50 | } 51 | else{ 52 | aa1.pb(a); 53 | } 54 | 55 | } 56 | 57 | sort(all(aa), greater()); 58 | sort(all(aa1)); 59 | 60 | ll a = 0, b = 0; 61 | for (ll i = 0; i < n; i++) 62 | { 63 | if (i % 2 == 0) 64 | { 65 | mohit.pb(aa[a]); 66 | a++; 67 | } 68 | else 69 | { 70 | mohit.pb(aa1[b]); 71 | b++; 72 | } 73 | } 74 | 75 | if (aa1.size() > 1) 76 | for (ll i = aa1.size() - 2; i >= 0; i--) 77 | { 78 | aa1[i] += aa1[i + 1]; 79 | } 80 | 81 | for (ll i = 0; i < aa.size() && i < aa1.size(); i++) 82 | { 83 | anurag += aa[i] * aa1[i]; 84 | } 85 | 86 | for (auto i : mohit) 87 | cout << i << " "; 88 | 89 | cout << "\n" << anurag << "\n"; 90 | } 91 | return 0; 92 | } -------------------------------------------------------------------------------- /sum-with-multiplicity: -------------------------------------------------------------------------------- 1 | # Time: O(n^2), n is the number of disctinct A[i] 2 | # Space: O(n) 3 | 4 | import collections 5 | import itertools 6 | 7 | 8 | class Solution(object): 9 | def threeSumMulti(self, A, target): 10 | """ 11 | :type A: List[int] 12 | :type target: int 13 | :rtype: int 14 | """ 15 | count = collections.Counter(A) 16 | result = 0 17 | for i, j in itertools.combinations_with_replacement(count, 2): 18 | k = target - i - j 19 | if i == j == k: 20 | result += count[i] * (count[i]-1) * (count[i]-2) // 6 21 | elif i == j != k: 22 | result += count[i] * (count[i]-1) // 2 * count[k] 23 | elif max(i, j) < k: 24 | result += count[i] * count[j] * count[k] 25 | return result % (10**9 + 7) 26 | -------------------------------------------------------------------------------- /sum_triangle.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program to create Special triangle. 3 | #include 4 | using namespace std; 5 | 6 | // Function to generate Special Triangle 7 | void printTriangle(int A[] , int n) 8 | { 9 | // Base case 10 | if (n < 1) 11 | return; 12 | 13 | // Creating new array which contains the 14 | // Sum of consecutive elements in 15 | // the array passes as parameter. 16 | int temp[n - 1]; 17 | for (int i = 0; i < n - 1; i++) 18 | { 19 | int x = A[i] + A[i + 1]; 20 | temp[i] = x; 21 | } 22 | 23 | // Make a recursive call and pass 24 | // the newly created array 25 | printTriangle(temp, n - 1); 26 | 27 | // Print current array in the end so 28 | // that smaller arrays are printed first 29 | for (int i = 0; i < n ; i++) 30 | { 31 | if(i == n - 1) 32 | cout << A[i] << " "; 33 | else 34 | cout << A[i] << ", "; 35 | } 36 | 37 | cout << endl; 38 | } 39 | 40 | // Driver function 41 | int main() 42 | { 43 | int A[] = { 1, 2, 3, 4, 5 }; 44 | int n = sizeof(A) / sizeof(A[0]); 45 | 46 | printTriangle(A, n); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /unit_converter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class input 5 | { 6 | public: 7 | float i; 8 | 9 | 10 | 11 | }; 12 | 13 | 14 | 15 | class length:public input 16 | { 17 | public: 18 | void mm_m(); 19 | void m_mm(); 20 | void cm_m(); 21 | void m_cm(); 22 | void cm_km(); 23 | void km_cm(); 24 | void m_mile(); 25 | void mile_m(); 26 | void km_mile(); 27 | void mile_km(); 28 | void feet_m(); 29 | void m_feet(); 30 | void inch_m(); 31 | void m_inch(); 32 | void yard_m(); 33 | void m_yard(); 34 | 35 | }; 36 | 37 | class temp:public input 38 | { 39 | public: 40 | void cel_f(); 41 | void f_cel(); 42 | void cel_k(); 43 | void k_cel(); 44 | void f_k(); 45 | void k_f(); 46 | 47 | }; 48 | 49 | class weight:public input 50 | { 51 | public: 52 | void milligm_gm(); 53 | void gm_milligm(); 54 | void gm_kg(); 55 | void kg_gm(); 56 | void kg_mton(); 57 | void mton_kg(); 58 | void pound_kg(); 59 | void kg_pound(); 60 | void gm_pound(); 61 | void pound_gm(); 62 | 63 | }; 64 | 65 | class area :public input 66 | { 67 | public: 68 | void mmSq_cmSq(); 69 | void cmSq_mmSq(); 70 | void cmSq_mSq (); 71 | void mSq_cmSq (); 72 | void mSq_kmSq (); 73 | void kmSq_mSq (); 74 | void feetSq_mSq(); 75 | void mSq_feetSq(); 76 | void yardSq_mSq(); 77 | void mSq_yardSq(); 78 | void mileSq_kmSq(); 79 | void kmSq_mileSq(); 80 | void acre_kmSq(); 81 | void kmSq_acre(); 82 | void acre_hect(); 83 | void hect_acre(); 84 | }; 85 | 86 | class vol:public input 87 | { 88 | public: 89 | void mL_L(); 90 | void L_mL(); 91 | void mmQ_cmQ(); 92 | void cmQ_mmQ(); 93 | void cmQ_mQ (); 94 | void mQ_cmQ (); 95 | void inchQ_mQ(); 96 | void mQ_inchQ(); 97 | void feetQ_mQ(); 98 | void mQ_feetQ(); 99 | void mQ_gallon(); 100 | void gallon_mQ(); 101 | void L_gallon(); 102 | void gallon_L(); 103 | 104 | }; 105 | 106 | void length ::mm_m() 107 | { 108 | cout<<"\n Millimeter= "; 109 | cin>>i; 110 | cout<<"\n Meter= "<>x; 456 | if(x==1) 457 | { clrscr(); 458 | 459 | cout<<"\n\n choose your unit convertion:\n"; 460 | 461 | cout<<"\n 1 : mm-m"; 462 | cout<<"\n 2 : m-mm"; 463 | cout<<"\n 3 : cm-m"; 464 | cout<<"\n 4 : m-cm"; 465 | cout<<"\n 5 : cm_km"; 466 | cout<<"\n 6 : km-cm"; 467 | cout<<"\n 7 : m-mile"; 468 | cout<<"\n 8 : mile-m"; 469 | cout<<"\n 9 : km-mile"; 470 | cout<<"\n 10 : mile-km"; 471 | cout<<"\n 11 : feet-m"; 472 | cout<<"\n 12 : m-feet"; 473 | cout<<"\n 13 : inch-m"; 474 | cout<<"\n 14 : m-inch"; 475 | cout<<"\n 15 : yard-m"; 476 | cout<<"\n 16 : m-yard"; 477 | cout<<"\n 17 : Back to The Main Menu"; 478 | 479 | 480 | while(1) 481 | { 482 | cout<<"\n\n Please Enter Your Choice= "; 483 | cin>>y; 484 | 485 | if (y==1) 486 | { b.mm_m(); } 487 | else if(y==2) 488 | { b.m_mm(); } 489 | else if (y==3) 490 | { b.cm_m(); } 491 | else if (y==4) 492 | { b.m_cm(); } 493 | else if (y==5) 494 | { b.cm_km(); } 495 | else if (y==6) 496 | { b.km_cm(); } 497 | else if (y==7) 498 | { b.m_mile(); } 499 | else if (y==8) 500 | { b.mile_m(); } 501 | else if (y==9) 502 | { b.km_mile(); } 503 | else if (y==10) 504 | { b.mile_km(); } 505 | else if (y==11) 506 | { b.feet_m(); } 507 | else if (y==12) 508 | { b.m_feet(); } 509 | else if (y==13) 510 | { b.inch_m();} 511 | else if(y==14) 512 | { b.m_inch();} 513 | else if (y==15) 514 | {b.yard_m();} 515 | else if (y==16) 516 | {b.m_yard();} 517 | else if (y==17) 518 | {break;} 519 | } 520 | } 521 | else if(x==2) 522 | { clrscr(); 523 | cout<<"\n\n choose your unit convertion:\n"; 524 | 525 | cout<<"\n 1: Celsius-Fahrenheit"; 526 | cout<<"\n 2: Fahrenheit-Celsius"; 527 | cout<<"\n 3: Celsius-Kelvin"; 528 | cout<<"\n 4: Kelvin-Celcius"; 529 | cout<<"\n 5: Back to The Main Menu"; 530 | while(1) 531 | { 532 | cout<<"\n\n Please Enter Your Choice= "; 533 | cin>>y; 534 | 535 | if(y==1) 536 | { f.cel_f(); } 537 | else if(y==2) 538 | {f.f_cel();} 539 | else if (y==3) 540 | {f.cel_k();} 541 | else if(y==4) 542 | {f.k_cel();} 543 | else if(y==5) 544 | {break;} 545 | } 546 | } 547 | else if(x==3) 548 | { clrscr(); 549 | cout<<"\n\nchoose your unit convertion :\n"; 550 | 551 | cout<<"\n 1: Milligm-Gramm"; 552 | cout<<"\n 2: Gramm-milligm"; 553 | cout<<"\n 3: Gramm-killogram"; 554 | cout<<"\n 4: killoGramm-Gramm"; 555 | cout<<"\n 5: pound-killogramm"; 556 | cout<<"\n 6: killogramm-pound"; 557 | cout<<"\n 7: Gramm-Pound"; 558 | cout<<"\n 8: Pound-gramm"; 559 | cout<<"\n 9: killogramm-Metric ton"; 560 | cout<<"\n 10: Metric ton-Killogramm"; 561 | cout<<"\n 11: Back to The Main Menu"; 562 | 563 | while(1) 564 | { 565 | cout<<"\n\nPlease Enter Your Choice= "; 566 | cin>>y; 567 | 568 | if (y==1) 569 | {c.milligm_gm();} 570 | else if (y==2) 571 | {c.gm_milligm();} 572 | else if (y==3) 573 | {c.gm_kg();} 574 | else if (y==4) 575 | {c.kg_gm();} 576 | else if (y==5) 577 | {c.pound_kg();} 578 | else if (y==6) 579 | {c.kg_pound();} 580 | else if (y==7) 581 | {c.gm_pound();} 582 | else if (y==8) 583 | {c.pound_gm();} 584 | else if (y==9) 585 | {c.kg_mton();} 586 | else if (y==10) 587 | {c.mton_kg();} 588 | else if (y==11) 589 | {break;} 590 | } 591 | } 592 | else if(x==4) 593 | { clrscr(); 594 | cout<<"\n\nchoose your unit convertion:\n"; 595 | 596 | cout<<"\n 1: Square mm-Square cm"; 597 | cout<<"\n 2: square cm-Square mm"; 598 | cout<<"\n 3: square cm-square m"; 599 | cout<<"\n 4: Square m-Square cm"; 600 | cout<<"\n 5: Square m-Square km"; 601 | cout<<"\n 6: Square km-Square m"; 602 | cout<<"\n 7: Square feet-Square m"; 603 | cout<<"\n 8: Square m-Square feet"; 604 | cout<<"\n 9: Square Yard-Square m"; 605 | cout<<"\n 10: Square m-Square yard"; 606 | cout<<"\n 11: Square mile-Square km"; 607 | cout<<"\n 12: Square km-Square mile"; 608 | cout<<"\n 13: Acre-Hectare"; 609 | cout<<"\n 14: Hectare-Acre"; 610 | cout<<"\n 15: Square km-Acre "; 611 | cout<<"\n 16: Acre-Square km"; 612 | cout<<"\n 17: Back to The Main Menu"; 613 | 614 | while(1) 615 | { 616 | cout<<"\n\nPlease Enter Your Choice= "; 617 | cin>>y; 618 | 619 | if(y==1) 620 | {e.mmSq_cmSq();} 621 | else if(y==2) 622 | {e.cmSq_mmSq();} 623 | else if(y==3) 624 | {e.cmSq_mSq();} 625 | else if(y==4) 626 | {e.mSq_cmSq();} 627 | else if(y==5) 628 | {e.mSq_kmSq();} 629 | else if(y==6) 630 | {e.kmSq_mSq();} 631 | else if(y==7) 632 | {e.feetSq_mSq();} 633 | else if(y==8) 634 | {e.mSq_feetSq();} 635 | else if(y==9) 636 | {e.yardSq_mSq();} 637 | else if(y==10) 638 | {e.mSq_yardSq();} 639 | else if(y==11) 640 | {e.mileSq_kmSq();} 641 | else if(y==12) 642 | {e.kmSq_mileSq();} 643 | else if(y==13) 644 | {e.acre_hect();} 645 | else if(y==14) 646 | {e.hect_acre();} 647 | else if(y==15) 648 | {e.kmSq_acre();} 649 | else if(y==16) 650 | {e.acre_kmSq();} 651 | else if(y==17) 652 | {break;} 653 | } 654 | } 655 | else if(x==5) 656 | { clrscr(); 657 | cout<<"\n\nchoose your unit convertion:\n"; 658 | 659 | cout<<"\n 1 : ml-Litre"; 660 | cout<<"\n 2 : Litre-ml"; 661 | cout<<"\n 3 : Cubic mm-Cubic cm"; 662 | cout<<"\n 4 : Cubic cm-Cubic mm"; 663 | cout<<"\n 5 : Cubic cm-Cubic m"; 664 | cout<<"\n 6 : Cubic m-Cubic cm"; 665 | cout<<"\n 7 : Cubic Inch-Cubic m"; 666 | cout<<"\n 8 : Cubic m-Cubic Inch"; 667 | cout<<"\n 9 : Cubic feet-Cubic m"; 668 | cout<<"\n 10 : Cubic m-Cubic feet"; 669 | cout<<"\n 11 : Cubic m-Gallon (uk)"; 670 | cout<<"\n 12 : Gallon-Cubic m"; 671 | cout<<"\n 13 : Litre-Gallon"; 672 | cout<<"\n 14 : Gallon-Litre"; 673 | cout<<"\n 15 : Back to The Main Menu"; 674 | 675 | 676 | while(1) 677 | { cout<<"\n\nPlease Enter Your Choice= "; 678 | cin>>y; 679 | 680 | if (y==1) 681 | {d.mL_L();} 682 | else if (y==2) 683 | {d.L_mL();} 684 | else if (y==3) 685 | {d.mmQ_cmQ();} 686 | else if (y==4) 687 | {d.cmQ_mmQ();} 688 | else if (y==5) 689 | {d.cmQ_mQ();} 690 | else if (y==6) 691 | {d.mQ_cmQ();} 692 | else if (y==7) 693 | {d.inchQ_mQ();} 694 | else if (y==8) 695 | {d.mQ_inchQ();} 696 | else if (y==9) 697 | {d.feetQ_mQ();} 698 | else if (y==10) 699 | {d.mQ_feetQ();} 700 | else if (y==11) 701 | {d.mQ_gallon();} 702 | else if (y==12) 703 | {d.gallon_mQ();} 704 | else if (y==13) 705 | {d.L_gallon();} 706 | else if(y==14) 707 | {d.gallon_L();} 708 | else if(y==15) 709 | {break;} 710 | } 711 | } 712 | 713 | 714 | 715 | 716 | else if(x==6){ break;} 717 | 718 | } 719 | 720 | 721 | } 722 | -------------------------------------------------------------------------------- /wildcard-matching.java: -------------------------------------------------------------------------------- 1 | public class WildcardMatching { 2 | 3 | * @param args 4 | * @throws Exception 5 | */ 6 | public static void main(String[] args) throws Exception { 7 | System.out.println(new WildcardMatching().isMatch("abebd", "?be******e")); 8 | } 9 | 10 | public boolean isMatch(String s, String p) { 11 | int starIdx = -1; 12 | int starPosAtStr = -1; 13 | int j = 0; 14 | for (int i = 0, l = s.length(); i < l; ) { 15 | if (j < p.length()) { 16 | if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') { 17 | i++; 18 | j++; 19 | } else if (p.charAt(j) == '*') { 20 | starIdx = j; 21 | starPosAtStr = i; 22 | j++; // increment only pattern index. This is because '*' can match also empty string. 23 | } else if (starIdx != -1) { 24 | i = ++starPosAtStr; 25 | j = starIdx + 1; 26 | } else return false; 27 | } else if (starIdx != -1) { 28 | i = ++starPosAtStr; 29 | j = starIdx + 1; 30 | } else return false; 31 | } 32 | while (j < p.length()) { 33 | if (p.charAt(j) == '*') { 34 | j++; 35 | } else break; 36 | } 37 | return j == p.length(); 38 | } 39 | } --------------------------------------------------------------------------------