NATIONAL INSTRUMENTS ROUND 1
[Link] AND LADDER
#include<bits/stdc++.h>
using namespace std;
vector<int>t(31,-1);
int sol(int ind, unordered_map<int, int>& h)
{
if (ind >= 30)
return 0;
else if (t[ind] != -1)
return t[ind];
int min_value = INT_MAX;
for (int j = 1; j <= 6; j++) {
int k = ind + j;
if ([Link](k) > 0) {
if (h[k] < k)
continue;
k = h[k];
}
min_value = min(min_value, sol(k, h) + 1);
}
t[ind] = min_value;
return t[ind];
}
int min_throw(int n, vector<int> arr)
{
unordered_map<int, int> h;
int i=0;
while ( i < 2 * n) {
h[arr[i]] = arr[i + 1];
i += 2;
}
return sol(1, h);
}
int main()
{
int N = 8;
vector<int> arr{ 3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 29, 9 };
cout<< min_throw(N, arr) << endl;
return 0;
}
[Link] and friendship challenge
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
int N;
cin >> N;
[Link]();
unordered_map<char, char> map, mapR;
for (int i = 0; i < 26; i++) {
char c = 'A' + i;
map[c] = c;
}
for (int i = 0; i < N; i++) {
string chars;
getline(cin, chars);
char c0 = chars[0];
char c1 = chars[2];
char temp = map[c0];
map[c0] = map[c1];
map[c1] = temp;
}
mapR[' '] = ' ';
for (const auto& entry : map) {
mapR[[Link]] = [Link];
mapR[[Link] + 32] = [Link] + 32;
}
string str;
getline(cin, str);
string result;
for (char c : str) {
result += mapR[c];
}
cout << result << endl;
return 0;
}
//java code
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
int N = [Link]([Link]());
Map<Character, Character> map = new HashMap<>(), mapR = new
HashMap<>();
for (int i = 0; i < 26; i++) {
Character c = (char) ('A' + i);
[Link](c, c);
}
for (int i = 0; i < N; i++) {
String chars[] = [Link]().split(" ");
char c0 = chars[0].charAt(0);
char c1 = chars[1].charAt(0);
char temp = [Link](c0);
[Link](c0, [Link](c1));
[Link](c1, temp);
}
[Link](' ', ' ');
for (Character c : [Link]()) {
[Link]([Link](c), c);
[Link]((char) ([Link](c) + 32), (char) (c + 32));
}
String str = [Link]();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < [Link](); i++) {
[Link]([Link]([Link](i)));
}
[Link](sb);
}
3. Given a string and a set of (m * 2) matrix where the matrix contains a pair of
characters to swap.
// (user need to swap every character that matches the characters of the string
until the pairs of character end in a matrix)
// Note: The string becomes new after every swap of pairs in the matrix and from
that new only further swaps need to be performed
#include<bits/stdc++.h>
using namespace std;
string sol(string s,vector<vector<char>>b)
{
unordered_map<char,char>m;
for(int i=0;i<[Link]();i++)
{
m[b[i][0]]=b[i][1];
m[b[i][1]]=b[i][0];
}
string news="";
for(int i=0;i<[Link]();i++)
{
if([Link](s[i])==[Link]())
news=news+s[i];
else
news=news+m[s[i]];
}
return news;
}
int main()
{
string a="abcdef";
vector<vector<char>>b={{'a','b'},{'c','d'},{'e','f'}};
cout<<sol(a,b);
}
[Link] an archer. He would not stop shooting arrows until and unless his average
is more that 9. Score for few shots were given and we had to calculate the
minimum number of shots he would have to play to get an average of 9.5 or
more.
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {8, 10,11, 10}; // Replace with the given scores
int totalScore = 0;
int numShots = 0;
for (int score : scores) {
totalScore += score;
numShots++;
double average = static_cast<double>(totalScore) / numShots;
if (average >= 9.5) {
std::cout << "Minimum number of shots: " << numShots << std::endl;
break;
}
}
return 0;
}
[Link] buildings
#include <bits/stdc++.h>
using namespace std;
const int md = 1E9 + 7;
int main() {
ios_base::sync_with_stdio(false);
[Link](NULL);
int t;
cin >> t;
while(t --) {
int n, k;
cin >> n >> k;
long long ans = k;
for(int i = 2; i <= n; i ++) {
ans = (ans * (k - 1)) % md;
}
cout << ans << '\n';
}
return 0;
}
[Link]-MIN weighted edge
#include <bits/stdc++.h>
using namespace std;
const int N = 2E3 + 5;
int len;
int d_len;
int start;
int vis[N];
int path[N];
set<int> nodes;
vector<int> v[N];
void dfs(int src) {
len ++;
if(len > d_len) {
d_len = len;
start = src;
}
vis[src] = 1;
for(auto i : v[src]) {
if(!vis[i])
dfs(i);
}
len --;
return;
}
void dfs_again(int src) {
len ++;
vis[src] = 1;
path[len] = src;
if(len == d_len) {
for(int i = 1; i <= len; i ++)
[Link](path[i]);
}
for(auto i : v[src]) {
if(!vis[i])
dfs_again(i);
}
len --;
return;
}
int main() {
ios_base::sync_with_stdio(false);
[Link](NULL);
int t;
cin >> t;
while(t --) {
int n, s;
cin >> n >> s;
int x, y;
for(int i = 1; i < n; i ++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
len = d_len = 0;
memset(vis, 0, sizeof vis);
dfs(1);
len = d_len = 0;
memset(vis, 0, sizeof vis);
dfs(start);
for(int i = 1; i <= n; i ++) {
len = 0;
memset(vis, 0, sizeof vis);
dfs_again(i);
}
bool not_fnd = 0;
for(int i = 1; i <= n; i ++) {
if([Link](i) == [Link]()) {
not_fnd = 1;
break;
}
}
int ans = 0;
if(!not_fnd) {
ans = s / (n - 1);
if(s % (n - 1) != 0)
ans ++;
}
cout << ans << '\n';
[Link]();
for(int i = 1; i <= n; i ++)
v[i].clear();
}
return 0;
}
[Link]
#include <iostream>
#define MAXN 100000
#define MAXP 100000
using namespace std;
typedef unsigned long ulong;
ulong CountNoOfWays(ulong N, ulong P, ulong pairs[][2])
ulong combi=0;
ulong count=0,sum=0,x=0,count1=0;
ulong check[N];
for (int i=0;i<N;i++)
check[i]=0;
for (int i=0;i<P;i++)
if(check[pairs[i][0]]!=0||check[pairs[i][1]]!=0)
if(check[pairs[i][0]]!=0)
check[pairs[i][1]]=check[pairs[i][0]];
if(check[pairs[i][1]]!=0)
check[pairs[i][0]]=check[pairs[i][1]];
}
if(check[pairs[i][0]]==0 && check[pairs[i][1]]==0)
count++;
check[pairs[i][0]]=count;
check[pairs[i][1]]=count;
for (int i=0;i<N;i++)
if(check[i]>0)
count1++;
ulong max=check[0];
for (int i=1;i<N;i++)
if(max<check[i])
max=check[i];
ulong headache[max+1];
for (int i=0;i<=max;i++)
headache[i]=0;
for (int i=0;i<N;i++)
if(check[i]>0)
headache[check[i]]++;
ulong result=1;
for (int i=0;i<=max;i++)
if(headache[i]>0)
result=result*headache[i];
// cout<<result<<endl;
x=N-count1;
sum=x*count1;
sum=sum+(x*(x-1)/2);
combi=combi+sum+result;
return combi;
int main()
ulong N, P;
cin >> N >> P;
ulong pairs[MAXP][2];
for(ulong i = 0; i < P; i++)
cin >> pairs[i][0] >> pairs[i][1];
cout << CountNoOfWays(N, P, pairs);
[Link] array by swapping one element
#include <bits/stdc++.h>
using namespace std;
int main() {
set<vector<int>> distinctArrays;
vector<int> a{2,3,2,2,3};
int n = [Link]();
[Link](a);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) { // Only swap when i < j to avoid duplicates
swap(a[i], a[j]);
[Link](a);
swap(a[i], a[j]);
cout << [Link]();
[Link] and thief
#include <bits/stdc++.h>
using namespace std;
int policeThief(char arr[], int n, int k)
int pol = -1, thi = -1, res = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 'P') {
pol = i;
break;
for (int i = 0; i < n; i++) {
if (arr[i] == 'T') {
thi = i;
break;
}
}
if (thi == -1 || pol == -1)
return 0;
while (pol < n && thi < n) {
if (abs(pol - thi) <= k) {
pol = pol + 1;
while (pol < n && arr[pol] != 'P')
pol = pol + 1;
thi = thi + 1;
while (thi < n && arr[thi] != 'T')
thi = thi + 1;
res++;
else if (thi < pol) {
thi = thi + 1;
while (thi < n && arr[thi] != 'T')
thi = thi + 1;
else {
pol = pol + 1;
while (pol < n && arr[pol] != 'P')
pol = pol + 1;
return res;
int main()
{
int k, n;
char arr1[] = { 'P', 'T', 'T', 'P', 'T' };
k = 2;
n = sizeof(arr1) / sizeof(arr1[0]);
cout << "Maximum thieves caught: "
<< policeThief(arr1, n, k) << endl;
char arr2[] = { 'T', 'T', 'P', 'P', 'T', 'P' };
k = 2;
n = sizeof(arr2) / sizeof(arr2[0]);
cout << "Maximum thieves caught: "
<< policeThief(arr2, n, k) << endl;
char arr3[] = { 'P', 'T', 'P', 'T', 'T', 'P' };
k = 3;
n = sizeof(arr3) / sizeof(arr3[0]);
cout << "Maximum thieves caught: "
<< policeThief(arr3, n, k) << endl;
return 0;
10.jon2snow3
class Solution{
public:
char decodeIt(string str, int k)
string ans="";
for(int i=0;i<[Link]();i++)
if(str[i]>='0'&&str[i]<='9')
{
string g=ans;
int t=str[i]-'0'-1;
while(t--)
ans+=g;
else
ans=ans+str[i];
return ans[k-1];
};
[Link] all the subarrays having sum divisible by k
#include <bits/stdc++.h>
using namespace std;
int subCount(int arr[], int n, int k)
int mod[k];
memset(mod, 0, sizeof(mod));
int cumSum = 0;
for (int i = 0; i < n; i++) {
cumSum += arr[i];
mod[((cumSum % k) + k) % k]++;
int result = 0;
for (int i = 0; i < k; i++)
if (mod[i] > 1)
result += (mod[i] * (mod[i] - 1)) / 2;
result += mod[0];
return result;
int main()
int arr[] = { 10,0,4,5};
int k = 5;
int n = sizeof(arr) / sizeof(arr[0]);
cout << subCount(arr, n, k) << endl;
int arr1[] = { 4, 5, 0, -12, -23, 1 };
int k1 = 5;
int n1 = sizeof(arr1) / sizeof(arr1[0]);
cout << subCount(arr1, n1, k1) << endl;
return 0;
}
12. Brand, Coop and Murph are scientists and are conducting n distinct
experiments of various difficulties. All three want to do the experiments in the
order of the difficulty of the experiment but there’s a catch. They are currently in
different time dimensions but are connected by one thing – gravity! If at least
two of them end up doing the experiment in the same order then due to
gravitational anomaly all the experiment will fail. Now given the number n and a
list of difficulties di for each experiment, is there a way to order the experiments
such that: All 3 do the experiments in the order of difficulty from least difficult
(denote by a lesser difficulty value) to most difficult. The order of these
experiments should be unique i.e no two scientists can do the experiments in the
same order.
Input: 1 <= n <=2000, 1 <= di <=2000
Output: YES or NO
Not sure
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string can_order_experiments(int n, const vector<int>& difficulties) {
vector<pair<int, int>> difficulty_pairs; // (difficulty, index)
for (int i = 0; i < n; ++i) {
difficulty_pairs.push_back(make_pair(difficulties[i], i));
sort(difficulty_pairs.begin(), difficulty_pairs.end());
for (int i = 1; i < n; ++i) {
if (difficulty_pairs[i].first == difficulty_pairs[i - 1].first) {
return "NO";
}
return "YES";
int main() {
int n;
cin >> n;
vector<int> difficulties(n);
for (int i = 0; i < n; ++i) {
cin >> difficulties[i];
string result = can_order_experiments(n, difficulties);
cout << result << endl;
return 0;
[Link] trade
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> currencies(N);
for (int i = 0; i < N; ++i) {
int ci;
cin >> ci;
currencies[i].resize(ci);
for (int j = 0; j < ci; ++j) {
cin >> currencies[i][j];
vector<vector<int>> graph(N, vector<int>(N, 0));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) {
continue;
for (int currency : currencies[i]) {
if (find(currencies[j].begin(), currencies[j].end(), currency) != currencies[j].end()) {
graph[i][j] = 1;
break;
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (graph[i][k] && graph[k][j]) {
graph[i][j] = 1;
vector<int> new_currency(N, 1);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (graph[i][j]) {
new_currency[i] = 0;
break;
int total_cost = 0;
for (int cost : new_currency) {
total_cost += cost;
cout << total_cost << endl;
return 0;
14. Given N and N elements Find the number of distinct sums. For example:
3
1, 2, 3
Possible sums of all subarrays: 1, 2, 3, 3, 5, 6. Return Value: 5 (Distinct sums are:
1, 2, 3, 5, 6)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int countDistinctSums(const vector<int>& nums) {
int n = [Link]();
unordered_set<int> distinctSums;
vector<int> prefixSum(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
for (int start = 0; start < n; ++start) {
for (int end = start + 1; end <= n; ++end) {
[Link](prefixSum[end] - prefixSum[start]);
return [Link]();
int main() {
int N;
cin >> N;
vector<int> nums(N);
for (int i = 0; i < N; ++i) {
cin >> nums[i];
int result = countDistinctSums(nums);
cout << "Distinct sums: " << result << endl;
return 0;
[Link] redundant braces
#include <bits/stdc++.h>
using namespace std;
int countRedundantBraces(string& str) {
stack<char> st;
int redundantCount = 0;
for (auto& ch : str) {
if (ch == ')') {
char top = [Link]();
[Link]();
bool flag = true;
while (![Link]() && top != '(') {
if (top == '+' || top == '-' || top == '*' || top == '/')
flag = false;
top = [Link]();
[Link]();
if (flag == true)
redundantCount++;
} else
[Link](ch);
return redundantCount;
int main() {
string str = "(A)+(B)";
int redundantCount = countRedundantBraces(str);
cout << "Number of redundant braces: " << redundantCount << endl;
return 0;
}
16.