public int[] tenRun(int[] nums) {
int len =nums.length;
int[] arr=new int[len];
for(int i=0;i<len;i++){
if(nums[i]<10){
arr[i]=nums[i]*10;
}
else{
arr[i]=nums[i];
}
}
return arr;
}
ASKER
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]i thought above should be as below according to challenge description?
ASKER
For each multiple of 10 in the given array, change all the values following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}.//however 3,4 became 10.10 i thought 5 should become 10tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]
Doing it by hand:
tenRun([2, 20, 3, 4, 10, 5]
2 - no change
20 - multiple of 10 - no change
3 - change it 20 since it follows that multiple of 10
4 - change it 20 since it follows that multiple of 10
10 - multiple of 10 - no change
5 - change it 10 since it follows that multiple of 10
You don't change the values that are multiples of 10.ASKER
>> However 3,4 became 10.10 i thought 5 should become 10
What multiple of 10 does the 5 follow???//[b]not sure what it means..10*0 is 0 10*1 is 10 so 5 falls in between 0 and 5..when you say What multiple of 10 does the 5 follow i am not following it?[/b]
What do you think the results of the following should be:
tenRun([2, 20, 3, 4, 10, 5]) → ???tenRun([2, 20, 10, 10, 10, 10])
No, that is not correct
For each multiple of 10 in the given array, change all the values following it to be that multiple of 10
Doing it by hand:
tenRun([2, 20, 3, 4, 10, 5]
2 - no change
20 - multiple of 10 - no change
3 - change it 20 since it follows that multiple of 10
4 - change it 20 since it follows that multiple of 10
10 - multiple of 10 - no change
5 - change it 10 since it follows that multiple of 10//[b][i]but challenge expecting to change it to 20 not 10[/i][/b]
ASKER
still thinking above
What multiple of 10 does the 5 follow in the array?
is the same as
What multiple of 10 comes before 5 in the array?
The answer in this case is 20. -- So change the 5 to 20.
ASKER
ASKER
public int[] tenRun(int[] nums) {
int len =nums.length;
int[] arr=new int[len];
for(int i=0;i<len;i++){
if((nums[i]/10)==0){
arr[i]=10*(nums[i]%10);
}
else{
arr[i]=nums[i];
}
}
return arr;
}
i think now i understood challene trying to fix it and failing due to not able to start from first occurence of 10 multiple but starting from index 0
Expected Run
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20] [20, 10, 30, 40, 20, 50] X
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20] [10, 10, 20, 20] OK
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20] [10, 10, 90, 20] X
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50] [10, 20, 50, 10] X
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50] [10, 20, 50, 10] X
tenRun([10, 10]) → [10, 10] [10, 10] OK
tenRun([10, 2]) → [10, 10] [10, 20] X
tenRun([0, 2]) → [0, 0] [0, 20] X
tenRun([1, 2]) → [1, 2] [10, 20] X
tenRun([1]) → [1] [10] X
tenRun([]) → [] [] OK
other tests
X
Your progress graph for this problem
ASKER
ASKER
how to use moduls operator for this challenge?You can search for the condition
nums[i]%10) == 0
that will be true for multiples of ten.
4. if yes multiply subsequent elements by same previous element value?No. As d-glitch has posted
... MULTIPLY are not mentioned in the Challenge.Multiply is an arithmetic operation. If we multiply 3 by 4 the result is 12. The numbers that are multiples of ten are 10, 20 ,30, 40, and so on.
ASKER
public int[] tenRun(int[] nums) {
int totalLen =nums.length;
//int[] arr=new int[len];
int firstVal=0;
for(int i=0;i<totalLen;i++){
if((nums[i]%10)==0){
firstVal=i;
break;
}
}
for(int k=firstVal;k<totalLen;k++){
if((nums[k]%10)==0){
secondVal=k;
break;
}
}
return nums;
}
ASKER
ASKER
public int[] tenRun(int[] nums) {
int totalLen =nums.length;
//int[] arr=new int[len];
int val=0;
boolean tenFlag=false;
for(int i=0;i<totalLen;i++){
if((nums[i]%10)==0){
val=nums[i];
tenFlag=true;
}
else if((nums[i]%10)!=0&&tenFlag){
nums[i]=val;
}
}
return nums;
}
now I passed all tests
Expected
Run
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20] [2, 10, 10, 10, 20, 20] OK
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20] [10, 10, 20, 20] OK
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20] [10, 10, 10, 20] OK
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50] [1, 2, 50, 50] OK
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50] [1, 20, 50, 50] OK
tenRun([10, 10]) → [10, 10] [10, 10] OK
tenRun([10, 2]) → [10, 10] [10, 10] OK
tenRun([0, 2]) → [0, 0] [0, 0] OK
tenRun([1, 2]) → [1, 2] [1, 2] OK
tenRun([1]) → [1] [1] OK
tenRun([]) → [] [] OK
other tests
OK
public int[] tenRun(int[] nums) {
int current = 1;
for(int i=0;i<nums.length; i++){
if((nums[i]%10) == 0){
current = nums[i];
}
else{
if(current != 1) nums[i] = current;
}
}
return nums;
}
ASKER
You are currently working on a problem that can use this exact trick.
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
Work on the pseudo code, not the code.
How important is the pseudo code?
Do you realize you are actually failing all of the tests?
The only cases that pass are those where the input string is already in final form.
You can not improve your English comprehension by studying Java.
You might be able to improve your Java skills by slowing down and working on one question at a time.
But I've said that several times already and you don't seem to agree.