Problem:
Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.
front11({1, 2, 3}, {7, 9, 8}) → {1, 7}
front11({1}, {2}) → {1, 2}
front11({1, 7}, {}) → {1}
Solution:
public int[] front11(int[] a, int[] b) { if (a.length == 0 && b.length == 0) { return a; } if (a.length == 0) { int[] myArray = new int[] {b[0]}; return myArray; } if (b.length == 0) { int[] myArray = new int[] {a[0]}; return myArray; } int[] myArray = new int[] {a[0], b[0]}; return myArray; }
public int[] front11(int[] a, int[] b) {
ReplyDeleteint []c =new int[0];
if(a.length>0 && b.length>0){
c = new int[2];
c[0]=a[0];
c[1]=b[0];
}else if( a.length == 0 && b.length>1){
c = new int[1];
c[0]=b[0];
}else if(b.length == 0 && a.length>1){
c = new int[1];
c[0]=a[0];
}
return c;
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteif(a.length == 0 && b.length == 0){
return new int[]{};
}
if(a.length == 0){
return new int[]{b[0]};
}
if(b.length == 0){
return new int[]{a[0]};
}
return new int[]{a[0], b[0]};
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteint[] arr=new int[2];
int[] ar1=new int[1];
int[] ar2=new int[0];
int aLen=a.length;
int bLen=b.length;
if(aLen==0 && bLen==1){
arr[0]=b[0];
return arr;
}
if((aLen==1 && bLen==0) || (aLen>1 && bLen==0)){
ar1[0]=a[0];
return ar1;
}
if((aLen==0 && bLen==0)){
return ar2;
}
if((aLen==0 && bLen==1) || (aLen==0 && bLen>0)){
ar1[0]=b[0];
return ar1;
}
if((aLen>2 && bLen>0)||(aLen==1 && bLen==1)||(aLen==1 && bLen>1)){
arr[0]=a[0];
arr[1]=b[0];
return arr;
} return a;
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteif(a.length>=1 && b.length>=1)
return new int[] {a[0],b[0]};
else if (a.length>=1 && !(b.length>=1))
return new int [] {a[0]};
else if (!(a.length>=1) && b.length>=1)
return new int [] {b[0]};
else return a;
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteif(a.length!=0&&b.length!=0) return new int[] {a[0],b[0]};
if(a.length==0&&b.length==0)return new int[] {};
if(a.length==0) return new int[] { b[0]};
else return new int[] {a[0]};
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteif(a.length==0 && b.length==0)
return a;
else if(a.length!=0 && b.length!=0)
{
int t[]={a[0],b[0]};
return t;
}
else if(a.length==0)
{
int t[]={b[0]};
return t;
}
int t[]={a[0]};
return t;
}
public int[] front11(int[] a, int[] b) {
ReplyDeletereturn ((a.length>0)&&(b.length>0))? new int [] {a[0],b[0]} :
(a.length>0)? new int [] {a[0]} : (b.length>0)? new int [] {b[0]}:
new int []{};
}