Problem:
Return a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces a the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given array or make a new array.
withoutTen({1, 10, 10, 2}) → {1, 2, 0, 0}
withoutTen({10, 2, 10}) → {2, 0, 0}
withoutTen({1, 99, 10}) → {1, 99, 0}
Solution:
public int[] withoutTen(int[] nums) { int[] result = new int[nums.length]; int j = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] == 10) { } else { result[j] = nums[i]; j++; } } for(int i = j; i < nums.length; i++) { result[i] = 0; } return result; }
If you create new array, it will be filled zero's value by default. So, it is possible to solve this problem like this:
ReplyDeletepublic int[] withoutTen(int[] nums) {
int[] tab = new int[nums.length];
int counter = 0;
for (int i = 0; i < nums.length; i++)
{
if (nums[i] != 10)
tab[counter++] = nums[i];
}
return tab;
}
Great solution!
Deletewithout creating new array but 2 cycles:
ReplyDeletestatic int[] withoutTen(int[] nums) {
int len = nums.length;
for(int i=0; i<len; i++){
if(nums[i]==10){
for(int j=i; j<len-1; j++){
nums[j] =nums[j+1];
}
nums[len-1]=0;
i--;
}
}
return nums;
}
this is THE BEST SOLUTION i need to know if we create new array it will cost memory but this one will take O(n2) it depends on ,THANKS
DeleteThis is a different approach looks heavy but it isnt reallly
ReplyDeletepublic int[] withoutTen(int[] nums) {
int ten = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 10)
for(int j = i; j < nums.length; j++)
if(nums[j] != 10){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
for(int i = 0; i < nums.length; i++)
if(nums[i] == 10)
nums[i] = 0;
return nums;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint count=0;
int index=0;
int i=0;
int[] arr=new int[nums.length];
while(i<nums.length){
if(nums[i]!=10){
arr[index]=nums[i];
index++;
i++;
}
else {count++; i++;}
}
for (int j = 0; j < count; j++) {
arr[index]=0;
index++;
}
return arr;
}
Using only one loop---
ReplyDeletepublic int[] withoutTen(int[] nums) {
int result = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 10){
int temp = nums[i];
nums[i] = 0;
nums[result] = temp;
result++;
}else{
nums[i] = 0;
}
}
return nums;
}
Quite interesting u just swapped it cleverly 💖
Deletepublic int[] withoutTen(int[] nums) {
ReplyDeleteint count10 = 0;
int not10= 0;
int[] arr = new int[nums.length];
for(int i = 0; i < nums.length; i++){
if(nums[i] != 10){
arr[not10] = nums[i];
not10++;
}else{
arr[nums.length - count10 - 1] = 0;
count10++;
}
}
return arr;
}
public static int[] withoutTen(int[] nums) {
ReplyDeleteint index = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i] != 10) {
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
index++;
}
if(nums[i] == 10) {
nums[i] = 0;
}
}
return nums;
}
I liked my solution.
ReplyDeleteint [] d= new int [nums.length];
int count=0;
for(int i=0;i<nums.length;i++) {
if(nums[i]!=10) {
d[count++]=nums[i];
}
}
return d;
public int[] withoutTen(int[] nums) {
ReplyDeleteint[] r = new int[nums.length];
int x = 0;
for(int i = 0 ; i < nums.length ; i++) {
if(nums[i] != 10) {
r[x] = nums[i];
x++;
}
}
return r;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint currentShift =0;
int medium =0;
for(int i=0; i<nums.length; i++){
if(nums[i] ==10 || nums[i] ==0 ){
currentShift ++;
nums[i]=0;
}
if(nums[i]!=10 && currentShift !=0 && nums[i]!=0){
medium = nums[i];
nums[i-currentShift] = medium;
nums[i]=0;
currentShift =1;
}
}
return nums;
}
One for loop, without creating new array
ReplyDeletepublic int[] withoutTen(int[] nums) {
for(int i = 0, j = 0; j < nums.length; j++) {
if(nums[j] == 10) {
nums[j] = 0;
}
else {
if(nums[i] == 0) {
nums[i] = nums[j];
nums[j] = 0;
}
i++;
}
}
return nums;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint a[]=new int[nums.length];
int k=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==10)
continue;
else
a[k++]=nums[i];
}
return a;
}
public int[] withoutTen(int[] nums) {
ReplyDelete//Two Pointer One Loop Solution//
int i = 0;
for (int j = 0; j < nums.length; j++)
{
if (nums[j] == 10)
{
while (i < nums.length && nums[i] == 10)
{
i++;
}
if (i < nums.length)
{
nums[j] = nums[i];
nums[i] = 10;
}
else
{
nums[j] = 0;
}
}
i++;
}
return nums;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint i = 0, j = 0;
while (j < nums.length) {
if (nums[j] != 10) {
nums[i++] = nums[j];
}
j++;
}
while (i < nums.length) {
nums[i++] = 0;
}
return nums;
}
public class WithoutTen {
ReplyDeletepublic int[] withoutTen(int[] nums) {
int[] tab = new int[nums.length];
int counter = 0;
for (int num : nums) {
if (num != 10) {
tab[counter++] = num;
}
}
return tab;
}
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint[] myArray = new int[nums.length];
int i =0;
int j=0;
while(i<nums.length) {
if(nums[i] != 10) {
myArray[j]=nums[i];
i++;
j++;
} else{
i++;
}
}
return myArray;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint[] result = new int[nums.length];
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 10) {
//روح حطها في الاراي الجديده من اولها
result[j] = nums[i];
j++;
}
}
/*for(int i = j; i < result.length; i++) { //املا الباقي ب اصفار
result[i] = 0;
}*/
return result;
}
/*public int[] withoutTen(int[] nums) {
ReplyDeleteint c= nums.length-1;
for( int i=0 ; i<nums.length ; i++){
if(nums[i]==10){
for(int j=i+1 ; j<nums.length ; j++){
nums[j-1]=nums[j];
}
nums[c]=0; c--;
}
}
return nums;
}
*/
for(int j=i+1 ; j<nums.length ; j++){
Deletenums[j-1]=nums[j];
}
بتعملها شيفت
nums[c]=0; c--;
وتصفر الاخيره
public int[] withoutTen(int[] nums) {
ReplyDeleteint[] arr=new int[nums.length];
int j=0;
for(int i=0;i<nums.length;i++)
if(nums[i]!=10){
arr[j]=nums[i];
j++;
}
for(int i = j; i < nums.length; i++) {
arr[i] = 0;
}
return arr;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint array[] = new int[nums.length];
int index=0, index1=1;
for(int i=0; i<nums.length; i++){
if(nums[i]!=10) {
array[index]=nums[i];
index++;
} else {
array[array.length-index1]=0;
index1++;
}
}
return array;
}
without new array in python
ReplyDeletedef withoutTen(n):
c = 0
for i in range(len(n)):
if n[i] != 10:
n.insert(c,n.pop(n.index(n[i])))
c += 1
else:
n[i] = 0
return n
The second for loop at the bottom is completely unnecessary, as an array of ints in Java automatically populates empty spaces with the default value of 0.
ReplyDeletepublic int[] withoutTen(int[] nums) {
ReplyDeleteint j = 0;
int[] t = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 10) {
t[j] = nums[i];
j++;
}
}
return t;
}
With Java Stream
ReplyDeletepublic int[] withoutTen(int[] nums) {
return java.util.stream.IntStream.of(nums).boxed()
.map(i -> i == 10 ? 0 : i)
.sorted(Comparator.comparing(n -> n == 0))
.mapToInt(i->i).toArray();
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint arr[]=new int[nums.length];
int c=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==10){
nums[i]=0;
}
if(nums[i]!=10&&nums[i]!=0){
arr[c]=nums[i];
c++;
}
}
return arr;
}
public int[] withoutTen(int[] nums) {
ReplyDeleteint count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 10) {
nums[i] = 0;
}
else {
count++;
}
}
boolean sorted = false;
while (!sorted(nums,count)) {
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] != 0 && nums[i-1] == 0) {
nums[i-1] = nums[i];
nums[i] = 0;
}
}
}
return nums;
}
public boolean sorted(int[] nums, int count) {
for (int i = 0; i < count; i++) {
if (nums[i] == 0) {
return false;
}
}
return true;
}