find the range over a incline

Viewed 41

goodday i am having an issue with my code. i am trying to iterate over an arraylist and see for which indices the values are increasing. lines = [5, 7, 10, 11, 8, 6, 5, 4, 7, 8]

for (int i = 0; i < lines.size(); i++) { //iterate over array  
    // System.out.println(lines.get(i) + " ");                 
    if (i == 9){                                               
        break;                                                 
    }else if (lines.get(i) < lines.get(i + 1)){                
        System.out.println((i) + "-" + (i+1));                 
    }else {                                                    
        continue;                                              
    }                                                          
}                                                              

and my output produces this:

0-1
1-2
2-3
7-8
8-9

however i want my output to look like this:

0-3
7-9
5 Answers

You print directly after the if presence. You would have to add another query after the first if. Something like this should solve the problem

`for (int i = 0; i < lines.size(); i++) { //iterate over array  
// System.out.println(lines.get(i) + " ");                 
if (i == 9){                                               
    break;                                                 
}else if (lines.get(i) < lines.get(i + 1)){
            if(lines.get(i) > lines.get(i+1){                 
    System.out.println((i) + "-" + (i+1));
            }                 
}else {                                                    
    continue;                                              
}                                                          

}`

You are printing every line, so it is expected the output is moving unit by unit.

What you can do is create a boolean that is initially set to false. Then, it will be set to true if the condition lines.get(i) < lines.get(i + 1) is true, and store the index i.

It will then change to false again whenever the condition lines.get(i) < lines.get(i + 1) is not true. Store the index again and only then print.

Trivial solution:

private static void compare( int start, int end )
{
  if( start != end )
  {
    System.out.println( start + " - " + end );
  }
}
    
public static void main(String args[]) {
  int[] lines = { 5, 7, 10, 11, 8, 6, 5, 4, 7, 8 };
      
  int start = 0;
  for( int i = 0; i < lines.length - 1; i++ )
  {
    if( lines[i] >= lines[i + 1] )
    {
      compare( start, i );                
      start = i + 1;
    }
  }
  compare( start, lines.length - 1 );
}
    

This can very likely be further optimized.

The code in your question is not a minimal, reproducible example so I understand that lines is a List. However you are accessing it as if it were an array so in the code, below, I used an array of int rather than a list of Integer.

The best way to understand how the code works is to run it through a debugger. If you are using an IDE, like IntelliJ or Eclipse, then it has a debugger.

Alternatively do a walk through of the code and write down the values of the variables as they change. This is known as debugging by hand.

int[] lines = new int[]{5, 7, 10, 11, 8, 6, 5, 4, 7, 8};
int end = lines.length - 1;
int lower = 0;
int upper = 0;
for (int i = 0; i < end; i++) { // iterate over array
    if (lines[i] < lines[i + 1]) {
        if (lower == upper) {
            lower = i;
        }
        upper = i + 1;
    }
    else {
        if (upper > lower) {
            System.out.println(lower + "-" + upper);
        }
        lower = upper = i;
    }
}
if (upper > lower) {
    System.out.println(lower + "-" + upper);
}

The limit of the for loop must be one less than the size of the array, otherwise the lines[i + 1], on the last iteration of the loop, will be greater than the index of the last element in lines and that will cause an exception to be thrown.

After the for loop terminates, you need to check the values of lower and upper to see whether the last elements of lines are increasing. Hence the if statement after the for loop.

When I run the above code I get the following output:

0-3
7-9

I understand that this is your desired output.

Although you are working in Java this is more of an algorithm question than something specific to the language.

You'll need to keep track of the beginning of an incline as an independent variable. This is initially null because you do not know if the ArrayList begins with an incline or decline (or steady?). Since you want to keep track of the indexes, you'll need to iterate via the index - which you're already doing.

You are iterating from 1. This makes sense as it allows you to compare the current with the previous. Remove the break on the index 9 - this will cripple your algorithm if the number of elements varies. You should not make assumptions that the data your instructor will test with will be the same as the sample provided. They will look for edge cases that break a naive algorithm.

On a given iteration there are four possible states

  1. you were on an incline and it continues through index
  2. you were on an incline but it ended at previous index
  3. you were not on an incline but you incline from previous index to index
  4. you were not on an incline and you are not incling from previous index to index

There are only two cases where you do something - when you either begin or end an incline. Note that the List may end on an incline so you need to check for that case after completing the iteration. This has the distinct odor of a class assignment so I'm not going to provide Java code but the follow pseudocode describes the general algorithm.

let beginning-of-incline be null
for each index starting with 1
  let inclining = is there an incline from the previous element to this element
  if inclining and beginning-of-incline is null
    capture beginning-of-incline
  else if not-inclining and beginning-of-incline is not null
    record incline from beginning-of-incline to previous index
    clear beginning-of-incline
  end-if-else
end for-each-index
if beginning-of-incline is not null
  record incline from beginning-of-incline to final index of ArrayList
endif
Related