In: Computer Science
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++)
{
count++;
}
}
}
}
}
public static void main(String[] args)
// It runs for 1 time so its complexity is O(1)
int[] array = {1,2,3,4,5};
// It runs for 1 time so its complexity is O(1)
public static void (int[] array)
// It runs for 1 time so its complexity is O(1)
int count = 0;
// It runs for 1 time so its complexity is O(1)
for(int i = 0; i < array.length; i++)
// It runs for n times so its complexity is O(n)
for(int j = i; j < array.length; j++)
// It runs for near to the n times so its complexity is O(n) and also runs for every value of i so its overall complexity is O(n2)
for(int k = j; k < array.length; k++)
// It runs for near to the n times so its complexity is O(n) and also runs for every value of j so its overall complexity is O(n3)
count++;
// Its complexity is O(n3)
The complexity of the program is O(n3).
Add print statement below loop body so its complexity is O(n3).
for(int k = j; k < array.length; k++)
{
count++;
}