## Myntra Interview Question for Software Engineer / Developers

• 0

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
7
of 7 vote

dp

At i=0;
sum=a[i];
sum=max(a,a)
sum[i]=max(sum[i-2]+a[i],sum[i-1])
return sum[|a|-1)]

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0

I went ahead and just implemented the simplest answer and here it is

``````ret = a;
ret = a;

for (int i = 2; i < a.length; i++){
if (ret < a[i] && ret > ret)
ret = a[i];
else if (ret < a[i])
ret = a[i];
}``````

if line has ret > ret so ret doesn't get changed when ret is smaller than ret

Comment hidden because of low score. Click to expand.
1
of 1 vote

int[] A = {10, 1, 3, 25, 2, 4, 20}; should indeed result in 55 {10, 25, 20} and so the algo is perfectly correct! What is your point?

Comment hidden because of low score. Click to expand.
2
of 2 vote

To solve these type of question, first thing is to find a recurring relation. In this case our recurring relation will tell the max sum till a given length. It means that we will get the max sum for running length of the array. If we denote the ith element as T(i) and max sum till ith element of the array as S(i), then

S(i) = MAX {S(i-1), S(i-2) + T(i) }

S(-1) = 0;
if i=0, S(i) = T(0);

Note: while developing this relationship, I assume array index is starting from 0.

Writing code for this problem is not a big deal now. Below is the code snippet for the same.

``````sum2 = 0;
sum = sum1 = array;

for(i=1; i<len; i++)
{
sum = MAX(sum2 + array[i], sum1);
sum2 = sum1;
sum1 = sum;
}``````

For a detailed explanation, visit bit.ly/ReP5zl

Comment hidden because of low score. Click to expand.
0

Thank you Anonymous, it's working fine.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Try this

for (int i=0;i<a.length-2;i++)
{
for(int j=i+2;j<a.length;j++)
{
sum = a[i]+a[j];
if (temp<sum)
{
temp = sum;
first = a[i];
second = a[j];
}
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

main()
{

int SIZE=3;
int arr[]= {10,1,3,25};

int max_sum=0;

for(int i=0; i<SIZE-1; i++)
{
int sum=arr[i]+arr[i+2];
if(sum > max_sum)
{

max_sum=sum;

}

}

int sum1=arr + arr[SIZE];
if(max_sum<sum1)
{

max_sum=sum1;
}
cout<<max_sum<<"is greatest";

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````import collections
'''
Problem :
Find the subsequences whose elements should not be adjacent and their sum should be maximum from the given array (contains only positive integers).
Eg: int[] A = {10, 1, 3, 25}
Sol: Sum: {10, 3} = 13
{1,25} = 26
{10,25} = 35
Here the Maximum subsequence is {10, 25}.

Approch :

compare the adj elements and find the max and keep them in the sum array
repeat until it's length is 2 (to form a pair)
'''
sumList = []
temp = array
if len(array) < 2:
print "Cannot form a pair"
return
while temp:
for i in range(1,len(temp)):
sumList.append(max(temp[i-1],temp[i]))
temp = None
temp = collections.OrderedDict.fromkeys(sumList).keys()#normal sets are unorderd
print temp
sumList = list()
if len(temp) == 2:
break

print "Result "+str(temp)

def main():
nums = [10,1,3,25]

if __name__ == "__main__":main()``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple and short implementation of Dynamic Programming idea mentioned here :

``````int sum_max_dp(int arr[], int n){
int result[n];
result=arr;
result=max(arr,arr);
for(int i=2;i<n;i++) result[i]= max(result[i-2]+arr[i], result[i-1]);
return result[n-1];
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.