You are viewing a single comment's thread. Return to all comments →
Here's another O(1) approach. I searched for the sequence on the Online Encylopedia of Integer Sequences and found it at https://oeis.org/A075427
There was as closed-form solution on the page.
f(n) = 2 ^ ((n+3)/2) + ((-1)^n - 3) / 2
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int numCases = in.nextInt(); for (int i = 0; i < numCases; i++) { int n = in.nextInt(); int answer = ((int) Math.pow(2, (n + 3) / 2)) + (((int) Math.pow(-1, n)) - 3) / 2; System.out.println(answer); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Utopian Tree
You are viewing a single comment's thread. Return to all comments →
Here's another O(1) approach. I searched for the sequence on the Online Encylopedia of Integer Sequences and found it at https://oeis.org/A075427
There was as closed-form solution on the page.
f(n) = 2 ^ ((n+3)/2) + ((-1)^n - 3) / 2