GATE CSE
First time here? Checkout the FAQ!
x
0 votes
79 views

Given an array of integers, update the index with multiplication of previous and next integers,

   e.g.

Input: 2 , 3, 4, 5, 6

Output: 2*3, 2*4, 3*5, 4*6, 5*6 

Please suggest an approach that gives output with maximum performance:

asked in Programming by Veteran (26.7k points)   | 79 views

1 Answer

+3 votes
void array (int A[],int Aux[], int n)
{
    Aux[0]=A[0]*A[1];        /* First and last index calculation are exception
                                    so, it can be manually done.*/
    Aux[n-1]=A[n-1]*A[n-2];
    
    for(int i=1;i<n-1;i++)
    Aux[i]=A[i-1]*A[i+1];
}

A is given array of integers and Aux is auxiliary array for storing final result and n is the length of array . And complexity of this solution is O(N).

answered by Active (2.2k points)  
edited by

Related questions

0 votes
1 answer
1
0 votes
0 answers
2
0 votes
2 answers
3
asked in Programming by Desert_Warrior Boss (8.9k points)   | 120 views


Top Users May 2017
  1. akash.dinkar12

    3338 Points

  2. pawan kumarln

    2108 Points

  3. Bikram

    1922 Points

  4. sh!va

    1682 Points

  5. Arjun

    1614 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1208 Points

  8. Angkit

    1056 Points

  9. LeenSharma

    1018 Points

  10. Arnab Bhadra

    812 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    1008 Points

  2. pawan kumarln

    734 Points

  3. Arnab Bhadra

    726 Points

  4. Arjun

    342 Points

  5. bharti

    328 Points


22,893 questions
29,196 answers
65,302 comments
27,695 users