1

I have an array and a long type variable called total that stands for it's sum.

If the array is of length n then the summation of it should be done like this: sum = arr[0] + arr[n-1] + arr[1] + arr[n-2] +... until sum >= total and I have to print a string of the elements in the sum

My Approach : I have executed loop till n/2 if n is even or n/2 + 1 if n is odd.

I am doing something like this :

String s = "";
if (n % 2 ==0)
     loop = n/2;
else 
     loop = n/2 + 1;

for(int i=0 ; i < loop ; i++){
     sum += arr[i];
     s += arr[i] + " ";
     if(check(sum , total)==true) break; //this function checks if sum>=total.
     sum += arr[n];
     s += arr[n] + " ";
     if(check(sum , total)==true) break;
     n--;
}
System.out.println(s);

Example : arr[ ]={10 , 20 , 30, 40, 50} and total = 120 then output should be :

10 50 20 40

my approach stills gives TLE in some test cases. I need more optimized solution than this. Help me here.

9
  • 2
    what's TLE? And how is check breaking the looping? Is it throwing an exception? Commented Sep 22, 2016 at 7:46
  • TLE is time limit exceeded. Commented Sep 22, 2016 at 7:47
  • TLE is time limit exceeded and it is not throwing an exception. Commented Sep 22, 2016 at 7:48
  • check shoudl be within an if Commented Sep 22, 2016 at 7:49
  • 1
    There is no real reason to keep the string and concatenating it. Print as soon as you add. Commented Sep 22, 2016 at 7:51

2 Answers 2

3

Why not to do that this way:

for (int i = 0; sum < total && i < loop; i++) {
    sum += array[i];
    System.out.print(String.valueOf(array[i]) + " ");
    if (sum < total) {
        sum+=array[n-1-i];
        System.out.print(String.valueOf(array[n-1-i]) + " ");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Upvoted. Out of interest, do the online judges penalise i++ over ++i?
@Bathsheba why should they in Java on integers?
@Bathsheba, that would be extremism for sure.
These days I agree but are not these online judges a law unto themselves?
0

you can organise your array first the do the tratement. something like this :

String s = "";
orgArray(arr);

for(int i=0 ; i < (arr.lenght)-1; i++){
     sum += arr[i];
     s += arr[i] + " ";
     check(sum , total);
}
System.out.println(s);

and the function will be some thing like this :

public static void orgArray(string tab[]) {
        int longueur = tab.length;
        int tampon = 0;
        boolean permut;

        do {

            permut = false;
            for (int i = 0; i < longueur - 1; i++) {

                if (i + 1 < longueur - 1 - i) {

                    tampon = tab[i + 1];
                    tab[i + 1] = tab[longueur - 1];
                    tab[longueur - 1] = tampon;
                    permut = true;
                }else {permut = false;}
            }
        } while (permut);
    }

hope that help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.