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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:21:17+00:00 2026-05-25T22:21:17+00:00

I have to write a method that will compare elements in an array of

  • 0

I have to write a method that will compare elements in an array of strings and return the index of the largest element. It’s going to be done recursively using a divide and conquer approach. I have an idea, and I was just looking to see if my idea was right or if it should be done in a different way.

I was planning on looking at the array from the left side to mid -1, then look at mid, and then look at mid +1 to right. I have a variable that will keep track of the largest index, and then after that make the recursive call for the left side and the right side.

Does that sound like a good way to approach this problem?

This is what I have so far:


public int longest()
{
    longest(0, a.length-1);
    return longestIndex;
}

private int longest( int left, int right)
{
    int longestIndex;
    int mid;

    if(left > right)
    {
        longestIndex = -1;
    }
    else if(left == right)
    {
        longestIndex = 0;
    }

    else
    {
        longestIndex = 0;

        mid = (left + right) / 2;
        longest(left, mid - 1);
        if (a[mid].compareTo(a[longestIndex]) > 0)
        {
            longestIndex = mid;
        }
        longest(mid + 1, right);            
    }            
    return longestIndex;
}

Also, since the methods are supposed to return an int, how would I pass the longestIndex n the private method up to the public method so that it would show up in my test program when longest is called?

  • 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-25T22:21:17+00:00Added an answer on May 25, 2026 at 10:21 pm

    Does it have to be recursive? Using recursion for this sounds like a case of:

    golden hammer

    And your recursion looks totally wrong anyways, because not only you are not keeping track of the actual index but also your base cases and recursive calls don’t make any sense.


    If I were compelled to use recursion, I would do something like:

    int longest(array):
        return longest_helper(0, 0, array)
    
    int longest_helper(max_index, curr_idx, array):
        # base case: reached the end of array
        if curr_idx == array.length:
            return max_index
    
        if array[curr_idx].length > array[max_index].length:
            max_index = curr_idx
    
        # recursive call
        return longest_helper(max_index, curr_idx + 1, array)
    

    And then I would proceed to drop the class and tell the professor to give students problems where recursion is actually helpful next time around.


    Since it doesn’t look like the array is sorted, the easiest (and fastest) way to do this would just be go through the whole thing (pseudocode):

    max_index  = 0
    max_length = array[0].length
    
    for index in 1 .. array.length:
        if array[index].length > max_length:
            max_length = array[index].length
            max_index  = index
    
    return max_index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a method that will return a Hibernate object based on
I'm trying to write a method that will return a list of DateTimes representing
I'm trying to write an extension method that will give me the MemberInfo representing
I'm trying to write a method in java that will increment a counter for
I am trying to write a method that will take an IntTree as a
Occasionally , we have to write methods that receive many many arguments , for
I have and old(ish) C# method I wrote that takes a number and converts
serial.write() method in pyserial seems to only send string data. I have arrays like
The debug screen says that django does not have write access to the db.
I'm writing a simple web app in PHP that needs to have write access

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.