Maximum money | Practice | GeeksforGeeks

Given a street of N houses (a row of houses), each house having K amount of money kept inside; now there is a thief who is going to steal this money but he has a constraint/rule that he cannot steal/rob two adjacent houses. Find the maximum money he can rob. 
Example 1:
Input:
N = 5 , K = 10
Output:
30
Explanation:
The Robber can rob from the first, third
and fifth houses which will result in 30.
Example 2:
Input:
N = 2 , K = 12
Output:
12
Explanation:
The Robber can only rob from the first
or second which will result in 12.

Your Task:
You don't need to read input or print anything. Your task is to complete the function maximizeMoney() which takes 2 Integers N and K as input and returns the answer.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N,K <= 103
0
devkumar6699432 weeks ago
python 
time = 0.07
 
import math
class Solution:   def maximizeMoney(self, N , K):       # code here        return ((math.ceil(N/2))*K)
0
pranavchandore20004 weeks ago
class Solution {
  public:
    int maximizeMoney(int N , int K) {
        // code here
        int p=0;
        for(int i=0;i<N;i+=2){
            p +=K;
        }
        return p;
    }
};

0
akashmarkad22101 month ago
static int maximizeMoney(int N , int K) {       // code here              int count = 0;              for(int i=0;i<N;i += 2)       {           count++;       }              return K*count;   }
+1
kartikeyashokgautam1 month ago
Simple JAVA Solution :- 
 
  static int maximizeMoney(int N , int K) {         int count =0;       for(int i=1;i<=N;i++)       {           if(i%2!=0)           {               count++;           }       }              return count*K;   }
0
kaushaloff2 months ago
FASTEST AND SHORTEST PYTHON SOLUTION:
 
return K*int((N/2)+0.5)
0
codewithshoaib192 months ago
 if(N%2==0){           return (N/2)*K;       }       return ((N/2)+1)*K;
0
codewithaddy2 months ago
SIMPLE C++ SOLUTION
int maximizeMoney(int N , int K) {
           return K*((N+1)/2);
    }
0
towfiq1845tasin
This comment was deleted.
0
saurabhjejurkar193 months ago
Java Solution
class Solution {
    static int maximizeMoney(int N , int K) {
        // code here
        int ans=0;
        
        if(N%2==0){
            ans=(N/2)*K;
        }else{
            ans=((N/2)+1)*K;
        }
        
        return ans;
    }
};
0
ll9nutbz963cw2rm9l7ssmmpcjfnohfquzp5i0uc3 months ago
def maximizeMoney(self, N , K):       div, mod = divmod(N,2)       return (div+mod)*K
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.