From a7c8f4c3b62262bed6801d4b2c9c684aeaf74a25 Mon Sep 17 00:00:00 2001 From: Mayank Goel Date: Wed, 3 Nov 2021 23:56:59 +0530 Subject: [PATCH 1/6] Added Spiral Matrix Program --- .../C++/Matrix/Spiral_Matrix.md | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Competitive_Programming/C++/Matrix/Spiral_Matrix.md diff --git a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md new file mode 100644 index 0000000000..7b19fb8daa --- /dev/null +++ b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md @@ -0,0 +1,114 @@ +# Displaying a Matrix in Spiral Form + In this question we will display a matrix in spiral form by traversing through +left-> right then +top-> bottom then +left->right then +bottom-> top and +so on till we reach the center element. +I have given a Example for you to get a better understanding: + +Given a matrix of size r\*c. Traverse the matrix in spiral form. + +## Example: + + *Input*: r = 4, c = 4 + + matrix[][] = {{1, 2, 3, 4}, {5,6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} + + *Output*: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 +*** +*Input*: r = 3, c = 4 + + matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12}} + + *Output*: 1 2 3 4 8 12 11 10 9 5 6 7 + + +## Code +The code is given below: + +``` + #include + using namespace std; + class Solution { + public: //Function wriiten by yellowberard to return a list of integers denoting spiral traversal of matrix. + vector spirallyTraverse(vector \> matrix, int r, int c) + { + vectorans; + int row,col,row_start=0,col_start=0,row_end=r-1,col_end=c-1; + while (row_start<=row_end && col_start<=col_end) + { + for(col=col_start;col<=col_end;col++) //top most row + { + ans.push_back(matrix[row_start][col]); + + } + + row_start++; + for(row=row_start;row<=row_end;row++) //right most column + { + ans.push_back(matrix[row][col_end]); + + } + + col_end--; + if(row_start<=row_end) + { + for(col=col_end;col>=col_start;col--)//bottom most row + { + ans.push_back(matrix[row_end][col]); + } + + } + + row_end--; + if(col_start<=col_end) + { + for(row=row_end;row>=row_start;row--) //left most column + { + ans.push_back(matrix[row][col_start]); + + } + } + col_start++; + + } + return ans ; + } + }; + int main() { int t; cin>>t; + while(t--) + { + int r,c; + cin>>r>>c; + vector > matrix(r); + for(int i=0; i>matrix[i][j]; + } + } + Solution ob; + vector result = ob.spirallyTraverse(matrix, r, c); + for (int i = 0; i < result.size(); ++i) + cout< Date: Mon, 8 Nov 2021 18:37:07 +0530 Subject: [PATCH 2/6] Changes in spiral matrix --- .../C++/Matrix/Spiral_Matrix.md | 111 ++++++++++-------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md index 7b19fb8daa..f22d6d8690 100644 --- a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md +++ b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md @@ -1,9 +1,9 @@ # Displaying a Matrix in Spiral Form - In this question we will display a matrix in spiral form by traversing through -left-> right then -top-> bottom then -left->right then -bottom-> top and + In this question we will display a matrix in spiral form by traversing through +* left-> right then +* top-> bottom then +* left->right then +* bottom-> top and so on till we reach the center element. I have given a Example for you to get a better understanding: @@ -11,72 +11,78 @@ Given a matrix of size r\*c. Traverse the matrix in spiral form. ## Example: - *Input*: r = 4, c = 4 + **Input**: r = 4, c = 4 - matrix[][] = {{1, 2, 3, 4}, {5,6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} + matrix[][] = `{{1, 2, 3, 4}, {5,6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}}` - *Output*: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 + **Output**: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 *** -*Input*: r = 3, c = 4 +**Input**: r = 3, c = 4 - matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12}} + matrix[][] = `{{1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12}}` - *Output*: 1 2 3 4 8 12 11 10 9 5 6 7 + **Output**: 1 2 3 4 8 12 11 10 9 5 6 7 ## Code The code is given below: ``` - #include - using namespace std; - class Solution { - public: //Function wriiten by yellowberard to return a list of integers denoting spiral traversal of matrix. - vector spirallyTraverse(vector \> matrix, int r, int c) + #include + using namespace std; + class Solution { - vectorans; - int row,col,row_start=0,col_start=0,row_end=r-1,col_end=c-1; - while (row_start<=row_end && col_start<=col_end) - { - for(col=col_start;col<=col_end;col++) //top most row - { - ans.push_back(matrix[row_start][col]); + public: //Function wriiten by yellowberard to return a list of integers denoting spiral traversal of matrix. + vector spirallyTraverse(vector > matrix, int r, int c) + { + vectorans; + int row,col,row_start=0,col_start=0,row_end=r-1,col_end=c-1; + while (row_start<=row_end && col_start<=col_end) + { + for(col=col_start;col<=col_end;col++) //top most row + { + ans.push_back(matrix[row_start][col]); - } + } - row_start++; - for(row=row_start;row<=row_end;row++) //right most column - { - ans.push_back(matrix[row][col_end]); + row_start++; + for(row=row_start;row<=row_end;row++) //right most column + { + ans.push_back(matrix[row][col_end]); - } + } - col_end--; - if(row_start<=row_end) - { - for(col=col_end;col>=col_start;col--)//bottom most row - { - ans.push_back(matrix[row_end][col]); - } + col_end--; + if(row_start<=row_end) + { + for(col=col_end;col>=col_start;col--)//bottom most row + { + ans.push_back(matrix[row_end][col]); + } - } + } - row_end--; - if(col_start<=col_end) - { - for(row=row_end;row>=row_start;row--) //left most column - { - ans.push_back(matrix[row][col_start]); + row_end--; + if(col_start<=col_end) + { + for(row=row_end;row>=row_start;row--) //left most column + { + ans.push_back(matrix[row][col_start]); - } - } - col_start++; + } + } + col_start++; - } - return ans ; - } - }; - int main() { int t; cin>>t; + + } + + return ans ; + } + }; + int main() + { + int t; + cin>>t; while(t--) { int r,c; @@ -97,7 +103,8 @@ The code is given below: cout< Date: Tue, 9 Nov 2021 16:02:27 +0530 Subject: [PATCH 3/6] Made changes in code format and input/output --- .../C++/Matrix/Spiral_Matrix.md | 178 +++++++++--------- 1 file changed, 86 insertions(+), 92 deletions(-) diff --git a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md index f22d6d8690..22d18bef77 100644 --- a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md +++ b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md @@ -9,19 +9,6 @@ I have given a Example for you to get a better understanding: Given a matrix of size r\*c. Traverse the matrix in spiral form. -## Example: - - **Input**: r = 4, c = 4 - - matrix[][] = `{{1, 2, 3, 4}, {5,6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}}` - - **Output**: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 -*** -**Input**: r = 3, c = 4 - - matrix[][] = `{{1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12}}` - - **Output**: 1 2 3 4 8 12 11 10 9 5 6 7 ## Code @@ -29,83 +16,82 @@ The code is given below: ``` #include - using namespace std; - class Solution - { - public: //Function wriiten by yellowberard to return a list of integers denoting spiral traversal of matrix. - vector spirallyTraverse(vector > matrix, int r, int c) - { - vectorans; - int row,col,row_start=0,col_start=0,row_end=r-1,col_end=c-1; - while (row_start<=row_end && col_start<=col_end) - { - for(col=col_start;col<=col_end;col++) //top most row - { - ans.push_back(matrix[row_start][col]); - - } - - row_start++; - for(row=row_start;row<=row_end;row++) //right most column - { - ans.push_back(matrix[row][col_end]); - - } - - col_end--; - if(row_start<=row_end) - { - for(col=col_end;col>=col_start;col--)//bottom most row - { - ans.push_back(matrix[row_end][col]); - } - - } - - row_end--; - if(col_start<=col_end) - { - for(row=row_end;row>=row_start;row--) //left most column - { - ans.push_back(matrix[row][col_start]); - - } - } - col_start++; - - - } - - return ans ; - } - }; - int main() - { - int t; - cin>>t; - while(t--) - { - int r,c; - cin>>r>>c; - vector > matrix(r); - for(int i=0; i>matrix[i][j]; - } - } - Solution ob; - vector result = ob.spirallyTraverse(matrix, r, c); - for (int i = 0; i < result.size(); ++i) - cout< spirallyTraverse(vector> matrix, int r, int c) + { + vector ans; + int row, col, row_start = 0, col_start = 0, row_end = r - 1, col_end = c - 1; + while (row_start <= row_end && col_start <= col_end) + { + for (col = col_start; col <= col_end; col++) //top most row + { + ans.push_back(matrix[row_start][col]); + + } + + row_start++; + for (row = row_start; row <= row_end; row++) //right most column + { + ans.push_back(matrix[row][col_end]); + + } + + col_end--; + if (row_start <= row_end) + { + for (col = col_end; col >= col_start; col--) //bottom most row + { + ans.push_back(matrix[row_end][col]); + } + } + row_end--; + if (col_start <= col_end) + { + for (row = row_end; row >= row_start; row--) //left most column + { + ans.push_back(matrix[row][col_start]); + + } + } + + col_start++; + + } + + return ans; + } +}; +int main() +{ + int t; + cin >> t; + while (t--) + { + int r, c; + cin >> r >> c; + vector> matrix(r); + for (int i = 0; i < r; i++) + { + matrix[i].assign(c, 0); + for (int j = 0; j < c; j++) + { + cin >> matrix[i][j]; + } + } + + Solution ob; + vector result = ob.spirallyTraverse(matrix, r, c); + for (int i = 0; i < result.size(); ++i) + cout << result[i] << " "; + cout << endl; + } + + return (0); +} ``` ## Time Complexity @@ -114,8 +100,16 @@ We can explain time complexity by as the loop will keep till total elements of a **Time Complexity: O(r*c)** **Space Complexity: O(r*c)** -## Acknowledgements +## Input/Output: - - [Question link](https://practice.geeksforgeeks.org/problems/spirally-traversing-a-matrix-1587115621/1#) - - Notes I made while studying + **Input**: r = 4, c = 4 + + matrix[][] = `{{1, 2, 3, 4}, {5,6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}}` + + **Output**: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 +*** +**Input**: r = 3, c = 4 + matrix[][] = `{{1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12}}` + + **Output**: 1 2 3 4 8 12 11 10 9 5 6 7 From 0908619fe98afcc246e478117a96c15e260a714f Mon Sep 17 00:00:00 2001 From: Mayank Goel <82977727+yellowberard@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:32:52 +0530 Subject: [PATCH 4/6] Update Spiral_Matrix.md --- Competitive_Programming/C++/Matrix/Spiral_Matrix.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md index 22d18bef77..ce5f8790e0 100644 --- a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md +++ b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md @@ -9,6 +9,14 @@ I have given a Example for you to get a better understanding: Given a matrix of size r\*c. Traverse the matrix in spiral form. +## Explaination +* The algorithm starts from the top left corner of the array, and traverses the first row from left to right and at the end it increments the top corner index. +* It traverses the right most column top to bottom and at the end it decrements the right corner index. +* It traverses the bottom most row and the end it decrements the bottom corner index afterward. +* It traverses the left most column and at he end it increments the left corner index once it’s done. + +The above steps continues till hte left index > the right index and the top index > the bottom index. + ## Code From 902549960ff14f4cf3efe67adac85ed6c64a72c9 Mon Sep 17 00:00:00 2001 From: Mayank Goel <82977727+yellowberard@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:36:05 +0530 Subject: [PATCH 5/6] Update Spiral_Matrix.md --- Competitive_Programming/C++/Matrix/Spiral_Matrix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md index ce5f8790e0..e35e93e405 100644 --- a/Competitive_Programming/C++/Matrix/Spiral_Matrix.md +++ b/Competitive_Programming/C++/Matrix/Spiral_Matrix.md @@ -23,7 +23,7 @@ The above steps continues till hte left index > the right index and the top inde The code is given below: ``` - #include +#include using namespace std; class Solution { From 085966b1609fa947ab7bc50c0fc3b080470ebd10 Mon Sep 17 00:00:00 2001 From: Mayank Goel <82977727+yellowberard@users.noreply.github.com> Date: Sun, 28 Nov 2021 16:41:34 +0530 Subject: [PATCH 6/6] Create longest_sub_without_repeat.md --- .../C++/Array/longest_sub_without_repeat.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Competitive_Programming/C++/Array/longest_sub_without_repeat.md diff --git a/Competitive_Programming/C++/Array/longest_sub_without_repeat.md b/Competitive_Programming/C++/Array/longest_sub_without_repeat.md new file mode 100644 index 0000000000..e5000eabb4 --- /dev/null +++ b/Competitive_Programming/C++/Array/longest_sub_without_repeat.md @@ -0,0 +1,60 @@ +# Length of the Longest Substring without Repeating Characters + +Here we will see a program to find the length of Longest substring without repeating characters. + +## Explanation +In this program we are using the concept of Window sliding i.e. Whenever we see repetition, we remove the previous occurrence and slide the window. +A loop is runniig from i=0 to n=sizeof(str). +In side this loop one more loop is running from j=i till n. +We define vector with all the value as false and keep it makin true for the characters that are visited once in the loop. + +## Code +``` cpp +#include +using namespace std; + +int Substtr(string str) +{ + int n = str.size(); + int result = 0; + + for (int i = 0; i < n; i++) { + + vector visited(256); + + for (int j = i; j < n; j++) { + + if (visited[str[j]] == true) // If current character is visited + break; // Break the loop + + else { // Else update the result if + res = max(res, j - i + 1); // this window is larger, and mark + visited[str[j]] = true; // current character as visited. + } + } + + visited[str[i]] = false; // Remove the first character of previous window + } + return result; +} + +int main() +{ + string str = "girlscriptwinterofcontributing"; + cout << "The string used is " << str << endl; + int length =Substtr(str); + cout << "The length of the longest non-repeating " + "character substring is " + << length; + return 0; +} + +``` +## Time and Space Complexity +**Time Complexity**: O(n^2) +**Space Complexity**:O(n) + +## Input/Output +**Input**: s = "abcabcbb" +**Output**: 3 +**Explanation**: The answer is "abc", with the length of 3.