Problem:
Return an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given array, or make a new array.
evenOdd({1, 0, 1, 0, 0, 1, 1}) → {0, 0, 0, 1, 1, 1, 1}
evenOdd({3, 3, 2}) → {2, 3, 3}
evenOdd({2, 2, 2}) → {2, 2, 2}
Solution:
public int[] evenOdd(int[] nums) { int countE = 0; int countO = nums.length-1; int[] array = new int[nums.length]; for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 == 0) { array[countE] = nums[i]; countE++; } else { array[countO] = nums[i]; countO--; } } return array; }
I know why my solution doesn't work, but I can't seem to figure out how to fix it. Can you find a way to fix my code?
ReplyDeletepublic int[] evenOdd(int[] nums) {
int[] result = nums;
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] % 2 == 1)
{
result[nums.length - 1] = nums[i];
for (int n = 0; n < nums.length - 1; n++)
{
result[i] = nums[i + 1];
}
}
}
return result;
}
public int[] evenOdd(int[] nums) {
ReplyDeleteint start = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] % 2 == 0) {
int temp = nums[i];
nums[i] = nums[start];
nums[start] = temp;
start++;
}
}
return nums;
}
int len = nums.length;
ReplyDeleteint temp = 0;
int pos = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
temp = nums[pos];
nums[pos] = nums[i];
nums[i] = temp;
pos++;
}
}
return nums;
public int[] evenOdd(int[] nums) {
ReplyDeleteint indexE=0;
int indexO=nums.length-1;
int[] arr=new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if(nums[i]%2==0){
arr[indexE]=nums[i];
indexE++;
}
else{
arr[indexO]=nums[i];
indexO--;
}
}
return arr;
}
This is my solution. It is no the best and i think that it is a little dummie, but i think that is a little intuitive
ReplyDeletepublic int[] evenOdd(int[] nums) {
int odd=0;
int even=0;
for(int i=0;i<nums.length;i++) {
if(nums[i]%2==0) even++;
if(nums[i]%2!=0) odd++;
}
int[]oddArr= new int[odd];
int aux=0;
for(int i=0;i<nums.length;i++) {
if(nums[i]%2!=0)
oddArr[aux++]=nums[i];
}
int [] evenArr= new int[even];
int count=0;
for(int j=0;j<nums.length;j++) {
if(nums[j]%2==0) {
evenArr[count]=nums[j];
count++;
}
}
int [] finish= new int [evenArr.length+oddArr.length];
int fls=0;
for(int i=0;i<evenArr.length;i++) {
finish[i]=evenArr[i];
fls++;
}
for(int j=0;j<oddArr.length;j++) {
finish[fls++]=oddArr[j];
}
return finish;
}
public int[] evenOdd(int[] nums) {
ReplyDeleteint pos = 0;
for (int i = nums.length - 1; i >= 0 && pos < i; i--)
{
while (pos < i && nums[pos] % 2 == 0)
{
pos++;
}
if (nums[i] % 2 == 0)
{
int temp = nums[pos];
nums[pos] = nums[i];
nums[i] = temp;
}
}
return nums;
}
public int[] evenOdd(int[] nums) {
ReplyDelete//Two Pointer Solution One loop//
int i = 0;
for (int j = 1; j < nums.length; j++)
{
if (nums[j] % 2 == 0 && nums[i] % 2 != 0)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
else if (nums[i] % 2 == 0)
{
i++;
}
}
return nums;
}
public static int[] evenOdd(int[] nums) {
ReplyDeleteint count = 0;
int countOdd= 0;
int[] numsNew = new int[nums.length];
for(int i=0; i<nums.length; i++){
if(nums[i]%2==0){
numsNew[count]=nums[i];
count++;
}
else {
countOdd++;
numsNew[nums.length-countOdd]=nums[i];
}
}
return numsNew;
}
public int[] evenOdd(int[] nums) {
ReplyDeleteint countE = 0; //زوجي
int countO = nums.length-1; //حط الفردي من الاخر للاول
int[] arr = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
arr[countE] = nums[i];
countE++;
}
else {
arr[countO] = nums[i];
countO--;
}
}
return arr;
}
/*
public int[] evenOdd(int[] nums) {
int[] arr= new int[nums.length];
int c=0;
for(int i= 0 ; i <nums.length ; i++){
if(nums[i]%2==0){arr[c++]=nums[i];}
}
for(int i= 0 ; i <nums.length ; i++){
if(nums[i]%2!=0){arr[c++]=nums[i];}
}
return arr;
}
*/
public int[] evenOdd(int[] nums) {
ReplyDeleteint count=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]%2 == 0)
{
int temp = nums[i];
nums[i] = nums[count];
nums[count] = temp;
count++;
}
}
return nums;
}
Found a way that is pretty clean and keeps the array in order if the need ever arose. Took the concept Nishi used and made the program work a little longer to keep things in order.
ReplyDeletepublic int[] evenOdd(int[] nums) {
for(int i=0;i nums[i+1] % 2) { //If i is odd and i+1 is even
int temp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = temp;
if (i != 0) i -= 2; \\ prevent i = -1 during code
else i--;
}
}
return nums;
}
Testing if it shows my name now
Deletepublic int[] evenOdd(int[] nums) {
ReplyDeleteint[] newArray = new int[nums.length];
int iterations = 0;
int i = 0;
for (int x = 0; x < nums.length ; x++){
if(iterations == 1 && nums[x] % 2 == 1){
newArray[i] = nums[x];
i++;
}
if(iterations == 0 && nums[x] % 2 == 0 ){
newArray[i] = nums[x];
i++;
}
if(x==nums.length - 1){
iterations++;
x=-1;
}
if (iterations == 2) {
break;
}
}
return newArray;
}
public int[] evenOdd(int[] nums) {
ReplyDeleteint[] newArray = new int[nums.length];
int x = 0;
for(int i=0; i < nums.length; i++){
if(nums[i] % 2 == 0){
newArray[x]=nums[i];
x++;
}
}
for(int i=0; i < nums.length; i++){
if(nums[i] % 2 != 0){
newArray[x]=nums[i];
x++;
}
}
return newArray;
}
public int[] evenOdd(int[] nums)
Delete{
int j = 0;
for(int i=0;i<nums.length;i++)
{
if(nums[i] % 2 == 0)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
j++;
}
}
return nums;
}
With Java Stream
ReplyDeletepublic static int[] evenOdd(int[] nums) {
int[] even = Arrays.stream(nums).filter(i -> i % 2 == 0).toArray();
int[] odd = Arrays.stream(nums).filter(i -> i % 2 != 0).toArray();
return IntStream.concat(Arrays.stream(even), Arrays.stream(odd)).toArray();
}
public int[] evenOdd(int[] nums)
ReplyDelete{
int j = 0;
for(int i=0;i<nums.length;i++)
{
if(nums[i] % 2 == 0)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
j++;
}
}
return nums;
}
public int[] evenOdd(int[] nums) {
ReplyDeleteint a[] = new int[nums.length];
int b[] = new int[nums.length];
int odd = 0;
int even = 0;
for(int i=0 ; i<nums.length ; i++){
if(nums[i]%2==0){
a[even] = nums[i];
even++;
}
else {
b[odd] = nums[i];
odd++;
}
}
for(int j=0 ; j<odd ; j++){
a[even] = b[j];
even++;
}
return a;
}