Solve it!!! Intresting One!!!

Discussion in 'C' started by asadullah.ansari, Apr 29, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Can you Anyone solve this problem...

    There is given one array lets say Arr[n]( n is the size of array). You have to store multiplication of all elements of Arr[n] except element Arr into a array OutVar. Example:
    Code:
        Suppose given am array as
        Arr[5]={1,2,3,4,5}
        Then
    
        OutVar[0]=Arr[1]*Arr[2]*Arr[3]*Arr[4]
        OutVar[1]=Arr[0]*Arr[2]*Arr[3]*Arr[4]
        OutVar[2]=Arr[0]*Arr[1]*Arr[3]*Arr[4]
        OutVar[3]=Arr[0]*Arr[1]*Arr[2]*Arr[4]
        OutVar[4]=Arr[0]*Arr[1]*Arr[2]*Arr[3]*
    
    Condition Apply: 1. Time Complexity should be O(n) (No matter for space better If you
    minimize space also)
    2. Division(/) or Modular division(%) are not allowed.
     
    Last edited: Apr 30, 2008
  2. blabla2028

    blabla2028 New Member

    Joined:
    Apr 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    The code would be something like this I suppose:

    Code:
    int main()
    {
    int arr[n],OutVar[n2];
    int counter1,counter2,result;
    result=0;
    
    for(counter1=0; counter1<n2; counter1++)
    {
    for(counter2=0; counter2<n; counter2++)
    {
    if counter2=counter1 continue;
    result=result*arr[counter2];
    }
    OutVar[counter1]=result;
    result=1;
    }
    
    }
    
     
  3. blabla2028

    blabla2028 New Member

    Joined:
    Apr 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    The code would be something like this I suppose:

    Code:
    int main()
    {
    int arr[n],OutVar[n2];
    int counter1,counter2,result;
    result=1;
    
    for(counter1=0; counter1<n2; counter1++)
    {
    for(counter2=0; counter2<n; counter2++)
    {
    if counter2=counter1 continue;
    result=result*arr[counter2];
    }
    OutVar[counter1]=result;
    result=1;
    }
    
    }
    
     
  4. blabla2028

    blabla2028 New Member

    Joined:
    Apr 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    the result int avariable must be set to 1 since the beginning
     
  5. blabla2028

    blabla2028 New Member

    Joined:
    Apr 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    also supposing the two array sizes are equal. n=n2
     
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
     
  7. subha040680

    subha040680 New Member

    Joined:
    Apr 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Can this be the solution?

    Code:
    void main()
    {
      for(int cnt = 0; cnt < 5; ++cnt)
      {
        OutVar[cnt] = Arr[(i+1)%5] *Arr[(i+2)%5]*Arr[(i+3)%5]*Arr[(i+4)%5];
      }
    }
     
  8. subha040680

    subha040680 New Member

    Joined:
    Apr 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Soorry, 'i' is wrong it will be 'cnt' everywhere in the previous one.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    When using code block try indenting your code so that its easy to read for others.
     
  10. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Nice approch!!! But sorry dear i forgot to mention you cant use division or modular division operator.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice