Calculate algorithmic complexity when calling another function - java

private static int f(int[] a, int low, int high)
{
int res = 0;
for (int i = low; i <= high; i++)
res += a[i];
return res;
}
/**
*
* #return the size of the largest gap in the array in which the combined values contained in the indexes are divisible by 3
*
*/
public static int what(int[] a)
{
int temp = 0;
for (int i = 0; i < a.length; i++)
{
for (int j = i; j < a.length; j++)
{
int c = f(a, i, j);
if (c % 3 == 0)
{
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
I need to calculate the algorithmic complexity of what. I think it might be n^2 because it has two loops, but it could also be n^3 because it uses f which has another loop. How can I determine its algorithmic complexity?

Related

Improve Efficiency of Alog (Rotate Array to Left by n iterations)

I am solving a challenge to rotate array to left by n number of iterations.
Code is pretty much working but lags on very very huge input.
How to more improve efficiency
// Complete the rotLeft function below.
static int[] rotLeft(int[] a, int iterations) {
for(int i=0;i<iterations;i++)
{
int[] temp=Arrays.copyOfRange(a, 1, a.length);
temp=Arrays.copyOf(temp,a.length);
temp[a.length-1]=a[0];
a=temp;
}
return a;
}
Suggestions are welcome.
Thanks
Instead on shifting iterations you can calculate the final position directly.
finalIndex=(index-iterations+a.length) % a.length
+a.length is added to ensure that the finalIndex is always a not negattive value.
If you apply this to your algorithm, you get rid of the loop and do the whole thing in one step.
This reduced time complexity of the algorithm from O(a.length*iterations) to O(a.length).
There are few flaws in the code.
First, you do redundant work - if iterations > a.length - then after a.length iterations the array just returns back to itself.
Second, each iteration creates a whole new copy of the array!
Third, the new location of each element in the array can be predetermined by looking only on the array length, the number of iterations required, and the index of this element, no need to repeatidly go over iterations.
When taking these into considerations, this can boil down to something in the form of:
static int[] rotLeftEfficient(int[] a, int iterations) {
iterations = iterations % a.length;
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
int newIndex = (i - iterations + a.length) % a.length;
b[newIndex] = a[i];
}
return b;
}
This boils down to O(n) solution - where n is the number of elements in the array, with decent constants as well.
Here is a rotate in place (doesn't use a second array), with O(a.length) time complexity. On my system (Intel 3770K 3.5ghz), it can rotate a 2^28 = 268,435,456 element array in .17 (rotate 76) to .85 seconds (rotate 1). The swaps are done in sequential access sequences, which is cache friendly.
// rotate in place
public static void rolip(int[] a, int r){
r = r % a.length;
if(r == 0)
return;
int t;
int n = a.length - r;
int i = 0;
int j;
while(true){
if(r <= n){ // shift left r places
for(j = i+r; i < j; i++){
t = a[i]; // swap(a[i], a[i+r])
a[i] = a[i+r];
a[i+r] = t;
}
n -= r;
if(n == 0)
break;
} else { // shift right n places
i += r;
for(j = i+n; i < j; i++){
t = a[i]; // swap(a[i], a[i-n])
a[i] = a[i-n];
a[i-n] = t;
}
i -= n+r;
r -= n;
if(r == 0)
break;
}
}
}
Simple rotate using a second array. On my system (Intel 3770K 3.5ghz), it can rotate a 2^28 = 268,435,456 element array in .11 to .50 seconds.
public static void rol(int[] a, int r){
r = r % a.length;
if(r == 0)
return;
int n = a.length - r;
int i;
int j;
if(r <= n){ // if left rotate
int[] b = new int[r]; // save elements
for(j = 0; j < r; j++)
b[j] = a[j];
for(i = 0; i < n; i++) // shift elements
a[i] = a[j++];
for(j = 0; j < r; j++) // copy saved elements
a[i++] = b[j];
} else { // else right rotate
int[] b = new int[n]; // save elements
i = 0;
for(j = r; j < a.length; j++)
b[i++] = a[j];
i = j-1; // shift elements
for(j = i-n; j >= 0; j--)
a[i--] = a[j];
for(j = 0; j < n; j++) // copy saved elements
a[j] = b[j];
}
}
You don't need to do it for every iteration. Instead you can create a formula to figure out where every element would go.
For example, let's say that the size of your array is n.
Then, if you need to move your array to the left by 1 iteration, then an element at position p would be at (p + 1) % n position.
For i iterations, every element would be at (p + i) % n location. So, a loop for every iteration is not needed.
static int[] rotLeft(int[] a, int iterations) {
int[] answer = new int[a.length];
for(int i=0;i<a.length;i++)
{
answer[i] = a[(i - iterations + a.length) % (a.length)];
}
return answer;
}
What about this:
static int leftRotate(int arr[], int iterations,
int k)
{
/* To get the starting point of
rotated array */
int mod = k % iterations;
// Prints the rotated array from
// start position
for(int i = 0; i < iterations; ++i)
System.out.print(arr[(i + mod) % iterations]
+ " ");
System.out.println();
}
leftRotate(arr, iterations, arr.length);
Ref: https://www.geeksforgeeks.org/print-left-rotation-array/

What is Submatrix Sum Queries program?. What is its use and concept to build it

I found this question in geeksforgeeks. It was asked in an amazon interview process. Following is the link:
http://www.geeksforgeeks.org/submatrix-sum-queries/
If anyone has any idea of is this ques, please explain to me.
The explanation on how to solve it is in great detail behind the link you provided.
public static int sumMatSumInConstantTime(int[][] mat, int is, int js, int ie, int je) {
processMatForConstantTimeSum(mat);
int sum = mat[ie][je];
int count = 0;
if (is - 1 >= 0) {
sum -= mat[is - 1][je];
count++;
}
if (js - 1 >= 0) {
sum -= mat[ie][js - 1];
count++;
}
/**
* since one rectangle is deducted twice
*/
if (count == 2)
sum += mat[is - 1][js - 1];
return sum;
}
private static void processMatForConstantTimeSum(int[][] mat) {
int r = mat.length;
int c = mat[0].length;
/**
* sum rows
*/
for (int i = 0; i < r; i++) {
int sum = 0;
for (int j = 0; j < c; j++) {
sum += mat[i][j];
mat[i][j] = sum;
}
}
/**
* sum cols
*/
for (int i = 0; i < c; i++) {
int sum = 0;
for (int j = 0; j < r; j++) {
sum += mat[j][i];
mat[j][i] = sum;
}
}
}

Count the number of checks each method had to perform before fully sorting the array

So I'm writing a program that compares Bubble, Selection, Merge, and Quick Sort. All 4 methods are given a randomized array of 1000 elements and I count to see how many times it takes a method to perform to fully sort the array. My question revolves around the placement of my counts. I know I should look at the Big O complexities for each to get a ballpark estimate of the number but I'm just asking if I put the counts in the right spots.
Bubble Sort:
public static int[] bubbleSort(int[] a)
{
boolean done = false;
int n = a.length;
while(done == false)//runs n times
{
done = true;
for(int i = 0; i < n-1; i++)//runs n times
{
if(a[i] > a[i+1])//Swap
{
bubbleCount++;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
done = false;
}
}
}
return a;
}
Selection Sort:
public static void selectionSort(int[] a )
{
for (int i = 0; i < a.length - 1; i++)
{
int pos = i;
for (int j = i + 1; j < a.length; j++)
{
// increments selection count
selectionCount++;
// if a[j] is less than a[pos], set pos to j
if (a[j] < a[pos])
{
pos = j;
}
}
// swaps a[i] and a[pos]
int temp = a[i];
a[i] = a[pos];
a[pos] = temp;
}
}
Merge Sort:
public static void mergeSort(int [] a)
{
//put counter for checks inside method
int size = a.length;
if(size < 2)//Halt recursion
{
return;
}
int mid = size/ 2;
int leftSize = mid;
int rightSize = size - mid;
int[] left = new int[leftSize];
int[] right = new int[rightSize];
//populate left
for(int i = 0; i < mid; i++)
{
mergeCount++;
left[i] = a[i];
}
//populate right
for(int i = mid; i < size; i++)
{
mergeCount++;
right[i-mid] = a[i];
}
mergeSort(left);
mergeSort(right);
//merge
merge(left, right, a);
}
public static void merge(int[] left, int[] right, int[] a)
{
int leftSize = left.length;
int rightSize = right.length;
int i = 0;//index for left
int j = 0;//index for right
int k = 0;//index for a
while(i < leftSize && j < rightSize)//compares until the end is reach in either
{
if(left[i] <= right[j])
{
//assigns a[k] to left[i] and increments both i and k
a[k] = left[i];
i++;
k++;
}
else
{
//assigns a[k] to right [j] and increments j and k
a[k] = right[j];
j++;
k++;
}
}
//fills in the rest
while(i<leftSize)
{
a[k] = left[i];
i++;
k++;
}
while(j<rightSize)
{
a[k] = right[j];
j++;
k++;
}
}
Quick Sort
public static void quickSort(int[] a, int left, int right)
{
int index = partition(a, left, right);
if(left < index - 1)
{
//increments quickCount and calls quickSort recursively
quickCount++;
quickSort(a, left, index-1);
}
if(index < right)
{
//increments quickCount and calls quicSort recursively
quickCount++;
quickSort(a, index, right);
}
}
public static int partition(int[] a, int left, int right)
{
int i = left;
int j = right;
int pivot = a[((left+right)/2)];
while(i<=j)
{
while(a[i] < pivot)
{
i++;//correct position so move forward
}
while(a[j] > pivot)
{
j--;//correct position
}
if(i <= j)
{
//swaps and increments i and decrements j
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
return i;
}
Your counts should be wherever you do a comparison of two values.
The bubbleCount should be moved up above the if because you want to count every comparison not sure the ones where the if clause is true. The selectionCount seems okay. The mergeCount should be moved to where you compare values.
You need to count comparisons, so I would create a method to compare and count:
static int count;
private static boolean isGreaterThan(int i, int j) {
count++;
return i > j;
}
and always use that method to compare values:
instead of:
while(i<=j)
do:
while(!isGreaterThan(i, j))
etc

Polynomial class Java

I'm trying to make two methods for a Polynomial class but I'm having troubles.
The first method checkZeros is supposed to check if there are any leading zeros in the coefficients of a polynomial. The method should resize the coefficient array if there are leading zeros. The second method should find the derivative of a polynomial, but I keep getting ArrayIndexOutOfBounds errors.
Here they are:
public class Poly {
private float[] coefficients;
public static void main (String[] args){
float[] fa = {3, 2, 4};
Poly test = new Poly(fa);
}
public Poly() {
coefficients = new float[1];
coefficients[0] = 0;
}
public Poly(int degree) {
coefficients = new float[degree+1];
for (int i = 0; i <= degree; i++)
coefficients[i] = 0;
}
public Poly(float[] a) {
coefficients = new float[a.length];
for (int i = 0; i < a.length; i++)
coefficients[i] = a[i];
}
public int getDegree() {
return coefficients.length-1;
}
public float getCoefficient(int i) {
return coefficients[i];
}
public void setCoefficient(int i, float value) {
coefficients[i] = value;
}
public Poly add(Poly p) {
int n = getDegree();
int m = p.getDegree();
Poly result = new Poly(Poly.max(n, m));
int i;
for (i = 0; i <= Poly.min(n, m); i++)
result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
if (i <= n) {
//we have to copy the remaining coefficients from this object
for ( ; i <= n; i++)
result.setCoefficient(i, coefficients[i]);
} else {
// we have to copy the remaining coefficients from p
for ( ; i <= m; i++)
result.setCoefficient(i, p.getCoefficient(i));
}
return result;
}
public void displayPoly () {
for (int i=0; i < coefficients.length; i++)
System.out.print(" "+coefficients[i]);
System.out.println();
}
private static int max (int n, int m) {
if (n > m)
return n;
return m;
}
private static int min (int n, int m) {
if (n > m)
return m;
return n;
}
public void checkForZeros(){
int newDegree = getDegree();
int length = coefficients.length;
double testArray[] = coefficients;
for (int i = length - 1; i >0; i--) {
if (coefficients[i] != 0) {
testArray[i] = coefficients[i];
}
}
for (int j = 0; j < testArray.length; j++){
coefficients[j] = testArray[j];
}
}
public Poly differentiate(){
int n = getDegree();
int newPolyDegree = n - 1;
Poly newResult = new Poly();
if (n == 0){
newResult.setCoefficient(0, 0);
}
for (int i =0; i<= n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));
}
return newResult;
}
}
I would suspect the problem is here
for (int i =0; i<= n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));
}
Since n = getDegree();, let's assume the polynomial is of 1st degree (1+x for example). Then n=1 I would guess, and coefficients has a length of 2. But you are going to be checking coefficients[2] (since you have i+1) which is out of bounds. I'm guessing you want
for (i=0; i<=newPolyDegree; i++){
newResult.setCoefficient(i, coefficients[i] * (i+1));
}
or something... It's hard to tell with the amount of code you gave.
You are most probably getting ArrayIndexOutofBounds because you've implemented checkzeroes in a wrong manner and hence getdegree() is returning a size less than the coefficient array. Consider the following polynomial:
f(x) = 2x^3 + 5x + 1
The coefficient array will be
[2,0,5,1]
After checkzeroes, it becomes
[2,5,1] (because you're removing all zeroes, not just leading zeroes.)
I suppose the degree function will still return 3 and you'll run out of array bounds in differentiate()
The error is in the loop.
for (int i =0; i<= n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));
Base on little information, I assume:
1. setCoefficient(power of x, coefficient of x)
So in this case, we have a polynomial of degree n for differentiation, which have n+1 term, starting from 0->n (x^0 -> x^n)
Look at the loop, when i=n, it have to fetch the coefficient of x^(n+1) which is not exist.
You should do.
for (int i =0; i< n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));

How can I modify the first for loop in this java method into a recursive call?

This is a problem for an Algorithms course I'm taking that I can't figure out.
// modify the array x to generate the next k-combination from x.
// In general, the first k-combination of n elements is { 1, 2, ..., k }
// and the last k-combination is { n-k+1, n-k+2, ..., n }.
public static boolean nextCombination (int x[], int k, int n) {
for (int j = k-1; j >= 0; j--)
if (x[j] <= (n - k + j)) {
x[j]++;
for (int i = 1; i < k - j; i++)
x[i+j] = x[j]+i;
return true;
}
return false;
}
It gets called by this method:
// print all k-combinations of n elements.
public static void enumerateCombinations (int k, int n) {
int x[] = new int[100]; // k <= 100
System.out.println("All " + k + "-combinations of " + n + " numbers:");
for (int j = 0; j < k; j++)
x[j] = j+1;
while (true) {
printArray(x, k);
if (nextCombination(x, k, n) == false)
break;
}
}
Any help would be appreciated.
With this code you transform the nextCombination method into a recursive one.
public static boolean nextCombinationRecursive (int j, int x[], int k, int n) {
if (j < 0 || j > k) return false;
if (x[j] <= (n - k + j)) {
x[j]++;
for (int i = 1; i < k - j; i++)
x[i+j] = x[j]+i;
return true;
}
return nextCombinationRecursive(j - 1, x, k, n);
}
And you call it from enumerateCombinations like this:
if (nextCombinationRecursive(k - 1, x, k, n) == false)

Categories

Resources