Understanding the time complexity

 Understanding the time complexity





In this blog I have started learning the DSA in c++ so , 
to understand the time complexity
Let's take a example to see weather the pairs of (x, y) in the  given array is equal Z , 
where Z = sum of the pair. 
to execute this I wrote the following code;----->

        : a[ ] is an array which I will declare while creating a function
int n is the size of an array in bytes and int z is the sum that is the sum of the pairs
the at should be equal to z;


#include<iostream>
using namespace std;
/*a is an array which I will declare while creating a function
int n is the size of an array in bytes and int z is the sum that is the sum of the pairs
the at should be equal to z;
*/
bool Findpair(int a[], int n , int z)
{
    //ireting all the pairs of an array in a for loop
    for(int i = 0; i<n; i++){
        for(int j = 0; j<i; j++){
            if(i!=j && a[i] + a[j] == z){
                return true;
            }
        }
    }
    return false;
}
int main(){
   
    int a[] = {7, 8, 9, 4, 5, 12, 30};//if we increase the size of the array the time of exicution also increases
    int z = 15;
    int n = sizeof(a) / sizeof(a[0]);
    cout<<n;
    if (Findpair(a, n, z))
    {
        cout<<"True";
    }
    else{
        cout<<"False";
    }
    return 0;
}

/*
Note:-
Order of growth is how the time of execution depends on the length of the input
from the above example,
it is clearly that the time of execution quadratically depends on the length of the array.
Big oh = O(n^2)
*/

Post a Comment

0 Comments