Skip to content

Commit

Permalink
POTD 1-Sep-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaySharmaGitHub committed Sep 1, 2024
1 parent c177939 commit 725a5ec
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
2022. Convert 1D Array Into 2D Array
You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
Example 1:
Input: original = [1,2,3,4], m = 2, n = 2
Output: [[1,2],[3,4]]
Explanation: The constructed 2D array should contain 2 rows and 2 columns.
The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
Example 2:
Input: original = [1,2,3], m = 1, n = 3
Output: [[1,2,3]]
Explanation: The constructed 2D array should contain 1 row and 3 columns.
Put all three elements in original into the first row of the constructed 2D array.
Example 3:
Input: original = [1,2], m = 1, n = 1
Output: []
Explanation: There are 2 elements in original.
It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
Constraints:
1 <= original.length <= 5 * 104
1 <= original[i] <= 105
1 <= m, n <= 4 * 104
*/
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {
if(m*n != original.size()) return {};
vector<vector<int>> vec(m,vector<int>(n));
int k = 0 ;
for(int i = 0 ; i<m && k<original.size();i++)
for(int j=0;j<n && k<original.size();j++) vec[i][j] = original[k++];
return vec;
}
};
54 changes: 54 additions & 0 deletions GFG/Medium Problems/Max_sum_path_in_two_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Max sum path in two arrays
Given two sorted arrays of distinct integers arr1 and arr2. Each array may have some elements in common with the other array. Find the maximum sum of a path from the beginning of any array to the end of any array. You can switch from one array to another array only at the common elements.
Note: When we switch from one array to other, we need to consider the common element only once in the result.
Examples :
Input: arr1 = [2, 3, 7, 10, 12] , arr2 = [1, 5, 7, 8]
Output: 35
Explanation: The path will be 1+5+7+10+12 = 35, where 1 and 5 come from arr2 and then 7 is common so we switch to arr1 and add 10 and 12.
Input: arr1 = [1, 2, 3] , arr2[] = [3, 4, 5]
Output: 15
Explanation: The path will be 1+2+3+4+5=15.
Expected Time Complexity: O(m + n)
Expected Auxiliary Space: O(1)
Constraints:
1 <= arr1.size(), arr2.size() <= 104
1 <= arr1[i], arr2[i] <= 105
*/
class Solution {
public:
int maxPathSum(vector<int> &arr1, vector<int> &arr2) {
// Code here
int i=0;
int j=0;
int ans=0;
int sum1=0;
int sum2=0;
while(i<arr1.size() && j<arr2.size()){
if(arr1[i]>arr2[j]){
sum2+=arr2[j];
j++;
}else if(arr1[i]<arr2[j]){
sum1+=arr1[i];
i++;
}else if(arr1[i]==arr2[j]){
ans+=max(sum1,sum2)+arr1[i];
sum1=0;
sum2=0;
i++;
j++;
}
}
// ans+=max(sum1,sum2);
while(i<arr1.size()){
sum1+=arr1[i];
i++;
}
while(j<arr2.size()){
sum2+=arr2[j];
j++;
}
ans+=max(sum1,sum2);
return ans;
}
};
37 changes: 37 additions & 0 deletions Leet Code Problems/Easy/2022_Convert_1D_Array_Into_2D_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
2022. Convert 1D Array Into 2D Array
You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
Example 1:
Input: original = [1,2,3,4], m = 2, n = 2
Output: [[1,2],[3,4]]
Explanation: The constructed 2D array should contain 2 rows and 2 columns.
The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
Example 2:
Input: original = [1,2,3], m = 1, n = 3
Output: [[1,2,3]]
Explanation: The constructed 2D array should contain 1 row and 3 columns.
Put all three elements in original into the first row of the constructed 2D array.
Example 3:
Input: original = [1,2], m = 1, n = 1
Output: []
Explanation: There are 2 elements in original.
It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
Constraints:
1 <= original.length <= 5 * 104
1 <= original[i] <= 105
1 <= m, n <= 4 * 104
*/
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {
if(m*n != original.size()) return {};
vector<vector<int>> vec(m,vector<int>(n));
int k = 0 ;
for(int i = 0 ; i<m && k<original.size();i++)
for(int j=0;j<n && k<original.size();j++) vec[i][j] = original[k++];
return vec;
}
};

0 comments on commit 725a5ec

Please sign in to comment.