Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Java program to find the index of the greater element whose value is sum of the remaining elements

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Java program to find the index of the greater element whose value is sum of the remaining elements

    I need to write a java program to find the index of the element whose value is the sum of the remaining elements. can anyone help me with this ?

    Recently I have been asked this question in an Interview which I couldnt solve properly.
    Iam writing a cumbersome code for this program which is not at all satisfying even for me.

    Thanks in advance !
    Last edited by yash_java; August 26th, 2014 at 04:35 AM.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    What have you tried so far to solve the problem? Post any code you have done with any problems
    or errors and people can help you better

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    yash_java (August 25th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    This was my answer in the interview which made me rejected in the interview.

    These are the steps I have followed in my piece of code,

    step 1 : accepted 3 numbers (for simplicity)

    step 2 : calucated biggest number of all

    step 3 : stored the biggest number in the array list

    step 4 : If the biggest number is the sum of the remaining returned the biggest number index otherwise I gave a message saying biggest number is not the sum of the remaining values.

    I have just adjusted this program as I thought taking more numbers could give me a cumbersome logic which I couldnt write down even now. I just couldnt find a simple hint to write a simplified logic to solve this program.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    Can you post a sample array and the desired index for that array?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    yash_java (August 25th, 2014)

  7. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    thank you for the response Norm !

    here is a sample array,
    int[] numberList = {100,100,100,100,900,100,100,100,100,100};

    But it would be good if we can accept a random array from the user and based on that we write our logic and that is what is my problem which i couldnt resolve in my interview.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    What would be the desired index for your example?

    I would assume the search logic would be the same for any array.

    Does "remaining" refer to all of the elements except the selected one?
    For example: Given the array: {1,2,1} the desired index is 1
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    yash_java (August 26th, 2014)

  10. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    For my example, program should return the index of the greatest number which is sum of the remaining i.,e '4' here !

    We must write a program which finds the index of the highest number in the given list (whose value is the sum of all the remaining elements ). And yes remaining elements means all the remaining except the highest number of all. Am i making myself clear here ?
    Last edited by yash_java; August 26th, 2014 at 01:48 AM.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    Ok, that sounds like it would work.

    What problems or questions do you have writing the code?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    This is the approach I have followed to write that program.

    These are the steps I have followed in my piece of code,

    step 1 : accepted 3 numbers (for simplicity)

    step 2 : calucated biggest number of all

    step 3 : stored the biggest number first and remaining next in the array list

    step 4 : If the biggest number is the sum of the remaining returned the biggest number index otherwise I gave a message saying biggest number is not the sum of the remaining values.

    I have just adjusted this program as I thought taking more numbers could give me a cumbersome logic which I couldnt write down even now. I just couldnt find a simple hint to write a simplified logic to solve this program.

    I think I couldnt find a simplified logic which can be applied to any 'n' numbers list given.

    For example if user is giving 100 numbers the test I have conducted to check greater of three numbers will obviously become even more tedious. That piece of code Iam really not convinced with. So I thought of looking for alternative. I need a little hint or help on that. Thanks alot for your efforts in understanding and resolving my issue.
    Last edited by yash_java; August 26th, 2014 at 08:48 AM.

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    The number of numbers will not be a problem if the numbers are stored in a list or array and loops are used to look at the numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    yash_java (August 27th, 2014)

  15. #11
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java program to find the index of the greater element whose value is sum of the remaining elements

    Ok thanks for the response !

Similar Threads

  1. Replies: 6
    Last Post: June 18th, 2014, 09:18 AM
  2. Replies: 9
    Last Post: August 23rd, 2013, 10:52 AM
  3. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  4. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 03:33 PM
  5. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM

Tags for this Thread