Return the smallest sorted list of ranges that cover all the numbers in the array exactly. code example
Example 1: how to find the smallest numbers in an arraylist java
import java.util.ArrayList;
import java.util.Scanner;
public class IndexOfSmallest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList list = new ArrayList<>();
while (true) {
int input = Integer.valueOf(scanner.nextLine());
if (input == 9999) {
break;
}
list.add(input);
}
System.out.println("");
int smallest = list.get(0);
int index = 0;
while (index < list.size()) {
if (list.get(index) < smallest) {
smallest = list.get(index);
}
index++;
}
System.out.println("Smallest number: " + smallest);
index = 0;
while (index < list.size()) {
if (list.get(index) == smallest) {
System.out.println("Found at index: " + index);
}
index++;
}
}
}
Example 2: largest subarray of 0's and 1's
public class Solution {
public int findMaxLength(int[] nums) {
Map map = new HashMap<>();
map.put(0, -1);
int maxlen = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
count = count + (nums[i] == 1 ? 1 : -1);
if (map.containsKey(count)) {
maxlen = Math.max(maxlen, i - map.get(count));
} else {
map.put(count, i);
}
}
return maxlen;
}
}