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:

+ + 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 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:

+ + 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 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:

+ + \ No newline at end of file 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 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

+ + + +

 

+

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:

+ + 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 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
  • +
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;iMatrix 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
  • +
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; + } +}; + 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
  • +
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 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.
  • +
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 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.
  • +
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 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
  • +
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 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
  • +
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 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:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • 1 <= nums[i], original <= 1000
  • +
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 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
  • +
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; + } +}; 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 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 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
  • +
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 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
  • +
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;iAlternating 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
  • +
+ +

 

+ 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; iRow 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.
  • +
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}; + } +}; 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
  • +
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 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
  • +
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; iPower 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 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 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.
  • +
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 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 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 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.
  • +
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 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
  • +
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 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/

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 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
  • +
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; + } +}; 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
  • +
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 || rToeplitz 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?
  • +
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;iPalindrome 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 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 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
  • +
diff --git a/932-monotonic-array/monotonic-array.cpp b/932-monotonic-array/monotonic-array.cpp new file mode 100644 index 0000000000000..b72eaa4049048 --- /dev/null +++ b/932-monotonic-array/monotonic-array.cpp @@ -0,0 +1,22 @@ +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; + } +}; + + + + +