For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
tripleUp:
public boolean tripleUp(int[] nums) { for (int i = 0; i <= nums.length - 3; i++) if (nums[i + 1] == nums[i] + 1 && nums[i + 2] == nums[i] + 2) return true; return false; }
shiftLeft:
public int[] shiftLeft(int[] nums) { if (nums.length > 0) { int first = nums[0]; for (int i = 0; i < nums.length - 1; i++) nums[i] = nums[i + 1]; nums[nums.length - 1] = first; } return nums; }
tenRun:
public int[] tenRun(int[] nums) { boolean replace = false; int multiple = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] % 10 == 0) { if (!replace) { replace = true; multiple = nums[i]; } else multiple = nums[i]; } if (nums[i] % 10 != 0 && replace) nums[i] = multiple; } return nums; }
pre4:
public int[] pre4(int[] nums) { int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 4) count++; else break; } int[] result = new int[count]; for (int i = 0; i < result.length; i++) result[i] = nums[i]; return result; }
post4:
public int[] post4(int[] nums) { int last4 = 0; for (int i = 0; i < nums.length; i++) if (nums[i] == 4) last4 = i; int[] res = new int[nums.length - (last4 + 1)]; for (int i = last4 + 1, j = 0; i < nums.length; i++, j++) res[j] = nums[i]; return res; }
notAlone:
public int[] notAlone(int[] nums, int val) { for (int i = 1; i < nums.length - 1; i++) if (nums[i] == val && nums[i - 1] != val && nums[i + 1] != val) nums[i] = Math.max(nums[i - 1], nums[i + 1]); return nums; }
zeroFront:
public int[] zeroFront(int[] nums) { int[] res = new int[nums.length]; int zeroPos = 0; int nonZeroPos = res.length - 1; for (int i = 0; i < nums.length; i++) if (nums[i] == 0) res[zeroPos++] = 0; else res[nonZeroPos--] = nums[i]; return res; }
Note that the order of the non-zero numbers does not matter. For an alternative solution that modifies the given array, please see the comment by ‘aaaaaaaa’ below.
withoutTen:
public int[] withoutTen(int[] nums) { int[] copy = new int[nums.length]; int index = 0; for (int i = 0; i < nums.length; i++) if (nums[i] != 10) { copy[index] = nums[i]; index++; } return copy; }
zeroMax:
public int[] zeroMax(int[] nums) { int largestOdd = 0; for (int i = nums.length - 1; i >= 0; i--) { if (nums[i] % 2 == 1 && nums[i] > largestOdd) largestOdd = nums[i]; if (nums[i] == 0) nums[i] = largestOdd; } return nums; }
evenOdd:
public int[] evenOdd(int[] nums) { int[] res = new int[nums.length]; int evenPos = 0; int oddPos = res.length - 1; for (int i = 0; i < nums.length; i++) if (nums[i] % 2 == 0) res[evenPos++] = nums[i]; else res[oddPos--] = nums[i]; return res; }
The solution is similar to “zeroFront”, which is given above.
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
your zero front solution is n^2 and others too….
public int[] zeroFront(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
nums[i] = nums[count];
nums[count] = 0;
count++;
}
}
return nums;
}
As I mentioned in the post, efficiency is hardly a concern, given these input sizes. But thanks for pointing this out.
The instructions on codingbat say to only use one loop.
You shouldn’t get too hung up on that. The instructions on the Array-2 page mention “2 loops”, even though you can solve some of them with just one loop. I’ll have a look later, though, and might rewrite the three solutions in which I used nested for-loops.
I don’t understand the solution to without ten. In particular, how are the elements of the initial array that ARE 10 replaced by zeros? What part of your code specifies that operation?
When you make a new array, it’s all 0’s til you change the number.
So in short, the code simply goes through, upping the index for the original array every time, and only upping the index on the new array when you’re adding something that’s not 10, thus it would add each number not 10 one by one to the new array, then the rest of the new array is left at the default 0.
Thank you for your clear explanation, which is very helpful.
Joseph
for tenRun small code which changes its elements itself..
for(int i=0;i<nums.length;i++)
{
int a = nums [i];
if (a%10==0)
{
if ( (i+1) < nums.length && nums [i+1] % 10! =0) nums [i+1] = a;
}
}
return nums;
}
Author’s solution is not N^2. It uses twice as much space, but it’s not doing any quadratic operations given it’s just one loop.
Shift left can use modulus
public int[] shiftLeft(int[] nums) {
int len = nums.length;
int[] result = new int[len];
for (int i = 0; i < len; i++) {
result[i] = nums[(i + 1) % len];
}
return result;
}
Wow nice code, just had to say that was brilliant to use the modulus to sort of reset it at the end, it would count up like normal til it got to i+1 == len, so of course the remainder would be 0 to grab the first. So neat ^_^
Here was mine, same basic idea only long winded:
public int[] shiftLeft(int[] nums) {
int res[] = new int[nums.length];
for(int i=0,j=1;i<res.length;i++,j++){//i is for res,j for nums
if(j==res.length)j=0;//resets j to get the first nums to put at end
res[i]=nums[j];
}
return res;
}
Why is my code not working? The only difference between my code and the solution give is instead of writing.
efirst[e++] I wrote
efirst[e];
e++.
When I changed the nation as below, it worked. Shouldn’t both work?
public int[] evenOdd(int[] nums) {
int [] efirst= new int [nums.length];
int e=0;
int o=efirst.length-1;
for (int i=0;i<nums.length;i++){
if (nums[i]%2==0)
efirst[e++]=nums[i];
e++;
if (nums[i]%2!=0)
efirst[o–]=nums[i];
o–;
}
return efirst;
}
Hey. Thanks for posting all these solutions. I wanted to comment on pre4 and post4.
In the codingbat Java > Array-2 is described as such:
Medium array problems — 1 loop. See the Java Arrays and Loops document for help.
The solution’s you provided use two loops, and there is actually a way to only use one. See my code @
[Admin: broken links removed; 18 Oct 2017]
Anyways. Thanks for putting all this together.
My solution for tenRun. Much simpler and it seems to work for all cases.
private static String tenRun(int[] nums) {
for (int i = 0; i < nums.length – 1; i++) {
if (nums[i] % 10 == 0) {
while (nums[i + 1] % 10 != 0) {
nums[i + 1] = nums[i];
}
}
}
return nums;
return Arrays.toString(nuns)
My solution is almost identical:
Actually, I do not really understand Gregor’s solution. Perhaps someone can explain the logic.
public int[] tenRun(int[] nums) {
for (int i= 0; i<nums.length; i++){
if(nums[i]%10==0)
for (int j=i+1; j<nums.length; j++){
if(nums[j]%10!=0) nums[j]=nums[i];
else break;}
}
return nums;
}
I’m new to this kind of For loop
for (int i = last4 + 1, j = 0; i < nums.length; i++, j++)
I only discovered this because of codingBat and Gregors solutions.
Basically i'm asking how the f. i train my brain to think like this because of 5 exercises where this kind of for loop was used i only managed to figure out one.
By myself
This just means that you initialize two variables, i and j, and increment both until a certain condition is no longer fulfilled (here: i < nums.length).
For TripleUp:
private static boolean trippleUp( int… nums )
{
for( int i = 0; i < nums.length – 2; i++ )
{
if( nums[i] == nums[i + 1] – 1 && nums[i + 1] == nums[i + 2] – 1 )
{
return true;
}
}
return false;
}
Cleaner solution:
public int[] evenOdd(int[] nums) {
int [] nums1 = new int [nums.length];
int evens = 0;
int odds = 0;
int even = 0;
int odd = 0;
for(int i = 0; i < nums.length; i++)
{
if(nums[i] % 2 == 0)
{
evens++;
even = nums[i];
}
else
{
odds++;
odd = nums[i];
}
}
for(int j = 0; j < evens; j++)
{
nums1[j] = even;
}
for(int k = evens; k < nums.length; k++)
{
nums1[k] = odd;
}
return nums1;
}
Wouldn’t this be much simpler solution to TenRun? It passed the test, hope I didn’t miss anything.
public int[] tenRun(int[] nums) {
for (int i=0; i<nums.length-1; i++){
if (nums[i] % 10 == 0 && nums[i+1] % 10 != 0) nums[i+1] = nums[i];
}
return nums;
}
Why must zeroMax start from int i=nums.length-1? What’s the difference between starting from 0?
Because in that case you are going in the array from right to left instead of left to right. If you start with i=0, you’ll have to refresh the largest odd number once a 0 passes it. Here is a solution in which i goes from 0:
public int[] zeroMax(int[] nums) {
int max = 0;
for(int i=0;i<nums.length-1;i++){
if(nums[i]==0){
max = 0;
for (int j=i;j=max) max = nums[j];
}
nums[i]=max;
}
}
return nums;
}
For some reason Ctrl+V didnt copy the code correctly so here is my solution:
public int[] zeroMax(int[] nums) {
int max = 0;
for(int i=0;i<nums.length-1;i++){
if(nums[i]==0){
max = 0;
for (int j=i;j=max) max = nums[j];
}
nums[i]=max;
}
}
return nums;
}
I’m learning to code… In Rust… This is my solution for withoutTen:
fn withoutTen(mut array: Vec) -> Vec {
let mut i = 0;
let a_len = array.len();
while i < array.len() {
if array[i] == 10 {
array.remove(i);
array.push(0);
} else {
i += 1;
}
}
return array;
}
Returns for the tests on CodingBats:
withoutTen: [1, 2, 0, 0]
withoutTen: [2, 0, 0]
withoutTen: [1, 99, 0]
withoutTen: [13, 14, 0, 0]
withoutTen: [13, 14, 0, 0, 0]
withoutTen: [3, 0, 0]
withoutTen: [1]
withoutTen: [13, 1]
withoutTen: [0]
withoutTen: []
Whoops… left an unused variable in the code. Here’s the final version:
fn withoutTen(mut array: Vec) -> Vec {
let mut i = 0;
while i < array.len() {
if array[i] == 10 {
array.remove(i);
array.push(0);
} else {
i += 1;
}
}
return array;
}