Contest 1:
1. Number star
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;
}
2. Left rotation
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 < [Link](); ++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;
}
3. Balanced brackets
#include <bits/stdc++.h>
using namespace std;
string isBalanced(string s) {
std::vector < char> stack;
bool flag = false;
for(int i = 0; (i < [Link]()) && !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([Link]() == 0){
flag=true;
}
else if('{' != [Link]()){
flag=true;
}
else{
stack.pop_back();
}
break;
case ']':
if([Link]() == 0){
flag=true;
}
else if('[' != [Link]()){
flag=true;
}
else{
stack.pop_back();
}
break;
case ')':
if([Link]() == 0){
flag=true;
}
else if('(' != [Link]()){
flag=true;
}
else{
stack.pop_back();
}
break;
}
}
return (flag || ([Link]() != 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;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;
node = node->next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
inglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
S
SinglyLinkedListNode *curr = llist;
while (curr != NULL) {
SinglyLinkedListNode *next = curr->next;
while (next != NULL && next->data == curr->data)
next = next->next;
curr->next = next;
curr = next;
if (next != NULL) next = next->next;
}
return llist;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int t;
cin >> t;
[Link](numeric_limits<streamsize>::max(), '\n');
for (int t_itr = 0; t_itr < t; t_itr++) {
SinglyLinkedList* llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
[Link](numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
[Link](numeric_limits<streamsize>::max(), '\n');
llist->insert_node(llist_item);
}
SinglyLinkedListNode* llist1 = removeDuplicates(llist->head);
rint_singly_linked_list(llist1, " ", fout);
p
fout << "\n";
free_singly_linked_list(llist1);
}
[Link]();
return 0;
}
5. Height of a binary tree
#include <bits/stdc++.h>
using namespace std;
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;
}
}; //End of Solution
int main() {
olution myTree;
S
Node* root = NULL;
int t;
int data;
std::cin >> t;
while(t-- > 0) {
std::cin >> data;
root = [Link](root, data);
}
int height = [Link](root);
std::cout << height;
return 0;
}
Contest 2:
1: seema and birju
include <cmath>
#
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
int summation(int n)
{
int sum =0;
while(n!=0)
{
int rem = n%10;
sum+=rem*rem;
n=n/10;
}
return sum;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
unordered_set<int> st;
while(n!=1 && [Link](n)==[Link]())
{
[Link](n);
n=summation(n);
}
if(n==1)
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;
}
2. Popular number
include<bits/stdc++.h>
#
using namespace std;
int main()
{
vector<int> arr;
int n;
while(cin>>n)
{
arr.push_back(n);
}
n = [Link]();
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;
}
3. Anomaly detection
include<bits/stdc++.h>
#
using namespace std;
int main()
{
vector<int> arr;
int n;
int target=0;
while(cin>>n)
{
arr.push_back(n);
target = n;
}
arr.pop_back();
n = [Link]();
int count1=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]+arr[j]<target){
count1++;
}
}
}
cout<<count1<<endl;
return 0;
}
OR
lass Solution {
c
public:
int countPairs(vector<int>& nums, int target) {
int n = [Link]();
sort([Link](), [Link]());
int count = 0;
int left = 0;
int right = n - 1;
while (left < right) {
if (nums[left] + nums[right] < target)
{
count += right - left;
left++;
} else {
right--;
}
}
r eturn count;
}
};
4) Finding burger
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
;
}
4. Finding burgers
//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 = [Link]();
unordered_map<int,int> mp;
for(int i=0;i<n;i++)
{
mp[arr[i]]++;
}
int maxi=-1;
for(auto it : mp)
{
if([Link](-[Link])!=[Link]())
maxi = max(maxi,abs([Link]));
}
cout<<maxi<<endl;
return 0;
}
5. Highest product
include <bits/stdc++.h>
#
using namespace std;
int main()
{
vector<int> arr;
int n;
while (cin >> n)
{
arr.push_back(n);
}
n = [Link]();
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([Link](),[Link]());
double median = 0;
if([Link]()%2==1){
median = (double)ans[[Link]()/2];
}
else{
median = (double)(ans[[Link]()/2]+ans[[Link]()/2-1])/2.0;
}
cout <<fixed<< setprecision(5)<<median;
// cout<<(double)median;
return 0;
}
OR
// Optimized Using: Two Pointer with Extra Space
// Time Complexity: O(m+n)
// Space Complexity: O(m+n)
class
Solution
{
public:
double
findMedianSortedArrays
(
vector
<
int
>
&
nums1
,
vector
<
i
nt
>
&
nums2
) {
// Create a single sorted by merging two sorted arrays
int
n1=
nums1
.
size
();
int
n2=
nums2
.
size
();
int
i=
0
;
int
j=
0
;
int
lastindex=-
1
;
// Initialize a new array
vector<
int
>
v
(n1+n2,
0
);
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++];
// Return the result
int
n=n1+n2;
return
n%
2
?
v
[n/
2
]:(
v
[n/
2
]+
v
[n/
2
-
1
])/
2.0
;
}
};
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 ([Link](c) == 0) {
[Link](c);
} else {
// Start a new substring when a repeated character is encountered
[Link]();
[Link](c);
substringCount++;
}
}
// Every unique character starts a new substring, so add 1 for each unique character
cout << substringCount + [Link]();
return 0;
}
3. Matrix binary search
include <iostream>
#
#include <vector>
#include <sstream>
int main() {
std::vector<std::vector<int>> ar;
// Input values into the vector of vectors
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream lineStream(line);
std::vector<int> row;
int num;
while (lineStream >> num) {
row.push_back(num);
}
ar.push_back(row);
}
int m = [Link]();
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;
while (top <= bottom) {
i = (top + bottom) / 2;
if (key <= ar[i][right] && ar[i][left] <= key) {
break;
} else if (ar[i][right] > key) {
bottom = i - 1;
} else {
top = i + 1;
}
}
while (left <= right) {
int mid = (left + right) / 2;
if (ar[i][mid] == key) {
std::cout << "(" << i << "," << mid << ")\n";
return 0;
} else if (ar[i][mid] < key) {
left = mid + 1;
} else if (ar[i][mid] > key) {
right = mid - 1;
}
}
td::cout << "-1\n";
s
return 0;
}
4. 2 lanes subway surfer
include <iostream>
#
#include <vector>
#include <sstream>
// Utility function to find the maximum of two integers
int max(int x, int y) {
return (x > y) ? x : y;
}
// 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;
// loop till `arr1` and `arr2` are empty
while (i < [Link]() && j < [Link]()) {
// to handle the duplicate elements in `arr1`
while (i < [Link]() - 1 && arr1[i] == arr1[i + 1]) {
sum_x += arr1[i++];
}
// to handle the duplicate elements in `arr2`
while (j < [Link]() - 1 && arr2[j] == arr2[j + 1]) {
sum_y += arr2[j++];
}
// 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++;
}
lse // if (arr1[i] == arr2[j])
e
{
// consider the maximum sum and include the current cell's value
sum += max(sum_x, sum_y) + arr1[i];
// move both indices by 1 position
i++, j++;
// reset both sums
sum_x = 0, sum_y = 0;
}
}
// process the remaining elements of `arr1` (if any)
while (i < [Link]()) {
sum_x += arr1[i++];
}
// process the remaining elements of `arr2` (if any)
while (j < [Link]()) {
sum_y += arr2[j++];
}
um += max(sum_x, sum_y);
s
return sum;
}
int main() {
std::string line;
// Read the first line
std::getline(std::cin, line);
std::istringstream iss1(line);
std::vector<int> arr1;
int num1;
while (iss1 >> num1) {
arr1.push_back(num1);
}
// Read the second line
std::getline(std::cin, line);
std::istringstream iss2(line);
std::vector<int> arr2;
int num2;
while (iss2 >> num2) {
arr2.push_back(num2);
}
int sum = subwaySurf(arr1, arr2);
td::cout << sum << std::endl;
s
return 0;
}
. Matrix layer rotation
5
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
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];
}
}
int numRings = min(M,N)/2;
for(int i = 0; i < numRings; i++) {
// Subtract the number of 360 degree rotations from R
// A 360 degree rotation = rotating the same number of times as the perimeter of
the current ring
int numRotations = R%(2*(M + N - 4*i) - 4);
for(int rotation = 0; rotation < numRotations; rotation++) {
// Rotate the ring (see the clockwise algorithm for an in-depth example of
how this is done)
// Rotate top row
for(int j = i; j < N-i-1; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][j+1];
matrix[i][j+1] = tmp;
}
// Rotate right column
for(int j = i; j < M-i-1; j++) {
int tmp = matrix[j][N-i-1];
matrix[j][N-i-1] = matrix[j+1][N-i-1];
matrix[j+1][N-i-1] = tmp;
}
// Rotate bottom row
for(int j = N-i-1; j > i; j--) {
int tmp = matrix[M-i-1][j];
matrix[M-i-1][j] = matrix[M-i-1][j-1];
matrix[M-i-1][j-1] = tmp;
}
// Rotate left column
for(int j = M-i-1; j > i+1; j--) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[j-1][i];
matrix[j-1][i] = tmp;
}
}
}
// Output final matrix
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
cout<<matrix[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}