0

I m trying to do this question: Question

I m getting error in the for loop: for(int i=b,int j=0;i< 2*b, j

My approach is storing all the differences in an array which we get by subtracting the initial value, last value and partition.

What is wrong and what is the alternate way to do this?

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        int a, b;
        Scanner in = new Scanner(System.in);
        a= in.nextInt();
        b= in.nextInt();
        in.nextLine();
        int[] ar = new int[b];

        for(int i=0;i<b;i++)
            {
            ar[i]= in.nextInt();
        }

        int[] ar2 = new int[a];
        // I m trying to get the values which can be obtained from each partition
        for(int i=0;i<b;i++)
            {
            ar2[i] = a - ar[i];
            }



       for(int i=b,int j=0;i< 2*b, j<b; i++, j++)
          {
           ar2[i] = ar[j];
        }


        for(int i=0;i<a;i++)
            {
            System.out.print(ar2[i]+" ");
            }


    }
}
2
  • 2
    You have not told us what error you are getting. Without that it's difficult for anybody to help. Please visit the help center and read about how to write questions on SO. Oct 6, 2015 at 20:17
  • Your code doesn't compile for me. You need to either tell us what the error is that you're getting, including the full stack trace, or at least give us a working code snippet to see it outselves. (Both is ideal).
    – azurefrog
    Oct 6, 2015 at 20:24

1 Answer 1

1

Is this compilation error you're getting? This particular line doesn't follow Java syntax:

for(int i=b,int j=0;i< 2*b, j<b; i++, j++)

For instance, you having 2 int declarations inside the for loop is not possible:

int i=b,int j=0;

This must be re-written to:

int i=b, j=0;

On the conditional part, it is not possible to set multiple conditions separated by comma. What you can do is use logical operators to combine 2 conditional expressions - use either || or &&

i< 2*b, j<b;

This must be rewritten to:

for(int i=b, j=0;i< 2*b && j<b; i++, j++)

Or this:

for(int i=b, j=0;i< 2*b || j<b; i++, j++)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.