Suppose I hold lists whose members have Integer values as follows
List<Integer> li = new ArrayList<>();
li.add(1);
li.add(2);
li.add(3);
li.add(4);
li.add(5);
List<Integer> li1 = new ArrayList<>();
li1.add(3);
li1.add(5);
li1.add(6);
li1.add(7);
li1.add(8);
li1.add(9);
I want to find out if the lists are sorted in ascending order, but with a fixed difference value between any two members, say 1. (You can think of a kind of Arithmetic progression whose 'd' is 1)
The naive solution proposed is to loop through all the members of the list and check each element[i 1], whether when the element[i] is subtracted from it, whether 1 is obtained, as follows
public static boolean checkSequenceList(List<Integer> li){
for (int i = 0; i < li.size() - 1; i ) {
if (li.get(i 1) - li.get(i) != 1)
return false;
}
return true;
}
Is there a more effective way / abbreviated writing style to do this?
CodePudding user response:
How about you all the values in an Array and then sorting the array by using Array.Sort(arrayname)
, then they should be put in ascending order automatically. If you just want to check it you can compare the array with the sorted array.
CodePudding user response:
An approach could be to use IntStream. If you use the indices and the values as x and y values of a mathematical function, like below:
x (index) y (list.get(x))
0 4
1 5
2 6
3 7
4 8
5 9
your requierment (that they should be sorted and have same difference) implies that they should satisfy a linear function y = mx b
where b
is the first value in list at index 0
and m
being the delta. So you could do something like:
public static boolean checkSequenceList(List<Integer> li, int m){
return IntStream.range(0, li.size()).allMatch(x -> m*x li.get(0) == li.get(x));
}
You can ommit m
if the delta you care about is always 1
public static boolean checkSequenceList(List<Integer> li){
return IntStream.range(0, li.size()).allMatch(x -> x li.get(0) == li.get(x));
}
and it will also short-circuit since allMatch is a short-circuiting terminal operation, i.e. you will not iterate over the whole list if a mismatch is found.
CodePudding user response:
public boolean checkSequenceList(List<Integer> integerList, int d) {
int lastNum = integerList.get(0);
for (int i = 1; i < integerList.size(); i ) {
Integer integer = integerList.get(i);
if (integer - lastNum != d) {
return false;
}
lastNum = integer;
}
return true;
}
CodePudding user response:
You can use this code with any difference between consequence items.
public class Main {
public static void main(String[] args) {
List<Integer> values = new ArrayList<>();
values.add(3);
values.add(4);
values.add(5);
values.add(6);
values.add(8);
values.add(9);
pairwiseDifference(values);
}
static void pairwiseDifference(List<Integer> values) {
int diff = 0;
for (int i = 0; i < values.size() - 1; i ) {
int curDiff = values.get(i 1) - values.get(i);
if (i == 0)
diff = curDiff;
else if (diff != curDiff) {
System.out.println("Items " values.get(i) " and " values.get(i 1) " have not same difference as previous pair");
return;
}
}
System.out.println("Items have same difference");
}
}