446 views
2 votes
2 votes

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:

1 Answer

4 votes
4 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).

edited by

Related questions

0 votes
0 votes
1 answer
1
Learner_jai asked Nov 27, 2018
480 views
Given an Unsorted array, Find maximum in less than O(n) time?How can we do this?
0 votes
0 votes
1 answer
2
Desert_Warrior asked May 16, 2016
603 views
#include<stdio.h #include<stdlib.h int main() { char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i); return 0; }(a) Segmentation Fault (b) Compile Err...
0 votes
0 votes
0 answers
3
0 votes
0 votes
2 answers
4
Desert_Warrior asked May 16, 2016
1,556 views
#include<stdio.h int main() { int a[10][20][30]={0}; printf("%ld",&a+1 - &a); return 0; }