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 5 of 5

Thread: Returning the equilibrium index in a sequence of integers

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Returning the equilibrium index in a sequence of integers

    Hi guys.

    I'm new to the forum. I was playing with an online Java test from codility about a function that returns the equilibrium index in a sequence of integers (Codility). The problem states:

    Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:

    A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0
    3 is an equilibrium index, because:

    A[0]+A[1]+A[2]=A[4]+A[5]+A[6]
    6 is also an equilibrium index, because:

    A[0]+A[1]+A[2]+A[3]+A[4]+A[5]=0
    (sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence A.

    [...]

    Assume the sum of zero elements is equal zero. Write a function
    int equi(int[] A);
    that given a sequence, returns its equilibrium index (any) or -1 if no equilibrium indexes exist. Assume that the sequence may be very long.
    My solution to this was the following:

    int equi ( int[] A ) {        
     
        int leftSum = 0;
        int rightSum = 0;
     
        for (int iterator= 0; iterator< A.length; iterator++){
            rightSum += A[iterator];
        }
     
        for(int eqIndexCandidate = 0; 
            eqIndexCandidate < A.length;
            eqIndexCandidate ++){
     
            if (eqIndexCandidate != 0)
               leftSum += A[eqIndexCandidate - 1];
     
            rightSum -= A[eqIndexCandidate];
     
            if (leftSum == rightSum)
                return eqIndexCandidate;
     
        }
     
        return -1;
    }

    However, when I got the results back it seems that I missed one case:



    How would you deal with a case that the numbers are too large to fit in an integer ? It seems that Codility would not let me use any packages from the API...


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Returning the equilibrium index in a sequence of integers

    Use big integers, or use longs.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Returning the equilibrium index in a sequence of integers

    large numbers - check for Integer.MAX_VALUE and MIN_VALUE before summing the stuff. I got 100% there.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Returning the equilibrium index in a sequence of integers

    ...
    100%
    Last edited by copeg; September 3rd, 2013 at 02:19 PM. Reason: Removed spoonfeeding

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Returning the equilibrium index in a sequence of integers

    @opastukhov, please don't spoonfeed, especially on a post that is almost 3 years old.

    Thread locked.

Similar Threads

  1. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  2. java integers
    By timeline in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2010, 02:54 AM
  3. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM
  4. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 04:04 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 11:19 AM