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 Digital Algorithms practice 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:
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.
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.
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.
int
can represent. If the first thing you do is to convert that representation to anint
, I'd consider the solution as a failure (if I were evaluating your code as an interviewer). And if you converted it into aBigInteger
instead, I would say that the solution works, but is still "cheating" by violating the spirit of the exercise. \$\endgroup\$9009
=>[9, 0, 0, 9]
. Add a number to the original number and return it as array. Right? \$\endgroup\$