EKO - Eko SPOJ Problem Solution | Binary Search Algorithm Explanation | C++ code

preview_player
Показать описание
In this video we are discussing EKO - Eko Spoj problem with detailed explanation, understanding of Algorithm and C++ source code.
I am implementing Binary Search algorithm to solve this problem.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
If you like this content please hit like and subscribe. Thanks for watching.
Playlists:
30 Days of LeetCode JavaScript Challenge:

HackerRank Problem Solving Solutions:

Codeforces Problems Solutions:

CodeChef Problems Solutions:

Spoj Algorithm Problem Solutions:

Unboxing Videos:

Frontend projects:

Chess Streams:

Connect with me here on Social Media -

Chapters:
00:00 -Introduction
00:05 -Problem Description
02:35 -Intuition and dry run
11:39 -Code
22:30 -Outro

Hashtags:
#endeavourmonk #CPP #spojsolution #cpp #binarysearch #algorithm

Tags:
Spoj, DSA, CP, CodeChef Starters 64 2022, competitive programming, coding solutions, programming, cp, problem solving, logic, Sphere, online, judge, Sphere Online Judge, Problemset, sheet, CP sheet, Binary search
Рекомендации по теме
Комментарии
Автор

Sir, please can you explain why my code fails 9th testcase though passes the rest?

#include <iostream>
#include <algorithm>
#define int long long int
using namespace std;

bool isPossible(int arr[], int n, int k, int mid){
int sum = 0;
for(int i=0; i<n; i++){
if(arr[i]>mid) sum += arr[i]-mid;
}
if(sum>=k) return true;
else return false;
}

int32_t main() {
int n, k; cin>>n>>k;

int* arr = new int[n];
for(int i=0; i<n; i++) cin>>arr[i];

sort(arr, arr+n);

int s=0, e=arr[n-1], ans=0;
int mid = s + (e-s)/2;

while(s<=e){
if(isPossible(arr, n, k, mid)){
ans = mid;
s = mid+1;
}
else{
e = mid-1;
}
mid = s + (e-s)/2;
}
cout<<ans;
return 0;
}

rishujeetrai
Автор

still the code is giving runtime error.
kindly check it

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

bool isPossible(vector<long long int>& arr, long long int n, long long int m, long long int mid) {
long long int sum = 0;
for(int i = 0; i < n; i++) {
if(arr[i] > mid) {
sum += (arr[i] - mid);
}
}
if(sum >= m)
return true;

else return false;
}

int main() {

long long int n, m;
cin >> n >> m;

vector<long long int> arr;
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

long long int s = 0, ans = 0;
long long int e = *max_element(arr.begin(), arr.end());

while(s <= e) {
long long int mid = s + (e-s) / 2;

if(isPossible(arr, n, m, mid)) {
ans = mid;
s = mid + 1;
}
else {
e = mid - 1;
}
}
cout << ans;

return 0;
}

ajringtones
visit shbcf.ru