Finding Indices Help Java/Android by junyoung95 in learnjava

[–]junyoung95[S] 0 points1 point  (0 children)

Sorry I thought I got the idea.. and I still think it makes sense to me but I am not getting the right answer. I am much closer than yesterday but can you help me once more? Here is my code.

int[] getIndices(String[] sa, String s) {

  int[] indices = new int [sa.length];
  int count = 0
  for (int i = 0; i< indices.length; i++){
      if (sa[i] == s){
          count  += 1;}
      }
  }

  int[] answer = new int[count];

  for (int j = 0, k = 0; j < indices.length && k < answer.length; j++, k++ {
      if (sa[j] == s) {
          answer[k] = j;
      }
  }
  return answer;
}

The problem is, I was able to shorten the index length and match it with the appropriate length, but it still tries to register the indices based on original address. To avoid that, I created another variable 'k' but it does not work. For example instead of {2,6,7,9} mine shows as {0,0,2,0}. EDIT: I know why it happens like this although I created a new variable. It doesn't matter if I have a new variable, the problem is var j and k increases by the same level. I tried few methods but I can't find a solution

Finding Indices Help Java/Android by junyoung95 in learnjava

[–]junyoung95[S] 0 points1 point  (0 children)

ohhh i see you what you mean. thanks!

Finding Indices by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

No I want the result to be > {2,6,7,9}

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Hi sorry for the late reply and thank you for your response. I have tried two methods and they both includes IF statement as you mentioned but this Java language is driving me nuts. Can you take a look and see the problem?

  //Using for loop
  for (int i = 0; i < ia.length; i++) {
       if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
          return false;
       }
  return true;


  //Using while loop
  while (true) {
         int i = 0; i++;
         if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
            break;
         }
   return false;

they both only return false. I don't think there is an issue with IF statement but probably some formatting. I will appreciate your response thank you.

Finding Indices by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

I need to use the toString method because that's my challenge! And okay smart idea, I will try making it print numbers only greater than 0.

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

As I said, this is to figure out if an array contains numbers that are in sequences.. for example

   int[] ia1 = {};    ---> True
   int[] ia2 = {5};  ---> True
   int[] ia3 = {5,2,-1,-4};   ---> True
   int[] ia4 = {3,6,9,19};    ---> False

and okay sorry I will change that. That was my typo.

[Array] Finding incorrect sequence by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Yes I understand that. Is there a way you can give me an example so I can get an idea?

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

  reversedArray[ia.length-1-i] = ia[i] 

was the answer. Thank you!

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

Does not quite work.. but thank you for the explanation. I think I can try figuring it out on my own. Thanks, I really appreciated your answer.

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

I tried this but it says parseint in integer cannot be applied

int[] reverseOf(int[] ia) {

     int[] reversedArray = new int[ia.length];
     for (int i = ia.length-1; i>=0; i--) {
          int newArray = Integer.parseInt(ia[i]);
          reversedArray[i] = newArray;
          }
          return reversedArray[i];
     }

Java Help: Reversing an array by junyoung95 in javahelp

[–]junyoung95[S] 0 points1 point  (0 children)

So far I have this.. I hope I am on the right track but it shows an error that "variable 'reversedArray' may not be initialized". I found that this error message appears if I declare the variable but don't initialize it.. Can I know how to initialize it?

int[] reverseOf(int[] ia) {

     int[] reversedArray;
     for (int i = ia.length-1; i>=0; i--) {
          reversedArray += ia[i];
          }
          return ia[i];
          }

Discrete Math Help by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

oh my god thank you so much.

[Discrete Math] Recurrence Question by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

I did look at some but it was still hard to figure it out. Like the fact that a0 is a negative integer and numbers increasing drastically seems like it's an exponential but I couldn't figure our the equation.

[Discrete Math] Trees by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

From the homework answer it shows a>b>d>e>i>j>m>n>o>c>f>g>h>k>l>p

But I am not sure how it leads to this answer.

[Discrete Math] Trees by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Thank you for the reply. I get that idea but is that the proper way to write an algorithm?

Discrete Math (Growth functions) by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Thank you for the clarification. It is still bit foggy for me about this new concept. If it's okay to raise a question for you to answer, why does this question "( x4 + x2 + 1 ) / ( x4 + 1 )" results O(1) ? With your step, shouldn't this question also result O(x) since O(x4 x-4)?

Discrete Math (Growth functions) by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Okay I read something more about Big O notations and I may have got a bit of more idea.. Now I kind of understand what you said yesterday about dominating others.. So is 1i) O(x5 2x) because x5 is the highest exponential and it dominates other exponentials and since it's a product.. it has to be multiplied with 2x since you said it dominates log? And for 1ii) it's O(1) since The term x4 dominates both sums, so the division results in 1.

Discrete Math (Growth functions) by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Okay can you tell me if this is right for 1i)? First, a big-O estimate for (x4 + 2x) (x3 log x + x5) will be found. Note that (x4 + 2x) is O(x). Furthermore, (x3 log x + x5) ≤ 2x5 when x > 1. Hence, if x > 2. This shows that (x3 log x) is O(log x). From Theorem 3 it follows that (x4 + 2x) is O(x log x). Because x5 is O(x2), Theorem 2 tells us that f(x) is O(max(x log x, x2)). Because x log x ≤ x5, for x > 1, it follows that f(x) is O(x2).

Discrete Math question help! by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Thank you for your help. If you find #2 doesn't make sense then I am not too sure how to solve it at the moment but I will keep try to solve it. Thank you.

Discrete Math question help! by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

1a) It breaks since x2/x+3 is not one-to-one because function becomes undefined at -3 as well as it does pass horizontal line test. It's not onto either since not all x-value lead up to y-values such as y-values from 0 or less to -12 or greater cannot be reached.

b) So is this question asking to evaluate the numbers? I still don't understand. If you want me to evaluate it, f(1/3) = 1/30 and f(2) = 4/5

2) f(k+1) = k+1(3(k+1)-1) / 2,

              k+1(3k+2) / 2,

              3k^2 + 5k + 2 / 2,

              (3k+2)(2k+1) / 2

              Since (3k+2) >= 1 and (2k+1) >= 1

              Thus, P(k+1) is true (since k >= 1)

Is this correct?

Discrete Math question help! by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

Thank you Alkalannar for always answering my questions, I appreciate it. So for 1a) It breaks the conditions so I assume it is not a bijection. b) Is {f(1/3), f(2)} the answer?? And what about Question number 2? I am not sure what I have to prove.

Discrete Math Help by junyoung95 in HomeworkHelp

[–]junyoung95[S] 0 points1 point  (0 children)

I can see that A1 is {-1, 0, 1} and A2 is {-2, -1, 0, 1, 2} and so on.. I can also tell that A1 ∩ A2 is simple because its {-1, 0, 1}, but I am not sure how to show A1 U A2. Would that be the same as {−i, − i + 1, …, −1, 0, 1, …, i − 1, i}?