-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8be25c7
commit f2e9412
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
class GFG | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int T = Integer.parseInt(br.readLine().trim()); | ||
while(T-->0) | ||
{ | ||
int n = Integer.parseInt(br.readLine().trim()); | ||
int[][] mat = new int[n][n]; | ||
String[] S = br.readLine().trim().split(" "); | ||
int i = 0; | ||
int j = 0; | ||
for(int k = 0; k < S.length; k++){ | ||
mat[i][j] = Integer.parseInt(S[k]); | ||
j++; | ||
if(j == n){ | ||
i++; | ||
j = 0; | ||
} | ||
} | ||
Solution obj = new Solution(); | ||
int[] ans = obj.matrixDiagonally(mat); | ||
for(int it = 0; it < ans.length; it++){ | ||
System.out.print(ans[it] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public int[] matrixDiagonally(int[][] mat) | ||
{ | ||
int n = mat.length, mode = 0, it = 0, lower = 0; | ||
ArrayList<Integer> arr = new ArrayList<>(); | ||
|
||
for (int t = 0; t < (2 * n - 1); t++) | ||
{ | ||
int t1 = t; | ||
if (t1 >= n) | ||
{ | ||
mode++; | ||
t1 = n - 1; | ||
it--; | ||
lower++; | ||
} | ||
else | ||
{ | ||
lower = 0; | ||
it++; | ||
} | ||
|
||
for (int i = t1; i >= lower; i--) | ||
{ | ||
if ((t1 + mode) % 2 == 0) | ||
{ | ||
arr.add(mat[i][t1 + lower - i]); | ||
} | ||
else | ||
{ | ||
arr.add(mat[t1 + lower - i][i]); | ||
} | ||
} | ||
} | ||
|
||
return arr.stream().mapToInt(i -> i).toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(1) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution | ||
{ | ||
public int pivotInteger(int n) | ||
{ | ||
int y = (n * n + n) / 2; | ||
int x = (int) Math.sqrt(y); | ||
return x * x == y ? x : -1; | ||
} | ||
} |