Problem:
Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array.
zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1}
zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zeroFront({1, 0}) → {0, 1}
Solution:
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; }
public int[] zeroFront(int[] nums)
ReplyDelete{
//imma dumb fuck so i just rushed and decided to sort the new list back, so
//to do that we will need to keep record of the new spots
//also we will check backwards
int count = nums.length - 1;
int[] temp = new int[nums.length];
for (int i = nums.length; i > 0; i--)
{
if (nums[i-1] != 0) //so if its not a zero
{
temp[count] = nums[i-1];
//puts it at the last position avaliable
count--;
//and takes record of that
}
}
//all the numbers that are not assigned a number are already initialized as 0 so we just
return temp;
}
// simple logic: count number of 0's in array and put 0 until that position and after that put other digit which is present.
Deletepublic int[] zeroFront(int[] nums) {
int ctr=0; //to find number of 0's
int num=0; // to store value which is there another than 0
int len=nums.length; // length of original array
int ans[]=new int[len]; //new array
for(int i=0;i<len;i++){
if(nums[i]==0) //check how many 0 are there
ctr++;
else // store the other digit in num
num=nums[i];
}
for(int i=0;i<len;i++){
if(i<ctr) //until i is below ctr we need 0 in new array
ans[i]=0;
else // after that we need the remaining value
ans[i]=num;
}
return ans;
}
public int[] zeroFront(int[] nums) {
ReplyDeleteint count=0;
int index=0;
int val=0;
int[] arr=new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if(nums[i]==0){
arr[i]=0;
index++;
}
else { count++; val=nums[i];}
}
for (int i = 0; i < count; i++) {
arr[index]=val;
index++;
}
return arr;
}
public int[] zeroFront(int[] nums) {
ReplyDeleteint nums2[] = new int[nums.length];
int count = 0;
for (int i = nums.length - 1; i >= 0; i--)
{
if (nums[i] != 0)
{
nums2[i + count] = nums[i];
}
else
{
count++;
}
}
return nums2;
}
public int[] zeroFront(int[] nums) {
ReplyDeleteint nums2[] = new int[nums.length];
int count = 0;
for (int i = nums.length - 1; i >= 0; i--)
{
if (nums[i] != 0)
{
nums2[i + count] = nums[i];
}
else
{
count++;
}
}
return nums2;
}
public int[] zeroFront(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++)
if (nums[i] == 0)
nums[i] = Integer.MIN_VALUE;
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++)
if (nums[i] == Integer.MIN_VALUE)
nums[i] = 0;
return nums;
}
public boolean either24(int[] nums) {
ReplyDeleteint j = 0;
boolean result = false;
for(int i = 1; i<nums.length; i++){
if(nums[j] == 2 && nums[i] == 2) {
if(result){
return false;
} else {
result = true;
}
}
if(nums[j] == 4 && nums[i] == 4) {
if(result) {
return false;
} else {
result = true;
}
}
j++;
}
return result;
}
public int[] zeroFront(int[] nums) {
ReplyDeleteint current = nums.length-1;
for(int i = nums.length-1; i>=0; i--){
if(Math.abs(nums[i])!= 0){
nums[current] = nums[i];
current--;
}
}
while (current >= 0) {
nums[current] = 0;
current--;
}
return nums;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint j=0;
int[] arr = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 10) {
continue;
}
else
{
arr[j]=nums[i];
j++;
}
}
return arr;
}
public int[] zeroFront(int[] nums) {
ReplyDeleteint countzero = 0;
int count = 0;
int notZero = 0;
for ( int i = 0 ; i < nums.length ; i++ ) {
if( nums[i] == 0){
countzero++;
}else{
count++;
notZero = nums[i];
}
}
for ( int j = 0 ; j < nums.length ; j++){
nums[j] = 0;
if(j >= countzero){
nums[j] = notZero;
}
}
return nums;
}
public int[] zeroFront(int[] nums) {
ReplyDelete//No new array allocation algorithm solution//
int i = 0;
for (int j = 1; j < nums.length; j++)
{
if (nums[i] == 0)
{
i++;
}
else if (nums[j] == 0)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
return nums;
}
// Easiest solution maybe
ReplyDeletepublic int[] zeroFront(int[] nums) {
int zeros = 0;
int[] arr = new int[nums.length];
for(int i=0;i<nums.length;i++){
if(nums[i] ==0) zeros +=1;
}
for(int i=0; i<zeros;i++){
arr[i] = 0;
}
for(int i=0;i<nums.length;i++){
if(nums[i] !=0){
arr[zeros++] = nums[i];
}
}
return arr;
}
Not the best solution but it uses nested for loops.
ReplyDeletepublic int[] zeroFront(int[] nums)
{
int temp = 0;
for (int i = 0; i < nums.length-1; i++)
{
for (int j = 0; j < nums.length; j++)
{
if (j != 0 && nums[j] == 0)
{
temp = nums[j-1];
nums[j-1] = 0;
nums[j] = temp;
}
}
}
return nums;
}
for(int i = 0; i<nums.length-1; i++) {
ReplyDeletefor(int j = 0; j<nums.length-1; j++) {
if(nums[j] != 0) {
int tmp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = tmp;
}
}
}
return nums;