How to Get the N-th Power in Java

The challenge

You are given an array with positive numbers and a number N. You should find the N-th power of the element in the array with the index N. If N is outside of the array, then return -1. Don’t forget that the first element has the index 0.

Let’s look at a few examples:

• array = [1, 2, 3, 4] and N = 2, then the result is 3^2 == 9;
• array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.

The solution in Java code

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```public class Solution {
public static int nthPower(int[] array, int n) {
return n >= array.length ? -1 : (int) Math.pow(array[n], n);
}
}
```Code language: Java (java)```

An alternative mapping:

``````public class Solution {
public static int nthPower(int[] array, int n) {
if (n < 0 || n >= array.length) return -1;
return (int) Math.pow(array[n], n);
}
}
```Code language: Java (java)```

Test cases to validate our Java code

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.stream.IntStream;

public class SolutionTest {
@Test
public void basicTests() {
assertEquals(-1, Solution.nthPower(new int[] {1,2}, 2));
assertEquals(8, Solution.nthPower(new int[] {3,1,2,2}, 3));
assertEquals(4, Solution.nthPower(new int[] {3,1,2}, 2));
}

@Test
assertEquals(9261, Solution.nthPower(new int[] {7,14,9,21,15}, 3));
assertEquals(169, Solution.nthPower(new int[] {2,7,13,17}, 2));
assertEquals(50625, Solution.nthPower(new int[] {11,23,3,4,15,112,12,4}, 4));
}

@Test
public void moreTests() {
assertEquals(1, Solution.nthPower(new int[] {2,1,2,1,2,1}, 5));
assertEquals(-1, Solution.nthPower(new int[] {11,23,3,4,15}, 7));
assertEquals(-1, Solution.nthPower(new int[] {3,2,1,2,3,1}, 6));
}

public int nthPowerSol(int[] array, int n) {
return n >= array.length ? -1 : (int) Math.pow(array[n], n);
}

private static int randomInRange(int min, int max) {
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}

@Test
public void randomTests() {
int[] array = new int[randomInRange(2,5)];
int n = randomInRange(2,6);
for (int i = 0; i < 50; i++) {
for (int k = 0; k < array.length; k++) {
array[k] = randomInRange(2,15);
}
assertEquals(nthPowerSol(array, n), Solution.nthPower(array, n));
}
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of