Samsung Interview Question for Software Engineers


Country: India




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

DP

S can move A, B or C.
X A B C X
X X S X X

If there are 3 candidate of S and each values are 3, 1 and 0.
0 1 1 0 0
0 S S S 0 <-- 0 3 1 0 0

Then we update next upper line with the maximum value between each candidate's moves.

First S = 3
3 4 4 0 0
_ 3 _ _ _

Second S = 1
0 2 2 1 0
_ _ 1 _ _

Third S = 0
0 1 1 0 0
_ _ _ 0 _

The results
3 4 4 1 0
0 3 1 0 0

Sample case

0 1 0 2 0
0 2 2 2 1
0 2 1 1 1
1 0 1 0 0
0 0 1 2 2
1 1 0 0 1
X X S X X <-- 0 0 0 0 0 (Initial)

...

S S X S S <-- 2 3 0 0 5 (Last)
S X X X S <-- 2 0 0 0 5 (5)
S X S S S <-- 2 0 4 4 3 (4)
S S S S X <-- 2 2 3 2 0 (3)
S S S X X <-- 1 1 2 0 0 (2)
X S S S X <-- 0 1 0 0 0 (1)
X X S X X <-- 0 0 0 0 0 (Initial)


In last line, the maximum value is 5.

- Anonymous September 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author SVN SAIRAM
 */
public class JavaApplication96 {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int arr[][]=new int[n][5];
        for(int i=0;i<n;i++)
        {
        
        for(int j=0;j<5;j++)
        {
        arr[i][j]=sc.nextInt();
        }
            
        }
    
       System.out.println(repeat(n,arr,1,2,n,0));
    }
    
    public static int repeat(int pos,int [][]arr,int bomb,int place,int n,int coins)
    { //System.out.println(pos+" "+place+" "+bomb+" "+coins);
      if(pos<0||place>=5||place<0)
           return coins;
      
      if(pos!=n){
      if(arr[pos][place]==2)
       {
       return coins;
       }
        if(arr[pos][place]==1)
            coins++;
      }
      
        int brr[][]=new int[n][n];
    if(bomb==1)
    { 
        for(int i=0;i<n;i++)
        {
        for(int j=0;j<5;j++)
        {
        brr[i][j]=arr[i][j];
        }    
        }
        int count=5;
        int index=pos;
        while(index-->0&&count-->0)
        {
        for(int j=0;j<5;j++)
        {
        if(brr[index][j]==2)
        {
        brr[index][j]=0;
        }
        }
        }
    
    }
    
    
        if(bomb==1)
        {
            int position=pos-1;
            int nm=place;
            int lm=place-1;
            int rm=place+1;
            int x=repeat(position,arr,1,lm, n,coins);
            int y=repeat(position,arr,1,nm, n,coins);
            int z=repeat(position,arr,1,rm,n,coins);
            int a=repeat(position,brr,0,lm,n,coins);
            int b=repeat(position,brr,0,nm,n,coins); 
            int c=repeat(position,brr,0,rm,n,coins);
            int d=Math.max(Math.max(Math.max(x, y), Math.max(z, a)),Math.max(b, c));
            return d;
            
        
        }
    else
        {int position=pos-1;
            int nm=place;
            int lm=place-1;
            int rm=place+1;
            int z=Math.max(repeat(position,arr,0,lm,n,coins), repeat(position,arr,0,nm,n,coins));
        return Math.max(z, repeat(position,arr,0,rm,n,coins));
        
        }
        
    
      
    }
    
    
}

- Anonymous October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you explain your approach?

- A_coder October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

this is 3d dp solution.

#include<iostream>
using namespace std;
int rec(int arr[][5],int dp[][2][5],int curr_i,int curr_j,int bomb,int x)
{
    if(dp[curr_i][bomb][curr_j]!=-1){
          //  cout<<"OVERLAP!"<<endl;
        return dp[curr_i][bomb][curr_j];
    }
    if(bomb==0)                              //no bomb left
    {
        if(curr_i<x)
        {
           if(curr_i-1>=0)
           {
               int maxcoin=-1;
               if(curr_j-1>=0)
               {
                  if(arr[curr_i-1][curr_j-1]==1)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                  }
                  else if(arr[curr_i-1][curr_j-1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                  }
               }
               if(arr[curr_i-1][curr_j]==1)
               {
                     maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
               }
               else if(arr[curr_i-1][curr_j]==0)
               {
                   maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
               }

               if(curr_j+1<5)
               {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                  else if(arr[curr_i-1][curr_j+1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                  }
               }

               if(maxcoin==-1)
                 maxcoin=0;
               return dp[curr_i][bomb][curr_j]=maxcoin;
           }
           else
           {
               return dp[curr_i][bomb][curr_j]=0;
           }
        }
        else
        {
            int maxcoin=-1;
            if(curr_i-1>=0)
            {
                if(curr_j-1>=0)
                {
                    if(arr[curr_i-1][curr_j-1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                    }
                }

                if(arr[curr_i-1][curr_j]==1)
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
                }

                if(curr_j+1<5)
                {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                    }
                }
                if(maxcoin==-1)
                     maxcoin=0;
                return dp[curr_i][bomb][curr_j]=maxcoin;
            }
            else
            {
                return (dp[curr_i][bomb][curr_j]=0);
            }
        }
    }
    else
    {
        if(curr_i-1>=0)
        {
            int maxcoin=-1;
            if(curr_j-1>=0)
            {
                if(arr[curr_i-1][curr_j-1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5));
                }
            }

            if(arr[curr_i-1][curr_j]==1)
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5)+1);
            }
            else
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5));
            }

            if(curr_j+1<5)
            {
                if(arr[curr_i-1][curr_j+1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5));
                }
            }
            if(maxcoin==-1)
                maxcoin=0;
            return dp[curr_i][bomb][curr_j]=maxcoin;
        }
        else
        {
            return (dp[curr_i][bomb][curr_j]=0);
        }
    }
}
int main()
{
    int n,m;
    cin>>n;
    int arr[n][5];
    for(int i=0;i<n;i++)
        for(int j=0;j<5;j++)
        cin>>arr[i][j];
    int dp[n+1][2][5];
    for(int i=0;i<=n;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<5;k++)
               dp[i][j][k]=-1;

    cout<<rec(arr,dp,n,2,1,-1)<<endl;
    return 0;
}

- gaurav bisht April 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

/**
 *
 * @author SVN SAIRAM
 */
public class JavaApplication96 {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int arr[][]=new int[n][5];
        for(int i=0;i<n;i++)
        {
        
        for(int j=0;j<5;j++)
        {
        arr[i][j]=sc.nextInt();
        }
            
        }
    
       System.out.println(repeat(n,arr,1,2,n,0));
    }
    
    public static int repeat(int pos,int [][]arr,int bomb,int place,int n,int coins)
    { //System.out.println(pos+" "+place+" "+bomb+" "+coins);
      if(pos<0||place>=5||place<0)
           return coins;
      
      if(pos!=n){
      if(arr[pos][place]==2)
       {
       return coins;
       }
        if(arr[pos][place]==1)
            coins++;
      }
      
        int brr[][]=new int[n][n];
    if(bomb==1)
    { 
        for(int i=0;i<n;i++)
        {
        for(int j=0;j<5;j++)
        {
        brr[i][j]=arr[i][j];
        }    
        }
        int count=5;
        int index=pos;
        while(index-->0&&count-->0)
        {
        for(int j=0;j<5;j++)
        {
        if(brr[index][j]==2)
        {
        brr[index][j]=0;
        }
        }
        }
    
    }
    
    
        if(bomb==1)
        {
            int position=pos-1;
            int nm=place;
            int lm=place-1;
            int rm=place+1;
            int x=repeat(position,arr,1,lm, n,coins);
            int y=repeat(position,arr,1,nm, n,coins);
            int z=repeat(position,arr,1,rm,n,coins);
            int a=repeat(position,brr,0,lm,n,coins);
            int b=repeat(position,brr,0,nm,n,coins); 
            int c=repeat(position,brr,0,rm,n,coins);
            int d=Math.max(Math.max(Math.max(x, y), Math.max(z, a)),Math.max(b, c));
            return d;
            
        
        }
    else
        {int position=pos-1;
            int nm=place;
            int lm=place-1;
            int rm=place+1;
            int z=Math.max(repeat(position,arr,0,lm,n,coins), repeat(position,arr,0,nm,n,coins));
        return Math.max(z, repeat(position,arr,0,rm,n,coins));
        
        }
        
    
      
    }
    
    
}

- svnsr444 October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you explain?

- A_Coder October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int graph(int n,int x,vector<vector<int > > map , int flag){
    int coins=0;
    if(x>map[0].size()||x<0)
        return -555;
    if(n>=map.size())
        return -556;
    if(map[n][x]==1) coins=1;
    if(map[n][x]==2) coins=-1;
    int coinsnbomb=max(graph(n+1,x+1,map,flag),max(graph(n+1,x-1,map,flag),graph(n+1,x,map,flag)));
    //right
    //left
    //notmove
    int coinsbomb=-555555;
    if(flag==1){
        vector<pair<int ,int > > temp;
        for(int i=n;i<4+n&&i<map.size();i++){
            for(int j=0;j<map[0].size();j++){
                if(map[i][j]==2){
                    temp.push_back(make_pair(i,j));
                    map[i][j]=0;
                }
            }
        }
    coinsbomb=max(graph(n+1,x+1,map,0),max(graph(n+1,x,map,0),graph(n+1,x-1,map,0)));
    for(int i=0;i<temp.size();i++){
        map[temp[i].first][temp[i].second]=2;
    }
    //bombnotnove
    //bombright
    // bombleft
    }
    return max(coinsnbomb,coinsbomb)+coins;

}

- Anonymous December 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- Anonymous December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- hpbee December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

}}}

#include <iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
int ans=0;
int a[n][5];
for(int i=0;i<n;i++)
{
if(i>=b && i<b+5)
{
for(int j=0;j<5;j++)
{
if(arr[i][j]==2) a[i][j]=0;
else a[i][j]=arr[i][j];
}
}
else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
}
//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
int p[5]={-1,-1,0,-1,-1};
int c[5]={-1,-1,-1,-1,-1};
for(int i=n-1;i>=0;i--)
{
//cout<<"level "<<i+1<<" :";
for (int j=0;j<5;j++)
{
if(a[i][j]==2) {c[j]=-1;continue;}
for(int k=max(j-1,0);k<=min(j+1,4);k++)
{
if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
}
ans=max(ans,c[j]);
}
for(int l=0;l<5;l++) p[l]=c[l];
}
return ans;
}
int main()
{
int n,m=5;
cin>>n;
int ans=0;
for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
cout<<ans<<endl;
return 0;
}
}}}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
	int ans=0;
	int a[n][5];
	for(int i=0;i<n;i++) 
	{
		if(i>=b && i<b+5)
		{
			for(int j=0;j<5;j++) 
			{
				if(arr[i][j]==2) a[i][j]=0;
				else a[i][j]=arr[i][j];
			}
		}
		else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
	}
	//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
	int p[5]={-1,-1,0,-1,-1};
	int c[5]={-1,-1,-1,-1,-1};
	for(int i=n-1;i>=0;i--)
	{	
		//cout<<"level "<<i+1<<" :";
		for (int j=0;j<5;j++)
		{
			if(a[i][j]==2) {c[j]=-1;continue;}
			for(int k=max(j-1,0);k<=min(j+1,4);k++)
			{
				if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
			}
			ans=max(ans,c[j]);
		}
		for(int l=0;l<5;l++) p[l]=c[l];
	}
	return ans;
}
int main()
{
	int n,m=5;
	cin>>n;
	int ans=0;
	for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
	for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
	cout<<ans<<endl;
	return 0;
}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int arr[100][5];
int func(int b,int n)
{
int ans=0;
int a[n][5];
for(int i=0;i<n;i++)
{
if(i>=b && i<b+5)
{
for(int j=0;j<5;j++)
{
if(arr[i][j]==2) a[i][j]=0;
else a[i][j]=arr[i][j];
}
}
else for(int j=0;j<5;j++) a[i][j]=arr[i][j];
}
//for(int i=0;i<n;i++) {{for(int j=0;j<5;j++) cout<<a[i][j]<<" ";}cout<<endl;}
int p[5]={-1,-1,0,-1,-1};
int c[5]={-1,-1,-1,-1,-1};
for(int i=n-1;i>=0;i--)
{
//cout<<"level "<<i+1<<" :";
for (int j=0;j<5;j++)
{
if(a[i][j]==2) {c[j]=-1;continue;}
for(int k=max(j-1,0);k<=min(j+1,4);k++)
{
if(p[k]!=-1) c[j]=max(c[j],(p[k]+a[i][j]));
}
ans=max(ans,c[j]);
}
for(int l=0;l<5;l++) p[l]=c[l];
}
return ans;
}
int main()
{
int n,m=5;
cin>>n;
int ans=0;
for(int i=0;i<n;i++){ for(int j=0;j<5;j++) {cin>>arr[i][j];}}
for(int i=0;i<n-4;i++) {ans=max(ans,func(i,n));}
cout<<ans<<endl;
return 0;
}

- hpb December 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class Airplane {

	static int max = 0;
	public static void main (String args[]) {
		
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[][] grid = new int[n+1][5];
		boolean bomp = true;
		int coins = 0;
		
		for (int i = 0; i < grid.length-1; i++) {
			for (int j = 0; j < grid[i].length; j++)
				grid[i][j]=s.nextInt();
		}
		s.close();
//		for (int i = 0; i < grid.length; i++) {
//			for (int j = 0; j < grid[i].length; j++) {
//				System.out.print(grid[i][j]);
//			}
//			System.out.println();
//		}

		validate(n+1, 2, bomp, grid, coins);
		System.out.println(max);
	}

	private static void validate(int y, int x, boolean bomp, int[][] grid, int coins) {
		
//		System.out.println( y +" | "+x+" | "+bomp+" | "+coins);
		if (y>0 && coins != -1) {
		if (x==0) {
			move(x+1, y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
		}
		if (x==4) {
			move(x-1,y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
		}
		if (x>0 && x<4) {
			move(x+1, y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
			move(x-1,y-1,bomp, grid, coins);
		}
		}
		max(coins);
//		System.out.println(coins);
			
	}

	private static int max(int coins) {
		if (max < coins )
			max = coins;
		
		return max;
	}

	private static void move(int x, int y, boolean bomp, int[][] grid, int coins) {
		
//		System.out.println( y +" | "+x+" | "+bomp+" | "+coins);
		
		if (grid[y][x]==0) {
			validate(y, x, bomp, grid, coins);
		}
		if (grid[y][x]==1) {
			validate(y, x, bomp, grid, coins+1);
		}
		if (grid[y][x]==2) {
			if (bomp) {
				bomp = false;
				for (int i =y ; i>y+5 && i>0 ; i--)
					for (int j =0 ; i<5 ; j++)
						if (grid[i][j]==2)
							grid[i][j]=0;
				validate(y, x, bomp, grid, coins);
			}else
				validate(y, x, bomp, grid, coins-1);
		}
		
	}
}

- Mashaal Al Momani December 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class Airplane {

	static int max = 0;
	public static void main (String args[]) {
		
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[][] grid = new int[n+1][5];
		boolean bomp = true;
		int coins = 0;
		
		for (int i = 0; i < grid.length-1; i++) {
			for (int j = 0; j < grid[i].length; j++)
				grid[i][j]=s.nextInt();
		}
		s.close();
//		for (int i = 0; i < grid.length; i++) {
//			for (int j = 0; j < grid[i].length; j++) {
//				System.out.print(grid[i][j]);
//			}
//			System.out.println();
//		}

		validate(n+1, 2, bomp, grid, coins);
		System.out.println(max);
	}

	private static void validate(int y, int x, boolean bomp, int[][] grid, int coins) {
		
//		System.out.println( y +" | "+x+" | "+bomp+" | "+coins);
		if (y>0 && coins != -1) {
		if (x==0) {
			move(x+1, y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
		}
		if (x==4) {
			move(x-1,y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
		}
		if (x>0 && x<4) {
			move(x+1, y-1,bomp, grid, coins);
			move(x,y-1,bomp, grid, coins);
			move(x-1,y-1,bomp, grid, coins);
		}
		}
		max(coins);
//		System.out.println(coins);
			
	}

	private static int max(int coins) {
		if (max < coins )
			max = coins;
		
		return max;
	}

	private static void move(int x, int y, boolean bomp, int[][] grid, int coins) {
		
//		System.out.println( y +" | "+x+" | "+bomp+" | "+coins);
		
		if (grid[y][x]==0) {
			validate(y, x, bomp, grid, coins);
		}
		if (grid[y][x]==1) {
			validate(y, x, bomp, grid, coins+1);
		}
		if (grid[y][x]==2) {
			if (bomp) {
				bomp = false;
				for (int i =y ; i>y+5 && i>0 ; i--)
					for (int j =0 ; i<5 ; j++)
						if (grid[i][j]==2)
							grid[i][j]=0;
				validate(y, x, bomp, grid, coins);
			}else
				validate(y, x, bomp, grid, coins-1);
		}
		
	}
}

- Mashaal Al Momani December 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int rec(int arr[][5],int dp[][2][5],int curr_i,int curr_j,int bomb,int x)
{
    if(dp[curr_i][bomb][curr_j]!=-1){
          //  cout<<"OVERLAP!"<<endl;
        return dp[curr_i][bomb][curr_j];
    }
    if(bomb==0)                              //no bomb left
    {
        if(curr_i<x)
        {
           if(curr_i-1>=0)
           {
               int maxcoin=-1;
               if(curr_j-1>=0)
               {
                  if(arr[curr_i-1][curr_j-1]==1)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                  }
                  else if(arr[curr_i-1][curr_j-1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                  }
               }
               if(arr[curr_i-1][curr_j]==1)
               {
                     maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
               }
               else if(arr[curr_i-1][curr_j]==0)
               {
                   maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
               }

               if(curr_j+1<5)
               {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                  else if(arr[curr_i-1][curr_j+1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                  }
               }

               if(maxcoin==-1)
                 maxcoin=0;
               return dp[curr_i][bomb][curr_j]=maxcoin;
           }
           else
           {
               return dp[curr_i][bomb][curr_j]=0;
           }
        }
        else
        {
            int maxcoin=-1;
            if(curr_i-1>=0)
            {
                if(curr_j-1>=0)
                {
                    if(arr[curr_i-1][curr_j-1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                    }
                }

                if(arr[curr_i-1][curr_j]==1)
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
                }

                if(curr_j+1<5)
                {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                    }
                }
                if(maxcoin==-1)
                     maxcoin=0;
                return dp[curr_i][bomb][curr_j]=maxcoin;
            }
            else
            {
                return (dp[curr_i][bomb][curr_j]=0);
            }
        }
    }
    else
    {
        if(curr_i-1>=0)
        {
            int maxcoin=-1;
            if(curr_j-1>=0)
            {
                if(arr[curr_i-1][curr_j-1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5));
                }
            }

            if(arr[curr_i-1][curr_j]==1)
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5)+1);
            }
            else
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5));
            }

            if(curr_j+1<5)
            {
                if(arr[curr_i-1][curr_j+1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5));
                }
            }
            if(maxcoin==-1)
                maxcoin=0;
            return dp[curr_i][bomb][curr_j]=maxcoin;
        }
        else
        {
            return (dp[curr_i][bomb][curr_j]=0);
        }
    }
}
int main()
{
    int n,m;
    cin>>n;
    int arr[n][5];
    for(int i=0;i<n;i++)
        for(int j=0;j<5;j++)
        cin>>arr[i][j];
    int dp[n+1][2][5];
    for(int i=0;i<=n;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<5;k++)
               dp[i][j][k]=-1;

    cout<<rec(arr,dp,n,2,1,-1)<<endl;
    return 0;
}

- gaurav bisht April 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i used 3D dp to solve this problem!

#include<iostream>
using namespace std;
int rec(int arr[][5],int dp[][2][5],int curr_i,int curr_j,int bomb,int x)
{
    if(dp[curr_i][bomb][curr_j]!=-1){
          //  cout<<"OVERLAP!"<<endl;
        return dp[curr_i][bomb][curr_j];
    }
    if(bomb==0)                              //no bomb left
    {
        if(curr_i<x)
        {
           if(curr_i-1>=0)
           {
               int maxcoin=-1;
               if(curr_j-1>=0)
               {
                  if(arr[curr_i-1][curr_j-1]==1)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                  }
                  else if(arr[curr_i-1][curr_j-1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                  }
               }
               if(arr[curr_i-1][curr_j]==1)
               {
                     maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
               }
               else if(arr[curr_i-1][curr_j]==0)
               {
                   maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
               }

               if(curr_j+1<5)
               {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                  else if(arr[curr_i-1][curr_j+1]==0)
                  {
                      maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                  }
               }

               if(maxcoin==-1)
                 maxcoin=0;
               return dp[curr_i][bomb][curr_j]=maxcoin;
           }
           else
           {
               return dp[curr_i][bomb][curr_j]=0;
           }
        }
        else
        {
            int maxcoin=-1;
            if(curr_i-1>=0)
            {
                if(curr_j-1>=0)
                {
                    if(arr[curr_i-1][curr_j-1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x));
                    }
                }

                if(arr[curr_i-1][curr_j]==1)
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x));
                }

                if(curr_j+1<5)
                {
                    if(arr[curr_i-1][curr_j+1]==1)
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                    }
                    else
                    {
                        maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x));
                    }
                }
                if(maxcoin==-1)
                     maxcoin=0;
                return dp[curr_i][bomb][curr_j]=maxcoin;
            }
            else
            {
                return (dp[curr_i][bomb][curr_j]=0);
            }
        }
    }
    else
    {
        if(curr_i-1>=0)
        {
            int maxcoin=-1;
            if(curr_j-1>=0)
            {
                if(arr[curr_i-1][curr_j-1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j-1,0,curr_i-5));
                }
            }

            if(arr[curr_i-1][curr_j]==1)
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5)+1);
            }
            else
            {
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j,0,curr_i-5));
            }

            if(curr_j+1<5)
            {
                if(arr[curr_i-1][curr_j+1]==1){
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,bomb,x)+1);
                maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5)+1);
                }
                else
                {
                    maxcoin=max(maxcoin,rec(arr,dp,curr_i-1,curr_j+1,0,curr_i-5));
                }
            }
            if(maxcoin==-1)
                maxcoin=0;
            return dp[curr_i][bomb][curr_j]=maxcoin;
        }
        else
        {
            return (dp[curr_i][bomb][curr_j]=0);
        }
    }
}
int main()
{
    int n,m;
    cin>>n;
    int arr[n][5];
    for(int i=0;i<n;i++)
        for(int j=0;j<5;j++)
        cin>>arr[i][j];
    int dp[n+1][2][5];
    for(int i=0;i<=n;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<5;k++)
               dp[i][j][k]=-1;

    cout<<rec(arr,dp,n,2,1,-1)<<endl;
    return 0;
}

- gaurav.15bcs1044@abes.ac.in April 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can someone please explain the solution to this problem?

- @nnonymous October 04, 2018 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More