Problem:
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}
Solution:
public int[] make2(int[] a, int[] b) { int[] myArray = new int[2]; int aLen = a.length; int bLen = b.length; if (aLen == 0) { myArray[0] = b[0]; myArray[1] = b[1]; return myArray; } else if (aLen == 1 && bLen == 1) { myArray[0] = a[0]; myArray[1] = b[0]; return myArray; } else if (aLen == 1) { myArray[0] = a[0]; myArray[1] = b[0]; return myArray; } else { myArray[0] = a[0]; myArray[1] = a[1]; return myArray; } }
public int[] make2(int[] a, int[] b) {
ReplyDeleteif (a.length == 1) return new int[]{ a[0], b[0] };
if (a.length == 0) return new int[]{ b[0], b[1] };
return new int[]{ a[0], a[1] };
}
+ 1
DeleteJesus! +1
Deletepublic int[] make2(int[] a, int[] b) {
Deleteint lena=a.length;
int lenb=b.length;
int index=0;
int[] arr=new int[lena+lenb];
for(int i=0;i<lena;i++)
{ arr[index]= a[i];
index++;
}
for(int i=0;i<lenb;i++)
{ arr[index]= b[i];
index++;
}
int[] int2=new int[2];
int2[0]=arr[0];
int2[1]=arr[1];
return int2;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint[]ab = new int [a.length + b.length];
for(int i = 0 ; i < a.length ; i++ ){
ab[i] = a[i];
}
for( int i=0; i<b.length; i++){
ab[a.length + i] = b[i];
}
int[]m = {ab[0], ab[1]};
return m;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint sum = a.length + b.length;
int[] concat = new int[sum];
for(int x = 0; x<a.length; x++)
{
concat[x] = a[x];
}
for(int x = 0; x<b.length; x++)
{
concat[a.length + x] = b[x];
}
int[] two = new int[2];
two[0] = concat[0];
two[1] = concat[1];
return two;
}
public int[] make2(int[] a, int[] b) {
int sum = a.length + b.length;
int[] concat = new int[sum];
for(int x = 0; x<a.length; x++)
{
concat[x] = a[x];
}
for(int x = 0; x<b.length; x++)
{
concat[a.length + x] = b[x];
}
int[] two = new int[2];
two[0] = concat[0];
two[1] = concat[1];
return two;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint[] my_arr = new int[2];
if (a.length >= 2) {
my_arr[0] = a[0];
my_arr[1] = a[1];
}
if (a.length ==1){
my_arr[0] = a[0];
my_arr[1] = b[0];
}
if (a.length == 0) {
my_arr[0] = b[0];
my_arr[1] = b[1];
}
return my_arr;
}
//here is how i did it (more easy)
ReplyDeletepublic int[] make2(int[] a, int[] b) {
int [] c = new int[2];
int x = 0;
int i = 0;
for(i = 0; i < c.length; i++){
if(a.length > i){
c[i] = a[i];
}
else if(b.length > x){
c[i] = b[x];
x++;
}
}
return c;
}
public int[] make2(int[] a, int[] b) {
ReplyDelete//initialize array
int newArray[] = new int[2];
//'a' array is empty
if (a.length == 0){
newArray[0] = b[0];
newArray[1] = b[1];
}
//'a' array has one element
else if (a.length == 1){
newArray[0] = a[0];
newArray[1] = b[0];
}
//'a' array has enough elements to fill new array
else if (a.length >= 2){
newArray[0] = a[0];
newArray[1] = a[1];
}
//returning new array
return newArray;
}
Of course it's easier with for-loops, but the Array-1 exercises should be solved without any loops....
ReplyDeleteStart reading guys!!
public int[] make2(int[] a, int[] b) {
ReplyDeleteint[] c = new int[2];
for (int i = 0; i < a.length; i++) {
if (i < 2) {
c[i] = a[i]; }
}
for (int i = a.length; i < 2; i++) {
c[i] = b[i - a.length];
}
return c;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint[]myArr = new int[2];
if(a.length==1){
myArr[0]=a[0];
myArr[1]=b[0];
return myArr;
}else if(a.length==0){
myArr[0]=b[0];
myArr[1]=b[1];
return myArr;
}
return new int[]{a[0],a[1]};
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteif(a.length >= 2){
return new int[] {a[0], a[1]};
}if(a.length == 1){
return new int [] {a[0], b[0]};
}else if(a.length == 0){
return new int [] {b[0], b[1]};
}
return new int[] {1};
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint arr[]=new int[2];
if(a.length==0 && b.length>1){
arr[0]=b[0];
arr[1]=b[1];
return arr;
}
if(a.length>2 && b.length>=0){
arr[0]=a[0];
arr[1]=a[1];
return arr;
}
if((a.length==1 && b.length==0)||(a.length==1 && b.length==1)){
arr[0]=a[0];
arr[1]=b[0];
return arr;
}
if(a.length==1 && b.length>1){
arr[0]=a[0];
arr[1]=b[0];
return arr;
}
else return a;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteint arr[]=new int[2];
if(a.length==0){
arr[0]=b[0];
arr[1]=b[1];
}
if(b.length==0){
arr[0]=a[0];
arr[1]=a[1];
}
if(a.length==1){
arr[0]=a[0];
arr[1]=b[0];
}
if(b.length==1){
arr[0]=a[0];
arr[1]=b[0];
}
if(a.length>1){
arr[0]=a[0];
arr[1]=a[1];
}
return arr;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteif(a.length==0) return b;
if(a.length==1)
return new int[]{a[0],b[0]};
else if(a.length>1)
return new int[]{a[0],a[1]};
return a;
}
if(a.length==0) return b;
ReplyDeleteif(a.length==1) return new int[] {a[0],b[0]};
return new int[] {a[0],a[1]};
public int[] make2(int[] a, int[] b) {
ReplyDeleteif(a.length==0)
{
int t[]={b[0],b[1]};
return t;
}
else if(a.length==1)
{
int t[]={a[0],b[0]};
return t;
}
else if(a.length==2)
{
int t[]={a[0],a[1]};
return t;
}
int t[]={a[0],a[1]};
return t;
}