Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x86",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x86",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/HP/Data-Structures-and-Algorithms",
"program": "c:/Users/HP/Data-Structures-and-Algorithms/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
43 changes: 43 additions & 0 deletions Java/BinarySearch/BinarySearchOnAnswers/AggressiveCow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class AggressiveCow {
public static void main(String args[])
{
int[] stalls={1,2,4,8,9};
int cows=3;
int result=aggressiveCows(stalls,cows);
System.out.println("The largest minimum distance is : "+result);
}
public static int aggressiveCows(int[] stalls,int cows)
{
int left=1;
int right=stalls[stalls.length-1]-stalls[0];
int answer=-1;
while(left<=right)
{
int mid=left+(right-left)/2;
if(canPlaceCows(stalls,cows,mid))
{
answer=mid;
left=mid+1;
}
else{
right=mid-1;
}
}
return answer;
}
public static boolean canPlaceCows(int[] stalls,int cows,int distance)
{
int countCows=1;
int lastPosition=stalls[0];
for(int i=1;i<stalls.length;i++)
{
if(stalls[i]-lastPosition>=distance)
{
countCows++;
lastPosition=stalls[i];

}
}
return countCows>=cows;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<h1>🌸 Minimum Number of Days to Make Bouquets</h1>

<h2> Problem Statement</h2>
<p align="center">
<img width="684" height="356" alt="Untitled Diagram drawio" src="Java\BinarySearch\BinarySearchOnAnswers\photo_Animation_of_approach\image2.png" />
</p>

<p>You are given an integer array <b>bloomDay[]</b> where:</p>

<ul>
<li><code>bloomDay[i]</code> represents the day the <i>i-th</i> flower blooms</li>
<li>You must create <b>M bouquets</b></li>
<li>Each bouquet requires <b>K consecutive flowers</b></li>
</ul>


<h3>Tasks</h3>
<ol>
<li>Return the <b>minimum number of days</b> required to make <b>M</b> bouquets</li>
<li>If it is <b>not possible</b>, return <code>-1</code></li>
</ol>

<hr>

<h2> Key Observations</h2>

<h3>1️) Feasibility Check (Fail Fast)</h3>

<p>To make <b>M</b> bouquets of <b>K</b> flowers each:</p>

<pre>
Total flowers required = M × K
</pre>

<p>If:</p>

<pre>
M × K &gt; bloomDay.length
</pre>

<p>It is <b>impossible</b> to form the bouquets, so return <code>-1</code>.</p>

<hr>
<p align="center">
<img width="806" height="531" alt="Untitled Diagram drawio (3)" src="Java\BinarySearch\BinarySearchOnAnswers\photo_Animation_of_approach\image1.png" />
</p>

<h3>2️) Why Binary Search Works</h3>

<p>This problem is <b>not</b> about searching indices.</p>
<p>It is about searchin<p>Define a function:</p>

<pre>
CanMakeBouquet(day)
</pre>

<ul>
<li>If bouquets can be made on day <b>D</b></li>
<li>They can also be made on any day <b>greater than D</b></li>
</ul>

<p>This creates a <b>monotonic pattern</b>:</p>

<pre>
F F F F F T T T T
</pre>

<p>Perfect use case for <b>Binary Search on Answer</b>.</p>

<hr>

<h2> Approach</h2>

<ul>
<li>Search space: minimum bloom day → maximum bloom day</li>
<li>Apply binary search on days</li>
<li>Check if <b>M</b> bouquets can be formed using <b>K consecutive flowers</b></li>
<li>Minimize the day</li>
</ul>

<hr>


<h2> Java Implementation</h2>

<pre>

<code>
class Solution {
public int minDays(int[] bloomDay, int m, int k) {

if ((long) m * k > bloomDay.length) return -1;

int low = Integer.MAX_VALUE, high = Integer.MIN_VALUE;
for (int day : bloomDay) {
low = Math.min(low, day);
high = Math.max(high, day);
}

int ans = -1;
while (low &lt;= high) {
int mid = low + (high - low) / 2;

if (canMakeBouquet(bloomDay, m, k, mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}

private boolean canMakeBouquet(int[] bloomDay, int m, int k, int days) {
int count = 0;

for (int day : bloomDay) {
if (day &lt;= days) {
count++;
if (count == k) {
m--;
count = 0;
}
} else {
count = 0;
}
}
return m &lt;= 0;
}
}
</code>

</pre>

<hr>

<h2>⏱ Complexity Analysis</h2>

<ul>
<li><b>Time Complexity:</b> O(n log(maxDay))</li>
<li><b>Space Complexity:</b> O(1)</li>
</ul>

<hr>

<p>
Binary Search is <b>not only for sorted arrays</b>.<br>
It can be applied whenever the answer space is <b>monotonic</b>.
</p>



<hr>


</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
public class CapacitytoShipPackageswithinDDays {
public static void main(String args[])
{
int[] weights={1,2,3,4,5,6,7,8,9,10};
int days=5;
int result=shipWithinDays(weights,days);
System.out.println("The least weight capacity of the ship is : "+result);

}
public static int shipWithinDays(int[] weights, int days) {
int left=0;
int right=0;
for(int weight:weights)
{
left=Math.max(left,weight);
right+=weight;
}
int answer=-1;
while(left<=right)
{
int mid=left+(right-left)/2;
if(canShip(weights,days,mid))
{
answer=mid;
right=mid-1;
}
else{
left=mid+1;
}
}
return answer;
}
public static boolean canShip(int[] weights,int days,int capacity)
{
int totalDays=1;
int currentLoad=0;
for(int weight:weights)
{
currentLoad+=weight;
if(currentLoad<=capacity)
{
continue;
}
else{
totalDays++;
currentLoad=weight;

}
}
return totalDays<=days;
}
}
Loading