-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finishing section1 from geekforgeeks
- Loading branch information
1 parent
3d7258b
commit 8715612
Showing
8 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |