-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shoaib Rayeen
authored
Jan 21, 2019
1 parent
74b26b4
commit 0bc9120
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
DATA STRUCTURES/Array/Chocolate Distribution Problem/code_1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// code_1.cpp | ||
// Algorithm | ||
// | ||
// Created by Mohd Shoaib Rayeen on 23/11/18. | ||
// Copyright © 2018 Shoaib Rayeen. All rights reserved. | ||
// | ||
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
|
||
int getMinDiff(vector<int>arr , int m) { | ||
long n = arr.size(); | ||
if (m==0 || n==0) { | ||
return 0; | ||
} | ||
sort(arr.begin(), arr.end()); | ||
if (n < m) { | ||
return -1; | ||
} | ||
int min_diff = INT_MAX; | ||
int first = 0, last = 0; | ||
for (int i = 0; i + m - 1 < n; i++ ) { | ||
int diff = arr[i+m-1] - arr[i]; | ||
if (diff < min_diff) { | ||
min_diff = diff; | ||
first = i; | ||
last = i + m - 1; | ||
} | ||
} | ||
return (arr[last] - arr[first]); | ||
} | ||
|
||
int main() { | ||
int n; | ||
cout << "\nEnter Size\t:\t"; | ||
cin >> n; | ||
int m; | ||
vector<int> arr(n); | ||
cout << "\nEnter Elements\n"; | ||
for ( int i = 0; i < n; i++ ) { | ||
cin >> arr[i]; | ||
} | ||
cout << "\nEnter m\t:\t"; | ||
cin >> m; | ||
cout << "\nMinimum Difference\t:\t" << getMinDiff(arr, m); | ||
arr.clear(); | ||
cout << endl; | ||
return 0; | ||
} |