Leetcode Move Zeroes: where are my bugs? - java

This is a easy question. a link
I just tried the simple insertionSorting-like method to solve it, however it failed.
public class Solution {
public void moveZeroes(int[] nums) {
for(int i = 0; i < nums.length; i++)
if(nums[i] == 0){
for(int j = i; j<nums.length-1; j++)
nums[j] = nums[j+1];
nums[nums.length-1] = 0;
}
}
}
Could anyone help me to debug my method?

So i went through your code and re-wrote it to work. If you're moving zeroes to the end this should work perfectly fine for you. Cheers.
What this code does is iterates over your array until it hits a zero. Upon hitting a zero it loops, moving the 0 one place to the right over and over, switching spots with the value to the right of it, until the 0 is moved to the end of the array.
example: 5-loop cycle
[0,1,0,2,3] > [1,0,0,2,3] > [1,0,0,2,3] > [1,0,2,0,3] > [1,0,2,3,0]
int[] array = new int[] {0,1,0,12,3};
for (int x = 0; x < array.length; x++) {
if (array[x] == 0) {
for (int y = x; y < array.length; y++) {
if (y != array.length-1) {
// Store our replacement as temp
int temp = array[y+1];
// Move 0 to the right +1
array[y+1] = array[y];
// Set current position to what used to be right+1
array[y] = temp;
}
}
}
}

I'll share the javascript solution for the Move Zeroes problem from leetcode. It has O(n) time complexity.
Optimized for many zeroes
const snowball1 = nums => {
let i = 0;
let j = 0;
while (i<nums.length) {
if(nums[i] != 0) {
nums[j] = nums[i];
j++;
}
i++;
}
console.log(j);
nums.fill(0, j);
return nums;
}
Optimized for fewer zeroes
const snowball2 = nums => {
for(let i = nums.length; i--;){
if(nums[i]===0){
nums.splice(i,1)
nums.push(0);
}
}
return nums
}
Example
console.log(snowball1([0,0,1,0,0,3,0,12,0]));
console.log(snowball2([0,1,0,3,12]));

I'll try to write a very intuitive and easy approach to this problem. This problem can be solved using 2 indexes, one of which is the read pointer(rp) and the other is the write pointer(wp).
If rp reads a value of 0, it sets the wp to this index. The rp then keeps incrementing until it finds a non-zero value. If it does it overwrites the value at wp and this process fills the non-zero values in the beginning.
We then just need to fill the remaining spots with zeros till the end. Here's a short solution in python:
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
wp=rp=0
while(rp<len(nums)):
if(nums[rp]!=0):
nums[wp]=nums[rp]
wp+=1
rp+=1
for i in range(wp,len(nums)):
nums[i]=0

Please find the optimal solution wrt space O(N) and time O(1) below
public void moveZeroes(int[] nums) {
// [0,1,0,3,12]
int j = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
j++;
}
}
}

Related

How to compare two arrays of different sizes?

So my task is to read a file line by line and store the integers into an array. Then to add the integers in spots 1-5, 2-6, 3-7 etc. and store those into a new array.
In array 1 there is 4 more values than array 2. I need to compare these Arrays and see if array1 is 0.999 bigger than array2.
If it is indeed larger, I need to print out the LOCATION of the number in the array 1.
Right now my problem is my code is outputting that every number is larger than the corresponding number in array 2.
Code:
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Asgn7
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner file = new Scanner(new File("asgn7data.txt"));
double[] array = new double[file.nextInt()];
double[] newArray = new double[array.length - 4];
double tempVal = 0;
int j = 0;
int count = 0;
while(file.hasNext())
{
for(int i = 0; i < array.length ; i++)
{
array[i] = file.nextInt();
}
for(j = 0; j < array.length - 4; j++)
{
for(int k = 0; k < 5; k++)
{
newArray[j] += array[j+k] / 5;
}
}
for(int i = 2; i < array.length; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
}
}
}
The values which should be compared are from 3-13.
Judging by the picture, you are not placing the values in the correct index in the second array, or you are not matching the correct ones.
If you want it to look exactly like in the picture, the second array should be declared:
double[] newArray = new double[array.length - 2];
And the loop to fill it should be changed to:
for(j = 2; j < array.length - 2; j++)
{
for(int k = -2; k <= 2; k++)
{
newArray[j] += array[j+k] / 5;
}
}
This will put the averages in the third, fourth, fifth... elements in newArray. And now you can compare them directly:
for(int i = 2; i < array.length - 2; i++)
{
if(array[i] > (newArray[i] + 0.999))
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
If you want to save the two unused spaces, as you originally did, rather than responding exactly to the picture, then you should calculate the values as you originally did. But remember to compare each element to the one two places before it and stop 2 places before the end.
Instead of
for(int i = 2; i < array.length; i++)
use
for(int i = 2; i < array.length - 2; i++)
To print the location, your construct with the count and tempVal is unnecessary. You just need to print i+1. Also note that you have a ; after your if. This means it's an empty if, and the block after it is always performed. Never have a ; after an if, for, while etc.
Not clear with what you are asking for in your question but without questioning what's the logic, by just looking at your code:
for(int i = 2; i < array.length; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
}
if you relocate the system.out line as follows, I think you will get what you expect as follows:
for(int i = 2; i < array.length - 2; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
System.out.println(tempVal);
// count++;
// tempVal = count;
}
}
}
PS: Please note that I have also changed the boundary for the loop to stop iteration on 13th member of the array, instead of 15.
Are you sure you're parsing the numbers correctly?
See Java: Reading integers from a file into an array
Why don't you print them out after parsing for verification?
btw, this will overflow the index of the 2nd array (since it is created using new double[array.length - 4]):
for(int i = 2; i < array.length; i++)
so does your code run?

Find majority number in an array

This question has been asked before. However I just want to know what is wrong with my code. It passes most of the test cases but not all of them on lintcode.
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.
public class Solution {
/**
* #param nums: a list of integers
* #return: find a majority number
*/
public int majorityNumber(ArrayList<Integer> nums) {
// write your code
Collections.sort(nums);
int j = 0, count = 0, out = 0, size = nums.size();
for(int i = 0; i < size; i++) {
if(nums.get(j) == nums.get(i)) {
count++;
if(count > size/2){
out = nums.get(i);
}
} else {
count = 1;
j = i;
}
}
return out;
}
}
EDIT
I changed the code to j = i & count = 1 as suggested by an answer.
For example for the input [1,1,1,2,2,2,2] the output should be 2.
My code works in this case. It doesn't work in large input cases.
I don't want another solution, as I have already found many O(n) solutions on other sites. I just want to fix my own code and know what I am doing wrong.
There's a smart solution that runs in O(n) worst case time, and no additional space:
public static int majorityNumber(List<Integer> nums) {
int candidate = 0;
int count = 0;
for (int num : nums) {
if (count == 0)
candidate = num;
if (num == candidate)
count++;
else
count--;
}
return candidate;
}
Note that it assumes the existence of a majority value, otherwise it returns an arbitrary value.
In your else block do:
...
else {
count = 1;
j = i;
}
Debug your code and print the values of i and j. I'm sure that will not be what you wanted to do.
You wanted to read each element and count it's frequency. That would be a O(n*n)(because the array is sorted O(n log(n))) solution.
ArrayList<Integer> readNums = new ArrayList();
for(int i = 0; i < a.length; ++i)
{
int currentNum = a[i];
if(list.contains(currentNum)
continue;//to check if you already processed that num
else
list.add(currentNum);
int count = 0;
for(int j = i + 1; j < a.length; ++j)
{
if(currentNum == a[j])
count ++;
}
if(count > size / 2)
reqNum = currentNum;
}
This is what you wanted to do.
A better method would be to use a space of O(n) and track the frequencies and then process the array in O(n).
HashMap<Integer, Intege> map = new HashMap();
for(int i = 0; i < a.length; ++i)
{
int currentNum = a[i];
int count = 1;
if(map.containsKey(currentNum))
{
count = map.getValue(currentNum);
map.put(currentNum, count + 1);
count ++;
}
else
{
map.put(currentNum, count);
}
if(count > size / 2)
reqNum = currentNum;
}

I need to sort through an object array and order them highest to lowest

I am a student in a beginning java class, I got some help earlier today on my assignment, which really helped! So I thought I would give it one more try, before I throw in the towel on this last part. I have been able to get everything going, but my sort just doesn't work. I have to use this format, as my professor does not want us to use sort APIs. It processes correctly, meaning I get the same results by hand as when I run it, so I think the problem is in the logic itself. Can anyone see what I am doing wrong and offer any hints or helps. Thanks in advance. Here is my code for my sort loop:
int i, j; // used to index into the array
double temp;
for (i = 1; i < count ; ++i) {
temp = students[i].getGPA();
j = i - 1;
while (j >= 0 && temp < students[j].getGPA())
{
students[j + 1] = students[j];
j = j - 1;
}
students[j + 1]= students[i];
}
Your are not doing the swapping correctly. Check this sample code:
for (int i = 0; i < students.length; i++)
{
for (int j = 1; j < students.length - i; j++)
{
if (students[j - 1].getGPA() > students[j].getGPA())
{
// assuming that your class name is Student
Student temp = students[j - 1];
students[j - 1] = students[j];
students[j] = temp;
}
}
}
Your problem is that the first iteration through the while loop overwrites students[i]. You need to keep students[i], and not just its GPA, in a temporary value when you enter the for loop.
Refined your logic little bit. Plz verify for appropriateness!
package com.kvvssut.misc;
public class SortArray {
public static void main(String[] args) {
double [] studentsGPA = {8.3,7.2,10,6.5,4.9};
int count = studentsGPA.length;
int i, j; // used to index into the array
double temp;
for (i = 1; i < count ; ++i) {
j = i;
while (j > 0 && (temp = studentsGPA[j]) < studentsGPA[--j]) { // can use students[j].getGPA() similarly
studentsGPA[j+1] = studentsGPA[j];
studentsGPA[j] = temp;
}
}
for (double sgpa : studentsGPA ) {
System.out.println(sgpa);
}
}
}

Specific comparison of two arrays in java

There are many symbol games working in this way so this should sound familiar to you.
Facts:
I have two arrays with same length of 4.
(A[4] and B[4])
I fill them with random integers from 1 to 6.
I can NOT sort them in any way (they must stay the same).
Problems:
I need to compare them and after that I need to have 3 values. FIRST one needs to count how many elements are the same and in the same place. I do it like this and it is working:
int first = 0;
int k = 0;
for (int j=1; j<=4; j++)
{
k++;
if (A[k] == B[j])
{
first++;
}
}
SECOND one needs to count how many elements are the same BUT not at the same place. THIRD one needs to count how many elements are not the same at all.
I need a solution to count either SECOND or THIRD number, because after that I can just subtract like 4-(first+second) or 4-(first+second).
Here's the logic you should use: loop over the first array; for each element, check if the corresponding element of the second array is the same - if yes, increment your first counter. If they are not the same, then check whether the second array contains the corresponding element of the first array. If it does, then it's definitely not in the same position (you just checked same positions) - increment your second count. Otherwise, increment your third count. The code can be as following:
int[] A = {...};
int[] B = {...};
List<Integer> lstB = new ArrayList<Integer>(B.length);
for (int index = 0; index < B.length; index++) {
lstB.add(B[index]);
}
int first = 0, second = 0, third = 0;
for(int i=0; i<4; i++) {
if(A[i] == B[i]) {
first++;
}
else if(lstB.contains(A[i]) {
second++;
}
else {
third++;
}
}
SOLUTION
Eventually I made the right algorithm. In general, the solution is to keep track of what fields you used when counting FIRST value. And here is the code:
int first = 0;
int second = 0;
int third = 0;
boolean[] codeUsed = new boolean[4];
boolean[] guessUsed = new boolean[4];
//same value and same place
for (int i = 0; i < 4; i++)
{
if (A[i] == B[i])
{
first++;
codeUsed[i] = guessUsed[i] = true;
}
}
//same value but not right place
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (!codeUsed[i] && !guessUsed[j] && A[i] == B[j])
{
second++;
codeUsed[i] = guessUsed[j] = true;
break;
}
}
}
//not the same value
third = 4 - first - second;

finding time and space complexity of the given java code

hi i need to find the time and space complexity of the program, pls help, if possible please suggest the optimization that can be performed,
.........................................................................................................................................................................................
public class Sol {
public int findMaxRectangleArea(int [][] as) {
if(as.length == 0)
return 0;
int[][] auxillary = new int[as.length][as[0].length];
for(int i = 0; i < as.length; ++i) {
for(int j = 0; j < as[i].length; ++j) {
auxillary[i][j] = Character.getNumericValue(as[i][j]);
}
}
for(int i = 1; i < auxillary.length; ++i) {
for(int j = 0; j < auxillary[i].length; ++j) {
if(auxillary[i][j] == 1)
auxillary[i][j] = auxillary[i-1][j] + 1;
}
}
int max = 0;
for(int i = 0; i < auxillary.length; ++i) {
max = Math.max(max, largestRectangleArea(auxillary[i]));
}
return max;
}
private int largestRectangleArea(int[] height) {
Stack<Integer> stack =
new Stack<Integer>();
int max = 0;
int i = 0;
while(i < height.length) {
if(stack.isEmpty() ||
height[i] >= stack.peek()) {
stack.push(height[i]);
i++;
}
else {
int count = 0;
while(!stack.isEmpty() &&
stack.peek() > height[i]) {
count++;
int top = stack.pop();
max = Math.max(max, top * count);
}
for(int j = 0; j < count + 1; ++j) {
stack.push(height[i]);
}
i++;
}
}
int count = 0;
while(!stack.isEmpty()) {
count++;
max = Math.max(max, stack.pop() * count);
}
return max;
}
thank you in advance
To find the space complexity take a look at the variables you declare and are larger than a single primitive variable. In fact I believe your space complexity will be determined my the array auxilary and the Stack stack. The size of the first one is pretty clear and I don't completely understand the second one but I see it's size will never be greater than the one of the array. So I would say the space complexity is O(size of(auxilary)) or O(N * M) where N=as.length() and M = as[0].length.
Now the time complexity is a bit trickier. You have two cycles over the whole auxilary array so for sure time complexity is at least O( N * M). You also have another cycle that invokes largestRectangleArea for each row of auxilary. If I get the code in this function correctly it seems this function is again linear, but I am not sure here. Since you know the logic better probably you will be able to compute its complexity better.
Hope this helps.

Categories