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
56 changes: 56 additions & 0 deletions Arrays and Functions/src/ArrayFunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// O(n) solution for finding smallest subarray with sum
// greater than x
#include <iostream>
using namespace std;

// Returns length of smallest subarray with sum greater than x.
// If there is no subarray with given sum, then returns n+1
int smallestSubWithSum(int arr[], int n, int x)
{
// Initialize current sum and minimum length
int curr_sum = 0, min_len = n+1;

// Initialize starting and ending indexes
int start = 0, end = 0;
while (end < n)
{
// Keep adding array elements while current sum
// is smaller than x
while (curr_sum <= x && end < n)
{
// Ignore subarrays with negative sum if
// x is positive.
if (curr_sum <= 0 && x > 0)
{
start = end;
curr_sum = 0;
}

curr_sum += arr[end++];
}

// If current sum becomes greater than x.
while (curr_sum > x && start < n)
{
// Update minimum length if needed
if (end - start < min_len)
min_len = end - start;

// remove starting elements
curr_sum -= arr[start++];
}
}
return min_len;
}
/* Driver program to test above function */
int main()
{
int arr1[] = {- 8, 1, 4, 2, -6};
int x = 6;
int n1 = sizeof(arr1)/sizeof(arr1[0]);
int res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1+1)? cout << "Not possible\n" :
cout << res1 << endl;

return 0;
}
71 changes: 71 additions & 0 deletions Arrays and Functions/src/ArrayFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* C++ program to count minimum number of operations
to get the given target array */
#include <bits/stdc++.h>
using namespace std;

// Returns count of minimum operations to covert a
// zero array to target array with increment and
// doubling operations.
// This function computes count by doing reverse
// steps, i.e., convert target to zero array.
int countMinOperations(unsigned int target[], int n)
{
// Initialize result (Count of minimum moves)
int result = 0;

// Keep looping while all elements of target
// don't become 0.
while (1)
{
// To store count of zeroes in current
// target array
int zero_count = 0;

int i; // To find first odd element
for (i=0; i<n; i++)
{
// If odd number found
if (target[i] & 1)
break;

// If 0, then increment zero_count
else if (target[i] == 0)
zero_count++;
}

// All numbers are 0
if (zero_count == n)
return result;

// All numbers are even
if (i == n)
{
// Divide the whole array by 2
// and increment result
for (int j=0; j<n; j++)
target[j] = target[j]/2;
result++;
}
// Make all odd numbers even by subtracting
// one and increment result.
for (int j=i; j<n; j++)
{
if (target[j] & 1)
{
target[j]--;
result++;
}
}
}
}

/* Driver program to test above functions*/
int main()
{
unsigned int arr[] = {16, 16, 16};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Minimum number of steps required to "
"get the given target array is "
<< countMinOperations(arr, n);
return 0;
}
77 changes: 77 additions & 0 deletions Arrays and Functions/src/FunctionArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// CPP program to find Minimum
// number of jumps to reach end
#include<bits/stdc++.h>
using namespace std;

// Returns Minimum number of
// jumps to reach end
int minJumps(int arr[], int n)
{
// jumps[0] will hold the result
int *jumps = new int[n];
int min;

// Minimum number of jumps needed
// to reach last element from last
// elements itself is always 0
jumps[n-1] = 0;


// Start from the second element,
// move from right to left and
// construct the jumps[] array where
// jumps[i] represents minimum number
// of jumps needed to reach
// arr[m-1] from arr[i]
for (int i = n-2; i >=0; i--)
{
// If arr[i] is 0 then arr[n-1]
// can't be reached from here
if (arr[i] == 0)
jumps[i] = INT_MAX;

// If we can direcly reach to
// the end point from here then
// jumps[i] is 1
else if (arr[i] >= n - i - 1)
jumps[i] = 1;

// Otherwise, to find out the minimum
// number of jumps needed to reach
// arr[n-1], check all the points
// reachable from here and jumps[]
// value for those points
else
{
// initialize min value
min = INT_MAX;

// following loop checks with all
// reachable points and takes
// the minimum
for (int j = i + 1; j < n && j <=
arr[i] + i; j++)
{
if (min > jumps[j])
min = jumps[j];
}

// Handle overflow
if (min != INT_MAX)
jumps[i] = min + 1;
else
jumps[i] = min; // or INT_MAX
}
}
return jumps[0];
}

// Driver program to test above function
int main()
{
int arr[] = {1, 3, 6, 1, 0, 9};
int size = sizeof(arr)/sizeof(int);
cout << "Minimum number of jumps to reach"
<< " end is " << minJumps(arr, size);
return 0;
}
28 changes: 28 additions & 0 deletions Arrays and Functions/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<iostream>
using namespace std;

/*This function returns the Largest Sum Contiguous Subarray*/

int maxSubArraySum(int a[], int size)
{
int max_so_far = a[0];
int curr_max = a[0];

for (int i = 1; i < size; i++)
{
curr_max = max(a[i], curr_max+a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}

/* Driver program to test maxSubArraySum */

int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]);
int max_sum = maxSubArraySum(a, n);
cout << "Maximum contiguous sum is " << max_sum;
return 0;
}
22 changes: 22 additions & 0 deletions DFS Function for Competition/DFS_Function_Cplusplus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
vector<int> a[100000];
int visited[100000];

void dfs(int k)
{
if(!visited[k])
{
visited[k]=1;
for(int i=0;i<a[k].size();i++)
{
if(!visited[a[k][i]])
dfs(a[k][i]);
}
}
}

/*int p,q;
cin>>p>>q;
a[p].push_back(q);

dfs(l);
if(visited[m]==1) cout<<"S"<<endl;*/