Skip to content

Commit 41eb090

Browse files
committed
arrays fundamental questions from tuf
1 parent bb79c42 commit 41eb090

11 files changed

+198
-0
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsa-2025.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

src/Main.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
System.out.println("Hello, World!");
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package arrays.fundamentals;
2+
3+
public class LargestElement {
4+
public static int largestElement(int[] nums) {
5+
int largestElement = nums[0];
6+
7+
for (int num : nums) {
8+
largestElement = Math.max(largestElement, num);
9+
}
10+
11+
return largestElement;
12+
}
13+
14+
public static void main(String[] args) {
15+
int[] nums = {3, 3, 6, 1};
16+
17+
System.out.println(largestElement(nums));
18+
}
19+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package arrays.fundamentals;
2+
3+
public class LinearSearch {
4+
public static int linearSearch(int[] nums, int target) {
5+
for (int i = 0; i < nums.length; i++) {
6+
if (nums[i] == target) {
7+
return i;
8+
}
9+
}
10+
11+
return -1;
12+
}
13+
14+
public static void main(String[] args) {
15+
int[] nums = {2, 3, 4, 5, 3};
16+
int target = 3;
17+
18+
System.out.println(linearSearch(nums, target));
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package arrays.fundamentals;
2+
3+
public class MaxConsecutiveOnes {
4+
public static int findMaxConsecutiveOnes(int[] nums) {
5+
int maxCount = 0;
6+
int count = 0;
7+
8+
for (int i = 0; i < nums.length; i++) {
9+
if (nums[i] == 1) {
10+
count += 1;
11+
maxCount = Math.max(maxCount, count);
12+
} else {
13+
count = 0;
14+
}
15+
}
16+
17+
return maxCount;
18+
}
19+
20+
public static void main(String[] args) {
21+
int[] nums = {0,1,0,1,0,1,0,1,0,1,1,1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,0,1,1,1,1,1,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,1,1,1,1};
22+
23+
System.out.println(findMaxConsecutiveOnes(nums));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package arrays.fundamentals;
2+
3+
import java.util.Arrays;
4+
5+
public class SecondLargestElement {
6+
public static int secondLargestBrute(int[] nums) {
7+
// Sort the given array in ascending ordering
8+
Arrays.sort(nums);
9+
10+
// Get the largest element
11+
int largestElement = nums[nums.length - 1];
12+
13+
// Search for the second-largest element
14+
for (int i = nums.length - 2; i >= 0; i--) {
15+
if (nums[i] < largestElement) {
16+
return nums[i];
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
23+
public static int secondLargestBetter(int[] nums) {
24+
// Avoid sorting in this approach
25+
// rather find the largest and then look for second-largest
26+
27+
int largestElement = Integer.MIN_VALUE;
28+
int secondLargestElement = Integer.MIN_VALUE;
29+
30+
for (int num: nums) {
31+
largestElement = Math.max(largestElement, num);
32+
}
33+
34+
for (int num : nums) {
35+
if (num > secondLargestElement && num != largestElement) {
36+
secondLargestElement = num;
37+
}
38+
}
39+
40+
return secondLargestElement == Integer.MIN_VALUE ? -1 : secondLargestElement;
41+
}
42+
43+
public static int secondLargestOptimal(int[] nums) {
44+
int largestElement = Integer.MIN_VALUE;
45+
int secondLargestElement = Integer.MIN_VALUE;
46+
47+
for (int i = 0; i < nums.length; i++) {
48+
if (nums[i] > largestElement) {
49+
secondLargestElement = largestElement;
50+
largestElement = nums[i];
51+
} else if (nums[i] > secondLargestElement && nums[i] != largestElement) {
52+
secondLargestElement = nums[i];
53+
}
54+
}
55+
56+
return secondLargestElement == Integer.MIN_VALUE ? -1 : secondLargestElement;
57+
}
58+
59+
public static void main(String[] args) {
60+
int[] nums = {3, 3, 6, 1};
61+
62+
System.out.println(secondLargestBrute(nums));
63+
System.out.println(secondLargestBetter(nums));
64+
System.out.println(secondLargestOptimal(nums));
65+
}
66+
}

0 commit comments

Comments
 (0)