C++ code to count maximum banknotes bank can gather

Suppose we have three numbers k, l and m, and have another array A with n elements. A robber failed to rob a bank but had managed to open all the safes of a bank. The blank client decides to take advantage of this failed robbery and steal some money from the safes. In a line there are many safes. There are n banknotes left in all the safes in total. The i-th banknote is in safe A[i]. The bank employee is now at safe k. There are two security guards, one of which guards the safe l such that l < k, So the first guard is to the left of the stuff. The other guard guards the safe m so that m > k, (he is to the right of the stuff). The two guards do not move. In every second, The stuff can either take all the banknotes from the current safe or move to any of the adjacent safes. However, he cannot visit any safe that is guarded by security guards at any time, because he might be charged for stealing. We have to find the maximum amount of banknotes the stuff can gather.

So, if the input is like k = 5; l = 3; m = 7; A = [4, 7, 5, 5, 3, 6, 2, 8], then the output will be 4, because


To solve this, we will follow these steps −

c1 := 0
n := size of A
c1 := 0
for initialize i := 0, when i < n, update (increase i by 1), do:
   x := A[i]
   if x > l and x < m, then:
      (increase c1 by 1)
return c1


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int k, int l, int m, vector<int> A){
   int c1 = 0, x;
   int n = A.size();
   c1 = 0;
   for (int i = 0; i < n; i++){
      x = A[i];
      if (x > l && x < m)
   return c1;
int main(){
   int k = 5;
   int l = 3;
   int m = 7;
   vector<int> A = { 4, 7, 5, 5, 3, 6, 2, 8 };
   cout << solve(k, l, m, A) << endl;


5, 3, 7, { 4, 7, 5, 5, 3, 6, 2, 8 }



Updated on: 30-Mar-2022


Kickstart Your Career

Get certified by completing the course

Get Started