Adobe Interview Question
Software Engineer / DevelopersCountry: India
Works flawlessly:
int findMax(int[] A){
int max[] = new int[A.length];
//Initialization Code
if (A.length >= 2){
max[0] = A[0] > 0 ? A[0] : 0 ;
max[1] = A[1] > 0 ? A[1] : 0 ;
}
else{
if (A.length > 1){
return (A[0] > A [1]) ? A[0] : A[1];
}
else
return A[0];
}
boolean posFlag = false;
//Dynamic programming to calculate max-sum sub-array
for (int i = 2; i< A.length; i++){
int num = 0;
if (A[i] > 0){
num = A[i];
posFlag = true; //to check if there was at least 1 positive number
}
max[i] = num + getMax(max,0,i-2);
}
//If all numbers were negative, the highest number is the max sum
if (!posFlag) return getMax(A,0,A.length-1);
//Else return the maximum
return getMax(max, 0, max.length-1);
}
int getMax(int[] A, int p, int r){
int max = A[p];
for(int i = p+1; i<= r; i++){
if (A[i] > max)
max = A[i];
}
return max;
}
is the given array sorted?
define "adjacency"... in the array 1 3 2 4 5, are 2 and 3 adjacent or 3 and 4 in the conventional sense?
I think here interviewer is not talking in conventional sense...3 and 2 are adjacent, 3 & 4 are not adjacent.
@DashDash- is this interview question ?? I also gave interview of Adobe at bangalore for Live cycle team.
My Interviewer was dumb as few months back someone posted same experience with some Adobe interviewer.
------------------------------
Regarding question - I think that this question is either very easy because if array only has positive numbers. Then only 2 sub-arrays we have to compare. One is, sum for all the elements at odd position and other is, sum for all the elements at even position. If adjacent here does not mean in conventional sense.
import java.util.Arrays;
public class ArraysSum {
public void arraySum(int[] arr){
int arraySize=arr.length;
int[] tmparr=new int[arraySize];
System.arraycopy(arr,0,tmparr,0, arr.length);
Arrays.sort(tmparr);
// getting another array with the sorted positions list
for(int i=arraySize-1;i>=0;i--){
int search=tmparr[i];
for(int j=0;j<arraySize;j++){
if(arr[j]==search){ tmparr[i]=j;break;}
}
}
System.out.println("Sortedpositions - array");
for(int j=0;j<arraySize;j++)
System.out.println(tmparr[j]+"-"+arr[j]);
//calculating the sum depending on the positions in tmparr array
int sum=arr[tmparr[arraySize-1]],prevPos=tmparr[arraySize-1];
System.out.println(sum);
for(int i=arraySize-2;i>=0;i--)
{
if(tmparr[i]!=prevPos+1 && tmparr[i]!=prevPos-1)
{
sum+=arr[tmparr[i]];
prevPos=tmparr[i];
}
}
System.out.println(sum);
}
public static void main(String args[]){
ArraysSum as=new ArraysSum();
int[] arr={1,4,3,5,9,23,2};
as.arraySum(arr);
}
}
#include<iostream>
using namespace std;
int maxsum(int a[],int n)//using Dynamic Programming
{
int lis[n];
for(int i =0;i<n;i++)
{
lis[i] = a[i];
}
for(int i =1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(j != i-1)
{
if(lis[i] < lis[j] + a[i])
{
lis[i] = lis[j] + a[i];
}
}
}
}
int max = lis[0];
for(int i =1;i<n;i++)
{
if(lis[i] > max)
{
max = lis[i];
}
}
return max;
}
- Anonymous October 08, 2012