Skip to content

Commit ec11a7a

Browse files
committed
Add more BONUS problems
1 parent e2f6899 commit ec11a7a

File tree

13 files changed

+513
-1
lines changed

13 files changed

+513
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.*;
2+
3+
class Play {
4+
public static void main(String[] args) {
5+
// Given array is stored in arr[][]
6+
int[][] arr = {{1, 2, 3, 4}, {1, 2, 3}, {1, 2}, {1}};
7+
8+
int sum = 0;
9+
System.out.println("The given 2D array is: ");
10+
for (int[] row: arr) {
11+
for (int element: row) {
12+
System.out.print(element + " ");
13+
sum += element;
14+
}
15+
System.out.println("");
16+
}
17+
18+
System.out.println("The sum of elements is = " + sum);
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Add All Elements
2+
3+
WAP to add all the elements in the given 2D array
4+
5+
## Solution
6+
7+
### [Java Implementation](./AddAllElements.java)
8+
9+
```java
10+
import java.util.*;
11+
12+
class Play {
13+
public static void main(String[] args) {
14+
// Given array is stored in arr[][]
15+
int[][] arr = {{1, 2, 3, 4}, {1, 2, 3}, {1, 2}, {1}};
16+
17+
int sum = 0;
18+
System.out.println("The given 2D array is: ");
19+
for (int[] row: arr) {
20+
for (int element: row) {
21+
System.out.print(element + " ");
22+
sum += element;
23+
}
24+
System.out.println("");
25+
}
26+
27+
System.out.println("The sum of elements is = " + sum);
28+
}
29+
}
30+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Check Identity Matrix
3+
* @author MadhavBahlMD
4+
* @date 26/01/2019
5+
*/
6+
7+
import java.util.*;
8+
9+
class CheckIdentity {
10+
public static void main(String[] args) {
11+
Scanner input = new Scanner (System.in);
12+
System.out.print("Enter the N (for NxN matrix): ");
13+
int n = input.nextInt();
14+
15+
// Input the array
16+
int[][] arr = new int[n][n];
17+
System.out.println("Enter the elements: ");
18+
for (int i=0; i<n; i++) {
19+
for (int j=0; j<n; j++) {
20+
System.out.print("arr[" + i + "][" + j + "] = ");
21+
arr[i][j] = input.nextInt();
22+
}
23+
}
24+
25+
// Check for identity
26+
for (int i=0; i<n; i++) {
27+
for (int j=0; j<n; j++) {
28+
if (i == j) {
29+
if (arr[i][j] != 1) {
30+
System.out.println("Given matrix is not identity!");
31+
System.exit(0);
32+
}
33+
} else {
34+
if (arr[i][j] != 0) {
35+
System.out.println("Given matrix is not identity!");
36+
System.exit(0);
37+
}
38+
}
39+
}
40+
}
41+
42+
System.out.println("Given matrix is an identity matrix!");
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Check Identity Matrix
2+
3+
WAP to find whether the given NxN matix is an identity matrix
4+
5+
## Solution
6+
7+
### [Java Implementation](./CheckIdentity.java)
8+
9+
```java
10+
/**
11+
* Check Identity Matrix
12+
* @author MadhavBahlMD
13+
* @date 26/01/2019
14+
*/
15+
16+
import java.util.*;
17+
18+
class CheckIdentity {
19+
public static void main(String[] args) {
20+
Scanner input = new Scanner (System.in);
21+
System.out.print("Enter the N (for NxN matrix): ");
22+
int n = input.nextInt();
23+
24+
// Input the array
25+
int[][] arr = new int[n][n];
26+
System.out.println("Enter the elements: ");
27+
for (int i=0; i<n; i++) {
28+
for (int j=0; j<n; j++) {
29+
System.out.print("arr[" + i + "][" + j + "] = ");
30+
arr[i][j] = input.nextInt();
31+
}
32+
}
33+
34+
// Check for identity
35+
for (int i=0; i<n; i++) {
36+
for (int j=0; j<n; j++) {
37+
if (i == j) {
38+
if (arr[i][j] != 1) {
39+
System.out.println("Given matrix is not identity!");
40+
System.exit(0);
41+
}
42+
} else {
43+
if (arr[i][j] != 0) {
44+
System.out.println("Given matrix is not identity!");
45+
System.exit(0);
46+
}
47+
}
48+
}
49+
}
50+
51+
System.out.println("Given matrix is an identity matrix!");
52+
}
53+
}
54+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class LinearSearch {
4+
public static void main(String[] args) {
5+
Scanner input = new Scanner (System.in);
6+
System.out.print("Enter the number of elements in array: ");
7+
int size = input.nextInt();
8+
9+
System.out.println("Enter the array elements: ");
10+
int[] arr = new int[size];
11+
for (int i=0; i<size; i++) {
12+
System.out.print("arr[" + i + "] = ");
13+
arr[i] = input.nextInt();
14+
}
15+
16+
System.out.print("Enter the number to search: ");
17+
int n = input.nextInt();
18+
Boolean found = false; int pos=-1;
19+
20+
for (int i=0; i<size; i++)
21+
if (arr[i] == n) {
22+
pos = i;
23+
found = true;
24+
break;
25+
}
26+
27+
if (found) System.out.println("The given number, " + n + " exists in the array at index: " + pos);
28+
else System.out.println("The given number, " + n + " does not exist in the array");
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Linear Search
2+
3+
WAP to perform linear search on the given array
4+
5+
## Solution
6+
7+
### [Java Implementation](./LinearSearch.java)
8+
9+
```java
10+
import java.util.*;
11+
12+
class LinearSearch {
13+
public static void main(String[] args) {
14+
Scanner input = new Scanner (System.in);
15+
System.out.print("Enter the number of elements in array: ");
16+
int size = input.nextInt();
17+
18+
System.out.println("Enter the array elements: ");
19+
int[] arr = new int[size];
20+
for (int i=0; i<size; i++) {
21+
System.out.print("arr[" + i + "] = ");
22+
arr[i] = input.nextInt();
23+
}
24+
25+
System.out.print("Enter the number to search: ");
26+
int n = input.nextInt();
27+
Boolean found = false; int pos=-1;
28+
29+
for (int i=0; i<size; i++)
30+
if (arr[i] == n) {
31+
pos = i;
32+
found = true;
33+
break;
34+
}
35+
36+
if (found) System.out.println("The given number, " + n + " exists in the array at index: " + pos);
37+
else System.out.println("The given number, " + n + " does not exist in the array");
38+
}
39+
}
40+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Print Matrix Sum
3+
*/
4+
5+
import java.util.*;
6+
7+
class MatrixSum {
8+
public static void main(String[] args) {
9+
Scanner input = new Scanner (System.in);
10+
System.out.print("Enter the number of rows: ");
11+
int row = input.nextInt();
12+
System.out.print("Enter the number of columns: ");
13+
int col = input.nextInt();
14+
15+
// Input the 1st Matrix
16+
int[][] arr1 = new int[row][col];
17+
System.out.println("Enter the elements of first matrix: ");
18+
for (int i=0; i<row; i++) {
19+
for (int j=0; j<col; j++) {
20+
System.out.print("arr1[" + i + "][" + j + "] = ");
21+
arr1[i][j] = input.nextInt();
22+
}
23+
}
24+
25+
// Input the 2nd Matrix
26+
int[][] arr2 = new int[row][col];
27+
System.out.println("Enter the elements of second matrix: ");
28+
for (int i=0; i<row; i++) {
29+
for (int j=0; j<col; j++) {
30+
System.out.print("arr2[" + i + "][" + j + "] = ");
31+
arr2[i][j] = input.nextInt();
32+
}
33+
}
34+
35+
// Print 1st matrix
36+
System.out.println("First Matrix: ");
37+
for (int i=0; i<row; i++) {
38+
for (int j=0; j<col; j++) {
39+
System.out.print(arr1[i][j] + " ");
40+
}
41+
System.out.println("");
42+
}
43+
44+
// Print 2nd matrix
45+
System.out.println("Second Matrix: ");
46+
for (int i=0; i<row; i++) {
47+
for (int j=0; j<col; j++) {
48+
System.out.print(arr2[i][j] + " ");
49+
}
50+
System.out.println("");
51+
}
52+
53+
// Print Transpose matrix
54+
int[][] sum = new int[row][col];
55+
System.out.println("Sum of input matrices: ");
56+
for (int i=0; i<row; i++) {
57+
for (int j=0; j<col; j++) {
58+
sum[i][j] = arr1[i][j] + arr2[i][j];
59+
System.out.print(sum[i][j] + " ");
60+
}
61+
System.out.println("");
62+
}
63+
}
64+
}

BONUS/Arrays/MatrixSum/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Matrix Sum
2+
3+
WAP to add 2 matrices
4+
5+
## Solution
6+
7+
### [Java Implementation](./MatrixSum.java)
8+
9+
```java
10+
/**
11+
* Print Matrix Sum
12+
*/
13+
14+
import java.util.*;
15+
16+
class MatrixSum {
17+
public static void main(String[] args) {
18+
Scanner input = new Scanner (System.in);
19+
System.out.print("Enter the number of rows: ");
20+
int row = input.nextInt();
21+
System.out.print("Enter the number of columns: ");
22+
int col = input.nextInt();
23+
24+
// Input the 1st Matrix
25+
int[][] arr1 = new int[row][col];
26+
System.out.println("Enter the elements of first matrix: ");
27+
for (int i=0; i<row; i++) {
28+
for (int j=0; j<col; j++) {
29+
System.out.print("arr1[" + i + "][" + j + "] = ");
30+
arr1[i][j] = input.nextInt();
31+
}
32+
}
33+
34+
// Input the 2nd Matrix
35+
int[][] arr2 = new int[row][col];
36+
System.out.println("Enter the elements of second matrix: ");
37+
for (int i=0; i<row; i++) {
38+
for (int j=0; j<col; j++) {
39+
System.out.print("arr2[" + i + "][" + j + "] = ");
40+
arr2[i][j] = input.nextInt();
41+
}
42+
}
43+
44+
// Print 1st matrix
45+
System.out.println("First Matrix: ");
46+
for (int i=0; i<row; i++) {
47+
for (int j=0; j<col; j++) {
48+
System.out.print(arr1[i][j] + " ");
49+
}
50+
System.out.println("");
51+
}
52+
53+
// Print 2nd matrix
54+
System.out.println("Second Matrix: ");
55+
for (int i=0; i<row; i++) {
56+
for (int j=0; j<col; j++) {
57+
System.out.print(arr2[i][j] + " ");
58+
}
59+
System.out.println("");
60+
}
61+
62+
// Print Transpose matrix
63+
int[][] sum = new int[row][col];
64+
System.out.println("Sum of input matrices: ");
65+
for (int i=0; i<row; i++) {
66+
for (int j=0; j<col; j++) {
67+
sum[i][j] = arr1[i][j] + arr2[i][j];
68+
System.out.print(sum[i][j] + " ");
69+
}
70+
System.out.println("");
71+
}
72+
}
73+
}
74+
```

BONUS/Arrays/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ Some more problems on arrays!
44

55
## Questions
66

7-
1. [WAP to reverse the given arary](./Reverse/)
7+
1. [WAP to reverse the given arary](./Reverse/)
8+
2. [WAP to perform linear search on the given array](./LinearSearch/)
9+
3. [WAP to find whether the given NxN matix is an identity matrix](./CheckIdentity/)
10+
4. [WAP to print the transpose of the given matrix](./Transpose/)
11+
5. [WAP to add 2 matrices](./MatrixSum/)
12+
6. [WAP to add all the elements in the given 2D array](./AddAllElements/)
13+
7. [WAP to read a sentence and display the sentence with reversed words](./ReverseWords/)

0 commit comments

Comments
 (0)