Sort List<List<Integer>> in Java [duplicate] - java

This question already has answers here:
Java Sort List of Lists
(5 answers)
Closed 1 year ago.
I am trying to sort a List<List<Integer>> lexicographical manner. But I'm unable to achieve the goal and don't get where is the issue.
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(Arrays.asList(1, 3, 76, 99));
result.add(Arrays.asList(1, 2, 84, 92));
result.add(Arrays.asList(1, 1, 76, 99));
java.util.Collections.sort(result, (item1, item2) -> {
if (item1.get(0) > item2.get(0) || item1.get(1) > item2.get(1) || item1.get(2) > item2.get(2)
|| item1.get(3) > item2.get(3)) {
return 1;
} else {
return -1;
}
});
Expected output:
[[1, 1, 76, 99], [1, 2, 84, 92], [1, 3, 76, 99]]
But I'm getting
[[1, 3, 76, 99], [1, 2, 84, 92], [1, 1, 76, 99]]
I want that index wise smallest number will come first. In the example, all three lists have 1 at the first position, so no change. At the second position, 3rd list has 1 which is the minimum among the three so 3rd list will be moved to the first. Then considering the 2nd item of 2nd and 3rd list 2 is lower so second list will remain at the 2nd position.

The condition you have is wrong. When item1.get(0) < item2.get(0) you simply continue your short-circuited comparison instead of returning -1. Also, any time you have a comparator that does not return 0, I get automatically suspicious.
A simple fix would be to use a loop:
for(int i = 0; i < 4; i++) {
int i1 = item1.get(i);
int i2 = item2.get(i);
if(i1 < i2) {
return -1;
} else if(i1 > i2) {
return 1;
}
}
return 0;
You could simplify the loop to
for(int i = 0; i < 4; i++) {
int d = item1.get(i) - item2.get(i);
if(d != 0) {
return d;
}
}
return 0;

You can do it in a generic way, based on MatinS answer:
class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
#Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
Then sorting is easy
List<List<Integer>> listOfLists = ...;
Collections.sort(listOfLists, new ListComparator<>());

Building off of Mad Physicist's answer, here is a more general solution and usage:
public final class SortingListOfListExample {
public static void main(final String[] args) {
final List<List<Integer>> result = new ArrayList<>();
result.add(List.of(1, 3, 76, 99));
result.add(List.of(1, 2, 84, 92));
result.add(List.of(1, 1, 76, 99));
System.out.println(result);
result.sort(new ListComparator<>());
System.out.println(result);
}
private static final class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
#Override
public final int compare(final List<T> list1, final List<T> list2) {
final int minSize = Math.min(list1.size(), list2.size());
for(int i = 0; i < minSize; i++) {
// Be wary of nulls.
final int compareResult = list1.get(i).compareTo(list2.get(i));
if(compareResult != 0) {
return compareResult;
}
}
return Integer.compare(list1.size(), list2.size());
}
}
}

Related

Given an integer array (of length n), find and return all the subsets of input array using recursion in Java

Suppose my input array is [15,20,12]
The required answer is a 2D array
The Required is output is as followed
[12
20
20 12
15
15 12
15 20
15 20 12
]
Not clear if it's homework or practical case. This is how would I solve it using Guava PowerSet:
public static void main(String[] args) {
Integer input[] = {15,20,12};
List<Integer> rev = Lists.reverse(Arrays.asList(input));
Set<Integer> indices = IntStream.range(0, input.length).boxed().collect(ImmutableSet.toImmutableSet());
Object output[] = Sets.powerSet(indices).stream()
.filter(indexset -> !indexset.isEmpty())
.map(indexset -> indexset.stream().map(i -> rev.get(i)).collect(Collectors.collectingAndThen(toList(), Lists::reverse)))
.map(List::toArray)
.toArray();
System.out.println(Arrays.deepToString(output));
}
Disclaimer:
This is my original work. No part of the solution has been copied from anywhere.
My solution works perfectly for 3 elements. However, this needs to be improved to work for arrays of other sizes. Despite this, I am publishing it so that OP or anyone else can extend this solution to work for an array of any size.
This question is close to the power set except for the fact that a power set can not have duplicate elements. If this exception is removed from this question, there are many solutions available e.g. at 1, 2, 3 etc.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = { 15, 20, 12 };
System.out.println(Arrays.deepToString(subsets(arr)));
}
public static int[][] subsets(int input[]) {
int[][] subarrs = new int[(int) Math.pow(2, input.length) - 1][input.length];
int[] indices = { 0 };
subsetsHelper(input, subarrs, 0, 0, 0, indices);
return subarrs;
}
private static void subsetsHelper(int input[], int[][] subarrs, int index, int i, int j, int[] indices) {
if (i == input.length) {
subarrs[index] = input;
return;
}
int[] subarr = new int[indices.length];
for (int x = 0; x < subarr.length; x++) {
subarr[x] = input[indices[x]];
}
subarrs[index] = subarr;
if (j == input.length - 1) {
subsetsHelper(input, subarrs, index + 1, i + 1, i + 1, new int[] { i + 1 });
} else {
subsetsHelper(input, subarrs, index + 1, i, j + 1, new int[] { i, j + 1 });
}
}
}
Output:
[[15], [15, 20], [15, 12], [20], [20, 12], [12], [15, 20, 12]]
Here you go.
public static void main(String[] args) {
int[] nums= {15, 20, 12};
int[][] subsets = subsets(nums);
for (int i = 1; i < subsets.length; i++) {
System.out.println(Arrays.toString(subsets[i]));
}
}
public static int[][] subsets(int input[]) {
List<List<Integer>> list = new ArrayList<>();
subsetsHelper(list, new ArrayList<>(), input, 0);
return convertListToArray(list);
}
private static void subsetsHelper(List<List<Integer>> list , List<Integer> resultList, int [] nums, int start){
list.add(new ArrayList<>(resultList));
for(int i = start; i < nums.length; i++){
// add element
resultList.add(nums[i]);
// Explore
subsetsHelper(list, resultList, nums, i + 1);
// remove
resultList.remove(resultList.size() - 1);
}
}
private static int[][] convertListToArray(List<List<Integer>> list) {
int[][] array = new int[list.size()][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[list.get(i).size()];
}
for(int i=0; i<list.size(); i++){
for (int j = 0; j < list.get(i).size(); j++) {
array[i][j] = list.get(i).get(j);
}
}
return array;
}
1.As each recursion call will represent subset here, add resultList(see recursion code above) to the list of subsets in each call.
2.Iterate over elements of a set.
3.In each iteration
Add elements to the list
explore(recursion) and make start = i+1 to go through remaining elements of the array.
Remove element from the list
Output:
[15]
[15, 20]
[15, 20, 12]
[15, 12]
[20]
[20, 12]
[12]
public static int[][]returnallsub(int arr[], int si){
if(si==arr.length)
{int[][]ret=new int [1][0];
return ret;
}
int [][]rss =returnallsub(arr,si+1);
int [][]tss=new int[rss.length*2][];
int i=0;
for(;i<rss.length;i++) {
tss[i]=new int [rss[i].length];
}
int k=0;
for(;k<rss.length;k++) {
tss[i]=new int [rss[k].length+1];
i++;
}
int j=0;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[i][j]=rss[i][j];
}
}
for(k=i;k<tss.length;k++) {
tss[k][0]=arr[si];
}
int r=i;
for(i=0;i<rss.length;i++) {
for(j=0;j<rss[i].length;j++){
tss[r][j+1]=rss[i][j];
}
r++;
}
return tss;
}
public static int[][] subsets(int arr[]) {
int start=0;
return returnallsub( arr,0);
}
}

Search Algorithm Java - Elevator Recursive function - Struggle with base cases

Lifts are usually limited in capacity, both in space (persons) as in load (kgs). Imagine we have
a small lift which is capable of transporting a maximum of 6 persons and a maximum load of
500kg. Suppose 13 people are waiting with the following weights: 10, 30, 40, 41, 80, 90, 50, 55,
92, 66, 82, 62 and 70kg. Write a recursive program that finds a group of people that does not
exceed the maximum capacities, but has the maximum possible load in kg. (Hint: there is a valid
solution that exceeds 470kg)
public static void main (String[] Args)
{
ArrayList<Integer> s = new ArrayList<Integer>(); //List of unexplored
int[] weight0 = { 10, 30, 40, 41, 80, 90, 50, 55, 92, 66, 82, 62,70}; //Initial state
int target = 500; //Goal state
System.out.println(liftGroup(weight0,0,target, s) + " way(s)"); //Recursive function
}
static int liftGroup (int[] weight,int c,int target, ArrayList<Integer> s){
assert weight != null : "array should be initialized";
assert c >= 0 && c <= weight.length;
assert s != null : "ArrayList should be initialized";
int sumOfUntried = 0;
if (c > 6) {
showSoulution(s);
return 1;
}
else if (target < 0) {
return 0;
}
else if (c >= weight.length) { //that's okay?
return 0;
}
int min = weight[c];
for (int i = c; i < weight.length; i++) {
sumOfUntried += weight[i];
if(weight[i]<min)
min=weight[i];
}
if(min>target) // If you find one BIG fatty
{
return 0;
}
if (sumOfUntried > target) { //Correct
return 0;
}
else {
s.add(weight[c]);
int with = liftGroup(weight, c + 1, target - weight[c], s);
s.remove(s.size() - 1);
int without = liftGroup(weight, c + 1, target, s);
return with + without;
}
}
/*
* Prints the ArrayList with the solution
*/
private static void showSoulution(ArrayList<Integer> s)
{
assert s != null : "ArrayList should be initialized";
System.out.println("Solution: " + s);
}}
My problem is understanding and using the base case:
When the number of persons does not exceed the maximum limits. You've got a solution.
But how do I comply with the two goals?
Here's a little bit of a messy solution*, which I threw together with credit from here, here and here.
Basically, for each iteration, add the combination, and its sum to a HashMap.
Then sort the HashMap by value.
Finally, loop through HashMap and find closest value to your target.
static Map<String, Integer> myMap = new HashMap<>();
static void combinationUtil(int arr[], int data[], int start,
int end, int index, int r) {
int sum = 0;
StringBuilder sb = new StringBuilder();
if (index == r) {
for (int j = 0; j < r; j++) {
sb.append(data[j]).append(",");
sum += data[j];
System.out.print(data[j] + " ");
}
myMap.put(sb.toString(), sum);
sum = 0;
sb = new StringBuilder();
System.out.println("");
return;
}
for (int i = start; i <= end && end - i + 1 >= r - index; i++) {
data[index] = arr[i];
combinationUtil(arr, data, i + 1, end, index + 1, r);
}
}
static void printCombination(int arr[], int n, int r) {
int data[] = new int[r];
combinationUtil(arr, data, 0, n - 1, 0, r);
}
public static void main(String[] args) {
int arr[] = {10, 30, 40, 41, 80, 90, 50, 55, 92, 66, 82, 62, 70};
int r = 6; //as you have 6 people
int n = arr.length;
printCombination(arr, n, r);
myMap = sortByValue(myMap);
System.out.println(searchClosest(myMap, 500)); //500 is the target
}
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
return map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(/*Collections.reverseOrder()*/))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
}
public static String searchClosest(Map<String, Integer> map, int value) {
double minDistance = Double.MAX_VALUE;
String bestString = null;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
double distance = Math.abs(entry.getValue() - value);
if (distance < minDistance) {
minDistance = distance;
bestString = entry.getKey();
}
}
return bestString;
}
Here's an online example with int arr[] = {1,2,3,4,5,6,7,8}; and the permutations set to 3, and target of 14.
*This is just copied/pasted with one or two minor modifications but is more just an idea of how to get your solution

Finding the number of distinct pairs of integers that sum up to an integer

I am trying to count the numbers of pairs in an array such that each pair gives the sum of an integer!
I used the following code :
public static int SumPairs(Integer []input, int k){
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
int tmp=0;
//System.out.println(pairs.toString());
for(int i=0;i<input.length;i++){
if(pairs.containsKey(input[i])){
System.out.println(pairs.containsKey(input[i]));
System.out.println(input[i] +", "+ pairs.get(input[i]));
input[i]=0;
tmp++;
}
else
pairs.put(k-input[i], input[i]);
}return tmp;
}
the problem is ; for example when my array is 1 2 2 2 3 4 4 4
and sum = 5
it compute as following
(4,1)
(4,1)
(4,1)
(3,2)
I want to prevent the method from using a number more than once !!
so the output will be
(4,1)
(3,2)
I hope this can help
def numberOfPairs(a, k):
# Let's do a o(n) approach by maintaining all the compliments of the K in a
# visited set
compliments = set()
result = set()
for v in a:
# See if the element is in the compliments set, if so thats the pair
if v in compliments:
result.add((v, k-v))
# If the element is not found in visited save the compliment of it in the visited set
else:
compliments.add(k-v)
return len(result)
I use a map storing values and their frequencies:
public static int SumPairs(Integer[] input, int k){
Map<Integer, Integer> frequencies = new HashMap<>();
int pairsCount = 0;
for(int i=0; i<input.length; i++){
int value = input[i];
int complement = k - input[i];
if(frequencies.containsKey(complement)){
int freq = frequencies.get(complement) - 1;
pairsCount++;
//System.out.println(value + ", " + complement);
if(freq == 0){
frequencies.remove(complement);
}else{
frequencies.put(complement, freq);
}
}else{
if(frequencies.containsKey(value)){
frequencies.put(value, frequencies.get(value) + 1);
}else{
frequencies.put(value, 1);
}
}
}
return pairsCount;
}
This works for all the test cases I could think of. Please add in the comment section any test case that this code fails so that I can fix it. If it works, please accept the solution.
public class DistinctPairs {
private static int count(int target, int... arr) {
int count = 0;
Set<String> seen = new HashSet<>();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
int k = target - arr[i];
int[] pair = new int[]{k, arr[i]};
Arrays.sort(pair);
String s = Arrays.toString(pair);
if (set.contains(k) && !seen.contains(s)) {
count++;
seen.add(s);
// uncomment this print statement to print the distinct pairs
// System.out.println(s);
} else {
set.add(arr[i]);
}
}
return count;
}
// test suite and driver method
public static void main(String[] args) {
System.out.println(count(10, 1, 2, 3, 6, 7, 8, 9, 1) == 3);
System.out.println(count(47, 6, 1, 3, 46, 1, 3, 9) == 1);
System.out.println(count(9, 3, 2, 1, 45, 27, 6, 78, 9, 0) == 2);
System.out.println(count(9, 3, 3, 2, 1, 45, 27, 6, 78, 9, 0) == 2);
System.out.println(count(6, 1, 5, 7, -1) == 2);
System.out.println(count(6, 1, 5, 7, -1, 5) == 2);
System.out.println(count(2, 1, 1, 1, 1) == 1);
System.out.println(count(5, 1, 2, 2, 2, 3, 4, 4, 4) == 2);
System.out.println(count(8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4) == 1);
System.out.println(count(7, 1, 5, 66, 2, 3, 4, 7, 0, 2, 5) == 3);
System.out.println(count(5) == 0);
System.out.println(count(5, 1) == 0);
System.out.println(count(7, 3, 4) == 1);
}
}
Another approach can be to follow the classic solution of Two Sum Problem and add the pairs in a set as you find them, all this in the same pass. This set will be of a custom wrapper class with arr[i] and (target - arr[i]) as it's members and you'll need to override hashcode() and equals() methods in such a way that (a,b) is the same as (b,a). At the end simply return the size of the set. This approach will have the same time and space complexity in Big-O terms as the first approach.
int count(int target, int... nums) {
Set<Pair> uniPairs = new HashSet<>();
Set<Integer> seen = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (seen.contains(diff)) {
Pair pair = new Pair(nums[i], diff);
uniPairs.add(pair);
}
seen.add(nums[i]);
}
return uniPairs.size();
}
class Pair {
int a;
int b;
public Pair (int a, int b) {
this.a = a;
this.b = b;
}
#Override
public boolean equals(Object obj) {
Pair pair2 = (Pair) obj;
return ((a == pair2.a) && (b == pair2.b)) || ((b == pair2.a) && (a == pair2.b));
}
#Override
public int hashCode() {
return Objects.hash(a, b) + Objects.hash(b, a);
}
}
public static int sumPairs(Integer[] input, int sum){
List<Integer> complementaries = new ArrayList<>(input.length);
int pairs = 0;
for(Integer number : input){
if(complementaries.contains(number)){
complementaries.remove(number);
pairs++;
}
else{
complementaries.add(sum-number);
}
}
return pairs;
}
Now it should work perfectly.
The complementaries array is used just for keeping track of the numbers needed for making the sum. If it contains the number it means that we iterated over its complementary before, so we can just add one pair and remove the number from the list of complementaries. Oherwise we add the complementary of the current number to the list without incresing the pairs counter.
The code takes an array and returns all possible pairs that have sum as specified. As the question asks to print number of pairs and not the pairs, the length of array divided by 2 would give the desired answer.
int notInArray(float a[],float m,int n)
{
int i,j,k;
for(i=0;i<n;i++)
{
if(a[i] == m)
return 0;
}
return 1;
}
int main() {
int i,j,k;
int n;
scanf("%d",&n); //Input the number of elements in array.
float arr[n];
for(i=0;i<n;i++)
scanf("%f",&arr[i]); //input the array elements
float copyArr = arr[0];
float m;
if (n == 0)
return 0;
scanf("%f",&m); //input the sum
float resArr[n];
int b;
int a=b=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]+arr[j]==m && notInArray(resArr,arr[i],n))
{
resArr[a++] = arr[i];
resArr[a++] = arr[j];
//printf("%.0f %.0f\n",arr[i],arr[j]);
}
}
}
printf("All possible pairs: \n");
for(i = 0;i<a;i+=2)
printf("%.0f %.0f\n",resArr[i],resArr[i+1]);
int len = (int)( sizeof(resArr) / sizeof(resArr[0]) )
printf("Number of such pairs: %d",len);
return 0;
}
public void distinctPairs(int[] arr, int k){
int length = arr.length;
int count = 0;
Map<Integer,Integer> pairs = new HashMap<Integer,Integer>();
for(int i=0;i<length;i++){
for(int j=i+1;j<length;j++){
if(arr[i]+arr[j] == k ){
if(!(pairs.containsKey(arr[j])&&pairs.containsValue(arr[i])))
pairs.put(arr[i], arr[j]);
}
}
}
count = pairs.size();
System.out.println("Pairs are "+pairs+" count = "+count);
}
This works for me. Steps I followed.
Check if sum of a pair is equal to required(k).
Check if the pair doesn't already exist in the map.
We can use the hashmap to store all values of the array. Then iterate over the array and check if the map contains (K - a[i] ). If the map contains then increment count and remove both keys from the map.
private int getDistinctPair(int k,int[] input){
HashMap<Integer,Integer> map = new HashMap<>();
int pairs = 0;
for (int i = 0; i < input.length-1; i++) {
map.put(input[i], input[i]);
}
for (int i = 0; i <input.length-1 ; i++) {
int diff = k - input[i];
if(map.containsKey(diff)){
pairs++;
map.remove(diff);
map.remove(input[i]);
}
}
return pairs;
}
You can slove by using below code:
def countPairs(arr, k):
possible_maps = []
for num in arr:
pair_matches = list(filter(lambda n: n + num == k, arr))
if len(pair_matches) > 0:
possible_maps += list(map(lambda nm: (num, nm), pair_matches))
return len(set(map(lambda pair: ','.join(str(n) for n in sorted(pair)), possible_maps)))
Hope this may help you.
My C# way to do this in a single loop with just another list to store temporary diff values.
private static int SumPairs(int[] arr, int sum)
{
Dictionary<int, int> frequency = new Dictionary<int, int>();
List<int> temp = new List<int>();
int count = 0;
foreach (int i in arr)
{
int diff = sum - i;
if (!frequency.ContainsKey(i))
{
if (temp.Contains(i))
{
frequency.Add(i, diff);
count++;
}
else
{
temp.Add(diff);
}
}
};
return count;
}
my C# implementation using Tuple
static List<Tuple<int,int>> GetUniquePairs(int[] arr, int sum)
{
Dictionary<Tuple<int, int>, int> kvp = new Dictionary<Tuple<int, int>, int>();
List<Tuple<int,int>> result = new List<Tuple<int,int>>();
int length = arr.Length;
for(int i = 0;i < length; i++)
{
int j = i + 1;
while (j < length)
{
if(arr[i]+arr[j] == sum)
{
Tuple<int, int> key = new Tuple<int, int>(arr[i], arr[j]);
if (!kvp.ContainsKey(key))
kvp.Add(key, 1);
}
j++;
}
}
var keys = kvp.Keys;
foreach(var k in keys)
{
result.Add(k);
}
return result;
}
The Simplest Solution of your problem of finding distinct pair:
public static int SumPairs(int[] input, int k) {
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
int tmp = 0;
for (int data : input) {
if (pairs.containsKey(k - data) && pairs.get(k - data) == 0) {
tmp++;
pairs.put((k - data), pairs.get(k - data) + 1);
} else if (!pairs.containsKey(data)) {
pairs.put(data, 0);
}
}
return tmp;
}
It has been tested for 1 2 2 2 3 4 4 4 and sum = 5. Also for 4 4 4 4 4 4 4 4 4 4 4 4 4 4 and sum = 8.
If any confusion feel free to ask me. Cheers.
import java.util.HashSet;
public class DistinctPairs {
static int numberOfPairs(int[] arr,int k)
{
HashSet<String> s=new HashSet<String>();
int n=arr.length;
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sum=arr[i]+arr[j];
if(i==j)
{
continue;
}
else
{
if(sum==k)
{
String l=String.valueOf("("+arr[i]+","+arr[j]+")");
StringBuilder sb=new StringBuilder(l);
String rl=sb.reverse().toString();
if(s.add(l)==false)
{
}
}
}
}
}
System.out.println(s.toString());
return s.size()/2;
}
public static void main(String args[])
{
int b[]={1,5,66,2,3,4,7,0,2,5};
int size=numberOfPairs(b,5);
System.out.println(size);
}
}

how can I get the result of addition and subtraction, with a recursive method that uses an ArrayList?

given a arraylist input, I have to create a recursive method that returns the sum of the values in the odd positions of the list from which the position of values are subtracted
For example:
private int method(ArrayList<Integer> list, int k)
{
int s = 0;
s = list.get(k);
if(k == list.size()) return s;
return s + method(k+1);
}
public int method(ArrayList<Integer> list)
{
return method(list,0);
}
(in main)
List<Integer> list = Arrays.asList(2, 5, 3, 7, 11, 1);
ArrayList<Integer> l2 = new ArrayList<>(list);
SumSub test = new SumSub(l2);
System.out.println(test.method(l2));
[2, 5, 3, 7, 11, 1] ---> 2-5+3-7+11-1=3 (the result that it should be showed)
but the result is always 22 and i can't understand why
Some pointers:
Give variables meaningful names, not k, s, list etc.
Declare collection interfaces (List) instead of implementation classes (ArrayList) where possible to improve the level of abstraction in your code.
Here an example of a recursive solution (untested):
private static int addOddAndSubtractEvenPositions(List<Integer> values, int position) {
// stop condition
if (position >= values.size()) {
return 0;
}
// recurse
int tailResult = addOddAndSubtractEvenPositions(values, position + 1);
// calculate
int currentValue = values.get(position);
if (position % 2 == 0) {
currentValue = -currentValue;
}
return currentValue + tailResult;
}
public static void main(String[] args) {
List<Integer> values = Arrays.asList(2, 5, 3, 7, 11, 1);
System.out.println(addOddAndSubtractEvenPositions(values, 0));
}
I haven't understood what the parameter k is used for
But a recursive method to subtract the elements in pair and then sum all the pairs can be:
public static int SumSub(ArrayList<Integer> list){
int result = 0;
int size = list.size();
if(list.size() > 2){
for(int i = 0; i < size; i++){
ArrayList<Integer> newList1 = new ArrayList<Integer>(list.subList(i, i+2));
result += SumSub(newList1);
i++;
}
} else {
result = list.get(0) - list.get(1);
}
return result;
}
}

Comparing 2D arrays

Im trying to compare two 2D arrays by using this. But I keep getting "Arrays are not the same." even when they are the same.
int i;
int j = 0;
int k;
int l;
List<Integer> list = new ArrayList<Integer>();
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> zero = new ArrayList<Integer>();
for ( i = 1; i <= 16; i++) {
list.add(i);
}
//System.out.println(list); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Collections.shuffle(list.subList(1, 15));
System.out.println(list);
Collections.replaceAll(list, 16, 0);
System.out.println(list);
// System.out.println(list); //[11, 5, 10, 9, 7, 0, 6, 1, 3, 14, 2, 4, 15, 13, 12, 8]
int[][] a2 = new int[4][4];
int [][] a3 = new int[4][4];
for ( i = 0; i < 4; i++) {
for ( j = 0; j< 4; j++) {
a2[i][j] = list.get(i*4 + j);
a3[i][j] = list.get(i*4 + j);
}
}
for (int[] row : a2) {
System.out.print("[");
for ( int a : row)
System.out.printf("%4d", a);
System.out.println("]");
}
for (int[] row : a3) {
System.out.print("[");
for ( int a : row)
System.out.printf("%4d", a);
System.out.println("]");
}
boolean check1 = Arrays.equals(a2, a3);
if(check1 == false)
System.out.println("Arrays are not same.");
else
System.out.println("Both Arrays are same.");
I can't do it like this either.
boolean check1 = Arrays.equals(a2[i][j], a3[i][j]);
The first one does not work because a two-D int array is really an array of arrays (that is, an array of objects). The Arrays.equals() method for an array of objects uses equals() to test whether corresponding elements are equal. Unfortunately for your code, equals() for arrays is the default Object implementation: they are equal() only if they are the identical object. In your case, they are not.
In the second case, when you code Arrays.equals and pass two int values, the compiler can't match it to any signature of the Arrays class.
One way to check equality is to use deepEquals:
boolean check1 = Arrays.deepEquals(a2, a3);
Another way is to iterate the outer array explicitly:
boolean check1 = true;
for (int i = 0; check1 && i < a2.length; ++i) {
check1 = Arrays.equals(a2[i], a3[i]);
}
boolean check1 = Arrays.deepEquals(a2, a3);
is definitely a possibility. The implementation of that uses Object[], which may be appealing to you because it checks the types of the arrays you pass on-the-fly.
But if you want stronger typing and a little less overhead, you can write your own as follows.
import java.util.Arrays;
/**
* Operations on multi-dimensional arrays.
*
* #author stephen harrison
*/
public class ArrayUtils {
private ArrayUtils() {
// Static methods only
}
public static <T> boolean equals(final T[][] a, final T[][] b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; ++i) {
if (!Arrays.equals(a[i], b[i])) {
return false;
}
}
return true;
}
public static <T> boolean equals(final T[][][] a, final T[][][] b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; ++i) {
if (!equals(a[i], b[i])) {
return false;
}
}
return true;
}
}
The first equals on 2D arrays calls Arrays.equals(). The 3D version similarly calls the 2D one.
I hope that helps.

Categories