codility challenge, test case OK , Evaluation report Wrong Answer

Posted by Hussein Fawzy on Stack Overflow See other posts from Stack Overflow or by Hussein Fawzy
Published on 2014-06-08T22:07:09Z Indexed on 2014/06/10 3:26 UTC
Read the original article Hit count: 1035

Filed under:
|

the aluminium 2014 gives me wrong answer [3 , 9 , -6 , 7 ,-3 , 9 , -6 , -10] got 25 expected 28

but when i repeated the challenge with the same code and make case test it gives me the correct answer

Your test case [3, 9, -6, 7, -3, 9, -6, -10] : NO RUNTIME ERRORS (returned value: 28)

what is the wrong with it ???

the challenge :-

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 = P = Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q]. The maximum sum is the maximum sum of any slice of A. For example, consider array A such that: A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 3 A[4] = 1 For example (0, 1) is a slice of A that has sum A[0] + A[1] = 5. This is the maximum sum of A. You can perform a single swap operation in array A. This operation takes two indices I and J, such that 0 = I = J < N, and exchanges the values of A[I] and A[J]. To goal is to find the maximum sum you can achieve after performing a single swap. For example, after swapping elements 2 and 4, you will get the following array A: A[0] = 3 A[1] = 2 A[2] = 1 A[3] = 3 A[4] = -6 After that, (0, 3) is a slice of A that has the sum A[0] + A[1] + A[2] + A[3] = 9. This is the maximum sum of A after a single swap. Write a function: class Solution { public int solution(int[] A); } that, given a non-empty zero-indexed array A of N integers, returns the maximum sum of any slice of A after a single swap operation. For example, given: A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 3 A[4] = 1 the function should return 9, as explained above.

and my code is :-

import java.math.*;

class Solution {
    public int solution(int[] A) {
        if(A.length == 1) 
            return A[0];
        else if (A.length==2)
            return A[0]+A[1];

        else{
            int finalMaxSum = A[0];

            for (int l=0 ; l<A.length ; l++){
                for (int k = l+1 ; k<A.length ; k++ ){

                    int [] newA = A;
                    int temp = newA[l];
                    newA [l] = newA[k];
                    newA[k]=temp;

                    int maxSum = newA[0];
                    int current_max = newA[0];
                    for(int i = 1; i < newA.length; i++)
                    {
                        current_max = Math.max(A[i], current_max + newA[i]); 
                        maxSum = Math.max(maxSum, current_max);
                    }

                    finalMaxSum = Math.max(finalMaxSum , maxSum);

                }
            }

            return finalMaxSum;
        }
    }
}

i don't know what's the wrong with it ??

© Stack Overflow or respective owner

Related posts about java

Related posts about codility