I want this code to separate even and odd numbers - java

It prints all the even numbers, but is only printing the odd numbers 13 and 11. Not 3 or 5. Does anyone know what I am doing wrong? Thanks in advance.
public class ReadjustingArray {
public static void main (String[]args){
int[]A={13,3,4,6,8,5,10,11};
int temp=0;
for (int i=0; i< A.length; i++){
if (A[i] % 2 ==0){
temp=A[i];
A[i-1] = A[i];
temp=A[i];
System.out.print(A[i] + " ");
}
}
for (int j=0; j< A.length; j++){
if (A[j] % 2 !=0){
System.out.print(A[j] + " ");
}
}
}
}

Don't modify the array with your first loop, then your second loop will work as you expect. Alternatively, in Java 8+, you might use IntStream and filter like
int[] A = { 13, 3, 4, 6, 8, 5, 10, 11 };
IntStream.of(A).filter(x -> x % 2 == 0)
.forEachOrdered(x -> System.out.printf("%d ", x));
IntStream.of(A).filter(x -> x % 2 != 0)
.forEachOrdered(x -> System.out.printf("%d ", x));
System.out.println(); // <-- Adds a new line (and an implicit flush)

this can help you.
public static void main(String[] args) {
int[] A = { 13, 3, 4, 6, 8, 5, 10, 11 };
for (int i : A) {
if (isEven(i)) {
System.out.println(i + " is even");
} else {
System.out.println(i + " is odd");
}
}
}
static boolean isEven(int number) {
if ((number % 2) == 0) {
return true;
}
return false;
}

There is no need to modify the Array. Just print the odds in the same way you are printing the evens. Use their corresponding comparative logic.

Related

insertion sort is not outputting right

My condition is not outputting the right numbers, the list is not changed trough the loop, this is my condition.
if (j + 1 <= length)
{
insertionSort(arrayIn, indexIn + 1);
}
}
The condition for the recursive call:
if (j + 1 <= length)
is wrong.
You should make the recursive call as long as indexIn is smaller than the last index of the array:
if (indexIn < length - 1) {
insertionSort(arrayIn, indexIn + 1);
}
The rest of your code is fine.
You could try this:
public static void insertionSort(int arrayIn[], int indexIn) {
if (indexIn <= 1)
return;
insertionSort(arrayIn, indexIn - 1);
int last = arrayIn[indexIn - 1];
int j = indexIn - 2;
while (j >= 0 && arrayIn[j] > last) {
arrayIn[j + 1] = arrayIn[j];
j--;
}
arrayIn[j + 1] = last;
}
public static void main(String[] args){
int[] array = {1, 3, 2, 4};
insertionSort(array, array.length);
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
}
I could a ArrayIndexOutOfBound operation, in your code,
Instead of,
if (j + 1 <= length)
{
insertionSort(arrayIn, indexIn + 1);
}
Use this,
if (j + 1 < length)
{
insertionSort(arrayIn, indexIn + 1);
}
And you have print your content of your array too,
public static void main(String[] args) {
int[] arr = new int[]{2, 3, 4, 1, 5};
insertionSort(arr, 0);
for(int i : arr)
{
System.out.print(i + " ");
}
}
Output:
1 2 3 4 5

replace element with string in an array of integers in java

I'm trying to replace specific elements of an array of integers, based on some condition, with strings. I'm identifying the int elements which has to be replaced by using modulus operator.
class Source {
public static void main(String args[]) {
int[] numbers = { 1, 2, 34, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
fizzBuzz(numbers);
}
public static void fizzBuzz(int[] numbers) {
// Write the function body here
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 3 == 0 && numbers[i] % 5 == 0) {
numbers[i] = "FizzBuzz";
} else if (numbers[i] % 5 == 0) {
numbers[i] = "Buzz";
} else if (numbers[i] % 3 == 0) {
numbers[i] = "Fizz";
} else {
numbers[i] = numbers[i];
}
System.out.print(numbers[i] + " ");
}
}
}
I'm getting the error:
Source.java:11: error: inconvertible types
numbers[i] = (int) "FizzBuzz";
^
required: int
found: String
Source.java:13: error: inconvertible types
numbers[i] = (int) "Buzz";
^
required: int
found: String
Source.java:15: error: inconvertible types
numbers[i] = (int) "Fizz";
^
required: int
found: String
3 errors
An array that was declared as an integer array cannot be filled with strings, as Java is a strongly typed language.
Here's how you can accomplish the same goal without violating Java's principles. You can print the result as soon as it is found based on the logic.
class Source {
public static void main(String args[]) {
int[] numbers = {1,2,34,5,6,7,8,9,10,11,12,13,14,15};
fizzBuzz(numbers);
}
public static void fizzBuzz(int[] numbers) {
//Write the function body here
for(int i = 0; i < numbers.length; i++){
if(numbers[i] % 3 == 0 && numbers[i] % 5 == 0){
System.out.print("FizzBuzz" + " ");
}else if(numbers[i] % 5 == 0){
System.out.print("Buzz" + " ");
}else if(numbers[i] % 3 == 0){
System.out.print("Fizz" + " ");
}else {
System.out.print(numbers[i] + " ");
}
}
}
}
I'm trying to replace specific elements of an array of integers, based on some condition, with strings.
In this particular case, the easiest way is to store the integers in the original array as Objects. Then you just need to cast them to an int to work with them.
public static void main(String args[]) {
Object[] numbers = {1,2,34,5,6,7,8,9,10,11,12,13,14,15};
fizzBuzz(numbers);
System.out.println(Arrays.toString(numbers));
}
public static void fizzBuzz(Object[] numbers) {
int i = 0;
for (Object o : numbers) {
int n = (int)o;
boolean t3 = n % 3 == 0;
boolean t5 = n % 5 == 0;
numbers[i] = t3 && t5 ? "FizzBuzz" : t3 ? "Fizz" :
t5 ? "Buzz" : n;
i++;
}
}
Prints
[1, 2, 34, Fizz, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
Points to consider.
establish the boolean results once and then simply apply them to a chained ternary operator. The ternary operator a?b:c means if a is true then b, else c
since this uses Objects there is no issue of putting the result (either String or int) back into the array
and since you are using only integers to begin with, you won't get a class cast exception when doing (int)o

linear search for multiple integers

Im trying to implement a linear search for an array of integers that if the number is a multiply of 3 its going to print the index of the number in the array and if there is no any number that is a multiple of 3 its going to return -1
im trying to implement this without using a method for it and my problem is how im going to do the printing/returning of -1?
knowing that my code is
int[] a = {3, 5, 22, 7, 9, 8, 21};
System.out.println("the index of 3 multiplies is" );
for (int i = 0; i < a.length; i++) {
if (a[i] % 3 == 0) {
System.out.print(i + " ");
continue;
}
}
You could use a boolean found = false and set it to true if the algorithm found a multiply of 3. After the for loop you could ask for result.
int[] a = {3, 5, 22, 7, 9, 8, 21};
int multipleOfThree = 0;
System.out.println("the index of 3 multiplies is" );
for (int i = 0; i < a.length; i++) {
if (a[i] % 3 == 0) {
System.out.print(i + " ");
multipleOfThree++;
}
}
if( multipleOfThree == 0 ){
System.out.println(-1);
}
I think this is what you are looking for. If you want to use a method, instead of printing the multipleOfThree, you should return it.
int[] a = {1,5,22,7,52,8,20};
System.out.println("the index of 3 multiplies is" );
boolean found = false;
for(int i = 0; i<a.length;i++){
if(a[i]%3==0){
found = true;
System.out.println(i+" ");
}
}
if(!found){
System.out.println("-1");
}
}``

Google Foobar Numbers Station

Im trying to solve a google foobar challenge but I am stuck on how to change this to use recursion. any pointers would be helpful
public static int[] answer(int[] l, int t) {
// convert the array into a list
List<Integer> list = new ArrayList<>();
for (int i : l) {
list.add(i);
}
for (int i = 0; i < list.size(); i++) {
Integer n = list.get(i);
if (i >= 1) {
Integer nMinus1 = list.get(i - 1);
Integer nMinus2;
Integer nMinus3;
Integer nMinus4;
Integer nMinus5;
Integer nMinus6;
if (n + nMinus1 == t) {
// done
return new int[]{i - 1, i};
} else if (i >= 2) {
nMinus2 = list.get(i - 2);
if (n + nMinus1 + nMinus2 == t) {
// done
return new int[]{i - 2, i};
} else if (i >= 3) {
nMinus3 = list.get(i - 3);
if (n + nMinus1 + nMinus2 + nMinus3 == t) {
// done
return new int[]{i - 3, i};
} else if (i >= 4) {
nMinus4 = list.get(i - 4);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 == t) {
// done
return new int[]{i - 4, i};
} else if (i >= 5) {
nMinus5 = list.get(i - 5);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 + nMinus5 == t) {
// done
return new int[]{i - 5, i};
} else if (i >= 6) {
nMinus6 = list.get(i - 6);
if (n + nMinus1 + nMinus2 + nMinus3 + nMinus4 + nMinus5 + nMinus6 == t) {
// done
return new int[]{i - 6, i};
}
}
}
}
}
}
}
}
return new int[]{-1, -1};
}
Here is the question:
Given the list l as [4, 3, 5, 7, 8] and the key t as 12, the function answer(l, t) would return the list [0, 2] because the list l contains the sub-list [4, 3, 5] starting at index 0 and ending at index 2, for which 4 + 3 + 5 = 12, even though there is a shorter sequence that happens later in the list (5 + 7). On the other hand, given the list l as [1, 2, 3, 4] and the key t as 15, the function answer(l, t) would return [-1, -1] because there is no sub-list of list l that can be summed up to the given target value t = 15.
You probably don't need an arraylist. You could perform a double loop on the array l. Why do you want recursion?
You could do something like:
public static int[] answer(int[] l, int t) {
int[] rets = {-1, -1};
int sum=0;
for (int i=0; i<l.length; i++) {
sum=0;
for (int j=i; j<l.length; j++) {
sum+=l[j];
if (sum > t) break;
if (sum == t) {
rets[0] = i;
rets[1] = j;
return rets;
}
}
}
return rets;
}
Just submitted my solution.. it got accepted, so I know it was accepted by Google:
(I believe this will be slightly faster than above solution since it will break the loops and return if the target is greater than sum of all numbers in the array.)
public static int[] answer(int[] l, int t){
int sum =0;
List<Integer> indexes = new ArrayList<>();
for(int j=0;j<l.length;j++){
sum = 0;
indexes.clear();
for(int i=j;i<l.length;i++){
sum += l[i];
indexes.add(i);
if(sum >= t){
break;
}
}
if(sum == t){
break;
}
else if(sum > t){
continue;
}
else{
return new int[] {-1,-1};
}
}
int[] returnArray = new int[2];
if(indexes.size()>0){
returnArray[0] = indexes.get(0);
returnArray[1] = indexes.get(indexes.size()-1);
}
return returnArray;
}

Find elements in array whose sum is equal to 10 - Java

I want to find all the pairs of numbers from an array whose sum is equal to 10, and am trying to improve upon this bit of code here:
for (int j = 0; j < arrayOfIntegers.length - 1; j++)
{
for (int k = j + 1; k < arrayOfIntegers.length; k++)
{
int sum = arrayOfIntegers[j] + arrayOfIntegers[k];
if (sum == 10)
return j + "," + k;
}
}
However, I'm having trouble moving through the array. Here's what I have so far:
int[] arrayOfIntegers = {0, 5, 4, 6, 3, 7, 2, 10};
Arrays.sort(arrayOfIntegers);
System.out.println(Arrays.toString(arrayOfIntegers));
int left = arrayOfIntegers[0];
int right = (arrayOfIntegers[arrayOfIntegers.length - 1]);
while (left < right)
{
int sum = left + right;
if (sum == 10) //check to see if equal to 10
{
System.out.println(left + "," + right);
}
if (sum > 10) // if sum is more than 10, move to lesser number
{
right --;
}
if (sum < 10) // if sum is less than 10, move to greater number
{
left++;
}
} // end of while
Try this code by passing the value of the sum and array in which you want to find the pair of elements equals to a given sum using one for loop
private void pairofArrayElementsEqualstoGivenSum(int sum,Integer[] arr){
List numList = Arrays.asList(arr);
for (int i = 0; i < arr.length; i++) {
int num = sum - arr[i];
if (numList.contains(num)) {
System.out.println("" + arr[i] + " " + num + " = "+sum);
}
}
}
You need to capture the values as well as the indexes:
int[] arrayOfIntegers = {0, 5, 4, 6, 3, 7, 2, 10};
Arrays.sort(arrayOfIntegers);
System.out.println(Arrays.toString(arrayOfIntegers));
int left = 0;
int right = arrayOfIntegers.length - 1;
while (left < right)
{
int leftVal = arrayOfIntegers[left];
int rightVal = (arrayOfIntegers[right]);
int sum = leftVal + rightVal;
if (sum == 10) //check to see if equal to 10
{
System.out.println(arrayOfIntegers[left] + "," + arrayOfIntegers[right]);
right --;
left++;
}
if (sum > 10) // if sum is more than 10, move to lesser number
{
right --;
}
if (sum < 10) // if sum is less than 10, move to greater number
{
left++;
}
} // end of while
output:
[0, 2, 3, 4, 5, 6, 7, 10]
0,10
3,7
4,6
This is sample code with javascrypt. Someone can use it
var arr = [0, 5, 4, 6, 3, 7, 2, 10]
var arr1 = arr;
for(var a=0; a<arr.length;a++){
for(var b=0; b<arr.length; b++){
if(arr[a]+arr[b]===10 && a!==b){
console.log(arr[a]+" + "+arr[b])
arr.splice(a,1);
}
}
}
Java - Using single loop
public static void findElements() {
List<Integer> list = List.of(0, 5, 4, 6, 3, 7, 2, 10);
for (int i = 0; i < list.size(); i++) {
int sum = 0;
if (i < list.size() - 1) {
sum = list.get(i) + list.get(i + 1);
if (sum == 10) {
System.out.println("Element: " + list.get(i) + "," + list.get(i + 1));
}
} else {
if (list.get(i) == 10) {
System.out.println("Element: " + list.get(i));
}
}
}
}

Categories