I\'m struggling with quite interesting assignment and looking for advice. The case is to find the longest sublist from the given list of pairs. First elements from these pai
I Wrote for you this algorithm it Does exactly what you want.
1) i create a results ArrayList;
2) initialize variable sum to 0;
3) loop through all values of array[][]; for each array value get the sum of its components
4) if the sum of the components of thhe array value is less or equal to sum then insert the array value in the results array
5) but if the sum of the components of the array value is greater than sum, then check the results array. if its empty then insert the array value. if its not empty check the sum of the components of each value of the results Arraylist with the sum of the components of the value of the soucrce array.Any value with sum less than that of source array component is removed then insert this particular value to the results arraylist.
import java.util.ArrayList;
class lists{
public static void findLagrestList() {
int arr[][] = {{1,3},{3,1},{2,0},{4,4},{5,3},{6,2}};
//ArrayList currentSublist = new ArrayList<>();
ArrayList resultSublist = new ArrayList<>();
int result_arr[][]={};
int sum =0;
for(int i=0;isum)
{
if(resultSublist.size()>0)
{
for(int k=0;ksumm)
{
resultSublist.remove(k) ;
}
}
resultSublist.add(inner_arr);
}else{
resultSublist.add(inner_arr);
sum = valuesum;
}
}
}
System.out.println(resultSublist.size());
printList(resultSublist);
}
private static void printList(ArrayList list) {
for (int[] is : list) {
System.out.println();
for (int i : is) {
System.out.print(i + " ");
}
}
}
public static void main(String [] oo)
{
lists.findLagrestList();
}
}
the output is
3
4 4
5 3
6 2