Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerEarth Thief and Warehouses problem solution

YASH PAL, 31 July 2024
In this HackerEarth Thief and Warehouses problem solution There are N warehouses. The warehouses are located in a straight line and are indexed from 1 to N. Each warehouse contains some number of sacks.
A thief decides to rob these warehouses. Thief figured out that he can escape the police if and only if he follows both the following 2 constraints:
  1. He will rob only one continuous segment of warehouses.
  2. He will rob a same number of sacks from each warehouse.
The thief wants to calculate the maximum number of sacks he can steal without getting caught by the police.
HackerEarth Thief and Warehouses problem solution

HackerEarth Thief and Warehouses problem solution.

#include<bits/stdc++.h>

#define ll long long
using namespace std;
stack<ll> s;
int main()
{
int testCases;
cin>>testCases;
while(testCases--){
int n,i;
scanf("%d",&n);
ll a[n];
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
ll marea=0,area=0;
for(i=0;i<n;){
if(s.empty()||a[s.top()]<=a[i])
s.push(i++);
else{
int tp=s.top();
s.pop();
if(s.empty())
area=a[tp]*i;
else
area=a[tp]*(i-s.top()-1);
if(area>marea)
marea=area;
}
}
while(!s.empty()){
int tp=s.top();
s.pop();
if(s.empty())
area=a[tp]*i;
else
area=a[tp]*(i-s.top()-1);
if(area>marea)
marea=area;
}
printf("%lldn",marea);
}
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes