Is there a way to do the sum of the n integers function that would have O(n^2)? - big-o

I want to write this simple function in a way that makes the big O notation of it O(n^2), how can I make that possible ?
int getSum(int n){
int sum = (n*(n+1))/2;
return sum;
any ideas?

I'm not really sure why you want this, but you could do it with two nested loops:
int getSum(int n) {
int sum = 0;
for(int i = 1; i <= n; i++) {
int x = 0;
while(x++ < i) {
sum++;
}
}
return sum;
}
This runs 1+2+3+...+n times, which simplifies to (n^2+n)/2, hence O(n^2)

Related

What is the Big O notation of a loop in loop

I have this code and cannot understand the Big-O of this... Thanks
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
One of the best ways to approach this problem is to break it down into smaller parts.
First, lets look at your inner loop:
for(j = i; j<n; j++){
if (arr[j]%2!=0){ // O(1)
if (minodd > arr[j]){ // O(1)
}
}
}
The if-statements are O(1) or constant time so we can ignore those and we get just the inner for loop:
for(j = i; j<n; j++){
... // O(1) + O(1)
}
Since the worst case scenario is it loops n times we have O(n) + O(1) + O(1) which can be simplified to O(n) which is called linear time.
Next, lets zoom out and replace the inner loop with our new info:
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
becomes:
for(i = 0; i<n; i++){
O(n)
}
Since we know the outside for loop will cycle n times in the worst case, and the inside for loop will cycle n times in the worst case: We get O(n x n) or O(n²) which is also know as polynomial time.
Doesn't this just go on for forever?
You have i < n in your inner loop, so I think it's O(inf).
Now that you've updated the loop, I think #e2-e4 is right:
#include <stdio.h>
int eqn(int n)
{
return n > 0 ? n + eqn(n - 1) : 0;
}
int main(int argc, char **argv)
{
int i, j, n, v, a;
v = 0;
n = 5;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
v++;
}
}
// v = 15 ? 15
printf("v = %d ? %d\n", v, eqn(n));
return 0;
}

How can I calculate the time complexity of my algorithm?

I want to know the time complexity of my code. How can I calculate it?
{
int q;
int w;
cout <<"please enter values" <<endl;
cin>>q;
for(w = 0; w<q; w++)
{
int p;
int o;
int sum = 0;
cin>>p;
for(o = 0; o < p; o++)
{
int x;
int y;
int z;
cin>>x ;
cin >>y;
cin>>z;
sum = sum + (x*z);
}
cout<<sum<<endl;
}
_getch();
return 0;
}
The first for loop will run q times, which is a time complexity of O(q).
The second (inner) for loop will run p times, thus, it has a time complexity of O(p) and since it is called once for every run of the first loop, their complexities multiply like this:
O(q) * O(p) = O(q * p)
In general, nested loops multiply and consecutive loops add.

Big Oh Logarithmic(ish) complexity calculation

So I've been trying to get a handle on Big Oh calculations. I feel I have the basics down but am stumped on what seems a really easy calculation. So if the calculation below has a big oh of O(n log n) (I really hope I've at least got that right) what does changing the order of the loops do to the complexity? Thanks so much in advance for your time.
int ONLogN(int N) //O(n log n)
{
int iIterations = 0;
for (int i = 0; i < N; ++i)
{
++iIterations;
for (int j = 1; j < N + 1; j *= 2)
++iIterations;
}
return iIterations;
}
int WhatBigOhIsThis(int N) //???
{
int iIterations = 0;
for (int j = 1; j < N + 1; j *= 2)
{
++iIterations;
for (int i = 0; i < N; ++i)
++iIterations;
}
return iIterations;
}
The index variables on the two loops are independent, hence the resulting complexity is necessarily the same.
You're still looping for the same number of iterations. Changing the order of the loops would have no effect on complexity

What is this sorting algorithm called?

int max(int[] a, int m)
{
int n = a.length;
int t = a[0];
for(int i=1; i<n-m; i++)
if(a[i]>t) t = a[i];
return t;
}
int[] unknownSort(int[] a)
{
int n = a.length;
for(int i=n-1,j=0; i>0; i--,j++)
a[i] = max(a,j);
return a;
}
It is much faster than bubble sort, but a little slower than insertion sort.
That's not a sorting algorithm, it doesn't work.
Looks like a clumsy implementation of selection sort: http://en.wikipedia.org/wiki/Selection_sort
It will be a Selection Sort if you write it correctly. Your algrorithm will just corrupt the data in the array.

How to represent Big O(n!) Time complexity from for loop?

For Example
O(n)
for (int i=0;i<n;i++)
After Edit : My Final Answer is
for(int i =(n - 1); i > 1; i--)
{
factorial = factorial * i;
}
for (int j=n-2;j<factorial;j++)
{
}
The simplest answer is
for (int i = 0; i < Factorial(n); i++) {...
In practice, usually O(n!) algorithms are those that work by trying all the different permutations of a list, that is, all the different ways you can reorder a list. One example is finding the shortest line that passes through all points in a map called the travelling salesman problem. You need to try all the different ways to go through all the points and that would be O(n!).
IEnumerable<List<int>> nextPermutation(List<int> nodesLeft)
{
if (nodesLeft.Count == 0)
{
yield return new List<int>();
}
else
{
for (int i = 0; i < nodesLeft.Count; i++)
{
List<int> newNodesLeft = new List<int>(nodesLeft);
newNodesLeft.removeAt(i);
foreach (List<int> subPermutation in nextPermutation(newNodesLeft)
{
subPermutation.add(nodesLeft[i]);
yield return subPermutation;
}
}
}
}
void main()
{
foreach (List<int> permutation in nextPermutation(new List<int>(new int[]{1,2,3,4,5}))) {
//every permutation of [1,2,3,4,5] will be generated here
//this will take O(n!) to complete, where n is the number of nodes given (5 in this case)
}
}
If recursion is allowed then:
void loop(int n)
{
if(n == 1)
return; // the program gets here exactly n! times
for(int i=0; i<n; i++)
loop(n-1);
}
If we're on the same page here... I think that would look like..
Tau(fetch) + Tau(store) + (2Tau(fetch) + Tau(<) )*(N + 1) + (2Tau(fetch) + Tau(+) + Tau(store)) * N
fact = 1;
for( c = 1 ; c <= n ; c++ )
{
fact = fact*c;
}
like this?

Resources