Answered Questions

  • How can you traverse a matrix of m lines and n columns in a zig-zag way ?

    Example : 1)m = 3,n = 2 a11 a12 a21 a22a31 a32Output : a11 a21 a12 a31 a22 a322) m = 3, n = 4a11 a12 a13 a14a21 a22 a23 a24a31 a32 a33 a34Output : a11 a21 a12 a31 a22 a13 a32 a23 a14 a33 a24 a34

    Amit Verma

    • Feb 21st, 2015

    A very basic solution and easy to understand

    Code
    1. int r=0;
    2.         for(int i=0;i<row;i++)
    3.         {
    4.                 r=i;
    5.                 for(int j=0;j<=i;j++)
    6.                 {
    7.                         if(j<col)
    8.                         {
    9.                         cout<<m[r][j]<<" ";
    10.                         r--;
    11.                         }
    12.                 }
    13.                
    14.         }
    15.        
    16.         int c=0;
    17.  
    18.         for(int i=1;i<=col-1;i++)
    19.         {
    20.                 c=i;
    21.                 r=row-1;
    22.                 for(int j=row-1;j>=0;j--)
    23.                 {
    24.                         if(c<col)
    25.                         {
    26.                                
    27.                                 cout<<m[r][c]<<" ";;
    28.                                 r--;
    29.                                 c++;
    30.                                
    31.                         }
    32.                 }
    33.         }

  • Data Structure - character that repeats itself

    Given a string of characters (let us say there are about 100 characters or more in the string), what is the most efficient method to use for finding out the character that repeats itself the most?Is it possible for us to use some kind of data structure for this?, if this is so,which is the best one that needs to be used?

    vinod148

    • Oct 23rd, 2009

    There is no way for less that O(n) solution as we have to traverse the string atleast once.By traversing only once we can find the solutionKeep another variable which tracks the current highest occured character.

    pvsola

    • Apr 3rd, 2008

    There is an way to solve this issue with complexity of O(n).1.  MAke an array size of 2562.  Using each charater read from the string as an index incremented the counter ...

  • function f(x) is continuous

    The function f(x) is continuous in [0,1], such that f(0)=-1, f(1/2)=1 and f(1)=-1, We can conclude that 1. f attains the value zero at least twice in [0,1] 2. f attains the value zero exactly once in [0,1] 3. f is non-zero in [0,1] 4. f attains the value zero exactly twice in [0,1]

    zeptov

    • Nov 7th, 2010

    Colloquially, "continuous" means that you can graph it without picking up your pencil. What you can't tell is whether f(x) crosses the axis lots, or just twice.If the function were disco...