0

This is a small code that if the length of input array is power of two then the size of segment array is 2*(input_array.length)-1 else 2*(next power of 2 after input_array.length)-1 .

public static int ByLogs(int n)
{
    double y = Math.floor(Math.log(n)/Math.log(2));
    return (int)Math.pow(2, y + 1);
}

public static void main(String [] args)
{
    int input_array[] = {-1,2,4,0,3};

    if(( input_array.length & (input_array.length - 1) == 0 ) && input_array.length > 0)
        int [] segment_array = new int[2*(input_array.length)-1];
    else
        int [] segment_array = new int[2*(ByLogs(input_array.length))-1];

    //Arrays.fill(segment_array,999);

    System.out.println(segment_array.length);
}

but after compilation the following errors have been occurred.

SegmentTree.java:17: error: '.class' expected
        int [] segment_array = new int[2*(input_array.length)-1];
               ^
SegmentTree.java:17: error: not a statement
        int [] segment_array = new int[2*(input_array.length)-1];
            ^
SegmentTree.java:17: error: illegal start of expression
        int [] segment_array = new int[2*(input_array.length)-1];
                             ^
SegmentTree.java:17: error: ';' expected
        int [] segment_array = new int[2*(input_array.length)-1];
                              ^
SegmentTree.java:17: error: ']' expected
        int [] segment_array = new int[2*(input_array.length)-1];
                                       ^
SegmentTree.java:17: error: not a statement
        int [] segment_array = new int[2*(input_array.length)-1];
                                      ^
SegmentTree.java:17: error: illegal start of expression
        int [] segment_array = new int[2*(input_array.length)-1];
                                        ^
SegmentTree.java:17: error: ';' expected
        int [] segment_array = new int[2*(input_array.length)-1];
                                         ^
SegmentTree.java:17: error: not a statement
        int [] segment_array = new int[2*(input_array.length)-1];
                                                     ^
SegmentTree.java:17: error: ';' expected
        int [] segment_array = new int[2*(input_array.length)-1];

I couldn't find out my mistake.

Soumya Kanti Naskar
  • 1,021
  • 3
  • 16
  • 29
  • Basically, the problem here, other than parenthesis is that you have initialized your array within the if statements, but that leaves in undeclared in the print statment – OneCricketeer Aug 04 '16 at 04:58

2 Answers2

3

First off, you can't actually declare a variable inside an if statement like that, since it goes out of scope as soon as the block ends. So instead of:

if (...) {
  int[] a = ...;
} else {
  int[] a = ...;
}

you probably want:

int[] a; // declare the variable first, *then* initialize it
if (...) {
  a = ...;
} else {
  a = ...;
}

Second, your conditional is mangled; the left-hand side of the & is an int but the right-hand side is a boolean. The bitwise-and operator supports both types, but not together; to convert a boolean to an int you can simply do so explicitly, e.g.

((input_array.length - 1) == 0) ? 1 : 0

To check whether input_array.length is a power of two you want:

(input_array.length & (input_array.length - 1)) == 0

Notice the extra close-paren after -1.

Finally your syntax is sloppy, which is likely making your code harder to work with. Although Java allows array declarations in many forms (e.g. int variable[], int [] variable, int[] variable) only the last one is in common usage. I would strongly suggest always declaring your arrays as type[] variableName, even though you don't have to. Similarly always use { } when writing blocks (if, else, for, etc.) as it makes your code easier to read and safer to modify in the future.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Code runs fine with a space `int []`, by the way. Surprised you didn't point out `int input_array[]`, though, which is also correct – OneCricketeer Aug 04 '16 at 04:54
  • @cricket_007 oh weird, it wasn't compiling for me before but that clearly was one of the other issues manifesting itself strangely. Thanks, I'll fix my answer! – dimo414 Aug 04 '16 at 04:55
  • Yes I have found out the problem and thanks for helping me out. I have to first initialise the array then in the if-else statement I need to allocate the space. But could you explain why is it ? – Soumya Kanti Naskar Aug 04 '16 at 05:02
  • 1
    @SoumyaKantiNaskar variables in Java are [scoped](https://en.wikipedia.org/wiki/Scope_(computer_science)) to the block containing them; in this case your `if` and `else` blocks. Once the block finishes the variable goes out of scope and can no longer be accessed. [This article](https://www.cs.umd.edu/~clin/MoreJava/Objects/local.html) provides some more background. – dimo414 Aug 04 '16 at 05:07
1

There is an error in the if statement.

See comment :

public static int ByLogs(int n)

{
    double y = Math.floor(Math.log(n)/Math.log(2));
    return (int)Math.pow(2, y + 1);
}

public static void main(String[] args)
{
    int input_array[] = {-1,2,4,0,3};
    int [] segment_array;
    int n = input_array.length;

    //for positive n 
    if( (n & (n - 1)) == 0 ) {
        segment_array = new int[(2*(input_array.length))-1];
    } else {
        segment_array = new int[(2*(ByLogs(input_array.length)))-1];
    }

    //Arrays.fill(segment_array,999);

    System.out.println(segment_array.length);
}
c0der
  • 18,467
  • 6
  • 33
  • 65