From 81c65c1c854461bcab7a7e0d109adc3871d231ab Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 02:08:20 -0700 Subject: [PATCH 01/91] Added README.md file for Find All Duplicates in an Array --- 442-find-all-duplicates-in-an-array/README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 442-find-all-duplicates-in-an-array/README.md diff --git a/442-find-all-duplicates-in-an-array/README.md b/442-find-all-duplicates-in-an-array/README.md new file mode 100644 index 0000000000000..352a79eb3dd1a --- /dev/null +++ b/442-find-all-duplicates-in-an-array/README.md @@ -0,0 +1,24 @@ +

Find All Duplicates in an Array

Difficulty: Medium

Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appears twice.

+ +

You must write an algorithm that runs in O(n) time and uses only constant auxiliary space, excluding the space needed to store the output

+ +

 

+

Example 1:

+
Input: nums = [4,3,2,7,8,2,3,1]
+Output: [2,3]
+

Example 2:

+
Input: nums = [1,1,2]
+Output: [1]
+

Example 3:

+
Input: nums = [1]
+Output: []
+
+

 

+

Constraints:

+ + From 535d65ebf3fff3d9c1942038e90b1c2e3085c526 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 02:08:22 -0700 Subject: [PATCH 02/91] Time: 124 ms (5.30%) | Memory: 69.6 MB (6.37%) - LeetSync --- .../find-all-duplicates-in-an-array.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 442-find-all-duplicates-in-an-array/find-all-duplicates-in-an-array.cpp diff --git a/442-find-all-duplicates-in-an-array/find-all-duplicates-in-an-array.cpp b/442-find-all-duplicates-in-an-array/find-all-duplicates-in-an-array.cpp new file mode 100644 index 0000000000000..64ef19e49c164 --- /dev/null +++ b/442-find-all-duplicates-in-an-array/find-all-duplicates-in-an-array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector findDuplicates(vector& nums) { + vectorv; + setst; + for(int i=0;i Date: Fri, 11 Apr 2025 02:33:41 -0700 Subject: [PATCH 03/91] Added README.md file for Find All Duplicates in an Array From c432f0fe2aedb05b092d77ab8c67d3b630919201 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 02:33:43 -0700 Subject: [PATCH 04/91] Time: 127 ms (5.30%) | Memory: 69.6 MB (6.37%) - LeetSync From 7463355eeb3725c9630552155f36414fe7fbaa93 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 03:17:28 -0700 Subject: [PATCH 05/91] Added README.md file for Minimum Number of Operations to Make Elements in Array Distinct --- .../README.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 3656-minimum-number-of-operations-to-make-elements-in-array-distinct/README.md diff --git a/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/README.md b/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/README.md new file mode 100644 index 0000000000000..f736ec60145ac --- /dev/null +++ b/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/README.md @@ -0,0 +1,62 @@ +

Minimum Number of Operations to Make Elements in Array Distinct

Difficulty: Easy

You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:

+ +
    +
  • Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
  • +
+ +

Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,2,3,4,2,3,3,5,7]

+ +

Output: 2

+ +

Explanation:

+ +
    +
  • In the first operation, the first 3 elements are removed, resulting in the array [4, 2, 3, 3, 5, 7].
  • +
  • In the second operation, the next 3 elements are removed, resulting in the array [3, 5, 7], which has distinct elements.
  • +
+ +

Therefore, the answer is 2.

+
+ +

Example 2:

+ +
+

Input: nums = [4,5,6,4,4]

+ +

Output: 2

+ +

Explanation:

+ +
    +
  • In the first operation, the first 3 elements are removed, resulting in the array [4, 4].
  • +
  • In the second operation, all remaining elements are removed, resulting in an empty array.
  • +
+ +

Therefore, the answer is 2.

+
+ +

Example 3:

+ +
+

Input: nums = [6,7,8,9]

+ +

Output: 0

+ +

Explanation:

+ +

The array already contains distinct elements. Therefore, the answer is 0.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
From da37ce3bc379d44fa0bf874c9d30bae902e435e2 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 03:17:29 -0700 Subject: [PATCH 06/91] Time: 0 ms (100.00%) | Memory: 27.8 MB (49.47%) - LeetSync --- ...erations-to-make-elements-in-array-distinct.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 3656-minimum-number-of-operations-to-make-elements-in-array-distinct/minimum-number-of-operations-to-make-elements-in-array-distinct.cpp diff --git a/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/minimum-number-of-operations-to-make-elements-in-array-distinct.cpp b/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/minimum-number-of-operations-to-make-elements-in-array-distinct.cpp new file mode 100644 index 0000000000000..c50855465a4e4 --- /dev/null +++ b/3656-minimum-number-of-operations-to-make-elements-in-array-distinct/minimum-number-of-operations-to-make-elements-in-array-distinct.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minimumOperations(vector& nums) { + int n=nums.size(); + unordered_setst; + for(int i=n-1;i>=0;i--){ + if(st.count(nums[i])){ + return ceil((i+1)/3.0); + } + st.insert(nums[i]); + } + return 0; + } +}; \ No newline at end of file From 2c3fa4336ac89cb534ec6b916d0f5b805ff1d816 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 03:43:39 -0700 Subject: [PATCH 07/91] Added README.md file for Find Minimum in Rotated Sorted Array --- .../README.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 153-find-minimum-in-rotated-sorted-array/README.md diff --git a/153-find-minimum-in-rotated-sorted-array/README.md b/153-find-minimum-in-rotated-sorted-array/README.md new file mode 100644 index 0000000000000..c42ddc4e0a363 --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/README.md @@ -0,0 +1,48 @@ +

Find Minimum in Rotated Sorted Array

Difficulty: Medium

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

+ +
    +
  • [4,5,6,7,0,1,2] if it was rotated 4 times.
  • +
  • [0,1,2,4,5,6,7] if it was rotated 7 times.
  • +
+ +

Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

+ +

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

+ +

You must write an algorithm that runs in O(log n) time.

+ +

 

+

Example 1:

+ +
+Input: nums = [3,4,5,1,2]
+Output: 1
+Explanation: The original array was [1,2,3,4,5] rotated 3 times.
+
+ +

Example 2:

+ +
+Input: nums = [4,5,6,7,0,1,2]
+Output: 0
+Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
+
+ +

Example 3:

+ +
+Input: nums = [11,13,15,17]
+Output: 11
+Explanation: The original array was [11,13,15,17] and it was rotated 4 times. 
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 5000
  • +
  • -5000 <= nums[i] <= 5000
  • +
  • All the integers of nums are unique.
  • +
  • nums is sorted and rotated between 1 and n times.
  • +
From ffe0a9a4e10d04c99a63319dcefdd1903931f3a9 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 11 Apr 2025 03:43:40 -0700 Subject: [PATCH 08/91] Time: 0 ms (100.00%) | Memory: 14.3 MB (12.40%) - LeetSync --- .../find-minimum-in-rotated-sorted-array.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 153-find-minimum-in-rotated-sorted-array/find-minimum-in-rotated-sorted-array.cpp diff --git a/153-find-minimum-in-rotated-sorted-array/find-minimum-in-rotated-sorted-array.cpp b/153-find-minimum-in-rotated-sorted-array/find-minimum-in-rotated-sorted-array.cpp new file mode 100644 index 0000000000000..536b9bffd7dcc --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/find-minimum-in-rotated-sorted-array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int findMin(vector& nums) { + int n=nums.size(); + int start=0,end=n-1; + while(start Date: Thu, 17 Apr 2025 02:54:58 -0700 Subject: [PATCH 09/91] Added README.md file for How Many Numbers Are Smaller Than the Current Number --- .../README.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1482-how-many-numbers-are-smaller-than-the-current-number/README.md diff --git a/1482-how-many-numbers-are-smaller-than-the-current-number/README.md b/1482-how-many-numbers-are-smaller-than-the-current-number/README.md new file mode 100644 index 0000000000000..0f17529a6a13c --- /dev/null +++ b/1482-how-many-numbers-are-smaller-than-the-current-number/README.md @@ -0,0 +1,39 @@ +

How Many Numbers Are Smaller Than the Current Number

Difficulty: Easy

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].

+ +

Return the answer in an array.

+ +

 

+

Example 1:

+ +
+Input: nums = [8,1,2,2,3]
+Output: [4,0,1,1,3]
+Explanation: 
+For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). 
+For nums[1]=1 does not exist any smaller number than it.
+For nums[2]=2 there exist one smaller number than it (1). 
+For nums[3]=2 there exist one smaller number than it (1). 
+For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
+
+ +

Example 2:

+ +
+Input: nums = [6,5,4,8]
+Output: [2,1,0,3]
+
+ +

Example 3:

+ +
+Input: nums = [7,7,7,7]
+Output: [0,0,0,0]
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 500
  • +
  • 0 <= nums[i] <= 100
  • +
From 5b1bc9dae90b1d474c2b5f17a6c6d112dec027b2 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 17 Apr 2025 02:55:00 -0700 Subject: [PATCH 10/91] Time: 0 ms (100.00%) | Memory: 14.3 MB (42.08%) - LeetSync --- ...rs-are-smaller-than-the-current-number.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1482-how-many-numbers-are-smaller-than-the-current-number/how-many-numbers-are-smaller-than-the-current-number.cpp diff --git a/1482-how-many-numbers-are-smaller-than-the-current-number/how-many-numbers-are-smaller-than-the-current-number.cpp b/1482-how-many-numbers-are-smaller-than-the-current-number/how-many-numbers-are-smaller-than-the-current-number.cpp new file mode 100644 index 0000000000000..b7a2ded34b354 --- /dev/null +++ b/1482-how-many-numbers-are-smaller-than-the-current-number/how-many-numbers-are-smaller-than-the-current-number.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector smallerNumbersThanCurrent(vector& nums) { + int n=nums.size(); + vectorans(n,0); + vectorcount(101,0); + for(int i=0;i Date: Thu, 17 Apr 2025 03:27:15 -0700 Subject: [PATCH 11/91] Added README.md file for Find All Numbers Disappeared in an Array --- .../README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 448-find-all-numbers-disappeared-in-an-array/README.md diff --git a/448-find-all-numbers-disappeared-in-an-array/README.md b/448-find-all-numbers-disappeared-in-an-array/README.md new file mode 100644 index 0000000000000..ca3a04430489c --- /dev/null +++ b/448-find-all-numbers-disappeared-in-an-array/README.md @@ -0,0 +1,21 @@ +

Find All Numbers Disappeared in an Array

Difficulty: Easy

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

+ +

 

+

Example 1:

+
Input: nums = [4,3,2,7,8,2,3,1]
+Output: [5,6]
+

Example 2:

+
Input: nums = [1,1]
+Output: [2]
+
+

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • 1 <= nums[i] <= n
  • +
+ +

 

+

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

From dc8e879bfd0b09e053c0a76351244c1fe904e78b Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 17 Apr 2025 03:27:17 -0700 Subject: [PATCH 12/91] Time: 0 ms (100.00%) | Memory: 53.1 MB (75.74%) - LeetSync --- ...ind-all-numbers-disappeared-in-an-array.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 448-find-all-numbers-disappeared-in-an-array/find-all-numbers-disappeared-in-an-array.cpp diff --git a/448-find-all-numbers-disappeared-in-an-array/find-all-numbers-disappeared-in-an-array.cpp b/448-find-all-numbers-disappeared-in-an-array/find-all-numbers-disappeared-in-an-array.cpp new file mode 100644 index 0000000000000..35c4cbfb90746 --- /dev/null +++ b/448-find-all-numbers-disappeared-in-an-array/find-all-numbers-disappeared-in-an-array.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + vector findDisappearedNumbers(vector& nums) { + int n=nums.size(); + vectorans; + for(int i=0;i Date: Fri, 25 Apr 2025 11:58:15 +0530 Subject: [PATCH 13/91] Added README.md file for Reverse Linked List --- 206-reverse-linked-list/README.md | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 206-reverse-linked-list/README.md diff --git a/206-reverse-linked-list/README.md b/206-reverse-linked-list/README.md new file mode 100644 index 0000000000000..bc88131cda39a --- /dev/null +++ b/206-reverse-linked-list/README.md @@ -0,0 +1,34 @@ +

Reverse Linked List

Difficulty: Easy

Given the head of a singly linked list, reverse the list, and return the reversed list.

+ +

 

+

Example 1:

+ +
+Input: head = [1,2,3,4,5]
+Output: [5,4,3,2,1]
+
+ +

Example 2:

+ +
+Input: head = [1,2]
+Output: [2,1]
+
+ +

Example 3:

+ +
+Input: head = []
+Output: []
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is the range [0, 5000].
  • +
  • -5000 <= Node.val <= 5000
  • +
+ +

 

+

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

From cc11d412cb0311658d83ad88a8025a4364b99f16 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 11:58:17 +0530 Subject: [PATCH 14/91] Time: 0 ms (100.00%) | Memory: 13.4 MB (39.35%) - LeetSync --- .../reverse-linked-list.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 206-reverse-linked-list/reverse-linked-list.cpp diff --git a/206-reverse-linked-list/reverse-linked-list.cpp b/206-reverse-linked-list/reverse-linked-list.cpp new file mode 100644 index 0000000000000..4fad719bf7805 --- /dev/null +++ b/206-reverse-linked-list/reverse-linked-list.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* curr=head; + ListNode* prev=NULL; + while(curr){ + ListNode* temp=curr->next; + curr->next=prev; + prev=curr; + curr=temp; + } + return prev; + } +}; \ No newline at end of file From a0307e29965f26683514d8a671d96ddcc5bae942 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:04:20 +0530 Subject: [PATCH 15/91] Added README.md file for Delete Node in a Linked List --- 237-delete-node-in-a-linked-list/README.md | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 237-delete-node-in-a-linked-list/README.md diff --git a/237-delete-node-in-a-linked-list/README.md b/237-delete-node-in-a-linked-list/README.md new file mode 100644 index 0000000000000..209d0c4ce5b44 --- /dev/null +++ b/237-delete-node-in-a-linked-list/README.md @@ -0,0 +1,49 @@ +

Delete Node in a Linked List

Difficulty: Medium

There is a singly-linked list head and we want to delete a node node in it.

+ +

You are given the node to be deleted node. You will not be given access to the first node of head.

+ +

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

+ +

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

+ +
    +
  • The value of the given node should not exist in the linked list.
  • +
  • The number of nodes in the linked list should decrease by one.
  • +
  • All the values before node should be in the same order.
  • +
  • All the values after node should be in the same order.
  • +
+ +

Custom testing:

+ +
    +
  • For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
  • +
  • We will build the linked list and pass the node to your function.
  • +
  • The output will be the entire list after calling your function.
  • +
+ +

 

+

Example 1:

+ +
+Input: head = [4,5,1,9], node = 5
+Output: [4,1,9]
+Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
+
+ +

Example 2:

+ +
+Input: head = [4,5,1,9], node = 1
+Output: [4,5,9]
+Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the given list is in the range [2, 1000].
  • +
  • -1000 <= Node.val <= 1000
  • +
  • The value of each node in the list is unique.
  • +
  • The node to be deleted is in the list and is not a tail node.
  • +
From e69d7f5aaf9abedc6049a025656b509ff37c9113 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:04:21 +0530 Subject: [PATCH 16/91] Time: 7 ms (51.41%) | Memory: 12.4 MB (53.01%) - LeetSync --- .../delete-node-in-a-linked-list.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 237-delete-node-in-a-linked-list/delete-node-in-a-linked-list.cpp diff --git a/237-delete-node-in-a-linked-list/delete-node-in-a-linked-list.cpp b/237-delete-node-in-a-linked-list/delete-node-in-a-linked-list.cpp new file mode 100644 index 0000000000000..0469c2f25bde5 --- /dev/null +++ b/237-delete-node-in-a-linked-list/delete-node-in-a-linked-list.cpp @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + void deleteNode(ListNode* node) { + node->val=node->next->val; + node->next=node->next->next; + } +}; \ No newline at end of file From ed17b4834d3ff6a31b562da9f7533b64b7f1564a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:16:48 +0530 Subject: [PATCH 17/91] Added README.md file for Middle of the Linked List --- 908-middle-of-the-linked-list/README.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 908-middle-of-the-linked-list/README.md diff --git a/908-middle-of-the-linked-list/README.md b/908-middle-of-the-linked-list/README.md new file mode 100644 index 0000000000000..57aa43325b20b --- /dev/null +++ b/908-middle-of-the-linked-list/README.md @@ -0,0 +1,28 @@ +

Middle of the Linked List

Difficulty: Easy

Given the head of a singly linked list, return the middle node of the linked list.

+ +

If there are two middle nodes, return the second middle node.

+ +

 

+

Example 1:

+ +
+Input: head = [1,2,3,4,5]
+Output: [3,4,5]
+Explanation: The middle node of the list is node 3.
+
+ +

Example 2:

+ +
+Input: head = [1,2,3,4,5,6]
+Output: [4,5,6]
+Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 100].
  • +
  • 1 <= Node.val <= 100
  • +
From 38e997a35b295a3594d34e117c4fab59491fd707 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:16:49 +0530 Subject: [PATCH 18/91] Time: 0 ms (100.00%) | Memory: 10.1 MB (25.88%) - LeetSync --- .../middle-of-the-linked-list.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 908-middle-of-the-linked-list/middle-of-the-linked-list.cpp diff --git a/908-middle-of-the-linked-list/middle-of-the-linked-list.cpp b/908-middle-of-the-linked-list/middle-of-the-linked-list.cpp new file mode 100644 index 0000000000000..ab409313b1e56 --- /dev/null +++ b/908-middle-of-the-linked-list/middle-of-the-linked-list.cpp @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + ListNode* p1=head; + ListNode* p2=head; + while(p2 && p2->next){ + p1=p1->next; + p2=p2->next->next; + } + return p1; + } +}; \ No newline at end of file From 4df994cb5b0f95ec9a9dec831f839e10a87fe1e8 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:20:23 +0530 Subject: [PATCH 19/91] Added README.md file for Linked List Cycle --- 141-linked-list-cycle/README.md | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 141-linked-list-cycle/README.md diff --git a/141-linked-list-cycle/README.md b/141-linked-list-cycle/README.md new file mode 100644 index 0000000000000..73d0401fae218 --- /dev/null +++ b/141-linked-list-cycle/README.md @@ -0,0 +1,42 @@ +

Linked List Cycle

Difficulty: Easy

Given head, the head of a linked list, determine if the linked list has a cycle in it.

+ +

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

+ +

Return true if there is a cycle in the linked list. Otherwise, return false.

+ +

 

+

Example 1:

+ +
+Input: head = [3,2,0,-4], pos = 1
+Output: true
+Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
+
+ +

Example 2:

+ +
+Input: head = [1,2], pos = 0
+Output: true
+Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
+
+ +

Example 3:

+ +
+Input: head = [1], pos = -1
+Output: false
+Explanation: There is no cycle in the linked list.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the list is in the range [0, 104].
  • +
  • -105 <= Node.val <= 105
  • +
  • pos is -1 or a valid index in the linked-list.
  • +
+ +

 

+

Follow up: Can you solve it using O(1) (i.e. constant) memory?

From e31b5f3698e31d489137d01a32e5f61d7b881bc5 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:20:24 +0530 Subject: [PATCH 20/91] Time: 12 ms (39.30%) | Memory: 11.8 MB (53.13%) - LeetSync --- 141-linked-list-cycle/linked-list-cycle.cpp | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 141-linked-list-cycle/linked-list-cycle.cpp diff --git a/141-linked-list-cycle/linked-list-cycle.cpp b/141-linked-list-cycle/linked-list-cycle.cpp new file mode 100644 index 0000000000000..0717e0bf5e0fe --- /dev/null +++ b/141-linked-list-cycle/linked-list-cycle.cpp @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode* p1=head; + ListNode* p2=head; + while(p2 && p2->next){ + p2=p2->next->next; + p1=p1->next; + if(p1==p2){ + return true; + } + } + return false; + } +}; \ No newline at end of file From ba1b3226984e065dd82a0977aeb614ec1b6de6af Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:57:03 +0530 Subject: [PATCH 21/91] Added README.md file for Linked List Cycle II --- 142-linked-list-cycle-ii/README.md | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 142-linked-list-cycle-ii/README.md diff --git a/142-linked-list-cycle-ii/README.md b/142-linked-list-cycle-ii/README.md new file mode 100644 index 0000000000000..61bf8c3faade6 --- /dev/null +++ b/142-linked-list-cycle-ii/README.md @@ -0,0 +1,42 @@ +

Linked List Cycle II

Difficulty: Medium

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

+ +

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

+ +

Do not modify the linked list.

+ +

 

+

Example 1:

+ +
+Input: head = [3,2,0,-4], pos = 1
+Output: tail connects to node index 1
+Explanation: There is a cycle in the linked list, where tail connects to the second node.
+
+ +

Example 2:

+ +
+Input: head = [1,2], pos = 0
+Output: tail connects to node index 0
+Explanation: There is a cycle in the linked list, where tail connects to the first node.
+
+ +

Example 3:

+ +
+Input: head = [1], pos = -1
+Output: no cycle
+Explanation: There is no cycle in the linked list.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of the nodes in the list is in the range [0, 104].
  • +
  • -105 <= Node.val <= 105
  • +
  • pos is -1 or a valid index in the linked-list.
  • +
+ +

 

+

Follow up: Can you solve it using O(1) (i.e. constant) memory?

From c9b7eb61171dea4463ee0f60cde48bc2719aa6b3 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:57:04 +0530 Subject: [PATCH 22/91] Time: 9 ms (47.84%) | Memory: 13.4 MB (11.70%) - LeetSync --- .../linked-list-cycle-ii.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 142-linked-list-cycle-ii/linked-list-cycle-ii.cpp diff --git a/142-linked-list-cycle-ii/linked-list-cycle-ii.cpp b/142-linked-list-cycle-ii/linked-list-cycle-ii.cpp new file mode 100644 index 0000000000000..1b824fa3d5d68 --- /dev/null +++ b/142-linked-list-cycle-ii/linked-list-cycle-ii.cpp @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + unordered_setsee; + while(head){ + if(see.find(head)!=see.end()){ + return head; + } + see.insert(head); + head=head->next; + } + return nullptr; + } +}; \ No newline at end of file From 3794ea3acf7aaedcf1b79d9d80ee795c76f6ec7a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:36:19 +0530 Subject: [PATCH 23/91] Added README.md file for Intersection of Two Linked Lists --- .../README.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 160-intersection-of-two-linked-lists/README.md diff --git a/160-intersection-of-two-linked-lists/README.md b/160-intersection-of-two-linked-lists/README.md new file mode 100644 index 0000000000000..ae93b548b89d3 --- /dev/null +++ b/160-intersection-of-two-linked-lists/README.md @@ -0,0 +1,67 @@ +

Intersection of Two Linked Lists

Difficulty: Easy

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

+ +

For example, the following two linked lists begin to intersect at node c1:

+ +

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

+ +

Note that the linked lists must retain their original structure after the function returns.

+ +

Custom Judge:

+ +

The inputs to the judge are given as follows (your program is not given these inputs):

+ +
    +
  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • +
  • listA - The first linked list.
  • +
  • listB - The second linked list.
  • +
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • +
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
  • +
+ +

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

+ +

 

+

Example 1:

+ +
+Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
+Output: Intersected at '8'
+Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
+- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
+
+ +

Example 2:

+ +
+Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
+Output: Intersected at '2'
+Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
+From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
+
+ +

Example 3:

+ +
+Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
+Output: No intersection
+Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
+Explanation: The two lists do not intersect, so return null.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes of listA is in the m.
  • +
  • The number of nodes of listB is in the n.
  • +
  • 1 <= m, n <= 3 * 104
  • +
  • 1 <= Node.val <= 105
  • +
  • 0 <= skipA <= m
  • +
  • 0 <= skipB <= n
  • +
  • intersectVal is 0 if listA and listB do not intersect.
  • +
  • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
  • +
+ +

 

+Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory? \ No newline at end of file From af42c576882ece4a7aadf75d54f8b4900e9410f2 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:36:20 +0530 Subject: [PATCH 24/91] Time: 36 ms (67.30%) | Memory: 18.7 MB (24.85%) - LeetSync --- .../intersection-of-two-linked-lists.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp diff --git a/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp b/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp new file mode 100644 index 0000000000000..c029e36a4f424 --- /dev/null +++ b/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp @@ -0,0 +1,45 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + int a=0,b=0; + ListNode* p1=headA; + ListNode* p2=headB; + while(p1){ + a++; + p1=p1->next; + } + while(p2){ + b++; + p2=p2->next; + } + int t=abs(a-b); + if(a>b){ + while(t){ + headA=headA->next; + t--; + } + } + else{ + while(t){ + headB=headB->next; + t--; + } + } + while(headA && headB){ + if(headA == headB){ + return headB; + } + headA=headA->next; + headB=headB->next; + } + return NULL; + } +}; \ No newline at end of file From 4bb2f4acaf401869c20a2c301e83be7033f4a57f Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:40:56 +0530 Subject: [PATCH 25/91] Update intersection-of-two-linked-lists.cpp Time Complexity: O(n + m) Space Complexity: O(1) --- .../intersection-of-two-linked-lists.cpp | 39 ++++--------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp b/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp index c029e36a4f424..9f7c909ba588e 100644 --- a/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp +++ b/160-intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp @@ -9,37 +9,12 @@ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { - int a=0,b=0; - ListNode* p1=headA; - ListNode* p2=headB; - while(p1){ - a++; - p1=p1->next; + ListNode *p1 = headA, *p2 = headB; + while(p1 != p2) + { + p1 = p1 == nullptr ? headB : p1->next; + p2 = p2 == nullptr ? headA : p2->next; } - while(p2){ - b++; - p2=p2->next; - } - int t=abs(a-b); - if(a>b){ - while(t){ - headA=headA->next; - t--; - } - } - else{ - while(t){ - headB=headB->next; - t--; - } - } - while(headA && headB){ - if(headA == headB){ - return headB; - } - headA=headA->next; - headB=headB->next; - } - return NULL; + return p1; } -}; \ No newline at end of file +}; From 4915931728ff41352431b776dc86dec15fdca46f Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 17 May 2025 20:07:59 +0530 Subject: [PATCH 26/91] Added README.md file for Sort Colors --- 75-sort-colors/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 75-sort-colors/README.md diff --git a/75-sort-colors/README.md b/75-sort-colors/README.md new file mode 100644 index 0000000000000..e9ec45cff5f2a --- /dev/null +++ b/75-sort-colors/README.md @@ -0,0 +1,32 @@ +

Sort Colors

Difficulty: Medium

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

+ +

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

+ +

You must solve this problem without using the library's sort function.

+ +

 

+

Example 1:

+ +
+Input: nums = [2,0,2,1,1,0]
+Output: [0,0,1,1,2,2]
+
+ +

Example 2:

+ +
+Input: nums = [2,0,1]
+Output: [0,1,2]
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 300
  • +
  • nums[i] is either 0, 1, or 2.
  • +
+ +

 

+

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

From ec297e580a112045d353ddcd9d04b3595a521f4e Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 17 May 2025 20:08:02 +0530 Subject: [PATCH 27/91] Time: 0 ms (100.00%) | Memory: 11.8 MB (11.80%) - LeetSync --- 75-sort-colors/sort-colors.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 75-sort-colors/sort-colors.cpp diff --git a/75-sort-colors/sort-colors.cpp b/75-sort-colors/sort-colors.cpp new file mode 100644 index 0000000000000..872c342d3cb60 --- /dev/null +++ b/75-sort-colors/sort-colors.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + void sortColors(vector& nums) { + unordered_map count = {{0, 0}, {1, 0}, {2, 0}}; + + for (int num : nums) { + count[num]++; + } + + int idx = 0; + for (int color = 0; color < 3; color++) { + int freq = count[color]; + for (int j = 0; j < freq; j++) { + nums[idx] = color; + idx++; + } + } + + } +}; \ No newline at end of file From 8f50515ed08a3230d6b9555da0470f41db5816e5 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 21 May 2025 16:55:27 +0530 Subject: [PATCH 28/91] Added README.md file for Set Matrix Zeroes --- 73-set-matrix-zeroes/README.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 73-set-matrix-zeroes/README.md diff --git a/73-set-matrix-zeroes/README.md b/73-set-matrix-zeroes/README.md new file mode 100644 index 0000000000000..2b023786284a5 --- /dev/null +++ b/73-set-matrix-zeroes/README.md @@ -0,0 +1,37 @@ +

Set Matrix Zeroes

Difficulty: Medium

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

+ +

You must do it in place.

+ +

 

+

Example 1:

+ +
+Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
+Output: [[1,0,1],[0,0,0],[1,0,1]]
+
+ +

Example 2:

+ +
+Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
+Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[0].length
  • +
  • 1 <= m, n <= 200
  • +
  • -231 <= matrix[i][j] <= 231 - 1
  • +
+ +

 

+

Follow up:

+ +
    +
  • A straightforward solution using O(mn) space is probably a bad idea.
  • +
  • A simple improvement uses O(m + n) space, but still not the best solution.
  • +
  • Could you devise a constant space solution?
  • +
From dd60f5dab4a2ef5000f001f806f516741f9db55f Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 21 May 2025 16:55:30 +0530 Subject: [PATCH 29/91] Time: 0 ms (100.00%) | Memory: 18.8 MB (24.60%) - LeetSync --- 73-set-matrix-zeroes/set-matrix-zeroes.cpp | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 73-set-matrix-zeroes/set-matrix-zeroes.cpp diff --git a/73-set-matrix-zeroes/set-matrix-zeroes.cpp b/73-set-matrix-zeroes/set-matrix-zeroes.cpp new file mode 100644 index 0000000000000..4993fd5495de6 --- /dev/null +++ b/73-set-matrix-zeroes/set-matrix-zeroes.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + void setZeroes(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + vectorrow(n),col(m); + for(int i=0;i Date: Wed, 21 May 2025 17:01:01 +0530 Subject: [PATCH 30/91] Added README.md file for Set Matrix Zeroes From 0ab82b6e2314b12c22ab1a044555303be65f23c0 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 21 May 2025 17:01:03 +0530 Subject: [PATCH 31/91] Time: 0 ms (100.00%) | Memory: 18.7 MB (52.14%) - LeetSync From b18fe536e48331a6319041af40417abc61eb5e94 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Mon, 2 Jun 2025 23:13:53 +0530 Subject: [PATCH 32/91] Added README.md file for Container With Most Water --- 11-container-with-most-water/README.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 11-container-with-most-water/README.md diff --git a/11-container-with-most-water/README.md b/11-container-with-most-water/README.md new file mode 100644 index 0000000000000..3bcfe48eb045a --- /dev/null +++ b/11-container-with-most-water/README.md @@ -0,0 +1,32 @@ +

Container With Most Water

Difficulty: Medium

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

+ +

Find two lines that together with the x-axis form a container, such that the container contains the most water.

+ +

Return the maximum amount of water a container can store.

+ +

Notice that you may not slant the container.

+ +

 

+

Example 1:

+ +
+Input: height = [1,8,6,2,5,4,8,3,7]
+Output: 49
+Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
+
+ +

Example 2:

+ +
+Input: height = [1,1]
+Output: 1
+
+ +

 

+

Constraints:

+ +
    +
  • n == height.length
  • +
  • 2 <= n <= 105
  • +
  • 0 <= height[i] <= 104
  • +
From c104cc398200fc7e0ae911438453b543fd6b7be0 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Mon, 2 Jun 2025 23:13:55 +0530 Subject: [PATCH 33/91] Time: 0 ms (100.00%) | Memory: 63 MB (12.54%) - LeetSync --- .../container-with-most-water.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 11-container-with-most-water/container-with-most-water.cpp diff --git a/11-container-with-most-water/container-with-most-water.cpp b/11-container-with-most-water/container-with-most-water.cpp new file mode 100644 index 0000000000000..870d730211384 --- /dev/null +++ b/11-container-with-most-water/container-with-most-water.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxArea(vector& height) { + int n = height.size(); + int start = 0; + int end = n - 1; + int maxarea = 0; + while (start < end) { + maxarea = max(maxarea, (end - start) * min(height[start], height[end])); + if (height[start] < height[end]) { + start++; + } else { + end--; + } + } + return maxarea; + } +}; \ No newline at end of file From 23736c62237bb9a613de0f83d698dee76d53d325 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Tue, 3 Jun 2025 23:16:19 +0530 Subject: [PATCH 34/91] Added README.md file for 3Sum --- 15-3sum/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 15-3sum/README.md diff --git a/15-3sum/README.md b/15-3sum/README.md new file mode 100644 index 0000000000000..0c1e6603eda13 --- /dev/null +++ b/15-3sum/README.md @@ -0,0 +1,41 @@ +

3Sum

Difficulty: Medium

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

+ +

Notice that the solution set must not contain duplicate triplets.

+ +

 

+

Example 1:

+ +
+Input: nums = [-1,0,1,2,-1,-4]
+Output: [[-1,-1,2],[-1,0,1]]
+Explanation: 
+nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
+nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
+nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
+The distinct triplets are [-1,0,1] and [-1,-1,2].
+Notice that the order of the output and the order of the triplets does not matter.
+
+ +

Example 2:

+ +
+Input: nums = [0,1,1]
+Output: []
+Explanation: The only possible triplet does not sum up to 0.
+
+ +

Example 3:

+ +
+Input: nums = [0,0,0]
+Output: [[0,0,0]]
+Explanation: The only possible triplet sums up to 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 3000
  • +
  • -105 <= nums[i] <= 105
  • +
From 4db4cf3b2b5cb874b6abdd587d3bf5bca51523a7 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:52:54 +0530 Subject: [PATCH 35/91] Added README.md file for 3Sum Closest --- 16-3sum-closest/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 16-3sum-closest/README.md diff --git a/16-3sum-closest/README.md b/16-3sum-closest/README.md new file mode 100644 index 0000000000000..4770ee02d1092 --- /dev/null +++ b/16-3sum-closest/README.md @@ -0,0 +1,31 @@ +

3Sum Closest

Difficulty: Medium

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

+ +

Return the sum of the three integers.

+ +

You may assume that each input would have exactly one solution.

+ +

 

+

Example 1:

+ +
+Input: nums = [-1,2,1,-4], target = 1
+Output: 2
+Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
+
+ +

Example 2:

+ +
+Input: nums = [0,0,0], target = 1
+Output: 0
+Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 500
  • +
  • -1000 <= nums[i] <= 1000
  • +
  • -104 <= target <= 104
  • +
From b169a60917fb82d410a868987663282b6b07890f Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:52:57 +0530 Subject: [PATCH 36/91] Time: 7 ms (88.53%) | Memory: 13.9 MB (61.83%) - LeetSync --- 16-3sum-closest/3sum-closest.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 16-3sum-closest/3sum-closest.cpp diff --git a/16-3sum-closest/3sum-closest.cpp b/16-3sum-closest/3sum-closest.cpp new file mode 100644 index 0000000000000..142220f1ab56d --- /dev/null +++ b/16-3sum-closest/3sum-closest.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int threeSumClosest(vector& nums, int target) { + sort(nums.begin(),nums.end()); + int diff=INT_MAX; + int n=nums.size(); + int ans; + for(int i=0;iabs(target-sum)){ + diff=abs(target-sum); + ans=sum; + } + if(sum==target) return sum; + else if(sum Date: Wed, 4 Jun 2025 19:10:01 +0530 Subject: [PATCH 37/91] Added README.md file for 4Sum --- 18-4sum/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 18-4sum/README.md diff --git a/18-4sum/README.md b/18-4sum/README.md new file mode 100644 index 0000000000000..ec4a0f4c6b353 --- /dev/null +++ b/18-4sum/README.md @@ -0,0 +1,33 @@ +

4Sum

Difficulty: Medium

Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

+ +
    +
  • 0 <= a, b, c, d < n
  • +
  • a, b, c, and d are distinct.
  • +
  • nums[a] + nums[b] + nums[c] + nums[d] == target
  • +
+ +

You may return the answer in any order.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,0,-1,0,-2,2], target = 0
+Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
+
+ +

Example 2:

+ +
+Input: nums = [2,2,2,2,2], target = 8
+Output: [[2,2,2,2]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 200
  • +
  • -109 <= nums[i] <= 109
  • +
  • -109 <= target <= 109
  • +
From 25220d43b18601849cb413fd8757e0242ee75a47 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 4 Jun 2025 19:10:03 +0530 Subject: [PATCH 38/91] Time: 23 ms (57.75%) | Memory: 17.5 MB (58.69%) - LeetSync --- 18-4sum/4sum.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 18-4sum/4sum.cpp diff --git a/18-4sum/4sum.cpp b/18-4sum/4sum.cpp new file mode 100644 index 0000000000000..99e2cf753ffac --- /dev/null +++ b/18-4sum/4sum.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + vector> fourSum(vector& nums, int target) { + vector>ans; + int n=nums.size(); + sort(nums.begin(),nums.end()); + for(int i=0;i0 && nums[i]==nums[i-1]) continue; + for(int j=i+1;jtarget) l--; + else if(sumtrip={nums[i],nums[j],nums[k],nums[l]}; + ans.push_back(trip); + k++; + l--; + while(k Date: Thu, 5 Jun 2025 23:51:32 +0530 Subject: [PATCH 39/91] Added README.md file for Next Permutation --- 31-next-permutation/README.md | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 31-next-permutation/README.md diff --git a/31-next-permutation/README.md b/31-next-permutation/README.md new file mode 100644 index 0000000000000..2f242acf9c938 --- /dev/null +++ b/31-next-permutation/README.md @@ -0,0 +1,47 @@ +

Next Permutation

Difficulty: Medium

A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

+ +
    +
  • For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
  • +
+ +

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

+ +
    +
  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • +
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • +
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
  • +
+ +

Given an array of integers nums, find the next permutation of nums.

+ +

The replacement must be in place and use only constant extra memory.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,2,3]
+Output: [1,3,2]
+
+ +

Example 2:

+ +
+Input: nums = [3,2,1]
+Output: [1,2,3]
+
+ +

Example 3:

+ +
+Input: nums = [1,1,5]
+Output: [1,5,1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 100
  • +
From be94dc80a71aed2a38b7e3c069d4dcf890fc37cd Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 5 Jun 2025 23:51:36 +0530 Subject: [PATCH 40/91] Time: 0 ms (100.00%) | Memory: 15.7 MB (32.61%) - LeetSync --- 31-next-permutation/next-permutation.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 31-next-permutation/next-permutation.cpp diff --git a/31-next-permutation/next-permutation.cpp b/31-next-permutation/next-permutation.cpp new file mode 100644 index 0000000000000..0460f57ec5cd6 --- /dev/null +++ b/31-next-permutation/next-permutation.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + void nextPermutation(vector& nums) { + int i = nums.size() - 1; + while (i > 0 && nums[i-1] >= nums[i]) { + i--; + } + if (i == 0) { + reverse(nums.begin(), nums.end()); + return; + } + int j = nums.size() - 1; + while (j >= i && nums[j] <= nums[i-1]) { + j--; + } + swap(nums[i-1], nums[j]); + reverse(nums.begin() + i, nums.end()); + } +}; \ No newline at end of file From d81ba411b17c263ad838a99ebaf6d405c6dddb60 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 5 Jun 2025 23:52:18 +0530 Subject: [PATCH 41/91] Added README.md file for Next Permutation From 9d395e19a4cd2bb9a229edafdb911cc09fdfd2cd Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 5 Jun 2025 23:52:20 +0530 Subject: [PATCH 42/91] Time: 3 ms (3.05%) | Memory: 15.7 MB (32.61%) - LeetSync From 7a1326e6df438f91032542793e1a23516a59622b Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 5 Jun 2025 23:54:34 +0530 Subject: [PATCH 43/91] Added README.md file for Search in Rotated Sorted Array --- 33-search-in-rotated-sorted-array/README.md | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 33-search-in-rotated-sorted-array/README.md diff --git a/33-search-in-rotated-sorted-array/README.md b/33-search-in-rotated-sorted-array/README.md new file mode 100644 index 0000000000000..552864e6f5fd0 --- /dev/null +++ b/33-search-in-rotated-sorted-array/README.md @@ -0,0 +1,29 @@ +

Search in Rotated Sorted Array

Difficulty: Medium

There is an integer array nums sorted in ascending order (with distinct values).

+ +

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

+ +

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [4,5,6,7,0,1,2], target = 0
+Output: 4
+

Example 2:

+
Input: nums = [4,5,6,7,0,1,2], target = 3
+Output: -1
+

Example 3:

+
Input: nums = [1], target = 0
+Output: -1
+
+

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 5000
  • +
  • -104 <= nums[i] <= 104
  • +
  • All values of nums are unique.
  • +
  • nums is an ascending array that is possibly rotated.
  • +
  • -104 <= target <= 104
  • +
From 7ec2918ad3f525b129d706f031dbb989886149cb Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 5 Jun 2025 23:54:35 +0530 Subject: [PATCH 44/91] Time: 0 ms (100.00%) | Memory: 15.3 MB (3.17%) - LeetSync --- .../search-in-rotated-sorted-array.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 33-search-in-rotated-sorted-array/search-in-rotated-sorted-array.cpp diff --git a/33-search-in-rotated-sorted-array/search-in-rotated-sorted-array.cpp b/33-search-in-rotated-sorted-array/search-in-rotated-sorted-array.cpp new file mode 100644 index 0000000000000..8407643c8ecf5 --- /dev/null +++ b/33-search-in-rotated-sorted-array/search-in-rotated-sorted-array.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int search(vector& nums, int target) { + int left = 0; + int right = nums.size() - 1; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target) { + return mid; + } else if (nums[mid] >= nums[left]) { + if (nums[left] <= target && target <= nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + if (nums[mid] <= target && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + return -1; + } +}; \ No newline at end of file From 2744810a1d21acb2cf411b07b224f31c68341d8b Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 6 Jun 2025 08:33:09 +0530 Subject: [PATCH 45/91] Added README.md file for Using a Robot to Print the Lexicographically Smallest String --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2520-using-a-robot-to-print-the-lexicographically-smallest-string/README.md diff --git a/2520-using-a-robot-to-print-the-lexicographically-smallest-string/README.md b/2520-using-a-robot-to-print-the-lexicographically-smallest-string/README.md new file mode 100644 index 0000000000000..a77cebecedaeb --- /dev/null +++ b/2520-using-a-robot-to-print-the-lexicographically-smallest-string/README.md @@ -0,0 +1,51 @@ +

Using a Robot to Print the Lexicographically Smallest String

Difficulty: Medium

You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:

+ +
    +
  • Remove the first character of a string s and give it to the robot. The robot will append this character to the string t.
  • +
  • Remove the last character of a string t and give it to the robot. The robot will write this character on paper.
  • +
+ +

Return the lexicographically smallest string that can be written on the paper.

+ +

 

+

Example 1:

+ +
+Input: s = "zza"
+Output: "azz"
+Explanation: Let p denote the written string.
+Initially p="", s="zza", t="".
+Perform first operation three times p="", s="", t="zza".
+Perform second operation three times p="azz", s="", t="".
+
+ +

Example 2:

+ +
+Input: s = "bac"
+Output: "abc"
+Explanation: Let p denote the written string.
+Perform first operation twice p="", s="c", t="ba". 
+Perform second operation twice p="ab", s="c", t="". 
+Perform first operation p="ab", s="", t="c". 
+Perform second operation p="abc", s="", t="".
+
+ +

Example 3:

+ +
+Input: s = "bdda"
+Output: "addb"
+Explanation: Let p denote the written string.
+Initially p="", s="bdda", t="".
+Perform first operation four times p="", s="", t="bdda".
+Perform second operation four times p="addb", s="", t="".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists of only English lowercase letters.
  • +
From 60814762e55ba4bcf6c70899cda5c8695f731a5a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 6 Jun 2025 08:33:11 +0530 Subject: [PATCH 46/91] Time: 54 ms (58.91%) | Memory: 32.6 MB (74.26%) - LeetSync --- ...-the-lexicographically-smallest-string.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2520-using-a-robot-to-print-the-lexicographically-smallest-string/using-a-robot-to-print-the-lexicographically-smallest-string.cpp diff --git a/2520-using-a-robot-to-print-the-lexicographically-smallest-string/using-a-robot-to-print-the-lexicographically-smallest-string.cpp b/2520-using-a-robot-to-print-the-lexicographically-smallest-string/using-a-robot-to-print-the-lexicographically-smallest-string.cpp new file mode 100644 index 0000000000000..e79b149118d8c --- /dev/null +++ b/2520-using-a-robot-to-print-the-lexicographically-smallest-string/using-a-robot-to-print-the-lexicographically-smallest-string.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string robotWithString(string s) { + int n = s.size(); + vector freq(26, 0); + for (char c : s) + freq[c - 'a']++; + stack t; + string p; + int i = 0; + for (char c : s) { + t.push(c); + freq[c - 'a']--; + char smallest = 'a'; + while (smallest <= 'z' && freq[smallest - 'a'] == 0) + smallest++; + while (!t.empty() && (smallest > 'z' || t.top() <= smallest)) { + p += t.top(); + t.pop(); + } + } + while (!t.empty()) { + p += t.top(); + t.pop(); + } + + return p; + + } +}; \ No newline at end of file From 074dc26e8c3c18ac2d7a91d5ceb4a56f87acef8f Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 7 Jun 2025 22:18:30 +0530 Subject: [PATCH 47/91] Added README.md file for Lexicographically Minimum String After Removing Stars --- .../README.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 3445-lexicographically-minimum-string-after-removing-stars/README.md diff --git a/3445-lexicographically-minimum-string-after-removing-stars/README.md b/3445-lexicographically-minimum-string-after-removing-stars/README.md new file mode 100644 index 0000000000000..cb028a333d109 --- /dev/null +++ b/3445-lexicographically-minimum-string-after-removing-stars/README.md @@ -0,0 +1,43 @@ +

Lexicographically Minimum String After Removing Stars

Difficulty: Medium

You are given a string s. It may contain any number of '*' characters. Your task is to remove all '*' characters.

+ +

While there is a '*', do the following operation:

+ +
    +
  • Delete the leftmost '*' and the smallest non-'*' character to its left. If there are several smallest characters, you can delete any of them.
  • +
+ +

Return the lexicographically smallest resulting string after removing all '*' characters.

+ +

 

+

Example 1:

+ +
+

Input: s = "aaba*"

+ +

Output: "aab"

+ +

Explanation:

+ +

We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest.

+
+ +

Example 2:

+ +
+

Input: s = "abc"

+ +

Output: "abc"

+ +

Explanation:

+ +

There is no '*' in the string.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • s consists only of lowercase English letters and '*'.
  • +
  • The input is generated such that it is possible to delete all '*' characters.
  • +
From e6a15fd75f10dd9e18066b2727b8ebfc31d5d9ab Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 7 Jun 2025 22:18:33 +0530 Subject: [PATCH 48/91] Time: 1949 ms (5.19%) | Memory: 118 MB (12.34%) - LeetSync --- ...ly-minimum-string-after-removing-stars.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 3445-lexicographically-minimum-string-after-removing-stars/lexicographically-minimum-string-after-removing-stars.cpp diff --git a/3445-lexicographically-minimum-string-after-removing-stars/lexicographically-minimum-string-after-removing-stars.cpp b/3445-lexicographically-minimum-string-after-removing-stars/lexicographically-minimum-string-after-removing-stars.cpp new file mode 100644 index 0000000000000..ab6843e915499 --- /dev/null +++ b/3445-lexicographically-minimum-string-after-removing-stars/lexicographically-minimum-string-after-removing-stars.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + string clearStars(string s) { + string p; + multiset ms; + for (char c : s) { + if (c != '*') { + p.push_back(c); + ms.insert(c); + } else { + if (!ms.empty()) { + char smallest = *ms.begin(); + ms.erase(ms.begin()); + for (int j = p.size() - 1; j >= 0; j--) { + if (p[j] == smallest) { + p.erase(j, 1); + break; + } + } + } + } + } + return p; + } +}; \ No newline at end of file From 5bbb759e910ff4cda7324420ee771251c0e0cfab Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:19:28 +0530 Subject: [PATCH 49/91] Added README.md file for Lexicographical Numbers --- 386-lexicographical-numbers/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 386-lexicographical-numbers/README.md diff --git a/386-lexicographical-numbers/README.md b/386-lexicographical-numbers/README.md new file mode 100644 index 0000000000000..1161322c71dea --- /dev/null +++ b/386-lexicographical-numbers/README.md @@ -0,0 +1,18 @@ +

Lexicographical Numbers

Difficulty: Medium

Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.

+ +

You must write an algorithm that runs in O(n) time and uses O(1) extra space. 

+ +

 

+

Example 1:

+
Input: n = 13
+Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
+

Example 2:

+
Input: n = 2
+Output: [1,2]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= n <= 5 * 104
  • +
From 901cc0f391e11563336efaf666018fc4a43d84c1 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:19:30 +0530 Subject: [PATCH 50/91] Time: 0 ms (100.00%) | Memory: 13.9 MB (83.88%) - LeetSync --- .../lexicographical-numbers.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 386-lexicographical-numbers/lexicographical-numbers.cpp diff --git a/386-lexicographical-numbers/lexicographical-numbers.cpp b/386-lexicographical-numbers/lexicographical-numbers.cpp new file mode 100644 index 0000000000000..e8145bbd2ffa6 --- /dev/null +++ b/386-lexicographical-numbers/lexicographical-numbers.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector lexicalOrder(int n) { + vector v; + int count = 1; + for (int i = 0; i < n; i++) { + v.push_back(count); + if (count * 10 <= n) { + count *= 10; + } else { + if (count >= n) count /= 10; + count++; + while (count % 10 == 0) count /= 10; + } + } + return v; + + } +}; \ No newline at end of file From 3eee85d003bff0b619ae6ab7a5143d49953898d0 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 11 Jun 2025 00:09:30 +0530 Subject: [PATCH 51/91] Added README.md file for Maximum Difference Between Even and Odd Frequency I --- .../README.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 3753-maximum-difference-between-even-and-odd-frequency-i/README.md diff --git a/3753-maximum-difference-between-even-and-odd-frequency-i/README.md b/3753-maximum-difference-between-even-and-odd-frequency-i/README.md new file mode 100644 index 0000000000000..d0bb7968000cc --- /dev/null +++ b/3753-maximum-difference-between-even-and-odd-frequency-i/README.md @@ -0,0 +1,50 @@ +

Maximum Difference Between Even and Odd Frequency I

Difficulty: Easy

You are given a string s consisting of lowercase English letters.

+ +

Your task is to find the maximum difference diff = freq(a1) - freq(a2) between the frequency of characters a1 and a2 in the string such that:

+ +
    +
  • a1 has an odd frequency in the string.
  • +
  • a2 has an even frequency in the string.
  • +
+ +

Return this maximum difference.

+ +

 

+

Example 1:

+ +
+

Input: s = "aaaaabbc"

+ +

Output: 3

+ +

Explanation:

+ +
    +
  • The character 'a' has an odd frequency of 5, and 'b' has an even frequency of 2.
  • +
  • The maximum difference is 5 - 2 = 3.
  • +
+
+ +

Example 2:

+ +
+

Input: s = "abcabcab"

+ +

Output: 1

+ +

Explanation:

+ +
    +
  • The character 'a' has an odd frequency of 3, and 'c' has an even frequency of 2.
  • +
  • The maximum difference is 3 - 2 = 1.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= s.length <= 100
  • +
  • s consists only of lowercase English letters.
  • +
  • s contains at least one character with an odd frequency and one with an even frequency.
  • +
From c41872f34e7369a7082bf60288374053d2d22b31 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 11 Jun 2025 00:09:32 +0530 Subject: [PATCH 52/91] Time: 0 ms (100.00%) | Memory: 9.8 MB (40.32%) - LeetSync --- ...rence-between-even-and-odd-frequency-i.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 3753-maximum-difference-between-even-and-odd-frequency-i/maximum-difference-between-even-and-odd-frequency-i.cpp diff --git a/3753-maximum-difference-between-even-and-odd-frequency-i/maximum-difference-between-even-and-odd-frequency-i.cpp b/3753-maximum-difference-between-even-and-odd-frequency-i/maximum-difference-between-even-and-odd-frequency-i.cpp new file mode 100644 index 0000000000000..cb787bb1ebec3 --- /dev/null +++ b/3753-maximum-difference-between-even-and-odd-frequency-i/maximum-difference-between-even-and-odd-frequency-i.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxDifference(string s) { + unordered_map freq; + for (char ch : s) { + freq[ch]++; + } + int maxOdd = INT_MIN; + int minEven = INT_MAX; + for (auto& p : freq) { + int f = p.second; + if (f % 2 == 1) { + maxOdd = max(maxOdd, f); + } else { + minEven = min(minEven, f); + } + } + return maxOdd - minEven; + } +}; \ No newline at end of file From 99ea3fd94e96d4ebfaf5f9f2d24ae999e90057aa Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Thu, 12 Jun 2025 23:05:30 +0530 Subject: [PATCH 53/91] Added README.md file for Maximum Difference Between Adjacent Elements in a Circular Array --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3747-maximum-difference-between-adjacent-elements-in-a-circular-array/README.md diff --git a/3747-maximum-difference-between-adjacent-elements-in-a-circular-array/README.md b/3747-maximum-difference-between-adjacent-elements-in-a-circular-array/README.md new file mode 100644 index 0000000000000..0a03d03839c0c --- /dev/null +++ b/3747-maximum-difference-between-adjacent-elements-in-a-circular-array/README.md @@ -0,0 +1,36 @@ +

Maximum Difference Between Adjacent Elements in a Circular Array

Difficulty: Easy

Given a circular array nums, find the maximum absolute difference between adjacent elements.

+ +

Note: In a circular array, the first and last elements are adjacent.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,2,4]

+ +

Output: 3

+ +

Explanation:

+ +

Because nums is circular, nums[0] and nums[2] are adjacent. They have the maximum absolute difference of |4 - 1| = 3.

+
+ +

Example 2:

+ +
+

Input: nums = [-5,-10,-5]

+ +

Output: 5

+ +

Explanation:

+ +

The adjacent elements nums[0] and nums[1] have the maximum absolute difference of |-5 - (-10)| = 5.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 100
  • +
  • -100 <= nums[i] <= 100
  • +
From b0b5a5485d523a9d7302a3b846fb01120a03c367 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Mon, 16 Jun 2025 23:18:27 +0530 Subject: [PATCH 54/91] Added README.md file for Maximum Difference Between Increasing Elements --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2144-maximum-difference-between-increasing-elements/README.md diff --git a/2144-maximum-difference-between-increasing-elements/README.md b/2144-maximum-difference-between-increasing-elements/README.md new file mode 100644 index 0000000000000..783f8740f7073 --- /dev/null +++ b/2144-maximum-difference-between-increasing-elements/README.md @@ -0,0 +1,41 @@ +

Maximum Difference Between Increasing Elements

Difficulty: Easy

Given a 0-indexed integer array nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j].

+ +

Return the maximum difference. If no such i and j exists, return -1.

+ +

 

+

Example 1:

+ +
+Input: nums = [7,1,5,4]
+Output: 4
+Explanation:
+The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
+Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
+
+ +

Example 2:

+ +
+Input: nums = [9,4,3,2]
+Output: -1
+Explanation:
+There is no i and j such that i < j and nums[i] < nums[j].
+
+ +

Example 3:

+ +
+Input: nums = [1,5,2,10]
+Output: 9
+Explanation:
+The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 2 <= n <= 1000
  • +
  • 1 <= nums[i] <= 109
  • +
From e2084fc41ce4b42e99b263a8aba44eb127bb581a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Mon, 16 Jun 2025 23:18:31 +0530 Subject: [PATCH 55/91] Time: 0 ms (100.00%) | Memory: 11.7 MB (92.10%) - LeetSync --- ...imum-difference-between-increasing-elements.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2144-maximum-difference-between-increasing-elements/maximum-difference-between-increasing-elements.cpp diff --git a/2144-maximum-difference-between-increasing-elements/maximum-difference-between-increasing-elements.cpp b/2144-maximum-difference-between-increasing-elements/maximum-difference-between-increasing-elements.cpp new file mode 100644 index 0000000000000..502fe53dd8aed --- /dev/null +++ b/2144-maximum-difference-between-increasing-elements/maximum-difference-between-increasing-elements.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int maximumDifference(vector& nums) { + int n=nums.size(); + int x=nums[0]; + int mini=-1; + for(int i=0;i Date: Tue, 17 Jun 2025 23:44:54 +0530 Subject: [PATCH 56/91] Added README.md file for Find First and Last Position of Element in Sorted Array --- .../README.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 34-find-first-and-last-position-of-element-in-sorted-array/README.md diff --git a/34-find-first-and-last-position-of-element-in-sorted-array/README.md b/34-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 0000000000000..dd7a3ba448a17 --- /dev/null +++ b/34-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,26 @@ +

Find First and Last Position of Element in Sorted Array

Difficulty: Medium

Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

+ +

If target is not found in the array, return [-1, -1].

+ +

You must write an algorithm with O(log n) runtime complexity.

+ +

 

+

Example 1:

+
Input: nums = [5,7,7,8,8,10], target = 8
+Output: [3,4]
+

Example 2:

+
Input: nums = [5,7,7,8,8,10], target = 6
+Output: [-1,-1]
+

Example 3:

+
Input: nums = [], target = 0
+Output: [-1,-1]
+
+

 

+

Constraints:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums is a non-decreasing array.
  • +
  • -109 <= target <= 109
  • +
From 84ef630a4455807d7a8f080ecb1fcd7f5ca92813 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 18 Jun 2025 22:18:50 +0530 Subject: [PATCH 57/91] Added README.md file for Divide Array Into Arrays With Max Difference --- .../README.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 3241-divide-array-into-arrays-with-max-difference/README.md diff --git a/3241-divide-array-into-arrays-with-max-difference/README.md b/3241-divide-array-into-arrays-with-max-difference/README.md new file mode 100644 index 0000000000000..0d81679d96577 --- /dev/null +++ b/3241-divide-array-into-arrays-with-max-difference/README.md @@ -0,0 +1,64 @@ +

Divide Array Into Arrays With Max Difference

Difficulty: Medium

You are given an integer array nums of size n where n is a multiple of 3 and a positive integer k.

+ +

Divide the array nums into n / 3 arrays of size 3 satisfying the following condition:

+ +
    +
  • The difference between any two elements in one array is less than or equal to k.
  • +
+ +

Return a 2D array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,3,4,8,7,9,3,5,1], k = 2

+ +

Output: [[1,1,3],[3,4,5],[7,8,9]]

+ +

Explanation:

+ +

The difference between any two elements in each array is less than or equal to 2.

+
+ +

Example 2:

+ +
+

Input: nums = [2,4,2,2,5,2], k = 2

+ +

Output: []

+ +

Explanation:

+ +

Different ways to divide nums into 2 arrays of size 3 are:

+ +
    +
  • [[2,2,2],[2,4,5]] (and its permutations)
  • +
  • [[2,2,4],[2,2,5]] (and its permutations)
  • +
+ +

Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since 5 - 2 = 3 > k, the condition is not satisfied and so there is no valid division.

+
+ +

Example 3:

+ +
+

Input: nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14

+ +

Output: [[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]

+ +

Explanation:

+ +

The difference between any two elements in each array is less than or equal to 14.

+
+ +

 

+

Constraints:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 105
  • +
  • n is a multiple of 3
  • +
  • 1 <= nums[i] <= 105
  • +
  • 1 <= k <= 105
  • +
From cb59a3440164fa30f963ff2adefacc2d8bda8bf5 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Wed, 18 Jun 2025 22:18:52 +0530 Subject: [PATCH 58/91] Time: 83 ms (19.62%) | Memory: 127.7 MB (16.51%) - LeetSync --- ...de-array-into-arrays-with-max-difference.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 3241-divide-array-into-arrays-with-max-difference/divide-array-into-arrays-with-max-difference.cpp diff --git a/3241-divide-array-into-arrays-with-max-difference/divide-array-into-arrays-with-max-difference.cpp b/3241-divide-array-into-arrays-with-max-difference/divide-array-into-arrays-with-max-difference.cpp new file mode 100644 index 0000000000000..37febe50dd6b2 --- /dev/null +++ b/3241-divide-array-into-arrays-with-max-difference/divide-array-into-arrays-with-max-difference.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector> divideArray(vector& nums, int k) { + int n=nums.size(); + vector>ans; + sort(nums.begin(),nums.end()); + for(int i=0;igroup={nums[i],nums[i+1],nums[i+2]}; + if(group[2]-group[0]>k){ + return {}; + } + ans.push_back(group); + } + return ans; + + } +}; \ No newline at end of file From 008d43ec0167b323d7e9d16da1ecef960dd44a20 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Fri, 20 Jun 2025 00:44:50 +0530 Subject: [PATCH 59/91] Added README.md file for Partition Array Such That Maximum Difference Is K --- .../README.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2387-partition-array-such-that-maximum-difference-is-k/README.md diff --git a/2387-partition-array-such-that-maximum-difference-is-k/README.md b/2387-partition-array-such-that-maximum-difference-is-k/README.md new file mode 100644 index 0000000000000..444146008ec45 --- /dev/null +++ b/2387-partition-array-such-that-maximum-difference-is-k/README.md @@ -0,0 +1,52 @@ +

Partition Array Such That Maximum Difference Is K

Difficulty: Medium

You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.

+ +

Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.

+ +

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

+ +

 

+

Example 1:

+ +
+Input: nums = [3,6,1,2,5], k = 2
+Output: 2
+Explanation:
+We can partition nums into the two subsequences [3,1,2] and [6,5].
+The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
+The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
+Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
+
+ +

Example 2:

+ +
+Input: nums = [1,2,3], k = 1
+Output: 2
+Explanation:
+We can partition nums into the two subsequences [1,2] and [3].
+The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
+The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
+Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
+
+ +

Example 3:

+ +
+Input: nums = [2,2,4,5], k = 0
+Output: 3
+Explanation:
+We can partition nums into the three subsequences [2,2], [4], and [5].
+The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
+The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
+The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
+Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 105
  • +
  • 0 <= k <= 105
  • +
From e3c48891482f90c8327f08c7a024d8b3621c87c3 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 21 Jun 2025 00:01:00 +0530 Subject: [PATCH 60/91] Added README.md file for Maximum Manhattan Distance After K Changes --- .../README.md | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 3754-maximum-manhattan-distance-after-k-changes/README.md diff --git a/3754-maximum-manhattan-distance-after-k-changes/README.md b/3754-maximum-manhattan-distance-after-k-changes/README.md new file mode 100644 index 0000000000000..0fc95b6189b39 --- /dev/null +++ b/3754-maximum-manhattan-distance-after-k-changes/README.md @@ -0,0 +1,87 @@ +

Maximum Manhattan Distance After K Changes

Difficulty: Medium

You are given a string s consisting of the characters 'N', 'S', 'E', and 'W', where s[i] indicates movements in an infinite grid:

+ +
    +
  • 'N' : Move north by 1 unit.
  • +
  • 'S' : Move south by 1 unit.
  • +
  • 'E' : Move east by 1 unit.
  • +
  • 'W' : Move west by 1 unit.
  • +
+ +

Initially, you are at the origin (0, 0). You can change at most k characters to any of the four directions.

+ +

Find the maximum Manhattan distance from the origin that can be achieved at any time while performing the movements in order.

+The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|. +

 

+

Example 1:

+ +
+

Input: s = "NWSE", k = 1

+ +

Output: 3

+ +

Explanation:

+ +

Change s[2] from 'S' to 'N'. The string s becomes "NWNE".

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MovementPosition (x, y)Manhattan DistanceMaximum
s[0] == 'N'(0, 1)0 + 1 = 11
s[1] == 'W'(-1, 1)1 + 1 = 22
s[2] == 'N'(-1, 2)1 + 2 = 33
s[3] == 'E'(0, 2)0 + 2 = 23
+ +

The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.

+
+ +

Example 2:

+ +
+

Input: s = "NSWWEW", k = 3

+ +

Output: 6

+ +

Explanation:

+ +

Change s[1] from 'S' to 'N', and s[4] from 'E' to 'W'. The string s becomes "NNWWWW".

+ +

The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 105
  • +
  • 0 <= k <= s.length
  • +
  • s consists of only 'N', 'S', 'E', and 'W'.
  • +
From b873c3d374ec11761c1bdb06b431fe44011b19d9 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 21 Jun 2025 00:01:12 +0530 Subject: [PATCH 61/91] Time: 52 ms (90.30%) | Memory: 38 MB (96.06%) - LeetSync --- ...mum-manhattan-distance-after-k-changes.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 3754-maximum-manhattan-distance-after-k-changes/maximum-manhattan-distance-after-k-changes.cpp diff --git a/3754-maximum-manhattan-distance-after-k-changes/maximum-manhattan-distance-after-k-changes.cpp b/3754-maximum-manhattan-distance-after-k-changes/maximum-manhattan-distance-after-k-changes.cpp new file mode 100644 index 0000000000000..d73c3571342b5 --- /dev/null +++ b/3754-maximum-manhattan-distance-after-k-changes/maximum-manhattan-distance-after-k-changes.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxDistance(string s, int k) { + int ans = 0; + int north=0,south=0,east=0,west=0; + for (int i=0;i Date: Sun, 22 Jun 2025 19:41:19 +0530 Subject: [PATCH 62/91] Added README.md file for Divide a String Into Groups of Size k --- .../README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2260-divide-a-string-into-groups-of-size-k/README.md diff --git a/2260-divide-a-string-into-groups-of-size-k/README.md b/2260-divide-a-string-into-groups-of-size-k/README.md new file mode 100644 index 0000000000000..8c15015bce8c2 --- /dev/null +++ b/2260-divide-a-string-into-groups-of-size-k/README.md @@ -0,0 +1,45 @@ +

Divide a String Into Groups of Size k

Difficulty: Easy

A string s can be partitioned into groups of size k using the following procedure:

+ +
    +
  • The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each element can be a part of exactly one group.
  • +
  • For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
  • +
+ +

Note that the partition is done so that after removing the fill character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be s.

+ +

Given the string s, the size of each group k and the character fill, return a string array denoting the composition of every group s has been divided into, using the above procedure.

+ +

 

+

Example 1:

+ +
+Input: s = "abcdefghi", k = 3, fill = "x"
+Output: ["abc","def","ghi"]
+Explanation:
+The first 3 characters "abc" form the first group.
+The next 3 characters "def" form the second group.
+The last 3 characters "ghi" form the third group.
+Since all groups can be completely filled by characters from the string, we do not need to use fill.
+Thus, the groups formed are "abc", "def", and "ghi".
+
+ +

Example 2:

+ +
+Input: s = "abcdefghij", k = 3, fill = "x"
+Output: ["abc","def","ghi","jxx"]
+Explanation:
+Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
+For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
+Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 100
  • +
  • s consists of lowercase English letters only.
  • +
  • 1 <= k <= 100
  • +
  • fill is a lowercase English letter.
  • +
From 47fdd7105a0fdbf7c7e4dc630c39410b1dbc3134 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 22 Jun 2025 19:41:21 +0530 Subject: [PATCH 63/91] Time: 0 ms (100.00%) | Memory: 10.1 MB (8.58%) - LeetSync --- .../divide-a-string-into-groups-of-size-k.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 2260-divide-a-string-into-groups-of-size-k/divide-a-string-into-groups-of-size-k.cpp diff --git a/2260-divide-a-string-into-groups-of-size-k/divide-a-string-into-groups-of-size-k.cpp b/2260-divide-a-string-into-groups-of-size-k/divide-a-string-into-groups-of-size-k.cpp new file mode 100644 index 0000000000000..15951e8ae2155 --- /dev/null +++ b/2260-divide-a-string-into-groups-of-size-k/divide-a-string-into-groups-of-size-k.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector divideString(string s, int k, char fill) { + vectorres; + for(int i=0;i Date: Sun, 22 Jun 2025 22:44:44 +0530 Subject: [PATCH 64/91] Added README.md file for Divide a String Into Groups of Size k From 36ec6becc001628f807d7c6a01b859b426de7f1d Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 22 Jun 2025 22:44:50 +0530 Subject: [PATCH 65/91] Time: 0 ms (100.00%) | Memory: 10.1 MB (8.58%) - LeetSync From 32653516bee189ec1553143a7047b73df101bb07 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:25:16 -0500 Subject: [PATCH 66/91] Added README.md file for Maximum Depth of Binary Tree --- 104-maximum-depth-of-binary-tree/README.md | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 104-maximum-depth-of-binary-tree/README.md diff --git a/104-maximum-depth-of-binary-tree/README.md b/104-maximum-depth-of-binary-tree/README.md new file mode 100644 index 0000000000000..ad72023cd100a --- /dev/null +++ b/104-maximum-depth-of-binary-tree/README.md @@ -0,0 +1,26 @@ +

Maximum Depth of Binary Tree

Difficulty: Easy

Given the root of a binary tree, return its maximum depth.

+ +

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

+ +

 

+

Example 1:

+ +
+Input: root = [3,9,20,null,null,15,7]
+Output: 3
+
+ +

Example 2:

+ +
+Input: root = [1,null,2]
+Output: 2
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 104].
  • +
  • -100 <= Node.val <= 100
  • +
From e2c83523f55059a3d34d68f85aa10ee70dae462c Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:25:17 -0500 Subject: [PATCH 67/91] Time: 0 ms (100.00%) | Memory: 19 MB (45.95%) - LeetSync --- .../maximum-depth-of-binary-tree.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 104-maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.cpp diff --git a/104-maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.cpp b/104-maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.cpp new file mode 100644 index 0000000000000..8db7e633904d1 --- /dev/null +++ b/104-maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.cpp @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int maxDepth(TreeNode* root) { + if(root==nullptr){ + return 0; + } + int le=1+maxDepth(root->left); + int ri=1+maxDepth(root->right); + return max(le,ri); + } +}; \ No newline at end of file From 4961c745bc4b1b9e157c5c665b9e55f996d8d5b0 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:34:21 -0500 Subject: [PATCH 68/91] Added README.md file for Kth Smallest Element in a BST --- 230-kth-smallest-element-in-a-bst/README.md | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 230-kth-smallest-element-in-a-bst/README.md diff --git a/230-kth-smallest-element-in-a-bst/README.md b/230-kth-smallest-element-in-a-bst/README.md new file mode 100644 index 0000000000000..5a1d28c580776 --- /dev/null +++ b/230-kth-smallest-element-in-a-bst/README.md @@ -0,0 +1,28 @@ +

Kth Smallest Element in a BST

Difficulty: Medium

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

+ +

 

+

Example 1:

+ +
+Input: root = [3,1,4,null,2], k = 1
+Output: 1
+
+ +

Example 2:

+ +
+Input: root = [5,3,6,2,4,null,null,1], k = 3
+Output: 3
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is n.
  • +
  • 1 <= k <= n <= 104
  • +
  • 0 <= Node.val <= 104
  • +
+ +

 

+

Follow up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?

From 25c7d940b544d4ec7a27da4f46f3832092970c30 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:34:24 -0500 Subject: [PATCH 69/91] Time: 0 ms (100.00%) | Memory: 24.5 MB (41.38%) - LeetSync --- .../kth-smallest-element-in-a-bst.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 230-kth-smallest-element-in-a-bst/kth-smallest-element-in-a-bst.cpp diff --git a/230-kth-smallest-element-in-a-bst/kth-smallest-element-in-a-bst.cpp b/230-kth-smallest-element-in-a-bst/kth-smallest-element-in-a-bst.cpp new file mode 100644 index 0000000000000..26854a06ee854 --- /dev/null +++ b/230-kth-smallest-element-in-a-bst/kth-smallest-element-in-a-bst.cpp @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int count=0; + int res=0; + void inorder(TreeNode* root, int k){ + if(!root) return; + inorder(root->left,k); + count++; + if(count==k){ + res=root->val; + return; + } + inorder(root->right,k); + } + + int kthSmallest(TreeNode* root, int k) { + inorder(root,k); + return res; + } +}; \ No newline at end of file From f703b78459b73d6fa0e96a51594800956f2d4e75 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:39:09 -0500 Subject: [PATCH 70/91] Added README.md file for Kth Smallest Element in a BST From 086676074d3ee08c1547222bc2cc087f68e200a5 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 01:39:11 -0500 Subject: [PATCH 71/91] Time: 0 ms (100.00%) | Memory: 24.2 MB (99.14%) - LeetSync From 439d4dc38ae9383fa5f54de870bbc6b7c9d6d2ef Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 02:35:32 -0500 Subject: [PATCH 72/91] Added README.md file for Balanced Binary Tree --- 110-balanced-binary-tree/README.md | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 110-balanced-binary-tree/README.md diff --git a/110-balanced-binary-tree/README.md b/110-balanced-binary-tree/README.md new file mode 100644 index 0000000000000..3e689bfce0357 --- /dev/null +++ b/110-balanced-binary-tree/README.md @@ -0,0 +1,31 @@ +

Balanced Binary Tree

Difficulty: Easy

Given a binary tree, determine if it is height-balanced.

+ +

 

+

Example 1:

+ +
+Input: root = [3,9,20,null,null,15,7]
+Output: true
+
+ +

Example 2:

+ +
+Input: root = [1,2,2,3,3,null,null,4,4]
+Output: false
+
+ +

Example 3:

+ +
+Input: root = []
+Output: true
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 5000].
  • +
  • -104 <= Node.val <= 104
  • +
From 84d4271c768a2938c1a585dc9596f227a0ed7310 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 02:35:33 -0500 Subject: [PATCH 73/91] Time: 0 ms (100.00%) | Memory: 23 MB (56.64%) - LeetSync --- .../balanced-binary-tree.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 110-balanced-binary-tree/balanced-binary-tree.cpp diff --git a/110-balanced-binary-tree/balanced-binary-tree.cpp b/110-balanced-binary-tree/balanced-binary-tree.cpp new file mode 100644 index 0000000000000..d74010abda10b --- /dev/null +++ b/110-balanced-binary-tree/balanced-binary-tree.cpp @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool isBalanced(TreeNode* root) { + return (checkheight(root)!=-1); + } + int checkheight(TreeNode* root){ + if(root==nullptr) return 0; + int le=checkheight(root->left); + if(le==-1) return -1; + int re=checkheight(root->right); + if(re==-1) return -1; + if(abs(le-re)>1) return -1; + return 1+max(le,re); + } +}; \ No newline at end of file From a1bda040837b26b8fafddcbfe02a9ab51f55050a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 02:50:10 -0500 Subject: [PATCH 74/91] Added README.md file for Convert Sorted Array to Binary Search Tree --- .../README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 108-convert-sorted-array-to-binary-search-tree/README.md diff --git a/108-convert-sorted-array-to-binary-search-tree/README.md b/108-convert-sorted-array-to-binary-search-tree/README.md new file mode 100644 index 0000000000000..b4558231b29e2 --- /dev/null +++ b/108-convert-sorted-array-to-binary-search-tree/README.md @@ -0,0 +1,28 @@ +

Convert Sorted Array to Binary Search Tree

Difficulty: Easy

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

+ +

 

+

Example 1:

+ +
+Input: nums = [-10,-3,0,5,9]
+Output: [0,-3,9,-10,null,5]
+Explanation: [0,-10,5,null,-3,null,9] is also accepted:
+
+
+ +

Example 2:

+ +
+Input: nums = [1,3]
+Output: [3,1]
+Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums is sorted in a strictly increasing order.
  • +
From f8c60a08b7b0e762f067871aab2ed6df9a27d70a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 02:50:12 -0500 Subject: [PATCH 75/91] Time: 0 ms (100.00%) | Memory: 23.2 MB (11.38%) - LeetSync --- ...ert-sorted-array-to-binary-search-tree.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 108-convert-sorted-array-to-binary-search-tree/convert-sorted-array-to-binary-search-tree.cpp diff --git a/108-convert-sorted-array-to-binary-search-tree/convert-sorted-array-to-binary-search-tree.cpp b/108-convert-sorted-array-to-binary-search-tree/convert-sorted-array-to-binary-search-tree.cpp new file mode 100644 index 0000000000000..6879e883c39fa --- /dev/null +++ b/108-convert-sorted-array-to-binary-search-tree/convert-sorted-array-to-binary-search-tree.cpp @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* sortedArrayToBST(vector& nums) { + return helper(nums,0,nums.size()-1); + } + TreeNode* helper(vector&nums,int start,int end){ + if(start>end) return nullptr; + int mid=(start+end)/2; + TreeNode* root=new TreeNode(nums[mid]); + root->left=helper(nums,start,mid-1); + root->right=helper(nums,mid+1,end); + return root; + } +}; \ No newline at end of file From 560cde7d8cf14e6a509f94182cc8ae5addd6367c Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 03:24:57 -0500 Subject: [PATCH 76/91] Added README.md file for Binary Tree Maximum Path Sum --- 124-binary-tree-maximum-path-sum/README.md | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 124-binary-tree-maximum-path-sum/README.md diff --git a/124-binary-tree-maximum-path-sum/README.md b/124-binary-tree-maximum-path-sum/README.md new file mode 100644 index 0000000000000..1070f6f0ce11a --- /dev/null +++ b/124-binary-tree-maximum-path-sum/README.md @@ -0,0 +1,30 @@ +

Binary Tree Maximum Path Sum

Difficulty: Hard

A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.

+ +

The path sum of a path is the sum of the node's values in the path.

+ +

Given the root of a binary tree, return the maximum path sum of any non-empty path.

+ +

 

+

Example 1:

+ +
+Input: root = [1,2,3]
+Output: 6
+Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
+
+ +

Example 2:

+ +
+Input: root = [-10,9,20,null,null,15,7]
+Output: 42
+Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 3 * 104].
  • +
  • -1000 <= Node.val <= 1000
  • +
From a52376b93b426eca1e527dd2768ecb9378fad85a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 03:24:58 -0500 Subject: [PATCH 77/91] Time: 0 ms (100.00%) | Memory: 28.1 MB (23.42%) - LeetSync --- .../binary-tree-maximum-path-sum.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 124-binary-tree-maximum-path-sum/binary-tree-maximum-path-sum.cpp diff --git a/124-binary-tree-maximum-path-sum/binary-tree-maximum-path-sum.cpp b/124-binary-tree-maximum-path-sum/binary-tree-maximum-path-sum.cpp new file mode 100644 index 0000000000000..76e6835ae1ed5 --- /dev/null +++ b/124-binary-tree-maximum-path-sum/binary-tree-maximum-path-sum.cpp @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int ans=INT_MIN; + int maxPathSum(TreeNode* root) { + helper(root); + return ans; + } + int helper(TreeNode* root){ + if(root==nullptr) return 0; + int left=max(helper(root->left),0); + int right=max(helper(root->right),0); + int currsum=root->val+left+right; + ans=max(ans,currsum); + return root->val+max(left,right); + } +}; \ No newline at end of file From 1487e007297f19d2e8c9f950c5441dc0a8732f07 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 03:36:47 -0500 Subject: [PATCH 78/91] Added README.md file for Binary Tree Inorder Traversal --- 94-binary-tree-inorder-traversal/README.md | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 94-binary-tree-inorder-traversal/README.md diff --git a/94-binary-tree-inorder-traversal/README.md b/94-binary-tree-inorder-traversal/README.md new file mode 100644 index 0000000000000..6accab2dbe06a --- /dev/null +++ b/94-binary-tree-inorder-traversal/README.md @@ -0,0 +1,53 @@ +

Binary Tree Inorder Traversal

Difficulty: Easy

Given the root of a binary tree, return the inorder traversal of its nodes' values.

+ +

 

+

Example 1:

+ +
+

Input: root = [1,null,2,3]

+ +

Output: [1,3,2]

+ +

Explanation:

+ +

+
+ +

Example 2:

+ +
+

Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]

+ +

Output: [4,2,6,5,7,1,3,9,8]

+ +

Explanation:

+ +

+
+ +

Example 3:

+ +
+

Input: root = []

+ +

Output: []

+
+ +

Example 4:

+ +
+

Input: root = [1]

+ +

Output: [1]

+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [0, 100].
  • +
  • -100 <= Node.val <= 100
  • +
+ +

 

+Follow up: Recursive solution is trivial, could you do it iteratively? \ No newline at end of file From eded8e5b13af2cb6bd186d2b17e4c08891f0c12c Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sat, 26 Jul 2025 03:36:49 -0500 Subject: [PATCH 79/91] Time: 4 ms (0.57%) | Memory: 12.6 MB (5.58%) - LeetSync --- .../binary-tree-inorder-traversal.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 94-binary-tree-inorder-traversal/binary-tree-inorder-traversal.cpp diff --git a/94-binary-tree-inorder-traversal/binary-tree-inorder-traversal.cpp b/94-binary-tree-inorder-traversal/binary-tree-inorder-traversal.cpp new file mode 100644 index 0000000000000..358258da60415 --- /dev/null +++ b/94-binary-tree-inorder-traversal/binary-tree-inorder-traversal.cpp @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + vectorans; + vector inorderTraversal(TreeNode* root) { + if(!root) return {}; + inorderTraversal(root->left); + ans.push_back(root->val); + inorderTraversal(root->right); + return ans; + } +}; \ No newline at end of file From d056c5f927090b22abb48ab247cd8d3134fa8ec8 Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 27 Jul 2025 23:44:54 +0530 Subject: [PATCH 80/91] Added README.md file for Count Hills and Valleys in an Array --- .../README.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2316-count-hills-and-valleys-in-an-array/README.md diff --git a/2316-count-hills-and-valleys-in-an-array/README.md b/2316-count-hills-and-valleys-in-an-array/README.md new file mode 100644 index 0000000000000..37f26609bda18 --- /dev/null +++ b/2316-count-hills-and-valleys-in-an-array/README.md @@ -0,0 +1,44 @@ +

Count Hills and Valleys in an Array

Difficulty: Easy

You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j].

+ +

Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.

+ +

Return the number of hills and valleys in nums.

+ +

 

+

Example 1:

+ +
+Input: nums = [2,4,1,1,6,5]
+Output: 3
+Explanation:
+At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
+At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill. 
+At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
+At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
+At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
+At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. 
+There are 3 hills and valleys so we return 3.
+
+ +

Example 2:

+ +
+Input: nums = [6,6,5,5,4,1]
+Output: 0
+Explanation:
+At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
+At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
+At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
+At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
+At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
+At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
+There are 0 hills and valleys so we return 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 3 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
From 2c35f5426d330076969c534e132d9edc63794f3a Mon Sep 17 00:00:00 2001 From: Yuktha mukhi <164024317+yuktham357@users.noreply.github.com> Date: Sun, 27 Jul 2025 23:44:56 +0530 Subject: [PATCH 81/91] Time: 0 ms (100.00%) | Memory: 12.2 MB (19.85%) - LeetSync --- .../count-hills-and-valleys-in-an-array.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2316-count-hills-and-valleys-in-an-array/count-hills-and-valleys-in-an-array.cpp diff --git a/2316-count-hills-and-valleys-in-an-array/count-hills-and-valleys-in-an-array.cpp b/2316-count-hills-and-valleys-in-an-array/count-hills-and-valleys-in-an-array.cpp new file mode 100644 index 0000000000000..f70ff00a81520 --- /dev/null +++ b/2316-count-hills-and-valleys-in-an-array/count-hills-and-valleys-in-an-array.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int countHillValley(vector& nums) { + int n=nums.size(); + int le=0; + int ans=0; + for(int i=1;inums[le] && nums[i]>nums[i + 1]) || + (nums[i] Date: Mon, 28 Jul 2025 06:39:31 +0530 Subject: [PATCH 82/91] Added README.md file for Count Number of Maximum Bitwise-OR Subsets --- .../README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2170-count-number-of-maximum-bitwise-or-subsets/README.md diff --git a/2170-count-number-of-maximum-bitwise-or-subsets/README.md b/2170-count-number-of-maximum-bitwise-or-subsets/README.md new file mode 100644 index 0000000000000..065f2a6f68a8e --- /dev/null +++ b/2170-count-number-of-maximum-bitwise-or-subsets/README.md @@ -0,0 +1,45 @@ +

Count Number of Maximum Bitwise-OR Subsets

Difficulty: Medium

Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.

+ +

An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.

+ +

The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).

+ +

 

+

Example 1:

+ +
+Input: nums = [3,1]
+Output: 2
+Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
+- [3]
+- [3,1]
+
+ +

Example 2:

+ +
+Input: nums = [2,2,2]
+Output: 7
+Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 23 - 1 = 7 total subsets.
+
+ +

Example 3:

+ +
+Input: nums = [3,2,1,5]
+Output: 6
+Explanation: The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
+- [3,5]
+- [3,1,5]
+- [3,2,5]
+- [3,2,1,5]
+- [2,5]
+- [2,1,5]
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 16
  • +
  • 1 <= nums[i] <= 105
  • +
From d98cd875c7817139d8bb9758cad7437dc4e60046 Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Mon, 28 Jul 2025 06:39:33 +0530 Subject: [PATCH 83/91] Time: 10 ms (70.29%) | Memory: 10.7 MB (43.96%) - LeetSync --- ...t-number-of-maximum-bitwise-or-subsets.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2170-count-number-of-maximum-bitwise-or-subsets/count-number-of-maximum-bitwise-or-subsets.cpp diff --git a/2170-count-number-of-maximum-bitwise-or-subsets/count-number-of-maximum-bitwise-or-subsets.cpp b/2170-count-number-of-maximum-bitwise-or-subsets/count-number-of-maximum-bitwise-or-subsets.cpp new file mode 100644 index 0000000000000..265c17ecff95b --- /dev/null +++ b/2170-count-number-of-maximum-bitwise-or-subsets/count-number-of-maximum-bitwise-or-subsets.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int n; + int helper(vector&nums, int maxi, int curr,int i){ + if(i>=n){ + if(maxi==curr) return 1; + return 0; + } + int t=helper(nums,maxi,nums[i]|curr,i+1); + int s=helper(nums,maxi,curr,i+1); + return t+s; + } + + int countMaxOrSubsets(vector& nums) { + n=nums.size(); + int maxi=0; + for(int i=0;i Date: Mon, 28 Jul 2025 15:21:17 +0530 Subject: [PATCH 84/91] Added README.md file for Largest Combination With Bitwise AND Greater Than Zero --- .../README.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2356-largest-combination-with-bitwise-and-greater-than-zero/README.md diff --git a/2356-largest-combination-with-bitwise-and-greater-than-zero/README.md b/2356-largest-combination-with-bitwise-and-greater-than-zero/README.md new file mode 100644 index 0000000000000..d7c943736d27b --- /dev/null +++ b/2356-largest-combination-with-bitwise-and-greater-than-zero/README.md @@ -0,0 +1,40 @@ +

Largest Combination With Bitwise AND Greater Than Zero

Difficulty: Medium

The bitwise AND of an array nums is the bitwise AND of all integers in nums.

+ +
    +
  • For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
  • +
  • Also, for nums = [7], the bitwise AND is 7.
  • +
+ +

You are given an array of positive integers candidates. Compute the bitwise AND for all possible combinations of elements in the candidates array.

+ +

Return the size of the largest combination of candidates with a bitwise AND greater than 0.

+ +

 

+

Example 1:

+ +
+Input: candidates = [16,17,71,62,12,24,14]
+Output: 4
+Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
+The size of the combination is 4.
+It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
+Note that more than one combination may have the largest size.
+For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
+
+ +

Example 2:

+ +
+Input: candidates = [8,8]
+Output: 2
+Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
+The size of the combination is 2, so we return 2.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= candidates.length <= 105
  • +
  • 1 <= candidates[i] <= 107
  • +
From 4d64e33e6ff62f2e4d602d3d99bd5b299bf8c52b Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:21:20 +0530 Subject: [PATCH 85/91] Time: 39 ms (32.93%) | Memory: 61.2 MB (96.59%) - LeetSync --- ...ation-with-bitwise-and-greater-than-zero.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2356-largest-combination-with-bitwise-and-greater-than-zero/largest-combination-with-bitwise-and-greater-than-zero.cpp diff --git a/2356-largest-combination-with-bitwise-and-greater-than-zero/largest-combination-with-bitwise-and-greater-than-zero.cpp b/2356-largest-combination-with-bitwise-and-greater-than-zero/largest-combination-with-bitwise-and-greater-than-zero.cpp new file mode 100644 index 0000000000000..22c58080a0cce --- /dev/null +++ b/2356-largest-combination-with-bitwise-and-greater-than-zero/largest-combination-with-bitwise-and-greater-than-zero.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int largestCombination(vector& candidates) { + int maxi = 0; + for (int i = 0; i < 32; i++) { + int count = 0; + for (int num : candidates) { + if (num & (1 << i)) { + count++; + } + } + maxi =max(maxi, count); + } + return maxi; + } + +}; From 8b3b240405fe9e82f09dfc819ab04b0b2c0250df Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Fri, 1 Aug 2025 09:46:46 +0530 Subject: [PATCH 86/91] Added README.md file for Pascal's Triangle --- 118-pascals-triangle/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 118-pascals-triangle/README.md diff --git a/118-pascals-triangle/README.md b/118-pascals-triangle/README.md new file mode 100644 index 0000000000000..dca1d4b2da87f --- /dev/null +++ b/118-pascals-triangle/README.md @@ -0,0 +1,18 @@ +

Pascal's Triangle

Difficulty: Easy

Given an integer numRows, return the first numRows of Pascal's triangle.

+ +

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

+ +

 

+

Example 1:

+
Input: numRows = 5
+Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
+

Example 2:

+
Input: numRows = 1
+Output: [[1]]
+
+

 

+

Constraints:

+ +
    +
  • 1 <= numRows <= 30
  • +
From 5f59bb56cc96da63500ddf45c6b3607d49fbff33 Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Fri, 1 Aug 2025 09:46:47 +0530 Subject: [PATCH 87/91] Time: 0 ms (100.00%) | Memory: 9.7 MB (62.59%) - LeetSync --- 118-pascals-triangle/pascals-triangle.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 118-pascals-triangle/pascals-triangle.cpp diff --git a/118-pascals-triangle/pascals-triangle.cpp b/118-pascals-triangle/pascals-triangle.cpp new file mode 100644 index 0000000000000..ee431151b7d88 --- /dev/null +++ b/118-pascals-triangle/pascals-triangle.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> generate(int numRows) { + vector>ans; + int rows=1; + for(int i=1;i<=numRows;i++){ + vectorrow; + int col=1; + row.push_back(1); + for(int j=1;j Date: Fri, 1 Aug 2025 12:36:41 +0530 Subject: [PATCH 88/91] Added README.md file for Sliding Window Maximum --- 239-sliding-window-maximum/README.md | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 239-sliding-window-maximum/README.md diff --git a/239-sliding-window-maximum/README.md b/239-sliding-window-maximum/README.md new file mode 100644 index 0000000000000..4288bdeaadb2e --- /dev/null +++ b/239-sliding-window-maximum/README.md @@ -0,0 +1,36 @@ +

Sliding Window Maximum

Difficulty: Hard

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

+ +

Return the max sliding window.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
+Output: [3,3,5,5,6,7]
+Explanation: 
+Window position                Max
+---------------               -----
+[1  3  -1] -3  5  3  6  7       3
+ 1 [3  -1  -3] 5  3  6  7       3
+ 1  3 [-1  -3  5] 3  6  7       5
+ 1  3  -1 [-3  5  3] 6  7       5
+ 1  3  -1  -3 [5  3  6] 7       6
+ 1  3  -1  -3  5 [3  6  7]      7
+
+ +

Example 2:

+ +
+Input: nums = [1], k = 1
+Output: [1]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
  • 1 <= k <= nums.length
  • +
From dae57176826896232b567653cc29c79480a40337 Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Fri, 1 Aug 2025 12:36:42 +0530 Subject: [PATCH 89/91] Time: 11 ms (97.31%) | Memory: 139.2 MB (60.17%) - LeetSync --- .../sliding-window-maximum.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 239-sliding-window-maximum/sliding-window-maximum.cpp diff --git a/239-sliding-window-maximum/sliding-window-maximum.cpp b/239-sliding-window-maximum/sliding-window-maximum.cpp new file mode 100644 index 0000000000000..6bfe2e7f8ec83 --- /dev/null +++ b/239-sliding-window-maximum/sliding-window-maximum.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + vector maxSlidingWindow(vector& nums, int k) { + vectorans; + dequedp; + for(int i=0;i=0) ans.push_back(nums[dp.front()]); + } + return ans; + } +}; \ No newline at end of file From b84d502a187b40f0debdae6569a531aa830feb2e Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Tue, 5 Aug 2025 21:18:01 +0530 Subject: [PATCH 90/91] Added README.md file for Fruits Into Baskets II --- 3790-fruits-into-baskets-ii/README.md | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 3790-fruits-into-baskets-ii/README.md diff --git a/3790-fruits-into-baskets-ii/README.md b/3790-fruits-into-baskets-ii/README.md new file mode 100644 index 0000000000000..e7a28aa1e272d --- /dev/null +++ b/3790-fruits-into-baskets-ii/README.md @@ -0,0 +1,57 @@ +

Fruits Into Baskets II

Difficulty: Easy

You are given two arrays of integers, fruits and baskets, each of length n, where fruits[i] represents the quantity of the ith type of fruit, and baskets[j] represents the capacity of the jth basket.

+ +

From left to right, place the fruits according to these rules:

+ +
    +
  • Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
  • +
  • Each basket can hold only one type of fruit.
  • +
  • If a fruit type cannot be placed in any basket, it remains unplaced.
  • +
+ +

Return the number of fruit types that remain unplaced after all possible allocations are made.

+ +

 

+

Example 1:

+ +
+

Input: fruits = [4,2,5], baskets = [3,5,4]

+ +

Output: 1

+ +

Explanation:

+ +
    +
  • fruits[0] = 4 is placed in baskets[1] = 5.
  • +
  • fruits[1] = 2 is placed in baskets[0] = 3.
  • +
  • fruits[2] = 5 cannot be placed in baskets[2] = 4.
  • +
+ +

Since one fruit type remains unplaced, we return 1.

+
+ +

Example 2:

+ +
+

Input: fruits = [3,6,1], baskets = [6,4,7]

+ +

Output: 0

+ +

Explanation:

+ +
    +
  • fruits[0] = 3 is placed in baskets[0] = 6.
  • +
  • fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.
  • +
  • fruits[2] = 1 is placed in baskets[1] = 4.
  • +
+ +

Since all fruits are successfully placed, we return 0.

+
+ +

 

+

Constraints:

+ +
    +
  • n == fruits.length == baskets.length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= fruits[i], baskets[i] <= 1000
  • +
From 5380f076fb4292e0984b56e2a2fc9857e7427661 Mon Sep 17 00:00:00 2001 From: Yukthamukhi Siddhoju <164024317+yuktham357@users.noreply.github.com> Date: Tue, 5 Aug 2025 21:18:03 +0530 Subject: [PATCH 91/91] Time: 1 ms (62.18%) | Memory: 49 MB (49.69%) - LeetSync --- .../fruits-into-baskets-ii.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 3790-fruits-into-baskets-ii/fruits-into-baskets-ii.cpp diff --git a/3790-fruits-into-baskets-ii/fruits-into-baskets-ii.cpp b/3790-fruits-into-baskets-ii/fruits-into-baskets-ii.cpp new file mode 100644 index 0000000000000..5d2fded140c73 --- /dev/null +++ b/3790-fruits-into-baskets-ii/fruits-into-baskets-ii.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int numOfUnplacedFruits(vector& fruits, vector& baskets) { + int n=fruits.size(); + int ans=n; + for(int i=0;i