Skip to content

Commit 94e6fab

Browse files
main
1 parent 0177649 commit 94e6fab

19 files changed

+889
-1
lines changed

3.Array/LeetCode/program57.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/*
3+
2614. Prime In Diagonal
4+
Easy
5+
203
6+
22
7+
Companies
8+
You are given a 0-indexed two-dimensional integer array nums.
9+
10+
Return the largest prime number that lies on at least one of the diagonals of nums. In case, no prime is present on any of the diagonals, return 0.
11+
12+
Note that:
13+
14+
An integer is prime if it is greater than 1 and has no positive integer divisors other than 1 and itself.
15+
An integer val is on one of the diagonals of nums if there exists an integer i for which nums[i][i] = val or an i for which nums[i][nums.length - i - 1] = val.
16+
17+
18+
In the above diagram, one diagonal is [1,5,9] and another diagonal is [3,5,7].
19+
20+
Example 1:
21+
Input: nums = [[1,2,3],[5,6,7],[9,10,11]]
22+
Output: 11
23+
Explanation: The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
24+
25+
Example 2:
26+
Input: nums = [[1,2,3],[5,17,7],[9,11,10]]
27+
Output: 17
28+
Explanation: The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
29+
30+
*/
31+
32+
33+
34+
package LeetCode;
35+
36+
import java.util.Vector;
37+
38+
public class program57 {
39+
40+
static boolean Prime(int num){
41+
42+
if(num<=1){
43+
return false;
44+
}
45+
46+
for(int i=2;i<=(int)Math.sqrt(num);i++){
47+
48+
if(num % i==0){
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
57+
static int diagonalPrime(int[][] nums) {
58+
59+
int maximum =0;
60+
for(int i=0;i<nums.length;i++){
61+
62+
if(Prime(nums[i][i])){
63+
maximum = Math.max(maximum, nums[i][i]);
64+
}
65+
66+
if(Prime(nums[i][nums.length-i-1])){
67+
maximum = Math.max(maximum, nums[i][i]);
68+
}
69+
}
70+
71+
72+
73+
return maximum;
74+
75+
}
76+
public static void main(String[] args) {
77+
78+
int[][] nums = {{1,2,3},{5,6,7},{9,10,11}};
79+
80+
System.out.println(diagonalPrime(nums));
81+
82+
83+
}
84+
85+
}

3.Array/LeetCode/program58.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
/*
3+
1. Two Sum
4+
Easy
5+
46.1K
6+
1.5K
7+
Companies
8+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
9+
10+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
11+
12+
You can return the answer in any order.
13+
14+
15+
16+
Example 1:
17+
Input: nums = [2,7,11,15], target = 9
18+
Output: [0,1]
19+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
20+
21+
22+
Example 2:
23+
Input: nums = [3,2,4], target = 6
24+
Output: [1,2]
25+
26+
Example 3:
27+
Input: nums = [3,3], target = 6
28+
Output: [0,1]
29+
30+
*/
31+
32+
package LeetCode;
33+
34+
import java.util.HashMap;
35+
36+
public class program58 {
37+
38+
static int[] twoSum(int[] nums, int target) {
39+
40+
int[] ans = new int[2];
41+
ans[0] = ans[1] = -1;
42+
HashMap<Integer, Integer> mpp = new HashMap<>();
43+
44+
for (int i = 0; i < nums.length; i++) {
45+
int num = nums[i];
46+
int Needed = target - num;
47+
if (mpp.containsKey(Needed)) {
48+
ans[0] = mpp.get(Needed);
49+
ans[1] = i;
50+
return ans;
51+
}
52+
53+
mpp.put(nums[i], i);
54+
}
55+
return ans;
56+
}
57+
58+
public static void main(String args[]) {
59+
60+
int[] nums = { 2, 7, 11, 15 };
61+
int target = 9;
62+
63+
int[] a = twoSum(nums, target);
64+
65+
for (int i = 0; i < a.length; i++) {
66+
System.out.println(a[i]);
67+
}
68+
}
69+
70+
}

3.Array/LeetCode/program9.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static int[][] mergeArrays(int[][] nums1, int[][] nums2) {
5555

5656
continue;
5757

58-
5958
}
6059
}
6160

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package SwitchCase.ClassCodes;
2+
3+
4+
public class program1 {
5+
public static void main(String[] args) {
6+
7+
int x =2;
8+
9+
switch (x) {
10+
case 1:
11+
System.out.println("One");
12+
break;
13+
14+
case 2:
15+
System.out.println("Two");
16+
17+
case 3:
18+
System.out.println("three");
19+
20+
case 4:
21+
System.out.println("Four");
22+
23+
default:
24+
System.out.println("a");
25+
break;
26+
}
27+
}
28+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package SwitchCase.ClassCodes;
2+
3+
public class program10 {
4+
5+
public static void main(String[] args) {
6+
int ch = 65;
7+
8+
switch (ch) {
9+
10+
case 1:
11+
System.out.println("One");
12+
break;
13+
14+
case 'A':
15+
System.out.println("65");
16+
break;
17+
18+
case 65:
19+
System.out.println("Second-65");
20+
break;
21+
22+
case 'B':
23+
System.out.println("B-65");
24+
break;
25+
26+
case 66:
27+
System.out.println("66");
28+
break;
29+
30+
31+
default 65:
32+
System.out.println("No Match Found...!");
33+
34+
}
35+
36+
System.out.println("After switch");
37+
}
38+
}
39+
40+
/*
41+
program10.java:18: error: : or -> expected
42+
default 65:
43+
^
44+
program10.java:18: error: not a statement
45+
default 65:
46+
^
47+
program10.java:18: error: ';' expected
48+
default 65:
49+
^
50+
3 errors
51+
error: compilation failed
52+
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package SwitchCase.ClassCodes;
2+
3+
public class program11 {
4+
public static void main(String[] args) {
5+
6+
int x = 7;
7+
8+
int a = 1;
9+
int b = 2;
10+
switch (x) {
11+
12+
case a:
13+
System.out.println("One");
14+
break;
15+
16+
case b:
17+
System.out.println("2");
18+
break;
19+
20+
case a + b:
21+
System.out.println(3);
22+
break;
23+
24+
case b + b:
25+
System.out.println("4");
26+
break;
27+
28+
case a + b + b:
29+
System.out.println("5");
30+
break;
31+
32+
default:
33+
System.out.println("No Match Found...!");
34+
break;
35+
36+
}
37+
38+
System.out.println("After switch");
39+
}
40+
}
41+
42+
43+
/*
44+
45+
program11.java:12: error: constant expression required
46+
case a:
47+
^
48+
program11.java:16: error: constant expression required
49+
case b:
50+
^
51+
program11.java:20: error: constant expression required
52+
case a + b:
53+
^
54+
program11.java:24: error: constant expression required
55+
case b + b:
56+
^
57+
program11.java:28: error: constant expression required
58+
case a + b + b:
59+
^
60+
5 errors
61+
error: compilation failed
62+
63+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package SwitchCase.ClassCodes;
2+
3+
public class program12 {
4+
public static void main(String[] args) {
5+
6+
String str = "Mon";
7+
8+
switch (str) {
9+
case "MON":
10+
System.out.println("MONDAY");
11+
break;
12+
13+
case "Mon":
14+
System.out.println("Monday");
15+
break;
16+
17+
case "mon":
18+
System.out.println("monday");
19+
break;
20+
21+
default:
22+
System.out.println("No Match Found....!");
23+
break;
24+
}
25+
}
26+
}
27+
28+
29+
/*
30+
31+
Before java 1.6 it is not working it gives an error but after that 1.7 they added.
32+
33+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package SwitchCase.ClassCodes;
2+
3+
public class program2 {
4+
public static void main(String[] args) {
5+
6+
int x = 10;
7+
8+
switch (x) {
9+
10+
case 1:
11+
System.out.println("One");
12+
13+
case 2:
14+
System.out.println("Two");
15+
16+
case 3:
17+
System.out.println("Three");
18+
19+
case 4:
20+
System.out.println("Four");
21+
22+
case 5:
23+
System.out.println("Five");
24+
25+
}
26+
27+
System.out.println("After switch");
28+
}
29+
}

0 commit comments

Comments
 (0)