We help IT Professionals succeed at work.

# make2 challenge

on
247 Views
Hi,

I am trying below challenge

make2
http://codingbat.com/prob/p143461

i have not understood the requirement.
Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
return new int[]{a.length>0?a:b,a.length>1?a:b[1-a.length]};

Commented:
elements from a followed by the elements from b:
{4, 5}, {1, 2, 3} → {4, 5,1, 2, 3}
{4}, {1, 2, 3})→ {4, 1,  2, 3}
{}, {1, 2} → {1, 2}
as much as will fit in in a new array length 2:
{4, 5,1, 2, 3 } → {4, 5}
{4, 1,  2, 3}→ {4, 1}
{1, 2} → {1, 2}

this is clear

Commented:
``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
d=a+b;
int[] e=new int;
e={d,d}
return e;

}
``````

i tried like this but not working

Commented:
``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int;
e=list;
e=list;
return e;

}
``````

i tried as above and got below error. Please advise on how to fix it.

What is the best way to add two array so that i can get its first two elements from resultant array?

Commented:
return new int[]{a.length>0?a:b,a.length>1?a:b[1-a.length]};

i di dnot understand above solution. why we are only checking a>0 and a>1 etc not b>0 or b>1 etc.

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Since we are guaranteed that "there will be 2 or more elements available between the 2 arrays.",
we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b or b[1-a.length] are valid

Commented:
there will be 2 or more elements available between the 2 arrays

what it mean by above sentence. I am not clear on that. please advise

when i try as below getting below error

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int;
e=list;
e=list;
return e;

}
``````

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

Also cannot we add one array with other array simply without converting to List and then add as below?

// d=a+b;//why this is not possible some waht like this to resolve this challenge
List<String> list = new ArrayList<String>(Arrays.asList(a));

Commented:
``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int;
e=list;
e=list;
return e;

}
``````

i thought this solution should work 100% but i got below error

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

is it beacuase codingbat site wont support ArrayList?

Commented:
``````b[1-a.length]};
``````

i am not clear on above. Why are we dependent on a withing b array. please advise

Commented:
we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b or b[1-a.length] are valid

what it means by negation of guards?

Commented:
``````return new int[]{a.length>0?a:b,a.length>1?a:b[1-a.length]};

``````

how above solution is working when a length is 0. please advise
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
make2({}, {1, 2}) → {1, 2}
when a length is 0
a.length>0 is not true, so a.length>0?a:b is b
a.length>1 is not true, so a.length>1?a:b[1-a.length] is b[1-a.length] == b[1-0] == b
so we return new int[]{b,b} as required

Commented:
b[1-a.length]

i never got this idea. Is there is any alternate, simple approach to resolve this challenge. Please advise

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int;
e=list;
e=list;
return e;

}
``````
i thought above solution should work 100% but i got below error

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

is it beacuase codingbat site wont support ArrayList? cannot i use arraylist in that site? please advise
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
public int[] make2(int[] a, int[] b) {
List<Integer> list=new ArrayList<Integer>(a.length+b.length);
for( int i:a ){
}
for( int i:b ){
}
int[] e=new int;
e=list.get(0);
e=list.get(1);
return e;
}

Commented:
``````  public int[] make2(int[] a, int[] b) {
List<Integer> list=new ArrayList<Integer>(a.length+b.length);
for( int i:a ){
}
for( int i:b ){
}
int[] e=new int;
e=list.get(0);
e=list.get(1);
return e;
}
``````

i like this solution close to what i am thinking with ArrayList approach. But i still do not understand why i fail with below code

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int;
int[] d=new int;
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int;
e=list;
e=list;
return e;

}

``````

i get below error
Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

i wonder why error messages are not so obviously clear to me but looks some gibberish.
what they meant constroctor not defined?

How to fix my code so that i can make use of Arrays.asList.

Commented:
``````import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestArrayEx {
public static void main(String[] args) {
int a[] = { 4, 5 };
int b[] = { 1, 2, 3 };

/*
* Integer c[]= toObject(a); Integer d[]=toObject(b);
*/

System.out.println("-->" + make2(a, b) + "-->" + make2(a, b));

}

public static int[] make2(int[] a, int[] b) {

Integer c[] = toObject(a);
Integer d[] = toObject(b);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));

/*
* int[] result = new int[IntegerArray.length]; for (int i = 0; i <
* IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
* return result;
*/

// Integer list2[] = new Integer[arrlist.size()];
// list2 = arrlist.toArray(list2);

/*
* int[] result = new int[list.length]; for (int i = 0; i <
* IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
* return result;
*/
Integer list2[] = new Integer[list.size()];
list2 = list.toArray(list2);

int[] e = new int;
e = list2;
e = list2;
return e;

}

public static Integer[] toObject(int[] intArray) {

Integer[] result = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++) {
result[i] = Integer.valueOf(intArray[i]);
}
return result;
}

}
``````

above worked. Not sure right way of doing or not. Please advise.

I still cannot run in codingbat as i have to move toObject method inside though

Commented:
``````public int[] make2(int[] a, int[] b) {
Integer c[] = toObject(a);
Integer d[] = toObject(b);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));
Integer list2[] = new Integer[list.size()];
list2 = list.toArray(list2);

int[] e = new int;
e = list2;
e = list2;
return e;
}

public Integer[] toObject(int[] intArray) {
Integer[] result = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++) {
result[i] = Integer.valueOf(intArray[i]);
}
return result;
}

``````

i refactored as above and passed all tests. Please advise if above approach is correct?
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.