Contests Solution
Contests Solution
include <cmath>
#
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin >> n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(n-j==i+1)
cout << "*";
else
cout << n-j ;
}
cout << endl;
}
return 0;
}
include <cmath>
#
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N, d; cin >> N >> d;
vector<int> v(N);
for (size_t i = 0; i < v.size(); ++i) {
cin >> v[i];
}
= d % N;
d
for (int i = d; i < N; ++i)
cout << v[i] << ' ';
for (int i = 0; i < d; ++i)
cout << v[i] << ' ';
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
#include <bits/stdc++.h>
string isBalanced(string s) {
std::vector < char> stack;
bool flag = false;
for(int i = 0; (i < s.length()) && !flag; i++){
switch(s[i]){
case '{':
stack.push_back(s[i]);
break;
case '[':
stack.push_back(s[i]);
break;
case '(':
stack.push_back(s[i]);
break;
case '}':
if(stack.size() == 0){
flag=true;
}
else if('{' != stack.back()){
flag=true;
}
else{
stack.pop_back();
}
break;
case ']':
if(stack.size() == 0){
flag=true;
}
else if('[' != stack.back()){
flag=true;
}
else{
stack.pop_back();
}
break;
case ')':
if(stack.size() == 0){
flag=true;
}
else if('(' != stack.back()){
flag=true;
}
else{
stack.pop_back();
}
break;
}
}
return (flag || (stack.size() != 0)) ? "NO" : "YES";
}
int main() {
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
string s;
cin >> s;
string result = isBalanced(s);
cout << result << endl;
}
return 0;
}
4. D
elete duplicate value in likedlist
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
node = node->next;
if (node) {
fout << sep;
}
}
}
free(temp);
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist->insert_node(llist_item);
}
free_singly_linked_list(llist1);
}
fout.close();
return 0;
}
#include <bits/stdc++.h>
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
class Solution {
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
/*The tree node has data, left child and right child
class Node {
int data;
Node* left;
Node* right;
};
*/
int height(Node* root) {
// Write your code here.
int leftHeight=-1,rightHeight=-1;
if(root->left){
leftHeight=height(root->left);
}
if(root->right)
rightHeight=height(root->right);
return max(leftHeight,rightHeight)+1;
}
int main() {
olution myTree;
S
Node* root = NULL;
int t;
int data;
return 0;
}
Contest 2:
include <cmath>
#
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
return 0;
}
include<bits/stdc++.h>
#
using namespace std;
int main()
{
vector<int> arr;
int n;
while(cin>>n)
{
arr.push_back(n);
}
n = arr.size();
int b = n/4;
int count=1;
for(int i=0;i<n;i++)
{
if(arr[i]==arr[i+1])
{
count++;
if(count>b){
cout<<arr[i]<<endl;
break;
}
}
else
{
count=1;
}
}
return 0;
}
OR
lass Solution {
c
public:
int countPairs(vector<int>& nums, int target) {
int n = nums.size();
sort(nums.begin(), nums.end());
r eturn count;
}
};
int
findMaxK
(
vector
<
int
>
&
nums
) {
map<
int
,
int
> mp ;
for
(
auto
it : nums )
mp
[it]++;
int
ans = INT_MIN ;
bool
gotAnsr =
false
;
for
(
int
i=
0
; i<
nums
.
size
() ; i++ ){
if
(
nums
[i] >
0
) {
if
(
mp
.
find
(
0
-
nums
[i] ) !=
mp
.
end
()
){
ans =
max
( ans,
nums
[i] );
gotAnsr =
true
;
}
}
}
if
( gotAnsr )
return
ans ;
return
-
1
;
}
//finding burgers
include<bits/stdc++.h>
#
using namespace std;
int main()
{
ector<int> arr;
v
int n;
while(cin>>n)
{
arr.push_back(n);
}
n = arr.size();
unordered_map<int,int> mp;
for(int i=0;i<n;i++)
{
mp[arr[i]]++;
}
int maxi=-1;
for(auto it : mp)
{
if(mp.find(-it.first)!=mp.end())
maxi = max(maxi,abs(it.first));
}
cout<<maxi<<endl;
return 0;
}
include <bits/stdc++.h>
#
using namespace std;
int main()
{
vector<int> arr;
int n;
while (cin >> n)
{
arr.push_back(n);
}
n = arr.size();
int pre = 1, suf = 1;
int ans = INT_MIN;
for (int i = 0; i < n; i++)
{
if (pre == 0)
pre = 1;
if (suf == 0)
uf = 1;
s
pre = pre * arr[i];
suf = suf * arr[n - 1 - i];
ans = max(ans, max(pre, suf));
}
cout<<ans<<endl;
}
Contest 3:
1. R
esult observation
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> ans;
int n;
while(cin>>n){
ans.push_back(n);
}
sort(ans.begin(),ans.end());
double median = 0;
if(ans.size()%2==1){
median = (double)ans[ans.size()/2];
}
else{
median = (double)(ans[ans.size()/2]+ans[ans.size()/2-1])/2.0;
}
cout <<fixed<< setprecision(5)<<median;
// cout<<(double)median;
return 0;
}
OR
while
(i<n1&&j<n2)
{
if
(
nums1
[i]<=
nums2
[j])
v
[++lastindex]=
nums1
[i++];
else
v
[++lastindex]=
nums2
[j++];
}
while
(i<n1)
v
[++lastindex]=
nums1
[i++];
while
(j<n2)
v
[++lastindex]=
nums2
[j++];
}
};
2. Disconnection
include <bits/stdc++.h>
#
using namespace std;
int main() {
tring s;
s
cin >> s;
if (s == "") {
cout << 0;
return 0;
}
nordered_set<char> uniqueChars;
u
int substringCount = 0;
for (char c : s) {
if (uniqueChars.count(c) == 0) {
uniqueChars.insert(c);
} else {
// Start a new substring when a repeated character is encountered
uniqueChars.clear();
uniqueChars.insert(c);
substringCount++;
}
}
// Every unique character starts a new substring, so add 1 for each unique character
cout << substringCount + uniqueChars.size();
return 0;
}
include <iostream>
#
#include <vector>
#include <sstream>
int main() {
std::vector<std::vector<int>> ar;
std::vector<int> row;
int num;
while (lineStream >> num) {
row.push_back(num);
}
ar.push_back(row);
}
int m = ar.size();
int n = ar[0].size();
int top = 0, left = 0, bottom = m - 2, right = n - 1;
int key = ar[m - 1][0];
int i = 0;
// Function to find the maximum sum path in two given arrays.
// The code is similar to the merge routine of the merge sort algorithm
int subwaySurf(const std::vector<int>& arr1, const std::vector<int>& arr2) {
int sum = 0;
int sum_x = 0, sum_y = 0;
// `i` and `j` denotes the current index of `arr1` and `arr2`, respectively
int i = 0, j = 0;
// if the current element of `arr2` is less than the current element of `arr1`
if (arr2[j] < arr1[i]) {
sum_y += arr2[j];
j++;
}
// if the current element of `arr1` is less than the current element of `arr2`
else if (arr1[i] < arr2[j]) {
sum_x += arr1[i];
i++;
}
um += max(sum_x, sum_y);
s
return sum;
}
int main() {
std::string line;
int main() {
int W,H,R;
cin>>H>>W>>R;
int ncycles=min(W,H)/2;
int m[W*H];
int cycles[150][1196];
int i,x,y;
int cycle,cyclelen;
for(i=0;i<W*H;i++)cin>>m[i];
for(cycle=0;cycle<ncycles;cycle++){
i=0;
x=y=cycle;
for(;y<H-cycle-1;y++)cycles[cycle][i++]=m[W*y+x];
for(;x<W-cycle-1;x++)cycles[cycle][i++]=m[W*y+x];
for(;y>cycle;y--)cycles[cycle][i++]=m[W*y+x];
for(;x>cycle;x--)cycles[cycle][i++]=m[W*y+x];
}
for(cycle=0;cycle<ncycles;cycle++){
cyclelen=2*(W-2*cycle)+2*(H-2*cycle)-4;
i=-R%cyclelen+cyclelen;
x=y=cycle;
for(;y<H-cycle-1;y++)m[W*y+x]=cycles[cycle][i++%cyclelen];
for(;x<W-cycle-1;x++)m[W*y+x]=cycles[cycle][i++%cyclelen];
for(;y>cycle;y--)m[W*y+x]=cycles[cycle][i++%cyclelen];
for(;x>cycle;x--)m[W*y+x]=cycles[cycle][i++%cyclelen];
}
for(y=0;y<H;y++){
for(x=0;x<W;x++){
if(x)cout<<' ';
cout<<m[W*y+x];
}
cout<<endl;
}
return 0;
}
Or
include <cmath>
#
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// Main algorithm: in order to rotate the whole matrix, we'll just rotate one ring at a time
// We can do this in-place to achieve O(1) additional space complexity
int main() {
int M, N, R;
cin>>M>>N>>R;
int **matrix = new int*[M];
for(int i = 0; i < M; i++) {
matrix[i] = new int[N];
for(int j = 0; j < N; j++) {
cin>>matrix[i][j];
}
}