Question

In: Computer Science

what is the time complexity of those ? //TODO - Question 18    public static int[][]...

what is the time complexity of those ?

//TODO - Question 18
   public static int[][] fillRandomArray4(int n) {
       int[][] arr = new int[n][n];
       for(int i = 0; i < arr.length; i++) {
           arr[i] = new int[] {(int)(Math.random() * 101),
                   (int)(Math.random() * 101),
                   (int)(Math.random() * 101)};
       }
       return arr;
   }
  
  
   //TODO - Question 19
   public static int factorial(int n) {
       if(n == 1 || n == 0) {
           return 1;
       }
       return n * factorial(n-1);
   }
  
  
   //TODO - Question 20 - assume str.length() == n
   public static boolean isPalindrome(String str) {
       if(str.length() == 0 || str.length() == 1) {
           return true;
       }
       if(str.charAt(0) != str.charAt(str.length()-1)) {
           return false;
       } else {
           return isPalindrome(str.substring(1,str.length()-1));
       }
   }

Solutions

Expert Solution

thanks for the questions.

=============================================================

Question 18

In this method, we are running a for loop n times which is passed to this method as an argument,
there is only 1 for loop without any inner loops, hence the code inside the for loop will run
n times (arr.length will return n)

So the time complexity will be linear and of order O(n)

=============================================================


Question 19

The method finds the factorial of a number, the function recursively calls itself until the value of n
is 1 or 0, in every call the value of n is decremented by 1, so the number of method call will be equal
to the value of n.

So the time complexity will be linear and of order O(n)

=============================================================


Question 20

The method finds if a string is palindrome, in every recursive call the length of the string is reduced by 2
that is the first character and last character are removed and the substring is passed as an argument in the
next recursive call.

The total iterations will be for a string of length n is n/2.

So the time complexity will be linear and of order O(n/2) or simply O(n)

=============================================================


Related Solutions

class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
public static int example3 (int [] arr ) { int n = arr.length , total =...
public static int example3 (int [] arr ) { int n = arr.length , total = 0; for (int j = 0; j < n ; j += 2) for (int k = 0; k <= j ; k ++) total += arr [ j ]; return total ; } what is the running time of this method?
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n. Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /). (b) What is the running time complexity of your function? Justify. (c) Give a number of multiplications used by your function to calculate x^63. Important Notes: • For the item (a): o You must add the main method in...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero or the initial reward. Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up....
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT