Skip to content

Commit a15ec80

Browse files
committed
Coding Ninjas java Code
1 parent e26d663 commit a15ec80

File tree

237 files changed

+9985
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+9985
-0
lines changed

2D Arrays/LargestRowOrColumn.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
public static int[][] takeInput()
5+
{
6+
7+
Scanner sc = new Scanner(System.in);
8+
//System.out.println("Enter R: ");
9+
int r = sc.nextInt();
10+
//System.out.println("Enter C: ");
11+
int c = sc.nextInt();
12+
13+
if(r==0 & c==0)
14+
{
15+
System.out.println("row 0 -2147483648");
16+
17+
}
18+
else if(r>0 && c>0){
19+
int arr[][]=new int[r][c];
20+
21+
for(int i = 0;i<r;i++)
22+
{
23+
for(int j = 0;j<c;j++)
24+
{
25+
//System.out.println("Enter "+i+"th row "+j+"th element");
26+
arr[i][j]=sc.nextInt();
27+
}
28+
}
29+
}
30+
return arr;
31+
}
32+
33+
public static void findLargest(int mat[][]){
34+
//Your code goes here
35+
int maxc= Integer.MIN_VALUE;
36+
int ci = 0;
37+
38+
int r = mat.length;
39+
int c = mat[0].length;
40+
41+
for(int j=0;j<c;j++)
42+
{ int sum = 0;
43+
for(int i = 0;i<r;i++)
44+
{
45+
sum+=mat[i][j];
46+
}
47+
if(sum>maxc)
48+
{
49+
maxc = sum;
50+
ci = j;
51+
52+
}
53+
54+
}
55+
56+
int maxr = Integer.MIN_VALUE;
57+
int ri = 0;
58+
for(int i = 0;i<r;i++)
59+
{ int sum = 0;
60+
for(int j = 0;j<c;j++)
61+
{
62+
sum+=mat[i][j];
63+
}
64+
if(sum>maxr)
65+
{
66+
maxr=sum;
67+
ri=i;
68+
}
69+
}
70+
71+
if(maxr>=maxc)
72+
{
73+
System.out.println("row "+ri+" "+maxr);
74+
}
75+
else
76+
{
77+
System.out.println("column "+ci+" "+maxc);
78+
}
79+
}
80+
81+
}

2D Arrays/PrintLikeAWave.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
public class Solution {
3+
4+
public static void wavePrint(int mat[][]){
5+
//Your code goes here
6+
7+
//the case when row = 0 and column = 0
8+
if(mat.length == 0){
9+
return;
10+
}
11+
int r = mat.length;
12+
int c = mat[0].length;
13+
14+
if(r==0 && c==0)
15+
{
16+
System.out.print(" ");
17+
18+
}
19+
else
20+
{
21+
22+
for(int j=0;j<c;j++)
23+
{
24+
25+
if(j%2==0)
26+
{ int i=0;
27+
while(i<r)
28+
{
29+
System.out.print(mat[i][j]+" ");
30+
i++;
31+
}
32+
}
33+
else
34+
{
35+
int i = r-1;
36+
while(i>=0)
37+
{
38+
System.out.print(mat[i][j]+" ");
39+
i--;
40+
}
41+
}
42+
43+
}
44+
}
45+
}
46+
}

2D Arrays/PrintSpiral.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
public class Solution {
3+
4+
public static void spiralPrint(int matrix[][]){
5+
//Your code goes here
6+
7+
if(matrix.length == 0)
8+
{
9+
return;
10+
}
11+
int rs = 0;
12+
int re = matrix.length-1;
13+
int cs = 0;
14+
int ce = matrix[0].length - 1;
15+
int n = matrix.length;
16+
int m = matrix[0].length;
17+
int count = 1;
18+
19+
while(count<=(n*m))
20+
{
21+
int i = rs;
22+
int j = cs;
23+
while(j<=ce)
24+
{
25+
System.out.print(matrix[rs][j]+" "); //1 2 3 4
26+
j++;
27+
count++;
28+
}
29+
rs++;
30+
i++;
31+
while(i<=re)
32+
{
33+
System.out.print(matrix[i][ce]+" "); // 1 2 3 4 5 6 7 8
34+
i++;
35+
count++;
36+
37+
}
38+
ce--;
39+
int k = ce;
40+
while(k>=cs)
41+
{
42+
System.out.print(matrix[re][k]+" ");// 1 2 3 4 5 6 7 8 9 10 11
43+
k--;
44+
count++;
45+
}
46+
47+
re--;
48+
int l = re;
49+
while(l>=rs)
50+
{
51+
System.out.print(matrix[l][cs]+" "); // 1 2 3 4 5 6 7 8 9 10 11 12 13 14
52+
l--;
53+
count++;
54+
}
55+
cs++;
56+
57+
}
58+
}
59+
}
60+
61+
62+

2D Arrays/RowWiseSum.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
public static void rowWiseSum(int[][] mat) {
5+
int row = mat.length;
6+
int col = mat[0].length;
7+
8+
int sum=0;
9+
for(int i = 0;i<row;i++)
10+
{
11+
for(int j = 0;j<col;j++)
12+
{
13+
sum+=mat[i][j];
14+
}
15+
System.out.print(sum+" ");
16+
sum = 0;
17+
}
18+
19+
}
20+
public static void main(String[] args) {
21+
// TODO Auto-generated method stub
22+
Scanner sc = new Scanner(System.in);
23+
int r = sc.nextInt();
24+
int c = sc.nextInt();
25+
int arr[][] = new int[r][c];
26+
rowWiseSum(arr);
27+
28+
}
29+
30+
}

2D Arrays/TotalSum.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import java.util.Scanner;
2+
import java.util.Arrays;
3+
public class Solution {
4+
5+
6+
public static void totalSum(int[][] mat) {
7+
//Your code goes here
8+
int r = mat.length;
9+
int c = mat[0].length;
10+
11+
int all[] = new int[40]; //arraylist
12+
int sum = 0;
13+
int k =0;
14+
//row boundaries
15+
for(int i = 0;i<1;i++)
16+
{
17+
for(int j = 0;j<c;j++)
18+
{
19+
20+
all[k]=mat[i][j];
21+
k++;
22+
}
23+
}
24+
25+
for(int i = r-1;i<r;i++)
26+
{
27+
for(int j = 0;j<c;j++)
28+
{
29+
30+
all[k]=mat[i][j];
31+
k++;
32+
}
33+
}
34+
//column boundaries
35+
for(int j=0;j<1;j++)
36+
{
37+
for(int i = 0; i<r;i++)
38+
{
39+
all[k]=mat[i][j];
40+
k++;
41+
}
42+
}
43+
44+
for(int j=c-1;j<c;j++)
45+
{
46+
for(int i = 0; i<r;i++)
47+
{
48+
all[k]=mat[i][j];
49+
k++;
50+
}
51+
}
52+
//diagonals
53+
for(int i = 0;i<r;i++)
54+
{
55+
for(int j = 0;j<c;j++)
56+
{
57+
if(i==j)
58+
{
59+
all[k]=mat[i][j];
60+
k++;
61+
}
62+
}
63+
}
64+
65+
for(int j = c-1;j>=0;j--)
66+
{
67+
all[k]=mat[r-j-1][j];
68+
k++;
69+
}
70+
//removing duplicates from array
71+
Arrays.sort(all);
72+
73+
// for(int i = 0;i<all.length;i++)
74+
// {
75+
// System.out.print(all[i]+" ");
76+
// }
77+
int n = all.length;
78+
int zeroi = 0;
79+
80+
for(int i = 0;i<n;i++)
81+
{
82+
if(all[i]!=0)
83+
{
84+
zeroi=i;
85+
break;
86+
}
87+
}
88+
//System.out.println(zeroi);
89+
90+
sum=all[zeroi];
91+
for(int i=zeroi+1;i<n;i++){
92+
if(all[i]==all[i-1]){
93+
continue;
94+
}
95+
else
96+
{
97+
sum+=all[i];
98+
}
99+
100+
}
101+
System.out.println(sum);
102+
103+
104+
}
105+
106+
}
107+

Arrays-1/ArrangeNumbersInJava.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
public class Solution {
3+
4+
public static int[] arrange(int n) {
5+
6+
int[] arr=new int[n];
7+
int left=0,right=n-1,counter=1;
8+
while(left<=right) {
9+
if(counter%2==1) {
10+
arr[left++]=counter;
11+
}else {
12+
arr[right--]=counter;
13+
}
14+
counter++;
15+
}
16+
17+
18+
return arr;
19+
20+
21+
}
22+
23+
public static void main(String[] args) {
24+
// TODO Auto-generated method stub
25+
int[] arr=arrange(11);
26+
for(int i : arr) {
27+
System.out.print(i+" ");
28+
}
29+
}
30+
31+
}

Arrays-1/ArrayIntersection.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
public class Solution{
3+
4+
public static void intersections(int arr1[], int arr2[]) {
5+
6+
int n1=arr1.length;
7+
int n2=arr2.length;
8+
9+
10+
11+
for(int i = 0;i<n1;i++)
12+
{
13+
for(int j=0;j<n2;j++)
14+
{
15+
if(arr1[i]==arr2[j])
16+
{
17+
System.out.print(arr1[i]+" ");
18+
arr2[j]=Integer.MAX_VALUE;
19+
break;
20+
}
21+
}
22+
}
23+
24+
}
25+
26+
public static void main(String[] args) {
27+
// TODO Auto-generated method stub
28+
int arr1[] = {2,6,1,2};
29+
int arr2[] = {1,2,3,4,2};
30+
intersections(arr1,arr2); // output 2 1 2
31+
}
32+
33+
}

0 commit comments

Comments
 (0)