Contents

Transforming a sequence based on conditions 2 (with.Java)

   Oct 8, 2023     4 min read

In this article, we learned about converting a sequence to fit a condition.

We’ll do this by solving a coding test problem, reflecting on the problem we solved, and exploring other ways to solve it.

Let’s start with the problem

Problem

You are given an array of integers, arr.

For each element in arr, divide by 2 if the value is an even number greater than or equal to 50, multiply by 2 if it is an odd number less than 50, and add 1 again.

The array resulting from repeating these operations x times is denoted arr(x), and there will always be an x such that arr(x) = arr(x + 1).

Complete the solution function to return the smallest of these x’s.

Note that the “=” for the two arrays means that the two arrays are the same size, and that the elements at the same index are equal to each other.

Example input and output
arrresult
[1, 2, 3, 100, 99, 98]5

My solution to the problem

import java.util.*;
class Solution {
    public int solution(int[] arr) {
        int answer = 0;
        int[] tempArr = new int[arr.length];
        do {
            for(int i = 0; i < arr.length; i++){
            tempArr[i] = arr[i];
            }
            for(int i = 0; i < arr.length; i++){
                if(arr[i] >= 50 && arr[i] % 2 == 0){
                    arr[i] = arr[i] / 2;
                } else if(arr[i] < 50 && arr[i] % 2 != 0){
                    arr[i] = arr[i] * 2 + 1;
                }
            }
            answer++;
        } while(!Arrays.equals(tempArr, arr));
        return answer - 1;
    }
}
Solution

The answer variable stores the number of times the conversion process is repeated.

The array tempArr is a temporary array for copying the current arr array value.

The do-while loop runs until tempArr and the arr array are not equal.

The first for loop copies the values in the current arr array into the tempArr array.

The second for loop changes the arr array by performing an operation based on the condition for each element.

We record the number of iterations by incrementing answer.

Arrays.equals(tempArr, arr) compares the arrays tempArr and arr to see if they are equal. If they are not equal, the loop continues.

When the loop ends, it returns answer - 1, which is the number of conversions.

As I was writing the code, I realized that I should be careful with references to arrays.

Here’s an example: ###### Be careful when copying arrays

Example

 int[] intArray1 = new int[] { 1, 2, 3 };
  int[] intArray2 = new int[] { 1, 2, 3 };

if(intArray1 == intArray2) {
    System.out.println("The two arrays are equal.");
  } else {
    System.out.println("The two arrays are not equal.");
  }
}

// return: The two arrays are not equal.

Why?

The reason is that in Java, arrays are considered objects, and array variables are references that point to array objects.

So when you use the == operator to compare array variables, you are actually comparing reference addresses.

This is a rule that applies to all objects in Java, so arrays at different memory locations are always judged to be unequal when compared with the == operator, even if their contents are identical.

This behavior is caused by Java’s object comparison rules, which apply to all objects, not just arrays.

To compare the contents of objects for equality, you must use the Arrays.equals() method or a method that compares the elements of the array directly.

Other languages have mostly similar behaviors, although some languages may have subtle differences in these behaviors.

However, since most comparisons of objects are based on reference addresses, the general principle will be similar: the value of a variable of a reference type is the address of the object in the Heap area, so the return result will be different because you are comparing the value of the address, not the value of the element.

This applies not only to arrays, but also to types like strings that compare objects.