how to optimise two for loops in java [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 1 year ago.
Improve this question
Is there any way to optimise these for loops ?
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(i < j)
{
if(a[i]+a[j] >= l && a[i]+a[j] <= r)
count++;
}
}
}

It depends what these 2 loops do.
Imagine that these 2 loops are traversing a 2 dimensional table.
If you need to find the sum of all the elements of the table this cannot be optimised. You have to traverse all the elements line by line.
If you want to find a specific value of the AI for example... that you have to traverse again and again the same values, in this case you can optimize by not traversing the same values you did before.
For example if these values of the table are values of a tree structure, and you traverse a branch, after you don't have to traverse the same branch. You can prune it as we say.
With this way you optimize your searching in the table using the loops.

Related

String comparison from a list in for loop [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 3 years ago.
Improve this question
I'm trying to compare strings from a list in a for loop but I can't really find the syntax for it. I want to change the iterator (in this case i) to not be the index, instead, I want it it to be the value.
list.add(lrow);
for (int i = 0; i < list.size(); i++) {
if (lrow.equals(i)){
}
}
handler.getGalagaState().entityManager.entities.add(new EnemyBee(0 , 0, 32 , 32, handler, row, column));
You have to get value from ArrayList by index. You can do it like this:
list.get(i);
I want to change the iterator (in this case i) to not be the index, instead, I want it it to be the value.
'i' is not the iterator. Its just a variable of type integer that let's you setup a condition for iterating through a Collection.
With that being said, how would you implement the condition-check for continuing or exiting the loop when the control-variable is of type String?
Your comparison should look like this:
if (lrow.equals(list.get(i)))
list.get(i) gets you the element at index i in your List.

How to check if integer array is spiral sorted? [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 3 years ago.
Improve this question
Spiral Sort Involves: a[0] <= a[n-1] <= a[1] <= a[n-2] <= a[2]....
How would i check if a given array is spiral sorted or not?
I have tried this the brute force way.
I assume that your "brute force" way is to iterate the array in the "spiral" sequence and check that each element is greater or equal to the previous one. For an array of size N, this requires N - 1 comparisons.
Modulo details of how you actually code that algorithm, there is no better way to check that an array is spiral sorted. The "brute force" algorithm does the minimum number of comparisons required by theory.

Is there a general function or way in Java to calculate an equation of the form (a+b+...+n)^2 with a,b,n >= 0? [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 need to find a way to implement an algorithm in Java that can calculate (a+b+...+n)^2 with a,b,n >= 0. The purpose is to use it afterwards in order to calculate Jain's Fairness index for my algorithm in networks. Is there any standard way to do that or any specific library for advanced math that i might have missed?
Just sum those n values in for loop and then multiply it by itself. Or am i missing something?
The number of possible problems you may encounter is infinite, so you should not be surprised if you often get into a situation where there is no method to help you. Let's suppose that you have an array of numbers, let's suppose it has double elements and the name of the array is input, then:
double sum = 0;
for (int index = 0; index < input.length; index++)
sum += input[index];
double result = Math.pow(sum, 2);
//output holds the result
If there is a possibility of overflow, then you will need to handle it. Also, you will need to handle the validation that your items are positive.

Displaying a specific amount of strings out of an ArrayList in java? [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 5 years ago.
Improve this question
I am currently trying to make a Mad Libs game, and all the words are stored in an ArrayList called List. Now, I want to display all the words within that list, but only 10 per line. Is there some formatting using %f or something that can solve this problem? I have looked far and wide on this site, but I haven't found anything.
Just use a loop. Iterate over the full list of words, and upon hitting every tenth word, print a newline separator. Otherwise, print a space separator between each word.
List<String> words;
for (int i=0; i < words.size(); ++i) {
if (i % 10 > 0) {
System.out.print(" ");
}
System.out.print(words.get(i));
if ((i+1) % 10 == 0) {
System.out.println(""); // print correct newline regardless of OS
}
}
Demo

(Java) Go thru array beginning at the last index [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 6 years ago.
Improve this question
I'm currently trying to make a method in Java that takes an array of size digits, and is supposed to go thru the array starting at the end, adding 1 to digits <= 8, and turning 9's into 0's.
My problem is that I'm not quite sure how to start going through the array beginning at the end. I'm coming off of Python, so I'm familiar with the syntax list[:-1], but I'm not sure how to apply that, or if it can be applied, to Java.
Thank you very much in advance.
Use this:
public void someMethod(int[] arr){
for(int i=arr.length-1; i >= 0; i--){
if(arr[i] <= 8){
arr[i]+=1;
}else if(arr[i] ==9){
arr[i] = 0;
}
}
}
Refer https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for better undersatnding of for loop construct.
Java has unfortunately no operation like list[:-1] so you will need to do a reversed for loop or a decremental lopp to achieve that:
for (int i = value; i > 0; i--) {
System.out.println("Am decreasing over the element: " + i);
}
Also, please take a look at the Oracle tutorial about for loops

Categories