diff --git a/Arrays and Functions/src/ArrayFunction.cpp b/Arrays and Functions/src/ArrayFunction.cpp new file mode 100644 index 0000000..2dc0a62 --- /dev/null +++ b/Arrays and Functions/src/ArrayFunction.cpp @@ -0,0 +1,56 @@ +// O(n) solution for finding smallest subarray with sum +// greater than x +#include +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; +} diff --git a/Arrays and Functions/src/ArrayFunctions.cpp b/Arrays and Functions/src/ArrayFunctions.cpp new file mode 100644 index 0000000..81c63d8 --- /dev/null +++ b/Arrays and Functions/src/ArrayFunctions.cpp @@ -0,0 +1,71 @@ +/* C++ program to count minimum number of operations + to get the given target array */ +#include +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 +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; +} diff --git a/Arrays and Functions/src/main.cpp b/Arrays and Functions/src/main.cpp new file mode 100644 index 0000000..1982fd1 --- /dev/null +++ b/Arrays and Functions/src/main.cpp @@ -0,0 +1,28 @@ +#include +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; +} diff --git a/DFS Function for Competition/DFS_Function_Cplusplus.cpp b/DFS Function for Competition/DFS_Function_Cplusplus.cpp new file mode 100644 index 0000000..91d1a6d --- /dev/null +++ b/DFS Function for Competition/DFS_Function_Cplusplus.cpp @@ -0,0 +1,22 @@ +vector a[100000]; +int visited[100000]; + +void dfs(int k) +{ + if(!visited[k]) + { + visited[k]=1; + for(int i=0;i>p>>q; + a[p].push_back(q); + + dfs(l); + if(visited[m]==1) cout<<"S"<