From 4974df5c799f05ee8a1a7b6df37df978c2a0212b Mon Sep 17 00:00:00 2001 From: nrLucas <56032832+nrLucas@users.noreply.github.com> Date: Wed, 23 Oct 2019 15:55:36 -0300 Subject: [PATCH 1/6] Largest Sum Contiguous Subarray --- Arrays and Functions/src/main.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Arrays and Functions/src/main.cpp 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; +} From 2be04cd030287870b8f63c169e3b327ee823386f Mon Sep 17 00:00:00 2001 From: nrLucas <56032832+nrLucas@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:15:17 -0300 Subject: [PATCH 2/6] Smallest subarray with sum greate ... Smallest subarray with sum greater than a given value --- Arrays and Functions/src/ArrayFunctions.cpp | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Arrays and Functions/src/ArrayFunctions.cpp diff --git a/Arrays and Functions/src/ArrayFunctions.cpp b/Arrays and Functions/src/ArrayFunctions.cpp new file mode 100644 index 0000000..0986a49 --- /dev/null +++ b/Arrays and Functions/src/ArrayFunctions.cpp @@ -0,0 +1,58 @@ +// 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; + } From e2f526644add3d3294027902ac0c2c3c81ba0db3 Mon Sep 17 00:00:00 2001 From: nrLucas <56032832+nrLucas@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:33:05 -0300 Subject: [PATCH 3/6] Count minimum steps to get the given desired array Consider an array with n elements and value of all the elements is zero. We can perform following operations on the array. Incremental operations:Choose 1 element from the array and increment its value by 1. Doubling operation: Double the values of all the elements of array. --- Arrays and Functions/src/ArrayFunctions.cpp | 95 ++++++++++++--------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/Arrays and Functions/src/ArrayFunctions.cpp b/Arrays and Functions/src/ArrayFunctions.cpp index 0986a49..81c63d8 100644 --- a/Arrays and Functions/src/ArrayFunctions.cpp +++ b/Arrays and Functions/src/ArrayFunctions.cpp @@ -1,58 +1,71 @@ -// O(n) solution for finding smallest subarray with sum -// greater than x -#include +/* C++ program to count minimum number of operations + to get the given target array */ +#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) +// 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 current sum and minimum length - int curr_sum = 0, min_len = n+1; + // Initialize result (Count of minimum moves) + int result = 0; - // Initialize starting and ending indexes - int start = 0, end = 0; - while (end < n) + // Keep looping while all elements of target + // don't become 0. + while (1) { - // Keep adding array elements while current sum - // is smaller than x - while (curr_sum <= x && end < n) + // To store count of zeroes in current + // target array + int zero_count = 0; + + int i; // To find first odd element + for (i=0; i 0) - { - start = end; - curr_sum = 0; - } + // If odd number found + if (target[i] & 1) + break; - curr_sum += arr[end++]; + // If 0, then increment zero_count + else if (target[i] == 0) + zero_count++; } - // 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; + // All numbers are 0 + if (zero_count == n) + return result; - // remove starting elements - curr_sum -= arr[start++]; + // All numbers are even + if (i == n) + { + // Divide the whole array by 2 + // and increment result + for (int j=0; j Date: Wed, 23 Oct 2019 16:38:59 -0300 Subject: [PATCH 4/6] Smallest subarray with sum greater than a given value Given an array of integers and a number x, find the smallest subarray with sum greater than the given value. A simple solution is to use two nested loops. The outer loop picks a starting element, the inner loop considers all elements (on right side of current start) as ending element. Whenever sum of elements between current start and end becomes more than the given number, update the result if current length is smaller than the smallest length so far. --- Arrays and Functions/src/ArrayFunction.cpp | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Arrays and Functions/src/ArrayFunction.cpp 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; +} From bda2ccd44c54ce8aaf13896d564d5159810dba88 Mon Sep 17 00:00:00 2001 From: nrLucas <56032832+nrLucas@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:53:04 -0300 Subject: [PATCH 5/6] Minimum number of jumps to reach end Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element. Example: Input: arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} Output: 3 (1-> 3 -> 8 ->9) First element is 1, so can only go to 3. Second element is 3, so can make at most 3 steps eg to 5 or 8 or 9. --- Arrays and Functions/src/FunctionArray.cpp | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Arrays and Functions/src/FunctionArray.cpp diff --git a/Arrays and Functions/src/FunctionArray.cpp b/Arrays and Functions/src/FunctionArray.cpp new file mode 100644 index 0000000..f12136d --- /dev/null +++ b/Arrays and Functions/src/FunctionArray.cpp @@ -0,0 +1,77 @@ +// CPP program to find Minimum +// number of jumps to reach end +#include +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; +} From cc428ef89e730fbbff145b1b07ba9f8e55319042 Mon Sep 17 00:00:00 2001 From: Lucas Nunes Rios <56032832+nrLucas@users.noreply.github.com> Date: Fri, 9 Oct 2020 01:33:04 -0300 Subject: [PATCH 6/6] Add files via upload --- .../DFS_Function_Cplusplus.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 DFS Function for Competition/DFS_Function_Cplusplus.cpp 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"<