Professional Documents
Culture Documents
National Instruments Round 1
National Instruments Round 1
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
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<b.size();i++)
{
m[b[i][0]]=b[i][1];
m[b[i][1]]=b[i][0];
}
string news="";
for(int i=0;i<s.size();i++)
{
if(m.find(s[i])==m.end())
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);
}
4.Given 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;
return 0;
}
5.Colourful buildings
#include <bits/stdc++.h>
int main() {
ios_base::sync_with_stdio(false);
cin.tie(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;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(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(nodes.find(i) == nodes.end()) {
not_fnd = 1;
break;
}
}
int ans = 0;
if(!not_fnd) {
ans = s / (n - 1);
if(s % (n - 1) != 0)
ans ++;
}
cout << ans << '\n';
nodes.clear();
for(int i = 1; i <= n; i ++)
v[i].clear();
}
return 0;
}
7.Skynet
#include <iostream>
ulong combi=0;
ulong count=0,sum=0,x=0,count1=0;
ulong check[N];
check[i]=0;
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]];
}
count++;
check[pairs[i][0]]=count;
check[pairs[i][1]]=count;
if(check[i]>0)
count1++;
ulong max=check[0];
if(max<check[i])
max=check[i];
ulong headache[max+1];
headache[i]=0;
if(check[i]>0)
headache[check[i]]++;
ulong result=1;
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;
ulong pairs[MAXP][2];
set<vector<int>> distinctArrays;
vector<int> a{2,3,2,2,3};
int n = a.size();
distinctArrays.insert(a);
for (int j = i + 1; j < n; j++) { // Only swap when i < j to avoid duplicates
swap(a[i], a[j]);
distinctArrays.insert(a);
swap(a[i], a[j]);
if (arr[i] == 'P') {
pol = i;
break;
if (arr[i] == 'T') {
thi = i;
break;
}
}
return 0;
pol = pol + 1;
pol = pol + 1;
thi = thi + 1;
thi = thi + 1;
res++;
thi = thi + 1;
thi = thi + 1;
else {
pol = pol + 1;
pol = pol + 1;
return res;
int main()
{
int k, n;
k = 2;
n = sizeof(arr1) / sizeof(arr1[0]);
k = 2;
n = sizeof(arr2) / sizeof(arr2[0]);
k = 3;
n = sizeof(arr3) / sizeof(arr3[0]);
return 0;
10.jon2snow3
class Solution{
public:
string ans="";
for(int i=0;i<str.size();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];
};
#include <bits/stdc++.h>
int mod[k];
memset(mod, 0, sizeof(mod));
int cumSum = 0;
cumSum += arr[i];
mod[((cumSum % k) + k) % k]++;
int result = 0;
if (mod[i] > 1)
result += mod[0];
return result;
int main()
int k = 5;
int k1 = 5;
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.
Output: YES or NO
Not sure
#include <iostream>
#include <vector>
#include <algorithm>
difficulty_pairs.push_back(make_pair(difficulties[i], i));
sort(difficulty_pairs.begin(), difficulty_pairs.end());
return "NO";
}
return "YES";
int main() {
int n;
cin >> n;
vector<int> difficulties(n);
return 0;
13.interplanetary trade
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int N, M;
vector<vector<int>> currencies(N);
int ci;
currencies[i].resize(ci);
for (int j = 0; j < ci; ++j) {
if (i == j) {
continue;
graph[i][j] = 1;
break;
graph[i][j] = 1;
new_currency[i] = 0;
break;
int total_cost = 0;
total_cost += cost;
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>
int n = nums.size();
unordered_set<int> distinctSums;
distinctSums.insert(prefixSum[end] - prefixSum[start]);
return distinctSums.size();
int main() {
int N;
cin >> N;
vector<int> nums(N);
return 0;
stack<char> st;
int redundantCount = 0;
st.pop();
flag = false;
top = st.top();
st.pop();
if (flag == true)
redundantCount++;
} else
st.push(ch);
return redundantCount;
int main() {
cout << "Number of redundant braces: " << redundantCount << endl;
return 0;
}
16.