Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array-1 Completed #1636

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Array-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : o(n)
// Space Complexity : o(1) for answer array
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : We calculate by taking product of running product
// with previous element and similary we do the same from right to left and will update the answer
// array from 1st iteration to the second iteration and return the array


public class problem1 {
public int[] productExceptSelf(int[] nums) {
int [] answer= new int[nums.length];
int rp=1;
answer[0]=rp;
for (int i=1; i<nums.length; i++){
rp=rp*nums[i-1];
answer[i]=rp;
}
rp=1;
for (int i=nums.length-2; i>=0; i--){
rp=rp*nums[i];
answer[i]*=rp;
}
return answer;
}
}
62 changes: 62 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Time Complexity : o(m*n)
// Space Complexity : o(1) for answer array; constant space O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english : We calculate by checking out the change in row, column and direction
// while moving from one cell to another; analyzing this we form the if and else logic for row and column variables in
// the negative and positive directions




public class problem2 {

public int[] findDiagnolOrder(int[][] mat)
{
int m=mat.length;
int n=mat[0].length;

int [] res= new int[m*n];
boolean dir= true;
int r=0, c=0;

for (int i=0; i<m*n; i++)
{
res[i]= mat[r][c];

if (dir)
{
if (r==0 && c!=m-1)
{
c++;
dir= false;
}
else if (r==n-1)
{
r++;
dir= false;
}
else {
r--;
c++;
}
}
else {
if (c==0 && r!=m-1)
{
r++;
dir= true;
} else if (r==m-1) {
c++;
dir= true;
}
else {
c--;
r++;
}
}
}
return res;
}


}
50 changes: 50 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time Complexity : o(n)
// Space Complexity : o(1) for answer array
// Did this code successfully run on Leetcode : Yes
// We solve by 4 pointers approach move the pointers based on the base conditions..

import java.util.ArrayList;
import java.util.List;

public class problem3 {

public List<Integer> spiralOrder(int[][] matrix) {

List<Integer> res= new ArrayList<>();

if(matrix.length == 0 || matrix[0].length == 0)
{
return res;
}
int left= 0, right= matrix[0].length-1, top=0, bottom= matrix.length;
while (left <= right && top <= bottom)
{
for (int i = left; i <= right; i++)
{
res.add(matrix[top][i]);
}
top++;

for (int i = top; i <= bottom; i++)
{
res.add(matrix[i][right]);
}
right--;
if (top<=bottom) {
for (int i = right; i >= left; i--) {
res.add(matrix[bottom][i]);
}
bottom--;
}
if (left<=right) {
for (int i = bottom; i >= top; i--) {
res.add(matrix[i][left]);
}
left++;
}

}
return res;

}
}