Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions app/src/main/java/control/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ public class Single {
* @return The sum of the first n natural numbers.
*/
public static int sumRange(int n) {
int[] arr = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = i;
}
for (int i : arr) {
sum += i;
}
return sum;
return n * (n - 1) / 2;
}

/**
Expand All @@ -29,7 +21,10 @@ public static int sumRange(int n) {
* @return The maximum value in the array.
*/
public static int maxArray(int[] arr) {
int max = 0;
if (arr == null || arr.length == 0) {
return Integer.MIN_VALUE; // Or throw an exception
}
int max = arr[0];
for (int i : arr) {
if (i > max) {
max = i;
Expand All @@ -45,13 +40,10 @@ public static int maxArray(int[] arr) {
* @param m The modulus.
*/
public static int sumModulus(int n, int m) {
Vector<Integer> multiples = new Vector<Integer>();
for (int i = 0; i < n; i++) {
if (i % m == 0) {
multiples.add(i);
}
int sum = 0;
for (int i = 0; i < n; i += m) {
sum += i;
}

return multiples.stream().mapToInt(Integer::valueOf).sum();
return sum;
}
}
14 changes: 3 additions & 11 deletions app/src/main/java/datastructures/DsVector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datastructures;

import java.util.Vector;
import java.util.Collections;

public class DsVector {
/**
Expand Down Expand Up @@ -40,16 +41,7 @@ public static Vector<Integer> searchVector(Vector<Integer> v, int n) {
*/
public static Vector<Integer> sortVector(Vector<Integer> v) {
Vector<Integer> ret = new Vector<Integer>(v);

for (int i = 0; i < ret.size(); i++) {
for (int j = 0; j < ret.size() - 1; j++) {
if (ret.get(j) > ret.get(j + 1)) {
int temp = ret.get(j);
ret.set(j, ret.get(j + 1));
ret.set(j + 1, temp);
}
}
}
Collections.sort(ret);
return ret;
}

Expand Down Expand Up @@ -106,4 +98,4 @@ public static Vector<Integer> mergeVectors(Vector<Integer> v1,
}
return ret;
}
}
}