Finding greatest difference between elements of two lists [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My code is:
int diff = 0;
for (int i = 0; i<listOne.size(); i++)
{
for (int j = 0; j<listTwo.size(); j++)
{
if (listOne.get(i)-listTwo.get(j)>diff)
diff = listOne.get(i)-listTwo.get(j);
if (listTwo.get(j)-listOne.get(i)>diff)
diff = listTwo.get(j)-listOne.get(i);
}
}
return diff;
The task is to find the greatest difference between any two numbers in two inputted lists (the difference must be between a number from list one and a number from list two).
I cannot tell what is wrong with my code.

You may be missing Math.abs executed on the diff. Difference is an absolute value, so difference between 5 to 7 and 7 to 5 is same - 2.
int diff = 0;
for (int i = 0; i<listOne.size(); i++) {
for (int j = 0; j<listTwo.size(); j++) {
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
if (elementDiff>diff) {
diff = elementDiff;
}
}
}
return diff;
So both below lines will produce same results:
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
int elementDiff = Math.abs(listTwo.get(i)-listOne.get(j));

Related

bubbleSorting by object variables in an arrayList [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I need to sort by cakes made then if if equal by alphabetical order
I need this sorting method to firstly sort my cakesMade which is an int and then secondly by name if cakesmade is equal. The code is working for the cakesMade, I just cant seem to get the second sort by name to work. Could someone please help
public int bubbleSort (){
for (int i = 0; i < team.size(); i++){
for (int j = 0; j < team.size() - 1;j++ ){
Employee t1 = team.get(j);
Employee t2 = team.get(j+1);
if (t1.getCakesMade() < t2.getCakesMade()) {
team.set(j, t2);
team.set(j + 1, t1);
} else if (t1.getCakesMade() == t2.getCakesMade()) {
if (t1.getName().compareTo(t2.getName()) < 0){
team.set(j, t2);
team.set(j + 1, t1);
}
}
}
}
return 0;
}

How to print out possible string with alphabet set and length? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am using java
For example, the following situation:
First, the function is used as a print all possible strings.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
Output:
0,1,2,a,b,c,01,02,0a,0b,0c,10,11,12,1a ..................... ccccc. stop in length = 5
Then, I want to add a loop stopper to fetch the specified string.
char[] alphabetSet = "012abc".toCharArray();
int length =5;
int loopStopper = 3;
Output:
a
Thank you
Use backtracking.
void print_all(char []ch,int maxLen){
for(int i=1;i<=maxLen;i++)
backTrack(ch,i,0,new char[i]);
}
void backTrack(char[] ch,int len,int k,char[] ans){
if(k==len){
System.out.print(new String(ans,0,len)+",");
return;
}
for(int i=0;i<ch.length;i++){
ans[k]=ch[i];
backTrack(ch,len,k+1,ans);
}
}
try this:
String alphabet = "012abc";// for example as your code "012abc"
char[] alphabetSet = alphabet.toCharArray();
int length = 5;
for (int i = 0; i < alphabetSet.length; i++) {
System.out.print(alphabetSet[i] + ",");
}
for (int j = 0; j <= length; j++) {
for (int i = 0; i < alphabetSet.length; i++) {
System.out.printf("%d%c,",j,alphabetSet[i]);
}
}

Trying to figure out how can I make a program to identify equal numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.

I have a function in java which may have bug and I can not find it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this code which suppose to do the following task:
For example , given M=3, and array built like this:
A[0]=1
A[1]=1
A[2]=3
A[3]=3
A[4]=5
A[5]=1
A[6]=3
the function may return 1 or 3
import java.util.*;
class Solution {
int solution(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
what could be the problem because it is not always working and I can see there is a bug in my program.
1 1 1 1 5 5 5 5 5 this is a case where your code may fail. Check and update the max occurance variable outside the loop too. Above case give enough justice to my point, i hope.

Matches of numbers into another array JAVA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to put into an integer for matches in the numbers of an array and another array, which do not have the same position in the array.
For example: I have these two Arrays of numbers:
4578
7539
It means that it have 1 number in the same position (5), and the number 7 is in the first array but not in the same position, so this case must increment 1 in my integer.
If it is in the same position like the number 5, I did this:
int introducido = Integer.parseInt(numero.getText());
for (int i = 0; i < String.valueOf(introducido).length(); i++) {
int entero = Integer.parseInt("" + numero.getText().charAt(i));
String temp = Integer.toString(numAleatorio);
int intarrNumeros = Integer.parseInt("" + temp.charAt(i));
if (intarrNumeros == entero) {
fijas++;
}
But I don't know how to do if is not in the same position.
UPD
Working for non-unique symbols in input strings
Try this code
pattern = "4578 ";
String toFind = "7539";
int samePosition = 0;
int notSamePosition = 0;
for (int i = 0; i < toFind.length(); ++i) {
char digit = toFind.charAt(i);
if (pattern.contains(String.valueOf(digit))) {
if (pattern.charAt(i) == digit) {
++samePosition;
} else {
++notSamePosition;
}
}
}
You can simply change the argument in the if statement to not equals.
if (intarrNumeros != entero)

Categories