TRENDING NOW

no image
A Blog about Programming,Algorithms,Coding Competitions, and Discrete Mathematics!
Our First Contest, and it was a huge success. Thank you for participating. 

Here are the editorial of the problems..


1. Simple Sum 4

Easiest in the Problem set.. and Almost everyone solved it.

Here is my solution code..

 #include <iostream>  
 #include <stdio.h>  
 using namespace std;  
 int main()  
 {  
   int n;  
   while ( scanf("%d",&n) != EOF )  
   {  
     for( int i = 0 ; i < n ; i++)  
     {  
       int x ,y;  
       scanf("%d %d",&x,&y);  
       printf("%d\n",x+y);  
     }  
   }  
   return 0;  
 }  


2 . Kazi And Quadrangles 

Summary : Given 4 lengths, you are to determine if these can form a square, rectangle, quadrangle or none of these.

Explanation :
Easiest way is to sort the array of lengths then check the following:
  • Square: all 4 sides are equal.
  • Rectangle: each 2 sides are equal.
  • Quadrangle: maximum of these lengths must be less than the sum of other 3.
  • None: if none of the above conditions is satisfied, then print "None".
 // Accepted  
 #include <algorithm>  
 #include <cstdio>  
 using namespace std;  
 int len[4];  
 int main(void){  
      int i, t;  
      scanf("%d", &t);  
      while (t--){  
           int s = 0;  
           for (i = 0; i < 4; i++){  
                scanf("%d", &len[i]);  
                s += len[i];  
           }  
           sort(len, len+4);  
           if (len[0] == len[1] && len[2] == len[3]){  
                if (len[1] == len[2])  
                     printf("square\n");  
                else  
                     printf("rectangle\n");  
           } else {  
                bool works = true;  
                for (i = 0; i < 4; i++)  
                     works &= s - len[i] > len[i];  
                if (works)  
                     printf("quadrangle\n");  
                else  
                     printf("None\n");  
           }  
      }  
 }  


3. Pooja And Java

In this problem you should answer to 4 questions:
1)       Can we use type byte to store N?
2)       Can we use type short to store N?
3)       Can we use type int to store N?
4)       Can we use type long to store N?
We should check these conditions in the given order. If all these conditions are wrong, the answer is BigInteger.
The simplest way to check these conditions is to store numbers as strings and write a function to compare such strings. In Java you can use type BigInteger.

 #include <iostream>  
 #include <string>  
 using namespace std;  
 int main()  
 {  
   string s;  
   cin >> s;  
   if(s.size() < 3 || (s.size() == 3 && s <= "127"))  
     cout <<"byte";  
   else if (s.size() < 5 || (s.size() == 5 && s <= "32767"))  
     cout <<"short";  
   else if(s.size() < 10 || (s.size() == 10 && s <= "2147483647"))  
     cout <<"int";  
   else if(s.size() < 19 || (s.size() == 19 && s <= "9223372036854775807"))  
     cout <<"long";  
   else cout <<"BigInteger";  
 }  


4. Fibonacci Revisited

In this problem you need to calculate two fibonacci number which are greater and smaller then given number.
So minimum steps required is min(greater-N,N-smaller)


 #include<iostream>  
 using namespace std;  
 int find(int N) {  
    int a = 0, b = 1;  
    while(b < N) {  
     int c = a+b;  
     a = b;  
     b = c;  
    }  
    return min(N-a, b-N);  
   }  
 }  
 int main() {  
   long long N;  
   cin>>N;  
  cout <<find(N) << endl;  
  return 0;  
 }  

5. Primorial Numbers

It was challenge Problem Toughest in the given Problem set.
I'll write detailed explanation in free time..
Here is my solution.. try to under the code :D

 #include<cstdio>  
 #include<algorithm>  
 #include<cstring>  
 using namespace std;  
 const int maxn=200000+10;  
 char st[maxn];  
 int res[maxn];  
 int need[maxn];  
 int n;  
 int main()  
 {  
     scanf("%s",st+1);  
     n=strlen(st+1);  
     for (int i=n;i;i--)  
     if (st[i]<'4') need[i]=0;else  
     if (st[i]=='4') need[i]=min(1,need[i+1]);else  
     if (st[i]<'7') need[i]=1;else  
     if (st[i]=='7') need[i]=need[i+1]+1;  
     else need[i]=1000000000;  
     if (n%2==0)  
     {  
         bool ok=1;  
         int a=0,b=0,c=4,flag=0;  
         for (int i=1;i<=n;i++)  
         if (flag)  
         {  
             if (a+1<=n/2) res[i]=4,a++;  
             else res[i]=7,b++;  
         } else  
         {  
             if (st[i]<'4' && a+1<=n/2) res[i]=4,a++,flag=1;else  
             if (st[i]=='4' && a+1<=n/2 && need[i+1]<=n/2-b)res[i]=4,a++;else  
             if (st[i]<'7' && b+1<=n/2) res[i]=7,b++,flag=1;else  
             if (st[i]=='7' && b+1<=n/2) res[i]=7,b++;  
             else ok=0;  
         }  
         if (ok)  
         {  
             for (int i=1;i<=n;i++) printf("%d",res[i]);  
             printf("\n");  
             return 0;  
         }  
         n+=2;  
     } else n+=1;  
     for (int i=1;i<=n/2;i++) printf("4");  
     for (int i=1;i<=n/2;i++) printf("7");  
     printf("\n");  
 }  

Thank you so much friends for participating in the contest. I hope you enjoyed. Our next Contest will be more competitive and Problems will be tricky and Tough. 

Keep Practicing Till then. 
Happy Coding! 
:D 

no image
A Blog about Programming,Algorithms,Coding Competitions, and Discrete Mathematics!
Hi there!  This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.  When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them. Each time that I prepared for an interview, I thought to myself "Why oh why hasn't someone created a nice Big-O cheat sheet?".  So, to save all of you fine folks a ton of time, I went ahead and created one.  Enjoy

Searching



Algorithm Data Structure Time Complexity Space Complexity


Average Worst Worst
Depth First Search (DFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Breadth First Search (BFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Binary search Sorted array of n elements     O(log(n)) O(log(n)) O(1)
Linear (Brute Force) Array      O(n) O(n) O(1)
Shortest path by Dijkstra,
using a Min-heap as priority queue
Graph with |V| vertices and |E| edges     O((|V| + |E|) log |V|) O((|V| + |E|)log |V|) O(|V|)
Shortest path by Dijkstra,
using an unsorted array as priority queue
Graph with |V| vertices and |E| edges    O(|V|^2) O(|V|^2) O(|V|)
Shortest path by Bellman-Ford Graph with |V| vertices and |E| edges    O(|V||E|) O(|V||E|) O(|V|)




Sorting



Algorithm Data Structure Time Complexity Worst Case Auxiliary Space Complexity


Best Average Worst Worst
Quicksort Array O(n log(n)) O(n log(n)) O(n^2)              O(n)
Mergesort Array O(n log(n)) O(n log(n)) O(n log(n))              O(n)
Heapsort Array O(n log(n)) O(n log(n)) O(n log(n))              O(1)
Bubble Sort Array O(n) O(n^2) O(n^2)               O(1)
Insertion Sort Array O(n) O(n^2) O(n^2)              O(1)
Select Sort Array O(n^2) O(n^2) O(n^2)              O(1)
Bucket Sort Array O(n+k) O(n+k) O(n^2)              O(nk)
Radix Sort Array O(nk) O(nk) O(nk)              O(n+k)


Data Structures



Data Structure Time Complexity Space Complexity

Average Worst Worst

Indexing Search Insertion Deletion Indexing Search Insertion Deletion
Basic Array O(1) O(n) - - O(1) O(n) - - O(n)
Dynamic Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
Singly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Doubly-Linked List O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
Skip List O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n log(n))
Hash Table - O(1) O(1) O(1) - O(n) O(n) O(n) O(n)
Binary Search Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
Cartresian Tree - O(log(n)) O(log(n)) O(log(n)) - O(n) O(n) O(n) O(n)
B-Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Red-Black Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
Splay Tree - O(log(n)) O(log(n)) O(log(n)) - O(log(n)) O(log(n)) O(log(n)) O(n)
AVL Tree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)


Heaps


Heaps Time Complexity

Heapify Find Max Extract Max Increase Key Insert Delete Merge
Linked List (sorted) - O(1) O(1) O(n) O(n) O(1) O(m+n)
Linked List (unsorted) - O(n) O(n) O(1) O(1) O(1) O(1)
Binary Heap  O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)
Binomial Heap - O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n))
Fibonacci Heap - O(1) O(log(n))* O(1)* O(1) O(log(n))* O(1)


Graphs

Node / Edge Management Storage Add Vertex Add Edge Remove Vertex Remove Edge Query
Adjacency list O(|V|+|E|) O(1) O(1) O(|V| + |E|) O(|E|) O(|V|)
Incidence list O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)
Adjacency matrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)
Incidence matrix O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|V| ⋅ |E|) O(|E|)


Notation for asymptotic growth

letter  bound   growth
(theta) Θ                             upper and lower, tight                                      equal
(big-oh) O upper, tightness unknown less than or equal
(small-oh) o upper, not tight less than
(big omega) Ω lower, tightness unknown greater than or equal
(small omega) ω lower, not tight greater than

[1]  Big O is the upper bound, while Omega is the lower bound. Theta requires both Big O and Omega, so that's why it's referred to as a tight bound (it must be both the upper and lower bound). For example, an algorithm taking Omega(n log n) takes at least n log n time but has no upper limit. An algorithm taking Theta(n log n) is far preferential since it takes AT LEAST n log n (Omega n log n) and NO MORE THAN n log n (Big O n log n). SO

[2]  f(x)=Θ(g(n)) means f (the running time of the algorithm) grows exactly like g when n (input size) gets larger. In other words, the growth rate of f(x) is asymptotically proportional to g(n).

[3]  Same thing. Here the growth rate is no faster than g(n). big-oh is the most useful because represents the worst-case behavior.
In short, if algorithm is __ then its performance is __ 

algorithm performance
o(n) < n
O(n) ≤ n
Θ(n) = n
Ω(n) ≥ n
ω(n) > n

Will edit this post soon to make it more easily readable...

Blog Post Credits - Pawan Tiwari(Microsoft SDET US)
A Blog about Programming,Algorithms,Coding Competitions, and Discrete Mathematics!
Hello friends, today I'm going to talk about factorial of Negative Numbers.

In mathematics, factorial of a positive Integer say n is given as 

where '*' is multiplication sign.

you know 0! is taken as 1.
Factorial Image

Factorial of a positive integers have great applications in Programming and mathematics. Permutations and combinations is one (where we find the number of ways a group of objects can be arranged or selected in given group of objects). Expansion of terms like (a+b)^n (binomial theorem) are computed using factorials.

We'll discuss about how to find factorials of very large numbers like say 100! or 1000! in programming in my upcoming posts.

Well, let's discuss about factorials about negative numbers here...
 
A Blog about Programming,Algorithms,Coding Competitions, and Discrete Mathematics!
Hello Friends, I'm gonna about Graph Theory in my upcoming posts, so here are some basic Graph terminology which will form a base to understand Graph Theory and Algorithms.

 

Overview

  • Definitions: Vertices, edges, paths, etc
  • Representations: Adjacency list and adjacency matrix



Definitions: Graph, Vertices, Edges

  • Define a graph G = (V, E) by defining a pair of sets:
    1. V = a set of vertices
    2. E = a set of edges

  • Edges:
    • Each edge is defined by a pair of vertices
    • An edge connects the vertices that define it
    • In some cases, the vertices can be the same

  • Vertices:
    • Vertices also called nodes
    • Denote vertices with labels

  • Representation:
    • Represent vertices with circles, perhaps containing a label
    • Represent edges with lines between circles

  • Example:
    • V = {A,B,C,D}
    • E = {(A,B),(A,C),(A,D),(B,D),(C,D)}




Motivation

  • Many algorithms use a graph representation to represent data or the problem to be solved

  • Examples:
    • Cities with distances between
    • Roads with distances between intersection points
    • Course prerequisites
    • Network
    • Social networks
    • Program call graph and variable dependency graph