Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4585216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:27:50+00:00 2026-05-21T21:27:50+00:00

We have an array of heights, representing the altitude along a walking trail. Given

  • 0

We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index. For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6. The start end end index will both be valid indexes into the array with start <= end.

sumHeights({5, 3, 6, 7, 2}, 2, 4) => 6       
sumHeights({5, 3, 6, 7, 2}, 0, 1) => 2       
sumHeights({5, 3, 6, 7, 2}, 0, 4) => 11    

i’m struggling to get this right, i have tried a part of this but im confused and im getting ArrayIndexOutOfBoundsException.

 public int sumHeights(int[] heights, int start, int end) {        

   int total =0;   
   int difference =0;   
      for(int i=start;i<=end;i++){           
        if(heights[i] > heights[i++]){         
           difference =heights[i] - heights[i++];                  
        }else if(heights[i++] > heights[i]){         
           difference =heights[i++] - heights[i];            
        }   
        total+=difference;   
      }   
   return total;   
 }  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:27:50+00:00Added an answer on May 21, 2026 at 9:27 pm

    You increment i in your loop thus possibly going out of bounds:

    for(int i=start;i<=end;i++){      // here i might be last valid index, i.e. 4 in your example     
        if(heights[i] > heights[i++]){    //here i might be 5 (because of i++), i.e. you'd get the ArrayIndexOutOfBoundsException     
           difference =heights[i] - heights[i++]; //here i might be 6 (because of another i++), i.e. you'd get the ArrayIndexOutOfBoundsException                 
        }else if(heights[i++] > heights[i]){     //here i might be 5, i.e. you'd get the ArrayIndexOutOfBoundsException    
           difference =heights[i++] - heights[i];            
        }   
        total+=difference;   
      }   
    

    To fix it, use:

    for(int i=start;i<end;i++){   
    int next = i + 1;     
        if(heights[i] > heights[next]){  
           difference =heights[i] - heights[next]; //assuming you mean next = i + 1 here and not next = i + 2 like in your code               
        }else if(heights[next] > heights[i]){    
           difference =heights[next] - heights[i];            
        }   
        else {
          difference = 0; //due to public demand I'll help you with this, too
        }
    
        total+=difference;   
      }   
    

    Edit: you could also make the loop much simpler:

    for(int i=start;i<end;i++){   
      total += Math.abs(heights[i] - heights[i+1]);  
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Width and Height parametr. I have been given an array of
I have a two dimensional array that I need to load data into. I
You have: val array = new Array[Array[Cell]](height, width) How do you initialize all elements
In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
i have array unknown size i want to transfer to matrix[n][2].Example; D[c]=1,2,3,4,5 D[c/2][2]= 1
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array of strings, String[] data and it's 10 elements has value P
if i have array array[0] = jack; array[1] = jill; array[2] = lisa; array[2]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.