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
37 changes: 37 additions & 0 deletions array/Find Minimum Distance between two Numbers
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// C++ program to Find the minimum
// distance between two numbers
#include <bits/stdc++.h>
using namespace std;

int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}

/* Driver code */
int main()
{
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int y = 6;

cout << "Minimum distance between " << x << " and " << y
<< " is " << minDist(arr, n, x, y) << endl;
}


49 changes: 49 additions & 0 deletions array/Find the subarray with least average
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// A Simple C++ program to find minimum average subarray
#include <bits/stdc++.h>
using namespace std;

// Prints beginning and ending indexes of subarray
// of size k with minimum average
void findMinAvgSubarray(int arr[], int n, int k)
{
// k must be smaller than or equal to n
if (n < k)
return;

// Initialize beginning index of result
int res_index = 0;

// Compute sum of first subarray of size k
int curr_sum = 0;
for (int i = 0; i < k; i++)
curr_sum += arr[i];

// Initialize minimum sum as current sum
int min_sum = curr_sum;

// Traverse from (k+1)'th element to n'th element
for (int i = k; i < n; i++) {
// Add current item and remove first item of
// previous subarray
curr_sum += arr[i] - arr[i - k];

// Update result if needed
if (curr_sum < min_sum) {
min_sum = curr_sum;
res_index = (i - k + 1);
}
}

cout << "Subarray between [" << res_index << ", "
<< res_index + k - 1 << "] has minimum average";
}

// Driver program
int main()
{
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
int k = 3; // Subarray size
int n = sizeof arr / sizeof arr[0];
findMinAvgSubarray(arr, n, k);
return 0;
}
43 changes: 43 additions & 0 deletions array/Minimum number of jumps to reach the end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C++ program to find Minimum
// number of jumps to reach end
#include <bits/stdc++.h>
using namespace std;

// Function to return the minimum number
// of jumps to reach arr[h] from arr[l]
int minJumps(int arr[], int n)
{

// Base case: when source and
// destination are same
if (n == 1)
return 0;

// Traverse through all the points
// reachable from arr[l]
// Recursively, get the minimum number
// of jumps needed to reach arr[h] from
// these reachable points
int res = INT_MAX;
for (int i = n - 2; i >= 0; i--) {
if (i + arr[i] >= n - 1) {
int sub_res = minJumps(arr, i + 1);
if (sub_res != INT_MAX)
res = min(res, sub_res + 1);
}
}

return res;
}

// Driver Code
int main()
{
int arr[] = { 1, 3, 6, 3, 2,
3, 6, 8, 9, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum number of jumps to";
cout << " reach the end is " << minJumps(arr, n);
return 0;
}

20 changes: 20 additions & 0 deletions array/largest contguos sum subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int maxSubarraySum(int arr[], int size)
{
int max_ending_here = 0, max_so_far = INT_MIN;
for (int i = 0; i < size; i++) {

// include current element to previous subarray only
// when it can add to a bigger number than itself.
if (arr[i] <= max_ending_here + arr[i]) {
max_ending_here += arr[i];
}

// Else start the max subarray from current element
else {
max_ending_here = arr[i];
}
if (max_ending_here > max_so_far)
max_so_far = max_ending_here;
}
return max_so_far;
}
102 changes: 102 additions & 0 deletions stack/Implementing two stacks in an array
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <stdlib.h>

using namespace std;

class twoStacks {
int* arr;
int size;
int top1, top2;

public:
// Constructor
twoStacks(int n)
{
size = n;
arr = new int[n];
top1 = n / 2 + 1;
top2 = n / 2;
}

// Method to push an element x to stack1
void push1(int x)
{
// There is at least one empty
// space for new element
if (top1 > 0) {
top1--;
arr[top1] = x;
}
else {
cout << "Stack Overflow"
<< " By element :" << x << endl;
return;
}
}

// Method to push an element
// x to stack2
void push2(int x)
{

// There is at least one empty
// space for new element
if (top2 < size - 1) {
top2++;
arr[top2] = x;
}
else {
cout << "Stack Overflow"
<< " By element :" << x << endl;
return;
}
}

// Method to pop an element from first stack
int pop1()
{
if (top1 <= size / 2) {
int x = arr[top1];
top1++;
return x;
}
else {
cout << "Stack UnderFlow";
exit(1);
}
}

// Method to pop an element
// from second stack
int pop2()
{
if (top2 >= size / 2 + 1) {
int x = arr[top2];
top2--;
return x;
}
else {
cout << "Stack UnderFlow";
exit(1);
}
}
};

/* Driver program to test twStacks class */
int main()
{
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is "
<< " : " << ts.pop1()
<< endl;
ts.push2(40);
cout << "\nPopped element from stack2 is "
<< ": " << ts.pop2()
<< endl;
return 0;
}