top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Largest Difference in an Array

+4 votes
388 views

You have an array of integers:
int[] A = { 10, 3, 6, 8, 9, 4, 3 };
My goal is to find the largest difference between A[Q] and A[P] such that Q > P.

If P = 1 and Q = 4

diff = A[Q] – A[P]
diff = 9 – 3
diff = 6
Since 6 is the largest number between all the difference, that is the answer.

posted Aug 19, 2016 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

algorithm:
1) consider an array 10,3,6,8,9,4,3. start from the 2nd ele. do 3-10=-7 save it in deatination array b[0]
2) consider 3rd ele 6. do 6-3=3, 6-10=-4 and 3>-4 so save 3 in b[1]
3}similarly consider 4th ele 8. do 8-6=2, 8-3=5, 8-10=-2 and 5>2>-2. therefore save 5 in b[2]
4)similary do for all ele. b[] will be -7 3 5 6 1 0. b[] size is one less than the given arr a[] because u ll start from 2nd ele in step 1).
5) the largest ele in the b[] is 6. therefore it ll be the highest difference between two ele in the array.

#include<stdio.h>

int main()
{
    int a[]={10,3,6,8,9,4,3};
    int na=7; // given array size
    int b[6]; //  destination array should be 1 less than the given array a[]
    int nb=6; // destination array size b[]
    int i,j,temp=-999,temp2=-999; //consider temp as the very smallest integer possible
    int max;

    for(i=1;i<na;i++)
    {
        for(j=i-1;j>=0;j--)
        {
            max=a[i]-a[j];
            temp2=max; // save max

            if(temp>max) //comparing previous iteration with the current iteration n keep the highest val in **max**
                max=temp;
            temp=temp2; //saved max in temp. temp ll always have the previous iteration value 
        }
        b[i-1]=max;

    }

    max=b[0]; // find the biggest ele in the dest arr b[] to find the highest diff
    for (i=0;i<nb-1;i++)
    {
        if(b[i+1]>max)
            max=b[i+1];
    }
    printf("max=%d\t",max);
    return 0;
}
answer Aug 24, 2016 by Sneha Maganahalli
Please add some lines as part of algo so that it can help people to understand the code. Just use the edit button...:)
Similar Questions
+3 votes

Input: arr[] = {5, 5, 10, -10, -20, 40, 50, 35, -20, -50}
Output: 125 (40, 50, 35)

+5 votes

Input: {5, 8, 5, 3, 3, 2, 7}
Output: {5, 8, 3, 2, 7, ?, ?}

Idea is that we should remove the duplicate number from the array and should return the array in the original form (obviously without duplicate).

Please help me with an efficient algorithm or C code.

+6 votes

I was trying to get maximum rectangle area for a given histogram but I used brute force approach which have O(n^2) time complexity so I want some better solution using stack so that we could reduce time complexity to O(n) or O(log n ).

0 votes

Write a c program that rotate elements of an array by the value of configured number "n".
For example:
Input array[ ] = { 2, 3, 5, 6, 8, 9}
value of n = 2
Output array[] = { 5, 6, 8, 9, 2, 3}
I am looking for efficient program.

...