Home Author
Author

nikoo28

  • Arrays

    Finding spans in an array.

    by nikoo28
    1 minutes read

    Question: Find spans in an array. Given an array arr[], the SPAN s[i] of arr[i] is the maximum number of consecutive elements arr[j] immediately before arr[i] such that arr[j] <= arr[i]. Let us try to understand the question once again. This is a very common problem in stock markets to find the peaks. Spans have applications to financial analysis(Example:- You might have heard a saying “STOCKS AT 52 WEEK HIGH”). The span of a stocks price on certain day, i , is the maximum number of consecutive days (up-to the…

    0 FacebookTwitterLinkedinWhatsappEmail
  • This is one of the very basic pointer usage in C and to understand it let us start with a very simple example. Suppose we have a program like:- In this case the first statement: declares p as a pointer to char. When we say “pointer to a char“, what does that mean? It means that the value of p is the address of a char; p tells us where in memory there is some space set aside to hold a char. The statement also initializes p to point to…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: How do we implement 2 stacks using only one array? Our stack routines should not indicate an exception unless every slot in the array is used? SOLUTION: Algorithm: Start with two indexes, one at the left end and other at the right end The left index simulates the first stack and the right index simulates the second stack. If we want to push an element into the first stack then put the element at left index. Similarly, if we want to push an element into the second stack then…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array of characters formed by a’s and b’s. The string is marked with special character ‘X’ which represents the middle of the list (for example ababa…ababXbabab…baaa). Check whether the string is palindrome or not? Input: ababXbaba Output: PALINDROME Input: ababababababbbbbXbbbbabababababa Output: PALINDROME Input: abababbbabbXabbbabbabbb Output: NOT PALINDROME This is one of the simplest algorithms. What we do is, start two indexes – one at the beginning of the string and other at the ending of the string. Each time compare whether the values at both the indexes…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Suppose we want to find the minimum element available in a stack at any point of time. The most trivial and common method would be to traverse the entire stack and keep a record of the least element encountered. At the end, we can return this element. This method no doubt is easy, but it consists of traversing the entire stack again and again, which can lead to time complexities. Thus, we need to optimize it, and it can be done with a very neat little trick:- ALGORITHM:- Take an…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Discuss Postfix Evaluation using stacks

    by nikoo28
    3 minutes read

    Post-fix expressions have a very special place in computer science. Most of the computations are performed using stacks. In-fact a very simple calculator works on the principle of post-fix evaluation to get the answer. A post-fix notation is also known as the reverse Polish notation. The steps involved in evaluating a post-fix expression can be defined as follows. We use the concept of stacks to evaluate the same. ALGORITHM: Scan the post-fix string from left to right. Initialize an empty stack. Repeat the below steps 4 and 5 till all…

    0 FacebookTwitterLinkedinWhatsappEmail
  • In infix expressions, the operator precedence is implicit unless we use parentheses. Therefore, for the infix to postfix conversion algorithm, we have to define the operator precedence inside the algorithm. We did this in the post covering infix, postfix and prefix expressions. What are infix, prefix and postfix expressions? Important properties:- Let us consider the infix expression 2 + 3 * 4 and its postfix will be 2 3 4 * +. Notice that between infix and postfix the order of the numbers(or operands) is unchanged. It is 2 3…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    What are infix, postfix and prefix expressions?

    by nikoo28
    2 minutes read

    Stacks can be used to implement algorithms involving Infix, postfix and prefix expressions. So let us learn about them:- INFIX:- An infix expression is a single letter, or an operator, proceeded by one infix string and followed by another infix string. A A + B (A + B) + (C – D) PREFIX:- A prefix expression is a single letter, or an operator, followed by two prefix strings. Every prefix string longer than a single variable contains an operator, first operand and second operand A + A B + +…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Stacks can be used to check if the given expression has balanced symbols or not. The algorithm is very much useful in compilers. Each time parser reads one character at a time. If the character is an opening delimiter like ‘(‘ , ‘{‘ or ‘[‘ then it is PUSHED in to the stack. When a closing delimiter is encountered like ‘)’ , ‘}’ or ‘]’ is encountered, the stack is popped. The opening and closing delimiter are then compared. If they match, the parsing of the string continues. If they…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More