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 9322547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:07:04+00:00 2026-06-19T04:07:04+00:00

I have been trying to learn divide and conquer algorithms and I have come

  • 0

I have been trying to learn divide and conquer algorithms and I have come up with what I thought would work using java. The algorithm is supposed to take an array of size n that is a base 2. It should divide the array to a base case of 4 then add those for indexes together. Then it will add all those together to find the sum of the entire array. Here is what I have done so far in java and my error. Am I at least on the right track for the divide and conquer algorithms?

Exception raised:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at getSum.sumArray(getSum.java:17)
at getSum.sumArray(getSum.java:21)
at getSum.main(getSum.java:7)

Here is the code:

public class getSum {
    static int sum = 0;
    public static void main(String[] args) {
            int[] numbers = {2,2,2,2,2,2,2,2};
            int amount = 0;
           amount = sumArray(0,numbers.length,numbers);
           System.out.print(amount);
    }

    public static int sumArray(int first, int last, int[] A){
        int index = last - first;
        if(index == 1){
            return sum;
        }else if(index <= 4 && index > 1){
            for(int i = first; first < last; i++){
                sum += A[i];
            }
            return sum;
        }
        return (sumArray(first, last / 2, A) + sumArray(last / 2, A.length, A));
    }
}
  • 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-06-19T04:07:05+00:00Added an answer on June 19, 2026 at 4:07 am

    You need to change:

    for(int i = first; first < last; i++){
    

    to:

    for(int i = first; i < last; i++){
    

    You keep comparing first and last, when you increment only i

    And as @Some1.Kill.The.DJ pointed out,

    sum should be part of method sumArray

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have been trying to learn Javascript using codeacademy.com and I have reached
I have been trying to learn MVVM using Laurent Bugnion MVVM light method. I
I have been experimenting and trying to learn JQuery, using AJAX to consume a
I have been trying to learn Java for the past week, and have been
I have been trying to learn Vim and have been using it for 2
I have been trying to learn Java for the past few days so my
I have been trying to learn more about lambda expressions lately, and thought of
So, I have been trying to learn wxpython using the videos on thenewboston.com, but
I have been trying to learn Java for Android development, so I decided to
I have been trying to learn how to build jQuery plugins, so I'm still

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.