ETSE Hackerrank Flashcards
Here is the revised implementation where the logic to check if a number is a power of 2 is directly included within the isPower method, eliminating the need for a helper method:
import java.util.*;
public class Solution {
public static List<Integer> isPower(List<Integer> arr) { List<Integer> result = new ArrayList<>(); for (int num : arr) { if (num > 0 && (num & (num - 1)) == 0) { result.add(1); // num is a power of 2 } else { result.add(0); // num is not a power of 2 } } return result; } public static void main(String[] args) { // Test example List<Integer> testList = Arrays.asList(1, 3, 8, 12, 16); List<Integer> result = isPower(testList); // Print the result System.out.println(result); // Expected output: [1, 0, 1, 0, 1] } }
Explanation:
1. Eliminating the Helper Method:
• The logic to check whether a number is a power of 2 is directly incorporated into the loop inside the isPower method.
2. Checking Power of 2:
• A number num is a power of 2 if:
• It is greater than 0.
• The bitwise condition (num & (num - 1)) == 0 is true.
3. Adding to the Result:
• If the number satisfies the power-of-2 condition, append 1 to the result list.
• Otherwise, append 0.
4. Input and Output:
• Input: [1, 3, 8, 12, 16]
• Output: [1, 0, 1, 0, 1]
Advantages:
• This approach simplifies the code by removing the need for a helper method.
• The logic is self-contained within the isPower method.
Two integer operations are defined as:
ADD_7: Increment the integer by 1
MULTIPLY 2: Multiply the integer by 2
2
Given an integer value k, determine the minimum number of operations it takes to get from Oto kusing the two operations specified above.
Example*
kValues[i7 = 8
Language Java 8
1 > import java.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class
Result
*
Compl
The f
The f
/
public st
// Write
}
}
31 > public class :
Starting from 0, add 1, then multiply by 2 three times. It takes a minimum of 4 operations to get to 8, so store 4 in index 0 of the return array.
Function Description
Complete the function getMinOperations in the editor.
getMinOperations has the following parameter(s):
kValues[n]: the given k values
Returns:
int[n]: answers to a list of queries in the given order
Constraints
1 <_ n <= 10^4
0 ≤ kValues ≤ 10^16. Here’s your starter code : class
Result {
© Environment
& Autocomplete Ready
/*
Complete the ‘getMinOperations’ function below.
The function is expected to return an INTEGER_ARRAY.
The function accepts LONG_INTEGER_ARRAY kValues as parameter.
/
public static List<Integer> getMinoperations (List<Long~ kValues) t
// Write your code here. Solve this in Java 8</Integer>
import java.util.*;
class Result {
/* * Complete the 'getMinOperations' function below. * * The function is expected to return a List<Integer>. * The function accepts List<Long> kValues as parameter. */ public static List<Integer> getMinOperations(List<Long> kValues) { List<Integer> result = new ArrayList<>(); for (long k : kValues) { int operations = 0; while (k > 0) { if (k % 2 == 0) { k /= 2; // Reverse MULTIPLY_2 } else { k -= 1; // Reverse ADD_7 } operations++; } result.add(operations); } return result; } public static void main(String[] args) { // Example usage List<Long> kValues = Arrays.asList(8L, 15L, 23L); List<Integer> result = getMinOperations(kValues); System.out.println(result); // Example output: [4, 6, 9] } }