Adobe Interview Question
Software Engineer / Developerspublic class ShareMax {
public static void main(String[] args)
{
int[] a={8,4,3,6,45,10,87,32,2};
int bestMax=0;
int bestMin=0;
int diff=0;
int max=a[0];
int min=a[0];
int i=0;
int len=a.length;
while(i<len)
{
if(a[i]>max)
{
max=a[i];
bestMin = min;
bestMax=max;
diff=max-min;
}
else
{
if(a[i]<min)
min=a[i];
}
i++;
}
System.out.println(bestMax+" "+bestMin);
}
}
I will write psuedo code
indexofmin = 0;
for(i=0 to n; i++)
if(a[i] < a[min]) i = min;
if(a[min] < a[n-i-min]) return;
#include<iostream>
using namespace std;
void findMaxDistanceOfIndices(int* inArray, int size)
{
if (size <= 0)
{
cout << "Empty array!" << endl;
return;
}
int maxStartIndex = 0;
int maxEndIndex = 0;
int maxDistance = -1;
int curStartIndex = 0;
for (int i = 0; i < size; i++)
{
if (inArray[i] <= inArray[curStartIndex])
{
curStartIndex = i;
}
else
{
if (i - curStartIndex > maxDistance)
{
maxStartIndex = curStartIndex;
maxEndIndex = i;
maxDistance = i - curStartIndex;
}
}
}
if (maxDistance < 0)
{
cout << "universally decreasing array! no answer found" << endl;
return;
}
cout << "start index: " << maxStartIndex << " end index: " << maxEndIndex << endl;
cout << "max distance: " << maxDistance << endl;
}
int main (int argc, char* argv[])
{
int size = 9;
int array[] = {5, 3, 4, 1, 3, 2, 6, 3, 2};
findMaxDistanceOfIndices(array, size);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[15]={110,5,5,7,6,6,6,4,4,4,3,-1,1,1,4};
int i,j,a,b;
i=0;
j=14;
while(i<j)
{
if(arr[i]<arr[j])
break;
else if(arr[i+1]<arr[j])
i++;
else if(arr[j-1]>arr[i])
j--;
else
{
i++;
}
}
a=0;
b=14;
while(a<b)
{
if(arr[a]<arr[b])
break;
else if(arr[a+1]<arr[b])
a++;
else if(arr[b-1]>arr[a])
b--;
else
{
b--;
}
}
if((j-i)<(b-a))
{
i=a;
j=b;
}
printf("%d %d",i,j);
}
/* For a given array arr[], returns the maximum j – i such that
arr[j] > arr[i] */
int maxIndexDiff(int arr[], int n)
{
int maxDiff;
int i, j;
int *LMin = (int *)malloc(sizeof(int)*n);
int *RMax = (int *)malloc(sizeof(int)*n);
/* Construct LMin[] such that LMin[i] stores the minimum value
from (arr[0], arr[1], ... arr[i]) */
LMin[0] = arr[0];
for (i = 1; i < n; ++i)
LMin[i] = min(arr[i], LMin[i-1]);
/* Construct RMax[] such that RMax[j] stores the maximum value
from (arr[j], arr[j+1], ..arr[n-1]) */
RMax[n-1] = arr[n-1];
for (j = n-2; j >= 0; --j)
RMax[j] = max(arr[j], RMax[j+1]);
/* Traverse both arrays from left to right to find optimum j - i
This process is similar to merge() of MergeSort */
i = 0, j = 0, maxDiff = -1;
while (j < n && i < n)
{
if (LMin[i] < RMax[j])
{
maxDiff = max(maxDiff, j-i);
j = j + 1;
}
else
i = i+1;
}
return maxDiff;
}
StartIndex = A[ 0];
EndIndex = A[1];
Max = abs( a[ 1] - A[ 0] )
for(i= 2: i < limit ; i++)
{
if (a[ StartIndex] > a[ i]) {
StartIndex = i;
}
elseif (abs(a[i] - a[StartIndex]) > max) {
EndIndex = i;
}
max = abs(A[EndIndex] - A[ StartIndex]);
}
}
printf("%d %d %d",StartIndex, EndIndex, Max);
int maxji(int [] a){
int l = a.length;
int i,j,min, max;
max = j = l-1;
min = i = 0;
while(i<l){
if ( a[i]<a[min])
min =i;
if(a[j]>a[max])
max=j;
if(a[min]<a[max])
return max-min;
j--;
i++;
}
return 0;
}
Where this ques was asked ... in Blore or Noida ...... & you have applied for which post ....... pls reply
- Anonymous May 19, 2011