• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Stickler thief

December 28, 2017 by Dhaval Dave

Stickler thief and wants to loot money from a society of n houses placed in a line. He is a weird person and follows a rule while looting the houses and according to the rule Stickler thief will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to find the maximum amount he can end up with. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i] amount of money present in it.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case contains an integer n which denotes the number of elements in the array a[]. Next line contains space separated n elements in the array a[ ]

Output:
Print an integer which denotes the maximum amount he can take home.

Constraints:
1<=T<=200
1<=n<=1000
1<=a[i]<=10000

Example:
Input:

2
6
5 5 10 100 10 5
3
1 2 3

Output:
110
4

 solution:

Explanation:- The money in each house will be array of integers as given in input example and we have to maximise this money without considering consecutive houses means we have to find maximum sum in array such that no two elements are adjacent.

Simple Solution:-

Loop for all elements in arr[] and maintain two sums sum1 and sum2l where sum1 = Max sum including the previous element and sum2 = Max sum excluding the previous element.

Max sum excluding the current element will be max(sum1, sum2) and max sum including the current element will be sum2 + current element (Note that only sum2 is considered because elements cannot be adjacent).

At the end of the loop return max of sum1 and sum2.

Example:

arr[] = {5,  5, 10, 40, 50, 35}
 sum1= 5
 sum2= 0

 For i = 1 (current element is 5)
 sum1=  (sum2+ arr[i])  = 5
 sum2=  max(5, 0) = 5

 For i = 2 (current element is 10)
 sum1 =  (sum2 + arr[i]) = 15
 sum2 =  max(5, 5) = 5

 For i = 3 (current element is 40)
 sum1 = (sum2+ arr[i]) = 45
 sum2 = max(5, 15) = 15

 For i = 4 (current element is 50)
 sum1= (sum2 + arr[i]) = 65
 sum2 =  max(45, 15) = 45

 For i = 5 (current element is 35)
 sum1=  (sum2 + arr[i]) = 80
 sum2 =  max(65, 45) = 65
And 35 is the last element. So, answer is max(sum1, sum2) = 80

Implementation:

#
include < bits / stdc++.h >
using namespace std;

int FindMaxSum(int arr[], int n)
{
    int sum1 = arr[0];
    int sum2 = 0;
    int result;
    for (int i = 1; i < n; i++){
        result = (sum1 > sum2) ? sum1 : sum2;
        sum1 = sum2 + arr[i];
        sum2 = result;
    }
    return ((sum1 > sum2) ? sum1 : sum2);
}
/* Driver program to test above function */
int main(){
    int t;
    cin >> t;
    while (t–){
        cin >> n;
        int arr[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        cout << FindMaxSum(arr, n);
}
    return 0;

}

DP Approach:

Explanation: If there is only one house i.e. n=1 then simply return that if two houses are there that is n=2 then return max of the two otherwise repeat the similar method as explained in above method.

implementation:

#
include < bits / stdc++.h >

    using namespace std;

int funcMax(int a[], int n){
    if (n == 1)
        return a[0];
    if (n == 2)
        return max(a[0], a[1]);
    int * dp = (int * ) malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
        dp[i] = 0;
    dp[0] = a[0];
    dp[1] = max(a[0], a[1]);
    for (int i = 2; i < n; i++)
        dp[i] = max(dp[i - 2] + a[i], dp[i - 1]);
    int t = dp[n - 1];
    free(dp);
    return t;
}

int main(){
    int t;
    cin >> t;
    while (t–){
        cin >> n;
        int arr[n];
        for (int i = 0; i < n; i++)
            cin >> arr[i];
        cout << funcMax(arr, n);
    }
    return 0;
}

 

Similar Articles

Filed Under: Uncategorized

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

BlueStone E-commerce Interview Experience

FizzBuzz Solution C C++

Find Percentage of Words matching in Two Strings

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Urban Ladder Written Test.

Word Break Problem

Code Chef PRGIFT Solution

Find and print longest consecutive number sequence in a given sequence in O(n)

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Difference between a LinkedList and a Binary Search Tree BST

Stock Buy Sell to Maximize Profit

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Calculate price of parking from parking start end time prices

Facebook Interview Question : Interleave List

Printing each word reverse in string

Adobe Interview Questions 8 month Exp

Given a float number convert it into the string WITHOUT using any inbuilt Function

Add Sub Multiply very large number stored as string

Find min element in Sorted Rotated Array (Without Duplicates)

LeetCode: Binary Tree Maximum Path Sum

Leetcode: Merge Intervals

Printing Longest Common Subsequence

Closed Parentheses checker

Introduction To Number Theory ( Part 1 )

Find if two rectangles overlap

VMWare Openings

Longest Increasing Subsequence

Reverse a Linked List in groups of given size

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Copyright © 2025 · Genesis Framework · WordPress · Log in