From 1f176e7507a995821fa87ed99ebc1ed0a366f22e Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 11:33:46 +0530 Subject: [PATCH 01/82] Added README.md file for Keep Multiplying Found Values by Two --- .../README.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2274-keep-multiplying-found-values-by-two/README.md diff --git a/2274-keep-multiplying-found-values-by-two/README.md b/2274-keep-multiplying-found-values-by-two/README.md new file mode 100644 index 0000000000000..db54e8e5f409a --- /dev/null +++ b/2274-keep-multiplying-found-values-by-two/README.md @@ -0,0 +1,41 @@ +

Keep Multiplying Found Values by Two

Difficulty: Easy

You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums.

+ +

You then do the following steps:

+ +
    +
  1. If original is found in nums, multiply it by two (i.e., set original = 2 * original).
  2. +
  3. Otherwise, stop the process.
  4. +
  5. Repeat this process with the new number as long as you keep finding the number.
  6. +
+ +

Return the final value of original.

+ +

 

+

Example 1:

+ +
+Input: nums = [5,3,6,1,12], original = 3
+Output: 24
+Explanation: 
+- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
+- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
+- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
+- 24 is not found in nums. Thus, 24 is returned.
+
+ +

Example 2:

+ +
+Input: nums = [2,7,9], original = 4
+Output: 4
+Explanation:
+- 4 is not found in nums. Thus, 4 is returned.
+
+ +

 

+

Constraints:

+ + From 08ab33ae66ad66634b79bbea08085dafc139215f Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 11:33:48 +0530 Subject: [PATCH 02/82] Time: 0 ms (100.00%) | Memory: 14 MB (68.68%) - LeetSync --- .../keep-multiplying-found-values-by-two.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 2274-keep-multiplying-found-values-by-two/keep-multiplying-found-values-by-two.cpp diff --git a/2274-keep-multiplying-found-values-by-two/keep-multiplying-found-values-by-two.cpp b/2274-keep-multiplying-found-values-by-two/keep-multiplying-found-values-by-two.cpp new file mode 100644 index 0000000000000..0c03e408a86c8 --- /dev/null +++ b/2274-keep-multiplying-found-values-by-two/keep-multiplying-found-values-by-two.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int findFinalValue(vector& nums, int original) { + while(find (nums.begin(),nums.end(),original)!=nums.end()){ + original *=2; + } + return original; + } +}; \ No newline at end of file From d281d859c47e6255905744b0a50045d1d07d6d1d Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 11:39:07 +0530 Subject: [PATCH 03/82] Added README.md file for Keep Multiplying Found Values by Two From 27e5f3c5ef23c4605443d48c4a918f76d65511e5 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 11:39:09 +0530 Subject: [PATCH 04/82] Time: 0 ms (100.00%) | Memory: 13.9 MB (90.19%) - LeetSync From 71db4660d38f7f4c5003e0f9db5d4ff1d838c5c7 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 12:54:57 +0530 Subject: [PATCH 05/82] Added README.md file for Count Equal and Divisible Pairs in an Array --- .../README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2277-count-equal-and-divisible-pairs-in-an-array/README.md diff --git a/2277-count-equal-and-divisible-pairs-in-an-array/README.md b/2277-count-equal-and-divisible-pairs-in-an-array/README.md new file mode 100644 index 0000000000000..5954839982347 --- /dev/null +++ b/2277-count-equal-and-divisible-pairs-in-an-array/README.md @@ -0,0 +1,30 @@ +

Count Equal and Divisible Pairs in an Array

Difficulty: Easy
Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k. +

 

+

Example 1:

+ +
+Input: nums = [3,1,2,2,2,1,3], k = 2
+Output: 4
+Explanation:
+There are 4 pairs that meet all the requirements:
+- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
+- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
+- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
+- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
+
+ +

Example 2:

+ +
+Input: nums = [1,2,3,4], k = 1
+Output: 0
+Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 1 <= nums[i], k <= 100
  • +
From 0eadf165bfd3ccc94983fb5d57bb086ae9ec6df3 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 12:54:59 +0530 Subject: [PATCH 06/82] Time: 0 ms (100.00%) | Memory: 15.4 MB (69.34%) - LeetSync --- .../count-equal-and-divisible-pairs-in-an-array.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 2277-count-equal-and-divisible-pairs-in-an-array/count-equal-and-divisible-pairs-in-an-array.cpp diff --git a/2277-count-equal-and-divisible-pairs-in-an-array/count-equal-and-divisible-pairs-in-an-array.cpp b/2277-count-equal-and-divisible-pairs-in-an-array/count-equal-and-divisible-pairs-in-an-array.cpp new file mode 100644 index 0000000000000..48f2b34d7e0ad --- /dev/null +++ b/2277-count-equal-and-divisible-pairs-in-an-array/count-equal-and-divisible-pairs-in-an-array.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int countPairs(vector& nums, int k) { + int res = 0, n = nums.size(); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (nums[i] == nums[j] && (i * j) % k == 0) res++; + } + } + return res; + } +}; From 69259ac62c3c4f407144dcb532c17ed413215ee3 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 12:58:33 +0530 Subject: [PATCH 07/82] Added README.md file for Count Equal and Divisible Pairs in an Array From 325a439bf7e29a226f0425e4e3456a4e8d2d09df Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 12:58:34 +0530 Subject: [PATCH 08/82] Time: 0 ms (100.00%) | Memory: 15.4 MB (69.34%) - LeetSync From 90ea5b4da18bf31bdbf3d042e981ba3f82807c89 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 15:15:29 +0530 Subject: [PATCH 09/82] Added README.md file for Maximum Product of Two Digits --- 3859-maximum-product-of-two-digits/README.md | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 3859-maximum-product-of-two-digits/README.md diff --git a/3859-maximum-product-of-two-digits/README.md b/3859-maximum-product-of-two-digits/README.md new file mode 100644 index 0000000000000..a0656d69d0689 --- /dev/null +++ b/3859-maximum-product-of-two-digits/README.md @@ -0,0 +1,61 @@ +

Maximum Product of Two Digits

Difficulty: Easy

You are given a positive integer n.

+ +

Return the maximum product of any two digits in n.

+ +

Note: You may use the same digit twice if it appears more than once in n.

+ +

 

+

Example 1:

+ +
+

Input: n = 31

+ +

Output: 3

+ +

Explanation:

+ +
    +
  • The digits of n are [3, 1].
  • +
  • The possible products of any two digits are: 3 * 1 = 3.
  • +
  • The maximum product is 3.
  • +
+
+ +

Example 2:

+ +
+

Input: n = 22

+ +

Output: 4

+ +

Explanation:

+ +
    +
  • The digits of n are [2, 2].
  • +
  • The possible products of any two digits are: 2 * 2 = 4.
  • +
  • The maximum product is 4.
  • +
+
+ +

Example 3:

+ +
+

Input: n = 124

+ +

Output: 8

+ +

Explanation:

+ +
    +
  • The digits of n are [1, 2, 4].
  • +
  • The possible products of any two digits are: 1 * 2 = 2, 1 * 4 = 4, 2 * 4 = 8.
  • +
  • The maximum product is 8.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 10 <= n <= 109
  • +
From dc29977948ba44649ee7fca08cf99911ff16dd84 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 15:15:32 +0530 Subject: [PATCH 10/82] Time: 0 ms (100.00%) | Memory: 9.2 MB (28.02%) - LeetSync --- .../maximum-product-of-two-digits.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 3859-maximum-product-of-two-digits/maximum-product-of-two-digits.cpp diff --git a/3859-maximum-product-of-two-digits/maximum-product-of-two-digits.cpp b/3859-maximum-product-of-two-digits/maximum-product-of-two-digits.cpp new file mode 100644 index 0000000000000..5b0a6add94ab3 --- /dev/null +++ b/3859-maximum-product-of-two-digits/maximum-product-of-two-digits.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int maxProduct(int n) { + vectorres; + while(n>0) + { + res.push_back(n%10); + n/=10; + + } + sort(res.begin(), res.end()); + return res[res.size()-1] * res[res.size()-2]; + } +}; \ No newline at end of file From 37130b77e3ad966610c44e70c4f75b6bf3377668 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 15:50:30 +0530 Subject: [PATCH 11/82] Added README.md file for Truncate Sentence --- 1944-truncate-sentence/README.md | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1944-truncate-sentence/README.md diff --git a/1944-truncate-sentence/README.md b/1944-truncate-sentence/README.md new file mode 100644 index 0000000000000..ecc0e28bafa08 --- /dev/null +++ b/1944-truncate-sentence/README.md @@ -0,0 +1,47 @@ +

Truncate Sentence

Difficulty: Easy

A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation).

+ +
    +
  • For example, "Hello World", "HELLO", and "hello world hello world" are all sentences.
  • +
+ +

You are given a sentence s​​​​​​ and an integer k​​​​​​. You want to truncate s​​​​​​ such that it contains only the first k​​​​​​ words. Return s​​​​​​ after truncating it.

+ +

 

+

Example 1:

+ +
+Input: s = "Hello how are you Contestant", k = 4
+Output: "Hello how are you"
+Explanation:
+The words in s are ["Hello", "how" "are", "you", "Contestant"].
+The first 4 words are ["Hello", "how", "are", "you"].
+Hence, you should return "Hello how are you".
+
+ +

Example 2:

+ +
+Input: s = "What is the solution to this problem", k = 4
+Output: "What is the solution"
+Explanation:
+The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
+The first 4 words are ["What", "is", "the", "solution"].
+Hence, you should return "What is the solution".
+ +

Example 3:

+ +
+Input: s = "chopper is not a tanuki", k = 5
+Output: "chopper is not a tanuki"
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 500
  • +
  • k is in the range [1, the number of words in s].
  • +
  • s consist of only lowercase and uppercase English letters and spaces.
  • +
  • The words in s are separated by a single space.
  • +
  • There are no leading or trailing spaces.
  • +
From b6e663f51bba1a1b3a6d42dc002b56a42ef21a3d Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 15:50:32 +0530 Subject: [PATCH 12/82] Time: 4 ms (1.89%) | Memory: 10.8 MB (5.30%) - LeetSync --- 1944-truncate-sentence/truncate-sentence.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1944-truncate-sentence/truncate-sentence.cpp diff --git a/1944-truncate-sentence/truncate-sentence.cpp b/1944-truncate-sentence/truncate-sentence.cpp new file mode 100644 index 0000000000000..bf782d76da88b --- /dev/null +++ b/1944-truncate-sentence/truncate-sentence.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + string truncateSentence(string s, int k) { + int count=0; + string ans=""; + for(char i:s) + { + if(i==' ') + { + count++; + if(count==k) + break; + } + ans=ans+i; + } + return ans; + + + } +}; \ No newline at end of file From 2e0089646c2dd51b2a9204797918417296db5bc4 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:25:37 +0530 Subject: [PATCH 13/82] Added README.md file for Minimum Cost of Buying Candies With Discount --- .../README.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2248-minimum-cost-of-buying-candies-with-discount/README.md diff --git a/2248-minimum-cost-of-buying-candies-with-discount/README.md b/2248-minimum-cost-of-buying-candies-with-discount/README.md new file mode 100644 index 0000000000000..b96d4ceaf0d0e --- /dev/null +++ b/2248-minimum-cost-of-buying-candies-with-discount/README.md @@ -0,0 +1,51 @@ +

Minimum Cost of Buying Candies With Discount

Difficulty: Easy

A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free.

+ +

The customer can choose any candy to take away for free as long as the cost of the chosen candy is less than or equal to the minimum cost of the two candies bought.

+ +
    +
  • For example, if there are 4 candies with costs 1, 2, 3, and 4, and the customer buys candies with costs 2 and 3, they can take the candy with cost 1 for free, but not the candy with cost 4.
  • +
+ +

Given a 0-indexed integer array cost, where cost[i] denotes the cost of the ith candy, return the minimum cost of buying all the candies.

+ +

 

+

Example 1:

+ +
+Input: cost = [1,2,3]
+Output: 5
+Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
+The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies.
+Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
+The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.
+
+ +

Example 2:

+ +
+Input: cost = [6,5,7,9,2,2]
+Output: 23
+Explanation: The way in which we can get the minimum cost is described below:
+- Buy candies with costs 9 and 7
+- Take the candy with cost 6 for free
+- We buy candies with costs 5 and 2
+- Take the last remaining candy with cost 2 for free
+Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.
+
+ +

Example 3:

+ +
+Input: cost = [5,5]
+Output: 10
+Explanation: Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
+Hence, the minimum cost to buy all candies is 5 + 5 = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= cost.length <= 100
  • +
  • 1 <= cost[i] <= 100
  • +
From bd476dd55443dd4cc2710bdacf10dfd43c044404 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:25:39 +0530 Subject: [PATCH 14/82] Time: 0 ms (100.00%) | Memory: 14.1 MB (79.94%) - LeetSync --- ...mum-cost-of-buying-candies-with-discount.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2248-minimum-cost-of-buying-candies-with-discount/minimum-cost-of-buying-candies-with-discount.cpp diff --git a/2248-minimum-cost-of-buying-candies-with-discount/minimum-cost-of-buying-candies-with-discount.cpp b/2248-minimum-cost-of-buying-candies-with-discount/minimum-cost-of-buying-candies-with-discount.cpp new file mode 100644 index 0000000000000..d1c5083ddcb4d --- /dev/null +++ b/2248-minimum-cost-of-buying-candies-with-discount/minimum-cost-of-buying-candies-with-discount.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int minimumCost(vector& cost) { + sort(cost.begin(), cost.end()); + int total = 0; + int take = 0; + for (int i = cost.size() - 1; i >= 0; i--){ + if (take == 2){ + take = 0; + } else { + total += cost[i]; + take += 1; + } + } + return total; + } +}; \ No newline at end of file From 36ab70c1be7c588f60ee5e133a64cdcf1680123b Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:55:43 +0530 Subject: [PATCH 15/82] Added README.md file for Maximum Units on a Truck --- 1829-maximum-units-on-a-truck/README.md | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 1829-maximum-units-on-a-truck/README.md diff --git a/1829-maximum-units-on-a-truck/README.md b/1829-maximum-units-on-a-truck/README.md new file mode 100644 index 0000000000000..475e5f3569757 --- /dev/null +++ b/1829-maximum-units-on-a-truck/README.md @@ -0,0 +1,40 @@ +

Maximum Units on a Truck

Difficulty: Easy

You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]:

+ +
    +
  • numberOfBoxesi is the number of boxes of type i.
  • +
  • numberOfUnitsPerBoxi is the number of units in each box of the type i.
  • +
+ +

You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.

+ +

Return the maximum total number of units that can be put on the truck.

+ +

 

+

Example 1:

+ +
+Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
+Output: 8
+Explanation: There are:
+- 1 box of the first type that contains 3 units.
+- 2 boxes of the second type that contain 2 units each.
+- 3 boxes of the third type that contain 1 unit each.
+You can take all the boxes of the first and second types, and one box of the third type.
+The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
+
+ +

Example 2:

+ +
+Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
+Output: 91
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= boxTypes.length <= 1000
  • +
  • 1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000
  • +
  • 1 <= truckSize <= 106
  • +
From 3a125ddaf787be0f74440a82017d56abad16f908 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:55:44 +0530 Subject: [PATCH 16/82] Time: 0 ms (100.00%) | Memory: 19.9 MB (45.43%) - LeetSync --- .../maximum-units-on-a-truck.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1829-maximum-units-on-a-truck/maximum-units-on-a-truck.cpp diff --git a/1829-maximum-units-on-a-truck/maximum-units-on-a-truck.cpp b/1829-maximum-units-on-a-truck/maximum-units-on-a-truck.cpp new file mode 100644 index 0000000000000..438432f267060 --- /dev/null +++ b/1829-maximum-units-on-a-truck/maximum-units-on-a-truck.cpp @@ -0,0 +1,29 @@ + +class Solution { +public: + + static int comp(vector&a,vector&b) + { + return a[1]>b[1]; + } + int maximumUnits(vector>& boxTypes, int truck) { + int n=boxTypes.size(); + sort(boxTypes.begin(),boxTypes.end(),comp); + int ans=0; + for(int i=0;i=0) + { + ans+=(boxTypes[i][0] * boxTypes[i][1]); + truck-=boxTypes[i][0]; + } + else + { + ans+=(truck * boxTypes[i][1]); + break; + } + } + return ans; + } +}; \ No newline at end of file From e8c1df306a4fe1480c5ef6d9fe65b57abd498b88 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:56:23 +0530 Subject: [PATCH 17/82] 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 ac3ede247fc4e1fd79e4fddf4c91f09eaf7bb891 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 23 May 2025 17:56:25 +0530 Subject: [PATCH 18/82] Time: 0 ms (100.00%) | Memory: 23 MB (56.66%) - LeetSync --- .../balanced-binary-tree.cpp | 27 +++++++++++++++++++ 1 file changed, 27 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..130ecf1005499 --- /dev/null +++ b/110-balanced-binary-tree/balanced-binary-tree.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: + bool isBalanced(TreeNode* root) { + return check(root)!=-1; + } + int check(TreeNode* root){ + if(root==NULL) return 0; + int lh=check(root->left); + if(lh==-1) return -1; + int rh=check(root->right); + if(rh==-1) return -1; + if(abs(lh-rh)>1) return -1; + + return max(lh,rh)+1; + } +}; \ No newline at end of file From 287c451482bdb1b686842dc4732b016da2dde767 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:04:34 +0530 Subject: [PATCH 19/82] Added README.md file for Apple Redistribution into Boxes --- .../README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 3334-apple-redistribution-into-boxes/README.md diff --git a/3334-apple-redistribution-into-boxes/README.md b/3334-apple-redistribution-into-boxes/README.md new file mode 100644 index 0000000000000..a40d8ab07bd76 --- /dev/null +++ b/3334-apple-redistribution-into-boxes/README.md @@ -0,0 +1,35 @@ +

Apple Redistribution into Boxes

Difficulty: Easy

You are given an array apple of size n and an array capacity of size m.

+ +

There are n packs where the ith pack contains apple[i] apples. There are m boxes as well, and the ith box has a capacity of capacity[i] apples.

+ +

Return the minimum number of boxes you need to select to redistribute these n packs of apples into boxes.

+ +

Note that, apples from the same pack can be distributed into different boxes.

+ +

 

+

Example 1:

+ +
+Input: apple = [1,3,2], capacity = [4,3,1,5,2]
+Output: 2
+Explanation: We will use boxes with capacities 4 and 5.
+It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
+
+ +

Example 2:

+ +
+Input: apple = [5,5,5], capacity = [2,4,2,7]
+Output: 4
+Explanation: We will need to use all the boxes.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n == apple.length <= 50
  • +
  • 1 <= m == capacity.length <= 50
  • +
  • 1 <= apple[i], capacity[i] <= 50
  • +
  • The input is generated such that it's possible to redistribute packs of apples into boxes.
  • +
From f2c81180915013a371c630a7131c0aba792dcb23 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:04:35 +0530 Subject: [PATCH 20/82] Time: 0 ms (100.00%) | Memory: 33 MB (73.18%) - LeetSync --- .../apple-redistribution-into-boxes.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 3334-apple-redistribution-into-boxes/apple-redistribution-into-boxes.cpp diff --git a/3334-apple-redistribution-into-boxes/apple-redistribution-into-boxes.cpp b/3334-apple-redistribution-into-boxes/apple-redistribution-into-boxes.cpp new file mode 100644 index 0000000000000..47a9d7f55572d --- /dev/null +++ b/3334-apple-redistribution-into-boxes/apple-redistribution-into-boxes.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int minimumBoxes(vector& apple, vector& c) { + int total = 0; + for(int i = 0; i= 0; i--) { + if(total > 0) { + total -= c[i]; + count++; + } + } + return count; + } +}; \ No newline at end of file From 22e9d15eacf2fb6809c3c4bbdad55a7106bf9e33 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:04:42 +0530 Subject: [PATCH 21/82] Added README.md file for Average of Levels in Binary Tree --- .../README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 637-average-of-levels-in-binary-tree/README.md diff --git a/637-average-of-levels-in-binary-tree/README.md b/637-average-of-levels-in-binary-tree/README.md new file mode 100644 index 0000000000000..518e225e81dfb --- /dev/null +++ b/637-average-of-levels-in-binary-tree/README.md @@ -0,0 +1,25 @@ +

Average of Levels in Binary Tree

Difficulty: Easy
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. +

 

+

Example 1:

+ +
+Input: root = [3,9,20,null,null,15,7]
+Output: [3.00000,14.50000,11.00000]
+Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
+Hence return [3, 14.5, 11].
+
+ +

Example 2:

+ +
+Input: root = [3,9,20,15,7]
+Output: [3.00000,14.50000,11.00000]
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the tree is in the range [1, 104].
  • +
  • -231 <= Node.val <= 231 - 1
  • +
From 36ebd2de06bf2c26d0397d2a7e5c013217d969d8 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:04:43 +0530 Subject: [PATCH 22/82] Time: 8 ms (4.42%) | Memory: 23.9 MB (26.08%) - LeetSync --- .../average-of-levels-in-binary-tree.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 637-average-of-levels-in-binary-tree/average-of-levels-in-binary-tree.cpp diff --git a/637-average-of-levels-in-binary-tree/average-of-levels-in-binary-tree.cpp b/637-average-of-levels-in-binary-tree/average-of-levels-in-binary-tree.cpp new file mode 100644 index 0000000000000..fc6d2697d7d5f --- /dev/null +++ b/637-average-of-levels-in-binary-tree/average-of-levels-in-binary-tree.cpp @@ -0,0 +1,30 @@ +/** + * 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: + vector averageOfLevels(TreeNode* root) { + vector ans; + if (!root) return ans; + queue q{{root}}; + while (!q.empty()) { + int n = q.size(); double sum = 0; + for (int i = 0; i < n; ++i) { + auto node = q.front(); q.pop(); + sum += node->val; + if (node->left) q.push(node->left); + if (node->right) q.push(node->right); + } + ans.push_back(sum / n); + } + return ans; + } +}; From f2868e9195455920c2664d91a731519b1372e643 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:24:22 +0530 Subject: [PATCH 23/82] Added README.md file for Average of Levels in Binary Tree From 7b63edd222021df1ca06b6d508268e690ddaed11 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sat, 24 May 2025 15:24:23 +0530 Subject: [PATCH 24/82] Time: 0 ms (100.00%) | Memory: 23.8 MB (53.99%) - LeetSync From eb013458b644d1a738e4a6a649ee6fd7390b064b Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:16:22 +0530 Subject: [PATCH 25/82] Added README.md file for Distance Between Bus Stops --- 1287-distance-between-bus-stops/README.md | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1287-distance-between-bus-stops/README.md diff --git a/1287-distance-between-bus-stops/README.md b/1287-distance-between-bus-stops/README.md new file mode 100644 index 0000000000000..0403e31ee74e4 --- /dev/null +++ b/1287-distance-between-bus-stops/README.md @@ -0,0 +1,49 @@ +

Distance Between Bus Stops

Difficulty: Easy

A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

+ +

The bus goes along both directions i.e. clockwise and counterclockwise.

+ +

Return the shortest distance between the given start and destination stops.

+ +

 

+

Example 1:

+ +

+ +
+Input: distance = [1,2,3,4], start = 0, destination = 1
+Output: 1
+Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
+ +

 

+ +

Example 2:

+ +

+ +
+Input: distance = [1,2,3,4], start = 0, destination = 2
+Output: 3
+Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
+
+ +

 

+ +

Example 3:

+ +

+ +
+Input: distance = [1,2,3,4], start = 0, destination = 3
+Output: 4
+Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 10^4
  • +
  • distance.length == n
  • +
  • 0 <= start, destination < n
  • +
  • 0 <= distance[i] <= 10^4
  • +
\ No newline at end of file From 07437b5e082f41c5ab18b0c3d7b04446e63e6545 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:16:24 +0530 Subject: [PATCH 26/82] Time: 3 ms (61.11%) | Memory: 12.5 MB (83.84%) - LeetSync --- .../distance-between-bus-stops.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1287-distance-between-bus-stops/distance-between-bus-stops.cpp diff --git a/1287-distance-between-bus-stops/distance-between-bus-stops.cpp b/1287-distance-between-bus-stops/distance-between-bus-stops.cpp new file mode 100644 index 0000000000000..976cd3db16746 --- /dev/null +++ b/1287-distance-between-bus-stops/distance-between-bus-stops.cpp @@ -0,0 +1,27 @@ + +class Solution { +public: + int distanceBetweenBusStops(vector& distance, int start, int destination) { + + int front=0; + int back=0; + + int n=distance.size(); + for(int i=start;i!=destination;i=(i+1)%n) + { + front+=distance[i]; + } + + for(int i=destination;i!=start;i=(i+1)%n) + { + //i=i%n; + back+=distance[i]; + } + + + return min(front,back); + + } +}; + + \ No newline at end of file From 6a6ab8f12bfe0b81870651d5553f1245dfdb5494 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:24:31 +0530 Subject: [PATCH 27/82] Added README.md file for Minimum Absolute Difference --- 1306-minimum-absolute-difference/README.md | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 1306-minimum-absolute-difference/README.md diff --git a/1306-minimum-absolute-difference/README.md b/1306-minimum-absolute-difference/README.md new file mode 100644 index 0000000000000..fa42bdabfd4f9 --- /dev/null +++ b/1306-minimum-absolute-difference/README.md @@ -0,0 +1,39 @@ +

Minimum Absolute Difference

Difficulty: Easy

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.

+ +

Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows

+ +
    +
  • a, b are from arr
  • +
  • a < b
  • +
  • b - a equals to the minimum absolute difference of any two elements in arr
  • +
+ +

 

+

Example 1:

+ +
+Input: arr = [4,2,1,3]
+Output: [[1,2],[2,3],[3,4]]
+Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
+ +

Example 2:

+ +
+Input: arr = [1,3,6,10,15]
+Output: [[1,3]]
+
+ +

Example 3:

+ +
+Input: arr = [3,8,-10,23,19,-4,-14,27]
+Output: [[-14,-10],[19,23],[23,27]]
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= arr.length <= 105
  • +
  • -106 <= arr[i] <= 106
  • +
From 3cea2042545b94bb9873ceb014738233b78fe54b Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:24:33 +0530 Subject: [PATCH 28/82] Time: 11 ms (98.67%) | Memory: 36.6 MB (89.89%) - LeetSync --- .../minimum-absolute-difference.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 1306-minimum-absolute-difference/minimum-absolute-difference.cpp diff --git a/1306-minimum-absolute-difference/minimum-absolute-difference.cpp b/1306-minimum-absolute-difference/minimum-absolute-difference.cpp new file mode 100644 index 0000000000000..951cdb581259f --- /dev/null +++ b/1306-minimum-absolute-difference/minimum-absolute-difference.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector> minimumAbsDifference(vector& arr) { + sort(arr.begin(), arr.end()); + vector> res; + int min = INT_MAX; + for (int i = 1, n = arr.size(); i < n; ++i) { + int diff = arr[i] - arr[i - 1]; + if (diff < min) { + min = diff; + res.clear(); + } + if (diff == min) res.push_back({arr[i - 1], arr[i]}); + } + return res; + } +}; \ No newline at end of file From fa16cf83f5ae22b716eb05d71af2912f8e6203ad Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:26:57 +0530 Subject: [PATCH 29/82] Added README.md file for Buy Two Chocolates --- 2756-buy-two-chocolates/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2756-buy-two-chocolates/README.md diff --git a/2756-buy-two-chocolates/README.md b/2756-buy-two-chocolates/README.md new file mode 100644 index 0000000000000..867f7cd39dd82 --- /dev/null +++ b/2756-buy-two-chocolates/README.md @@ -0,0 +1,31 @@ +

Buy Two Chocolates

Difficulty: Easy

You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.

+ +

You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.

+ +

Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.

+ +

 

+

Example 1:

+ +
+Input: prices = [1,2,2], money = 3
+Output: 0
+Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
+
+ +

Example 2:

+ +
+Input: prices = [3,2,3], money = 3
+Output: 3
+Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= prices.length <= 50
  • +
  • 1 <= prices[i] <= 100
  • +
  • 1 <= money <= 100
  • +
From a18eaa3dac552a5b7d0b158e6f5701eb60453c95 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:26:58 +0530 Subject: [PATCH 30/82] Time: 0 ms (100.00%) | Memory: 52.3 MB (31.59%) - LeetSync --- 2756-buy-two-chocolates/buy-two-chocolates.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 2756-buy-two-chocolates/buy-two-chocolates.cpp diff --git a/2756-buy-two-chocolates/buy-two-chocolates.cpp b/2756-buy-two-chocolates/buy-two-chocolates.cpp new file mode 100644 index 0000000000000..a36fd9d06c64c --- /dev/null +++ b/2756-buy-two-chocolates/buy-two-chocolates.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int buyChoco(vector& prices, int money) { + sort(prices.begin(),prices.end()); + int price= prices[0]+prices[1]; + if(price <= money) + { + return money-price; + } + return money; + } +}; \ No newline at end of file From 80e6833eeb4469731241e2767dfd2f102c3744c3 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:49:17 +0530 Subject: [PATCH 31/82] Added README.md file for Buy Two Chocolates From dd70bb7a8accaf63e087776ae13de9cb81fa2ba2 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 25 May 2025 19:49:19 +0530 Subject: [PATCH 32/82] Time: 1 ms (23.80%) | Memory: 52.3 MB (4.45%) - LeetSync From 8cbf61065d47080eb4edbc5fc70cd9427c2ed49b Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 26 May 2025 17:19:08 +0530 Subject: [PATCH 33/82] Added README.md file for Delete Greatest Value in Each Row --- .../README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2585-delete-greatest-value-in-each-row/README.md diff --git a/2585-delete-greatest-value-in-each-row/README.md b/2585-delete-greatest-value-in-each-row/README.md new file mode 100644 index 0000000000000..e88701fe32bfb --- /dev/null +++ b/2585-delete-greatest-value-in-each-row/README.md @@ -0,0 +1,45 @@ +

Delete Greatest Value in Each Row

Difficulty: Easy

You are given an m x n matrix grid consisting of positive integers.

+ +

Perform the following operation until grid becomes empty:

+ +
    +
  • Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them.
  • +
  • Add the maximum of deleted elements to the answer.
  • +
+ +

Note that the number of columns decreases by one after each operation.

+ +

Return the answer after performing the operations described above.

+ +

 

+

Example 1:

+ +
+Input: grid = [[1,2,4],[3,3,1]]
+Output: 8
+Explanation: The diagram above shows the removed values in each step.
+- In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer.
+- In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer.
+- In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer.
+The final answer = 4 + 3 + 1 = 8.
+
+ +

Example 2:

+ +
+Input: grid = [[10]]
+Output: 10
+Explanation: The diagram above shows the removed values in each step.
+- In the first operation, we remove 10 from the first row. We add 10 to the answer.
+The final answer = 10.
+
+ +

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 50
  • +
  • 1 <= grid[i][j] <= 100
  • +
From e6797dd361219b0b2639c8e7d0cfb50321f4d6d5 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 26 May 2025 17:19:10 +0530 Subject: [PATCH 34/82] Time: 0 ms (100.00%) | Memory: 13.1 MB (58.00%) - LeetSync --- .../delete-greatest-value-in-each-row.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 2585-delete-greatest-value-in-each-row/delete-greatest-value-in-each-row.cpp diff --git a/2585-delete-greatest-value-in-each-row/delete-greatest-value-in-each-row.cpp b/2585-delete-greatest-value-in-each-row/delete-greatest-value-in-each-row.cpp new file mode 100644 index 0000000000000..5be6545fa29d3 --- /dev/null +++ b/2585-delete-greatest-value-in-each-row/delete-greatest-value-in-each-row.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int deleteGreatestValue(vector>& grid) + { + int n = grid.size(); + int m = grid[0].size(); + int answer = 0; + + for(int i=0;i()); + } + for(int i=0;i Date: Mon, 26 May 2025 17:20:54 +0530 Subject: [PATCH 35/82] Added README.md file for Lexicographically Smallest String After a Swap --- .../README.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3484-lexicographically-smallest-string-after-a-swap/README.md diff --git a/3484-lexicographically-smallest-string-after-a-swap/README.md b/3484-lexicographically-smallest-string-after-a-swap/README.md new file mode 100644 index 0000000000000..8bbc6c0cfad86 --- /dev/null +++ b/3484-lexicographically-smallest-string-after-a-swap/README.md @@ -0,0 +1,36 @@ +

Lexicographically Smallest String After a Swap

Difficulty: Easy

Given a string s containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s with the same parity at most once.

+ +

Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not.

+ +

 

+

Example 1:

+ +
+

Input: s = "45320"

+ +

Output: "43520"

+ +

Explanation:

+ +

s[1] == '5' and s[2] == '3' both have the same parity, and swapping them results in the lexicographically smallest string.

+
+ +

Example 2:

+ +
+

Input: s = "001"

+ +

Output: "001"

+ +

Explanation:

+ +

There is no need to perform a swap because s is already the lexicographically smallest.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= s.length <= 100
  • +
  • s consists only of digits.
  • +
From a0854f25c805c86c86fbaaa701d32f24f04cf394 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 26 May 2025 17:20:55 +0530 Subject: [PATCH 36/82] Time: 0 ms (100.00%) | Memory: 8.4 MB (23.38%) - LeetSync --- ...raphically-smallest-string-after-a-swap.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 3484-lexicographically-smallest-string-after-a-swap/lexicographically-smallest-string-after-a-swap.cpp diff --git a/3484-lexicographically-smallest-string-after-a-swap/lexicographically-smallest-string-after-a-swap.cpp b/3484-lexicographically-smallest-string-after-a-swap/lexicographically-smallest-string-after-a-swap.cpp new file mode 100644 index 0000000000000..fcb23dea43808 --- /dev/null +++ b/3484-lexicographically-smallest-string-after-a-swap/lexicographically-smallest-string-after-a-swap.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + string getSmallestString(string s) + { + int i; + int n=s.size(); + + for(i=0;is[i+1])) + { + swap(s[i],s[i+1]); + break; + } + } + return s; + } +}; \ No newline at end of file From cc1cfd99d08c5df544346315ddd5d332246022d0 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Tue, 27 May 2025 17:14:36 +0530 Subject: [PATCH 37/82] Added README.md file for Find Center of Star Graph --- 1916-find-center-of-star-graph/README.md | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1916-find-center-of-star-graph/README.md diff --git a/1916-find-center-of-star-graph/README.md b/1916-find-center-of-star-graph/README.md new file mode 100644 index 0000000000000..e19e63d5a60f7 --- /dev/null +++ b/1916-find-center-of-star-graph/README.md @@ -0,0 +1,31 @@ +

Find Center of Star Graph

Difficulty: Easy

There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.

+ +

You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.

+ +

 

+

Example 1:

+ +
+Input: edges = [[1,2],[2,3],[4,2]]
+Output: 2
+Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
+
+ +

Example 2:

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

 

+

Constraints:

+ +
    +
  • 3 <= n <= 105
  • +
  • edges.length == n - 1
  • +
  • edges[i].length == 2
  • +
  • 1 <= ui, vi <= n
  • +
  • ui != vi
  • +
  • The given edges represent a valid star graph.
  • +
From 159a8f0382e346e4a8b50968c9d33f2898a4994e Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Tue, 27 May 2025 17:14:38 +0530 Subject: [PATCH 38/82] Time: 0 ms (100.00%) | Memory: 99.2 MB (72.50%) - LeetSync --- .../find-center-of-star-graph.cpp | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 1916-find-center-of-star-graph/find-center-of-star-graph.cpp diff --git a/1916-find-center-of-star-graph/find-center-of-star-graph.cpp b/1916-find-center-of-star-graph/find-center-of-star-graph.cpp new file mode 100644 index 0000000000000..7e9eb6922e4de --- /dev/null +++ b/1916-find-center-of-star-graph/find-center-of-star-graph.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int findCenter(vector>& e) { + return e[0][0] == e[1][0] || e[0][0] == e[1][1] ? e[0][0] : e[0][1]; +} +}; \ No newline at end of file From 43a8b09a48ee8725b5e8386973abcb082de782d2 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Wed, 28 May 2025 18:39:48 +0530 Subject: [PATCH 39/82] Added README.md file for Alternating Digit Sum --- 2630-alternating-digit-sum/README.md | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2630-alternating-digit-sum/README.md diff --git a/2630-alternating-digit-sum/README.md b/2630-alternating-digit-sum/README.md new file mode 100644 index 0000000000000..971232f6decf3 --- /dev/null +++ b/2630-alternating-digit-sum/README.md @@ -0,0 +1,49 @@ +

Alternating Digit Sum

Difficulty: Easy

You are given a positive integer n. Each digit of n has a sign according to the following rules:

+ +
    +
  • The most significant digit is assigned a positive sign.
  • +
  • Each other digit has an opposite sign to its adjacent digits.
  • +
+ +

Return the sum of all digits with their corresponding sign.

+ +

 

+

Example 1:

+ +
+Input: n = 521
+Output: 4
+Explanation: (+5) + (-2) + (+1) = 4.
+
+ +

Example 2:

+ +
+Input: n = 111
+Output: 1
+Explanation: (+1) + (-1) + (+1) = 1.
+
+ +

Example 3:

+ +
+Input: n = 886996
+Output: 0
+Explanation: (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 109
  • +
+ +

 

+ From 641fcce9ee36999020ec776cca3c8815d4ff4e98 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Wed, 28 May 2025 18:39:49 +0530 Subject: [PATCH 40/82] Time: 0 ms (100.00%) | Memory: 8 MB (50.25%) - LeetSync --- .../alternating-digit-sum.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2630-alternating-digit-sum/alternating-digit-sum.cpp diff --git a/2630-alternating-digit-sum/alternating-digit-sum.cpp b/2630-alternating-digit-sum/alternating-digit-sum.cpp new file mode 100644 index 0000000000000..f3b2aa3649156 --- /dev/null +++ b/2630-alternating-digit-sum/alternating-digit-sum.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int alternateDigitSum(int n) { + string s = to_string(n); + int sum=0, fl=1; + for(int i=0; i Date: Wed, 28 May 2025 18:50:51 +0530 Subject: [PATCH 41/82] Added README.md file for Minimum Number Game --- 3226-minimum-number-game/README.md | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3226-minimum-number-game/README.md diff --git a/3226-minimum-number-game/README.md b/3226-minimum-number-game/README.md new file mode 100644 index 0000000000000..3319940471f8d --- /dev/null +++ b/3226-minimum-number-game/README.md @@ -0,0 +1,36 @@ +

Minimum Number Game

Difficulty: Easy

You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:

+ +
    +
  • Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
  • +
  • Now, first Bob will append the removed element in the array arr, and then Alice does the same.
  • +
  • The game continues until nums becomes empty.
  • +
+ +

Return the resulting array arr.

+ +

 

+

Example 1:

+ +
+Input: nums = [5,4,2,3]
+Output: [3,2,5,4]
+Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
+At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
+
+ +

Example 2:

+ +
+Input: nums = [2,5]
+Output: [5,2]
+Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 100
  • +
  • 1 <= nums[i] <= 100
  • +
  • nums.length % 2 == 0
  • +
From 65288c2648dc8c081a14dedeb498fb0280e13068 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Wed, 28 May 2025 18:50:53 +0530 Subject: [PATCH 42/82] Time: 0 ms (100.00%) | Memory: 28.4 MB (26.83%) - LeetSync --- 3226-minimum-number-game/minimum-number-game.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 3226-minimum-number-game/minimum-number-game.cpp diff --git a/3226-minimum-number-game/minimum-number-game.cpp b/3226-minimum-number-game/minimum-number-game.cpp new file mode 100644 index 0000000000000..75bf1be7ee15e --- /dev/null +++ b/3226-minimum-number-game/minimum-number-game.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector numberGame(vector& nums) { + vector arr; + sort(nums.begin() , nums.end()); + for(int i=1; i Date: Wed, 28 May 2025 18:51:02 +0530 Subject: [PATCH 43/82] Added README.md file for Minimum Number Game From 8795e5852f70e5367f0c0b151491d068de945e82 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Wed, 28 May 2025 18:51:03 +0530 Subject: [PATCH 44/82] Time: 4 ms (13.93%) | Memory: 28.4 MB (43.82%) - LeetSync From ddf96bf2d1df9057ab07a093b3f0f76a16f421f9 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 10:06:11 +0530 Subject: [PATCH 45/82] Added README.md file for Reverse Integer --- 7-reverse-integer/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 7-reverse-integer/README.md diff --git a/7-reverse-integer/README.md b/7-reverse-integer/README.md new file mode 100644 index 0000000000000..bb7ea1dd20000 --- /dev/null +++ b/7-reverse-integer/README.md @@ -0,0 +1,32 @@ +

Reverse Integer

Difficulty: Medium

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

+ +

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

+ +

 

+

Example 1:

+ +
+Input: x = 123
+Output: 321
+
+ +

Example 2:

+ +
+Input: x = -123
+Output: -321
+
+ +

Example 3:

+ +
+Input: x = 120
+Output: 21
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= x <= 231 - 1
  • +
From 0238bdb80c4cb6fb93e1c0614ca9374e906b075f Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 10:06:13 +0530 Subject: [PATCH 46/82] Time: 5 ms (19.58%) | Memory: 8.6 MB (21.26%) - LeetSync --- 7-reverse-integer/reverse-integer.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 7-reverse-integer/reverse-integer.cpp diff --git a/7-reverse-integer/reverse-integer.cpp b/7-reverse-integer/reverse-integer.cpp new file mode 100644 index 0000000000000..962c4a1930f12 --- /dev/null +++ b/7-reverse-integer/reverse-integer.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int reverse(int x) { + int r =0; + while(x!=0) + { + int temp=x%10; + x=x/10; + if(r>INT_MAX/10 || r Date: Fri, 30 May 2025 10:41:38 +0530 Subject: [PATCH 47/82] Added README.md file for Palindrome Number --- 9-palindrome-number/README.md | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 9-palindrome-number/README.md diff --git a/9-palindrome-number/README.md b/9-palindrome-number/README.md new file mode 100644 index 0000000000000..60ccf187263d5 --- /dev/null +++ b/9-palindrome-number/README.md @@ -0,0 +1,36 @@ +

Palindrome Number

Difficulty: Easy

Given an integer x, return true if x is a palindrome, and false otherwise.

+ +

 

+

Example 1:

+ +
+Input: x = 121
+Output: true
+Explanation: 121 reads as 121 from left to right and from right to left.
+
+ +

Example 2:

+ +
+Input: x = -121
+Output: false
+Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
+
+ +

Example 3:

+ +
+Input: x = 10
+Output: false
+Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= x <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without converting the integer to a string? \ No newline at end of file From a6d5c71e06c4d4b306793454c314d4049fced9a0 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 10:41:40 +0530 Subject: [PATCH 48/82] Time: 0 ms (100.00%) | Memory: 8.5 MB (92.18%) - LeetSync --- 9-palindrome-number/palindrome-number.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 9-palindrome-number/palindrome-number.cpp diff --git a/9-palindrome-number/palindrome-number.cpp b/9-palindrome-number/palindrome-number.cpp new file mode 100644 index 0000000000000..5327f8a794c33 --- /dev/null +++ b/9-palindrome-number/palindrome-number.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isPalindrome(int x) { + if(x<0) + return false; + long rev = 0; + int y=x; + while(y>0){ + rev=rev*10+y%10; + y/=10; + } + return rev == x; + } +}; \ No newline at end of file From 02758668cc1a35cf20797120588df7eea21685d1 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:05:35 +0530 Subject: [PATCH 49/82] Added README.md file for N-th Tribonacci Number --- 1236-n-th-tribonacci-number/README.md | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 1236-n-th-tribonacci-number/README.md diff --git a/1236-n-th-tribonacci-number/README.md b/1236-n-th-tribonacci-number/README.md new file mode 100644 index 0000000000000..23672bcb16f2f --- /dev/null +++ b/1236-n-th-tribonacci-number/README.md @@ -0,0 +1,31 @@ +

N-th Tribonacci Number

Difficulty: Easy

The Tribonacci sequence Tn is defined as follows: 

+ +

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

+ +

Given n, return the value of Tn.

+ +

 

+

Example 1:

+ +
+Input: n = 4
+Output: 4
+Explanation:
+T_3 = 0 + 1 + 1 = 2
+T_4 = 1 + 1 + 2 = 4
+
+ +

Example 2:

+ +
+Input: n = 25
+Output: 1389537
+
+ +

 

+

Constraints:

+ +
    +
  • 0 <= n <= 37
  • +
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
  • +
From 64f19bbaf9b945bead9e96e6dc3f913132f72c2d Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:05:36 +0530 Subject: [PATCH 50/82] Time: 0 ms (100.00%) | Memory: 8 MB (51.87%) - LeetSync --- .../n-th-tribonacci-number.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp diff --git a/1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp b/1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp new file mode 100644 index 0000000000000..94f727ffabd61 --- /dev/null +++ b/1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int tribonacci(int n) { + if(n==0) return 0; + if(n==1 || n==2) return 1; + int a=0, b=1, c=1, result=0; + for(int i=3; i<=n; i++){ + result = a+b+c; + a=b; + b=c; + c=result; + } + return result; + } +}; \ No newline at end of file From d23ef7639c18deabcf1336d3ebba910b75014395 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:05:43 +0530 Subject: [PATCH 51/82] Added README.md file for N-th Tribonacci Number From ae058605054212a4144690b31ebf10395d339b1e Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:05:44 +0530 Subject: [PATCH 52/82] Time: 0 ms (100.00%) | Memory: 7.7 MB (97.98%) - LeetSync From 2c8aa97b6903b5c5519db5b43b01dafe049c35f0 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:23:14 +0530 Subject: [PATCH 53/82] Added README.md file for Three Divisors --- 2083-three-divisors/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2083-three-divisors/README.md diff --git a/2083-three-divisors/README.md b/2083-three-divisors/README.md new file mode 100644 index 0000000000000..9facaee4daa8d --- /dev/null +++ b/2083-three-divisors/README.md @@ -0,0 +1,27 @@ +

Three Divisors

Difficulty: Easy

Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.

+ +

An integer m is a divisor of n if there exists an integer k such that n = k * m.

+ +

 

+

Example 1:

+ +
+Input: n = 2
+Output: false
+Explantion: 2 has only two divisors: 1 and 2.
+
+ +

Example 2:

+ +
+Input: n = 4
+Output: true
+Explantion: 4 has three divisors: 1, 2, and 4.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 104
  • +
From 05bfe0876611870ccc96abc0e5e8ac10ae3a9293 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:23:15 +0530 Subject: [PATCH 54/82] Time: 0 ms (100.00%) | Memory: 7.8 MB (24.31%) - LeetSync --- 2083-three-divisors/three-divisors.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 2083-three-divisors/three-divisors.cpp diff --git a/2083-three-divisors/three-divisors.cpp b/2083-three-divisors/three-divisors.cpp new file mode 100644 index 0000000000000..13d261889c6da --- /dev/null +++ b/2083-three-divisors/three-divisors.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + bool isThree(int n) { + int count=0; + for(int i=1; i<=n; i++){ + if(n%i==0) + count++; + } + if(count ==3) + return true; + return false; + } +}; \ No newline at end of file From 651b349ba56a9e5a855a392a0a00d7ec0bb8ef63 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:52:09 +0530 Subject: [PATCH 55/82] Added README.md file for Number of Common Factors --- 2507-number-of-common-factors/README.md | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2507-number-of-common-factors/README.md diff --git a/2507-number-of-common-factors/README.md b/2507-number-of-common-factors/README.md new file mode 100644 index 0000000000000..e648382e2a825 --- /dev/null +++ b/2507-number-of-common-factors/README.md @@ -0,0 +1,27 @@ +

Number of Common Factors

Difficulty: Easy

Given two positive integers a and b, return the number of common factors of a and b.

+ +

An integer x is a common factor of a and b if x divides both a and b.

+ +

 

+

Example 1:

+ +
+Input: a = 12, b = 6
+Output: 4
+Explanation: The common factors of 12 and 6 are 1, 2, 3, 6.
+
+ +

Example 2:

+ +
+Input: a = 25, b = 30
+Output: 2
+Explanation: The common factors of 25 and 30 are 1, 5.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= a, b <= 1000
  • +
From 8744205a1842d8c0cdb80baea991f357e766da97 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 11:52:11 +0530 Subject: [PATCH 56/82] Time: 0 ms (100.00%) | Memory: 7.8 MB (46.37%) - LeetSync --- .../number-of-common-factors.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 2507-number-of-common-factors/number-of-common-factors.cpp diff --git a/2507-number-of-common-factors/number-of-common-factors.cpp b/2507-number-of-common-factors/number-of-common-factors.cpp new file mode 100644 index 0000000000000..d0b4293879111 --- /dev/null +++ b/2507-number-of-common-factors/number-of-common-factors.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int commonFactors(int a, int b) { + int count=0; + int m= max(a,b); + for(int i=1; i<=m; i++) + { + if(a%i==0 && b%i==0) + count ++; + } + return count; + } +}; \ No newline at end of file From 60606dcb8bdbfc7c6d367e372ce4957639917faa Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:33:34 +0530 Subject: [PATCH 57/82] Added README.md file for Power of Two --- 231-power-of-two/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 231-power-of-two/README.md diff --git a/231-power-of-two/README.md b/231-power-of-two/README.md new file mode 100644 index 0000000000000..077afae03ec09 --- /dev/null +++ b/231-power-of-two/README.md @@ -0,0 +1,37 @@ +

Power of Two

Difficulty: Easy

Given an integer n, return true if it is a power of two. Otherwise, return false.

+ +

An integer n is a power of two, if there exists an integer x such that n == 2x.

+ +

 

+

Example 1:

+ +
+Input: n = 1
+Output: true
+Explanation: 20 = 1
+
+ +

Example 2:

+ +
+Input: n = 16
+Output: true
+Explanation: 24 = 16
+
+ +

Example 3:

+ +
+Input: n = 3
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion? \ No newline at end of file From 3366f2de99673d2f7aaf8bc883479aafbee12920 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:33:36 +0530 Subject: [PATCH 58/82] Time: 0 ms (100.00%) | Memory: 7.9 MB (18.55%) - LeetSync --- 231-power-of-two/power-of-two.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 231-power-of-two/power-of-two.cpp diff --git a/231-power-of-two/power-of-two.cpp b/231-power-of-two/power-of-two.cpp new file mode 100644 index 0000000000000..9dd90bd7e2bbd --- /dev/null +++ b/231-power-of-two/power-of-two.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool isPowerOfTwo(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%2==0)?isPowerOfTwo(n/2):false; + } +}; \ No newline at end of file From b2a9eef3ac9f2b60fcb7434b2b0f2f5a7f9f44cd Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:37:34 +0530 Subject: [PATCH 59/82] Added README.md file for Power of Three --- 326-power-of-three/README.md | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 326-power-of-three/README.md diff --git a/326-power-of-three/README.md b/326-power-of-three/README.md new file mode 100644 index 0000000000000..99fe79d09ecab --- /dev/null +++ b/326-power-of-three/README.md @@ -0,0 +1,38 @@ +

Power of Three

Difficulty: Easy

Given an integer n, return true if it is a power of three. Otherwise, return false.

+ +

An integer n is a power of three, if there exists an integer x such that n == 3x.

+ +

 

+

Example 1:

+ +
+Input: n = 27
+Output: true
+Explanation: 27 = 33
+
+ +

Example 2:

+ +
+Input: n = 0
+Output: false
+Explanation: There is no x where 3x = 0.
+
+ +

Example 3:

+ +
+Input: n = -1
+Output: false
+Explanation: There is no x where 3x = (-1).
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion? \ No newline at end of file From 36fbcbd1ce03484091ecfc2b125c54b00f8185cf Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:37:35 +0530 Subject: [PATCH 60/82] Time: 8 ms (16.59%) | Memory: 9 MB (46.81%) - LeetSync --- 326-power-of-three/power-of-three.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 326-power-of-three/power-of-three.cpp diff --git a/326-power-of-three/power-of-three.cpp b/326-power-of-three/power-of-three.cpp new file mode 100644 index 0000000000000..074936abf1613 --- /dev/null +++ b/326-power-of-three/power-of-three.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool isPowerOfThree(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%3==0)?isPowerOfThree(n/3):false; + } +}; \ No newline at end of file From b2dd9031202416599c20c750b26c8bdcd2fae575 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:38:58 +0530 Subject: [PATCH 61/82] Added README.md file for Power of Four --- 342-power-of-four/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 342-power-of-four/README.md diff --git a/342-power-of-four/README.md b/342-power-of-four/README.md new file mode 100644 index 0000000000000..59ce70e5278e6 --- /dev/null +++ b/342-power-of-four/README.md @@ -0,0 +1,24 @@ +

Power of Four

Difficulty: Easy

Given an integer n, return true if it is a power of four. Otherwise, return false.

+ +

An integer n is a power of four, if there exists an integer x such that n == 4x.

+ +

 

+

Example 1:

+
Input: n = 16
+Output: true
+

Example 2:

+
Input: n = 5
+Output: false
+

Example 3:

+
Input: n = 1
+Output: true
+
+

 

+

Constraints:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without loops/recursion? \ No newline at end of file From 8edb773615e55361d4673a58c62bbcbb4a62a7f8 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:38:59 +0530 Subject: [PATCH 62/82] Time: 0 ms (100.00%) | Memory: 7.9 MB (18.25%) - LeetSync --- 342-power-of-four/power-of-four.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 342-power-of-four/power-of-four.cpp diff --git a/342-power-of-four/power-of-four.cpp b/342-power-of-four/power-of-four.cpp new file mode 100644 index 0000000000000..65ace3e322c5d --- /dev/null +++ b/342-power-of-four/power-of-four.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + bool isPowerOfFour(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%4==0)?isPowerOfFour(n/4):false; + + } +}; \ No newline at end of file From 957df22a51490b39504c9c195c9f24e149e9687d Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:49:01 +0530 Subject: [PATCH 63/82] Added README.md file for Number Complement --- 476-number-complement/README.md | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 476-number-complement/README.md diff --git a/476-number-complement/README.md b/476-number-complement/README.md new file mode 100644 index 0000000000000..11227f5179a73 --- /dev/null +++ b/476-number-complement/README.md @@ -0,0 +1,34 @@ +

Number Complement

Difficulty: Easy

The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

+ +
    +
  • For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.
  • +
+ +

Given an integer num, return its complement.

+ +

 

+

Example 1:

+ +
+Input: num = 5
+Output: 2
+Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
+
+ +

Example 2:

+ +
+Input: num = 1
+Output: 0
+Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= num < 231
  • +
+ +

 

+

Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

From cdfe8910caaf8eff3d6cf516006a69e489f1fc45 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Fri, 30 May 2025 14:49:02 +0530 Subject: [PATCH 64/82] Time: 0 ms (100.00%) | Memory: 8.2 MB (7.69%) - LeetSync --- 476-number-complement/number-complement.cpp | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 476-number-complement/number-complement.cpp diff --git a/476-number-complement/number-complement.cpp b/476-number-complement/number-complement.cpp new file mode 100644 index 0000000000000..e2c8c2c760f28 --- /dev/null +++ b/476-number-complement/number-complement.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int findComplement(int num) { + return (pow(2,floor(log2(num))+1)-1)-num; + } +}; \ No newline at end of file From 6b0e68255fe929dd5b4071b59272e2aef5d5636d Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:33:15 +0530 Subject: [PATCH 65/82] Added README.md file for Monotonic Array --- 932-monotonic-array/README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 932-monotonic-array/README.md diff --git a/932-monotonic-array/README.md b/932-monotonic-array/README.md new file mode 100644 index 0000000000000..8d85bee36e3ec --- /dev/null +++ b/932-monotonic-array/README.md @@ -0,0 +1,35 @@ +

Monotonic Array

Difficulty: Easy

An array is monotonic if it is either monotone increasing or monotone decreasing.

+ +

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

+ +

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

+ +

 

+

Example 1:

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

Example 2:

+ +
+Input: nums = [6,5,4,4]
+Output: true
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -105 <= nums[i] <= 105
  • +
From 3c5e94cf36d1f3099537cfbaaf0e4054f088634e Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:33:17 +0530 Subject: [PATCH 66/82] Time: 0 ms (100.00%) | Memory: 100.2 MB (90.84%) - LeetSync --- 932-monotonic-array/monotonic-array.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 932-monotonic-array/monotonic-array.cpp diff --git a/932-monotonic-array/monotonic-array.cpp b/932-monotonic-array/monotonic-array.cpp new file mode 100644 index 0000000000000..744f6c7de4195 --- /dev/null +++ b/932-monotonic-array/monotonic-array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool isMonotonic(vector& nums) { + bool increasing = true ; + bool decreasing = true ; + for(int i = 1;i nums[i - 1]){ + decreasing=false; + } + if(nums[i] < nums[i - 1]){ + increasing = false; + } + + } + return increasing || decreasing; + } +}; From 2201851453ed3a5ee4caa6c041174c093c9c5e13 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:33:54 +0530 Subject: [PATCH 67/82] Added README.md file for Monotonic Array From 8d57004411fc085be72ba9558193691544b931d0 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:33:55 +0530 Subject: [PATCH 68/82] Time: 1 ms (40.43%) | Memory: 100.4 MB (31.65%) - LeetSync From 1e827f917593af727fa7d0cb93c4f1a5c6d76c35 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:36:46 +0530 Subject: [PATCH 69/82] Added README.md file for Shift 2D Grid --- 1386-shift-2d-grid/README.md | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1386-shift-2d-grid/README.md diff --git a/1386-shift-2d-grid/README.md b/1386-shift-2d-grid/README.md new file mode 100644 index 0000000000000..aa94061ddcbfe --- /dev/null +++ b/1386-shift-2d-grid/README.md @@ -0,0 +1,45 @@ +

Shift 2D Grid

Difficulty: Easy

Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.

+ +

In one shift operation:

+ +
    +
  • Element at grid[i][j] moves to grid[i][j + 1].
  • +
  • Element at grid[i][n - 1] moves to grid[i + 1][0].
  • +
  • Element at grid[m - 1][n - 1] moves to grid[0][0].
  • +
+ +

Return the 2D grid after applying shift operation k times.

+ +

 

+

Example 1:

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

Example 2:

+ +
+Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
+Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
+
+ +

Example 3:

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

 

+

Constraints:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m <= 50
  • +
  • 1 <= n <= 50
  • +
  • -1000 <= grid[i][j] <= 1000
  • +
  • 0 <= k <= 100
  • +
From bb5ea0616f56d487c6e2b0e5d4e1bef72b637f99 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:36:47 +0530 Subject: [PATCH 70/82] Time: 1 ms (54.80%) | Memory: 18.2 MB (66.74%) - LeetSync --- 1386-shift-2d-grid/shift-2d-grid.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1386-shift-2d-grid/shift-2d-grid.cpp diff --git a/1386-shift-2d-grid/shift-2d-grid.cpp b/1386-shift-2d-grid/shift-2d-grid.cpp new file mode 100644 index 0000000000000..54ca52e025768 --- /dev/null +++ b/1386-shift-2d-grid/shift-2d-grid.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> shiftGrid(vector>& grid, int k) { + int n = grid.size(); + int m = grid[0].size(); + int total = n * m; + k = k%total; + vector>res(n,vector(m)); + for(int i = 0;i Date: Sun, 8 Jun 2025 20:38:04 +0530 Subject: [PATCH 71/82] Added README.md file for Shift 2D Grid From 3bf314ce63b47037cb37d6a88b8b9587c7f6095c Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Sun, 8 Jun 2025 20:38:05 +0530 Subject: [PATCH 72/82] Time: 0 ms (100.00%) | Memory: 18.2 MB (66.74%) - LeetSync From d2abc421a84a27d674febafb87b7e7d1e20fa08c Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:12:01 +0530 Subject: [PATCH 73/82] Added README.md file for Monotonic Array From c8db5a33d4d1191f8af15d1546a247fd92182f96 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:12:02 +0530 Subject: [PATCH 74/82] Time: 1 ms (40.25%) | Memory: 100.4 MB (31.38%) - LeetSync --- 932-monotonic-array/monotonic-array.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/932-monotonic-array/monotonic-array.cpp b/932-monotonic-array/monotonic-array.cpp index 744f6c7de4195..b72eaa4049048 100644 --- a/932-monotonic-array/monotonic-array.cpp +++ b/932-monotonic-array/monotonic-array.cpp @@ -15,3 +15,8 @@ class Solution { return increasing || decreasing; } }; + + + + + From da5512e3358ca68346ebb559e9a5745e08f845f9 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:13:00 +0530 Subject: [PATCH 75/82] Added README.md file for Shift 2D Grid From ac6f0b5c89252e5f23156cb5699e8747e540f9c7 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:13:01 +0530 Subject: [PATCH 76/82] Time: 4 ms (30.77%) | Memory: 18.1 MB (66.40%) - LeetSync From be714aa377afd923b581d979c99b8da4e246e0d1 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:15:11 +0530 Subject: [PATCH 77/82] Added README.md file for Row With Maximum Ones --- 2737-row-with-maximum-ones/README.md | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2737-row-with-maximum-ones/README.md diff --git a/2737-row-with-maximum-ones/README.md b/2737-row-with-maximum-ones/README.md new file mode 100644 index 0000000000000..7cc90363f415d --- /dev/null +++ b/2737-row-with-maximum-ones/README.md @@ -0,0 +1,40 @@ +

Row With Maximum Ones

Difficulty: Easy

Given a m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.

+ +

In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.

+ +

Return an array containing the index of the row, and the number of ones in it.

+ +

 

+

Example 1:

+ +
+Input: mat = [[0,1],[1,0]]
+Output: [0,1]
+Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1). So, the answer is [0,1]. 
+
+ +

Example 2:

+ +
+Input: mat = [[0,0,0],[0,1,1]]
+Output: [1,2]
+Explanation: The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].
+
+ +

Example 3:

+ +
+Input: mat = [[0,0],[1,1],[0,0]]
+Output: [1,2]
+Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
+
+ +

 

+

Constraints:

+ +
    +
  • m == mat.length 
  • +
  • n == mat[i].length 
  • +
  • 1 <= m, n <= 100 
  • +
  • mat[i][j] is either 0 or 1.
  • +
From 86dd5b6431c3e3d149955259c9c4b0038e0486de Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:15:13 +0530 Subject: [PATCH 78/82] Time: 0 ms (100.00%) | Memory: 64.6 MB (89.53%) - LeetSync --- .../row-with-maximum-ones.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 2737-row-with-maximum-ones/row-with-maximum-ones.cpp diff --git a/2737-row-with-maximum-ones/row-with-maximum-ones.cpp b/2737-row-with-maximum-ones/row-with-maximum-ones.cpp new file mode 100644 index 0000000000000..d4d0eb0d8d081 --- /dev/null +++ b/2737-row-with-maximum-ones/row-with-maximum-ones.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector rowAndMaximumOnes(vector>& mat) { + int max = 0; + int row = 0; + for(int i = 0;imax){ + max = count ; + row = i; + } + } + return {row , max}; + } +}; From 4d323688fd58a2956f45715519add4e6f8a87a95 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:16:39 +0530 Subject: [PATCH 79/82] Added README.md file for Toeplitz Matrix --- 777-toeplitz-matrix/README.md | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 777-toeplitz-matrix/README.md diff --git a/777-toeplitz-matrix/README.md b/777-toeplitz-matrix/README.md new file mode 100644 index 0000000000000..430a2731d2ae6 --- /dev/null +++ b/777-toeplitz-matrix/README.md @@ -0,0 +1,42 @@ +

Toeplitz Matrix

Difficulty: Easy

Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

+ +

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

+ +

 

+

Example 1:

+ +
+Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
+Output: true
+Explanation:
+In the above grid, the diagonals are:
+"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
+In each diagonal all elements are the same, so the answer is True.
+
+ +

Example 2:

+ +
+Input: matrix = [[1,2],[2,2]]
+Output: false
+Explanation:
+The diagonal "[1, 2]" has different elements.
+
+ +

 

+

Constraints:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 20
  • +
  • 0 <= matrix[i][j] <= 99
  • +
+ +

 

+

Follow up:

+ +
    +
  • What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
  • +
  • What if the matrix is so large that you can only load up a partial row into the memory at once?
  • +
From be33784541d1195b136ce17b1b780e4f0104f8f0 Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:16:41 +0530 Subject: [PATCH 80/82] Time: 0 ms (100.00%) | Memory: 21.2 MB (37.10%) - LeetSync --- 777-toeplitz-matrix/toeplitz-matrix.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 777-toeplitz-matrix/toeplitz-matrix.cpp diff --git a/777-toeplitz-matrix/toeplitz-matrix.cpp b/777-toeplitz-matrix/toeplitz-matrix.cpp new file mode 100644 index 0000000000000..7ccae480a3cb4 --- /dev/null +++ b/777-toeplitz-matrix/toeplitz-matrix.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + bool isToeplitzMatrix(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + for(int i = 1;i Date: Mon, 9 Jun 2025 08:18:23 +0530 Subject: [PATCH 81/82] Added README.md file for Matrix Diagonal Sum --- 1677-matrix-diagonal-sum/README.md | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1677-matrix-diagonal-sum/README.md diff --git a/1677-matrix-diagonal-sum/README.md b/1677-matrix-diagonal-sum/README.md new file mode 100644 index 0000000000000..84dec171bf0ad --- /dev/null +++ b/1677-matrix-diagonal-sum/README.md @@ -0,0 +1,41 @@ +

Matrix Diagonal Sum

Difficulty: Easy

Given a square matrix mat, return the sum of the matrix diagonals.

+ +

Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

+ +

 

+

Example 1:

+ +
+Input: mat = [[1,2,3],
+              [4,5,6],
+              [7,8,9]]
+Output: 25
+Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
+Notice that element mat[1][1] = 5 is counted only once.
+
+ +

Example 2:

+ +
+Input: mat = [[1,1,1,1],
+              [1,1,1,1],
+              [1,1,1,1],
+              [1,1,1,1]]
+Output: 8
+
+ +

Example 3:

+ +
+Input: mat = [[5]]
+Output: 5
+
+ +

 

+

Constraints:

+ +
    +
  • n == mat.length == mat[i].length
  • +
  • 1 <= n <= 100
  • +
  • 1 <= mat[i][j] <= 100
  • +
From 6fd12fd1d0e0b460bf8f99e9b83161bded8cf78f Mon Sep 17 00:00:00 2001 From: achuatchaya Date: Mon, 9 Jun 2025 08:18:24 +0530 Subject: [PATCH 82/82] Time: 0 ms (100.00%) | Memory: 15 MB (53.49%) - LeetSync --- .../matrix-diagonal-sum.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1677-matrix-diagonal-sum/matrix-diagonal-sum.cpp diff --git a/1677-matrix-diagonal-sum/matrix-diagonal-sum.cpp b/1677-matrix-diagonal-sum/matrix-diagonal-sum.cpp new file mode 100644 index 0000000000000..5c4f0f2af9214 --- /dev/null +++ b/1677-matrix-diagonal-sum/matrix-diagonal-sum.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int diagonalSum(vector>& mat) { + int n = mat.size(); + int sum = 0; + + for (int i = 0; i < n; i++) { + + sum = sum+ mat[i][i]; + + int m = n - 1 - i; + if (i != m) { + sum += mat[i][m]; + } + } + + return sum; + } +}; +