diff --git a/geekForgeeks/section1_basics/addingOnes.cpp b/geekForgeeks/section1_basics/addingOnes.cpp new file mode 100644 index 0000000..5e85a3c --- /dev/null +++ b/geekForgeeks/section1_basics/addingOnes.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +void update(int a[], int n, int update[], int k) { + + for(int i = 0; i < n; ++i) + a[i] = 0; + + for(int i = 0; i < k; ++i) + a[update[i] - 1]++; + + for(int i = 1; i < n; ++i) + a[i] += a[i-1]; + +} + diff --git a/geekForgeeks/section1_basics/convertArrayToReduced.cpp b/geekForgeeks/section1_basics/convertArrayToReduced.cpp new file mode 100644 index 0000000..90c7005 --- /dev/null +++ b/geekForgeeks/section1_basics/convertArrayToReduced.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +void convert(int arr[], int n) { + + int arr2[n+1]; + for(int i = 0; i < n; ++i) + arr2[i] = arr[i]; + + sort(arr2, arr2+n); + + unordered_map unmap; + for(int i = 0; i < n; ++i) + unmap[arr2[i]] = i; + + for(int i = 0; i < n; ++i) + arr[i] = unmap[arr[i]]; + +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/frequenceOfLimitedVector.cpp b/geekForgeeks/section1_basics/frequenceOfLimitedVector.cpp new file mode 100644 index 0000000..8434cea --- /dev/null +++ b/geekForgeeks/section1_basics/frequenceOfLimitedVector.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +int lower_bound(vector &arr, int x) { + + int l = 0, r = arr.size() - 1, target = -1; + while(l <= r) { + int mid = l+r/2; + if(arr[mid] == x) { + target = mid; + r = mid-1; + arr[mid] = 0; + } else if (arr[mid] > x) { + r = mid-1; + } else { + l = mid+1; + } + } + + return target; +} + +int upper_bound(vector &arr, int x) { + + int l = 0, r = arr.size() - 1, target = -1; + while(l <= r) { + int mid = l+r/2; + if(arr[mid] == x) { + target = mid; + l = mid+1; + arr[mid] = 0; + } else if (arr[mid] > x) { + r = mid - 1; + } else { + l = mid+1; + } + } + + return target; +} + +void frequencyCount(vector& arr, int N, int P) { + + int size = arr.size() - 1; + sort(arr.begin(),arr.end()); + for(int i = 0; i < size; ++i) { + if(arr[i] == 0) + continue; + + int val = arr[i]; + int f = lower_bound(arr, val); + int l = upper_bound(arr, val); + l = l-f+1; + arr[val-1] = l; + } +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/minAndMaxArray.cpp b/geekForgeeks/section1_basics/minAndMaxArray.cpp new file mode 100644 index 0000000..958c462 --- /dev/null +++ b/geekForgeeks/section1_basics/minAndMaxArray.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +typedef long long ll; + +pair getMinMax(vector arr) { + + int size = arr.size(); + pair minMax; + if(size == 1) { + minMax.first = minMax.second = arr[0]; + return minMax; + } + minMax.first = LLONG_MAX; + minMax.second = LLONG_MIN; + + for(int i = 0; i < size; ++i) { + minMax.first = min(minMax.first,arr[i]); + minMax.second = max(minMax.second,arr[i]); + } + + return minMax; + +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/minimumSumOfSubarray.cpp b/geekForgeeks/section1_basics/minimumSumOfSubarray.cpp new file mode 100644 index 0000000..5ebf478 --- /dev/null +++ b/geekForgeeks/section1_basics/minimumSumOfSubarray.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +vector findMinSum(vector vals, int n) { + + vector res(n,0); + + for(int i = 0; i < n; ++i) { + + int ans = vals[i]; + int minEnding = vals[i]; + for(int j = i+1; j < n; ++j) { + minEnding = min(minEnding + vals[j], vals[j]); + ans = min(ans, minEnding); + } + + res[i] = ans; + + } + + return res; + +} + +int main(){ + + int t; cin >>t; + while(t--) { + int n; cin >> n; + vector vals(n,0); + for(int i = 0; i < n; ++i) + cin >> vals[i]; + vector minSums = findMinSum(vals, n); + for(auto x : minSums) + cout << x << " "; + cout << endl; + } + +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/palindromeString.cpp b/geekForgeeks/section1_basics/palindromeString.cpp new file mode 100644 index 0000000..2eea669 --- /dev/null +++ b/geekForgeeks/section1_basics/palindromeString.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int isPalindrome(string S) { + + int size = S.length(); + for(int i = 0, j = size-1; i < (size/2); ++i, --j) { + if(S[i] != S[j]) + return 0; + } + + return 1; +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/reverseArray.cpp b/geekForgeeks/section1_basics/reverseArray.cpp new file mode 100644 index 0000000..e2e4cef --- /dev/null +++ b/geekForgeeks/section1_basics/reverseArray.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +void reverseArray(vector &arr) { + int size = arr.size()-1; + for(int i = 0; i < size; ++i) { + int temp = arr[i]; + arr[i] = arr[size]; + arr[size] = temp; + size--; + } +} + +int main() { + +} \ No newline at end of file diff --git a/geekForgeeks/section1_basics/reverseSquaredSum.cpp b/geekForgeeks/section1_basics/reverseSquaredSum.cpp new file mode 100644 index 0000000..5f9b005 --- /dev/null +++ b/geekForgeeks/section1_basics/reverseSquaredSum.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +int squaredReverseSum(vector vec) { + + int size = vec.size() - 1, sum; + sum = vec[size]*vec[size]; + bool sub = true; + for(int i = size-1; i >= 0; --i) { + if(sub) { + sum -= (vec[i] * vec[i]); + sub = false; + } else { + sum += (vec[i] * vec[i]); + sub = true; + } + } + + return sum; +} + +int main(){ + + int t, n; + cin >> t; + while(t--) { + cin >> n; + vector vec(n); + for(int i = 0; i < n; ++i) + cin >> vec[i]; + int ans = squaredReverseSum(vec); + cout << ans << endl; + } + +} \ No newline at end of file