mardi 30 juillet 2019

adding a certain value to Int Array, need optimizing suggestions

I'm a student in my first semester and in my free time and Holidays, I do some java training to keep my mind from rusting...so I downloaded this amazing app and came across the plus One problem and that inspired me to code the plus-Target problem:

Question

Given an Integer Array (non-empty Array) you have to add specific number = target to this array then return an Array to main.

my humble solution: let's assume the following:

int [] x={9,9,9,9}

so, what first came to my mind is that the Elements

  • x[0] represent Mathematically 9000.
  • x[1 ] represent Mathematically 900.
  • x[2] represent Mathematically 90.
  • x[3] represent Mathematically 9.

then turning the array to int by summing 9000+900+90+9.

so now after converting the arrays to number, we can add the target to it. (target can be<0).

After adding the target I had to look upon to scenarios:

  1. if the result is not zero then I convert the int value of temp to string and assign the chars as numbers to a newly made array according to the length of the string minus one.

    2.if the temp is zero which would mean that the target=-9999 in our particular case. so I had to assign each and every element of the forwarded array to zero and then return it.

so now enough talking and here is my code:

private static int [] plusTarget(int[]x,int target){
    int size=x.length;//get the ze of the array
    int toPower=1;//to help set the power of 10..
    int temp=0;//this will have the array value
    char offSet='0';
    String convert=null;
    int []arr=null;
    /*
     * 9000=9*10^4-1
     * where 4 is the size of the forwarded array.
     * and so on...
     */
    for (int i = 0; i < x.length; i++) {
        temp +=x[i]*( Math.pow(10,(size-toPower) ) );
        toPower++;
    }
    System.out.println("the value of the array after turning it to number "+temp);
    temp+=target;
    System.out.println("value after adding target!. "+ temp);

    if(temp!=0){
        convert = Integer.toString(temp);//converting the number to string..
        if (temp>0) {
            arr= new int [convert.length()];
        } else if (temp<0){
            arr= new int [convert.length()-1];//minus one because the string size is more by one because of the minus sign..    
        }

        for (int i = 0; i < arr.length; i++) {
            if (convert.charAt(i)- offSet >0) {
                arr[i]=convert.charAt(i)- offSet;
            }
        }
        if (arr[0]==0) {//if the first emelent is zero then the number is minus..

            for (int i = 0; i < arr.length; i++) {
                if (i+1 != arr.length) {
                    //shifting the elements backwards one step 
                    arr[i]=arr[i+1];
                }
            }
            arr[0]*=-1;//multiplying the first element by -1
        }
        return arr;

    }else{//if (temp+(-target)==0)..then we have to assign zeros to each element of the fowarded array
        for (int i = 0; i < x.length; i++) {
            x[i]=0;
        }
    return x;
    }   
}

tests done:

target=-9999;
output=[0, 0, 0, 0]

target=-19998;
output=[-9, 9, 9, 9]

target=1;
output=[1, 0, 0, 0, 0]

target=-1
output=[9, 9, 9, 8]

I couldn't find any bugs. I might have made some mistakes unintentionally and that's why I'm posting the code here to get some suggestions from you guys.

Pls, feel free to give me feedback and if you see some mistakes in the code I would love to correct them.

my solution for the one plus problem:

private static int [] plusOne(int[]x){
    int size=x.length;
    int power=1;
    int temp=0;
    for (int i = 0; i < x.length; i++) {
        temp +=x[i]*( Math.pow(10,(size-power) ) );
        power++;
    }
    System.out.println("the value of the array after turning it to number "+temp);
    temp++;
    System.out.println("temp after adding one!. "+ temp);
    String convert = Integer.toString(temp);//converting the number to string..
    int [] arr= new int [convert.length()];
    for (int i = 0; i < convert.length(); i++) {
        arr[i]=(int)(convert.charAt(i)-'0'); 
    }
    return arr;
}

the app solution for the plus One case:

private static int [] app (int[]x){
    int n=x.length;
    for(int i=n-1;i>=0;i--){
        if (x[i]<9) {
            x[i]++;
            return x;
        }
        x[i]=0;
    }
    int [] newNumber= new int[n+1];
    newNumber[0]=1;
    return newNumber;
}

please evalute my solution for the plus one problem vs the app solution as well. there is no solution for the plus-Target problem thus it came across my mind after finishing the plus-one exercise.

thanks in advance.

Aucun commentaire:

Enregistrer un commentaire