Question: This is efficient method that takes as its input a sorted array of n elements and returns true if the array contains at least k

This is efficient method that takes as its input a sorted array of n elements and returns true if the array contains at least k positive integers. Otherwise, returns false. The method signature is: static boolean posCount(int [] array, int k);

static boolean posCount(int [] array, int k) { int cnt = 0; for (int i = 0; i < array.length; i++) if (array[i] > 0) cnt++; return (cnt >= k); }

Write a modified version of the posCount() function above that simply returns the number of positive integers in an array. Can you think of a way to do this in better than O(n) time? The function signature is:

int posCount(int [] array);

Please code in JAVA.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!