Skip to content

Commit

Permalink
finishing section1 from geekforgeeks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 committed Sep 27, 2024
1 parent 3d7258b commit 8715612
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 0 deletions.
16 changes: 16 additions & 0 deletions geekForgeeks/section1_basics/addingOnes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <bits/stdc++.h>
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];

}

19 changes: 19 additions & 0 deletions geekForgeeks/section1_basics/convertArrayToReduced.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
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<int,int> unmap;
for(int i = 0; i < n; ++i)
unmap[arr2[i]] = i;

for(int i = 0; i < n; ++i)
arr[i] = unmap[arr[i]];

}
56 changes: 56 additions & 0 deletions geekForgeeks/section1_basics/frequenceOfLimitedVector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <bits/stdc++.h>
using namespace std;

int lower_bound(vector<int> &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<int> &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<int>& 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;
}
}
24 changes: 24 additions & 0 deletions geekForgeeks/section1_basics/minAndMaxArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

pair<ll,ll> getMinMax(vector<ll> arr) {

int size = arr.size();
pair<ll,ll> 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;

}
39 changes: 39 additions & 0 deletions geekForgeeks/section1_basics/minimumSumOfSubarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> findMinSum(vector<int> vals, int n) {

vector<int> 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<int> vals(n,0);
for(int i = 0; i < n; ++i)
cin >> vals[i];
vector<int> minSums = findMinSum(vals, n);
for(auto x : minSums)
cout << x << " ";
cout << endl;
}

}
13 changes: 13 additions & 0 deletions geekForgeeks/section1_basics/palindromeString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <bits/stdc++.h>
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;
}
16 changes: 16 additions & 0 deletions geekForgeeks/section1_basics/reverseArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <bits/stdc++.h>
using namespace std;

void reverseArray(vector<int> &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() {

}
35 changes: 35 additions & 0 deletions geekForgeeks/section1_basics/reverseSquaredSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

int squaredReverseSum(vector<int> 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<int> vec(n);
for(int i = 0; i < n; ++i)
cin >> vec[i];
int ans = squaredReverseSum(vec);
cout << ans << endl;
}

}

0 comments on commit 8715612

Please sign in to comment.