This media is not supported in your browser
VIEW IN TELEGRAM
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll mod(ll x,ll mod)
{
if(x<mod) return 0;
return x%mod;
}
bool check(ll l,ll r,ll k,vector<ll>&a)
{
unordered_map<ll,ll>mpp;
for(ll i=l;i<=r;i++) mpp[i]++;
for(auto it:mpp)
{
if(it.second!=k) return false;
}
return true;
}
ll solve(ll n,ll k,vector<ll>&a,ll q,vector<vector<ll>>&queries)
{
if(k==1)
{
ll sum=0;
for(ll i=1;i<=q;i++) sum+=i;
return sum;
}
vector<ll>p(q);
ll t=0,tt=0;
ll L,R;
for(ll i=0;i<q;i++)
{
L=mod((t+queries[i][0]),n)+1;
R=mod((tt+queries[i][1]),n)+1;
t=L;
tt=R;
if(check(L,R,k,a)) p[i]=i+1;
}
ll sum=0;
ll m=1e9+7;
for(auto it:p) sum=(sum+it)%m;
return sum%m;
}
signed main()
{
ll n,k; cin>>n>>k;
vector<ll>a(n);
for(ll i=0;i<n;i++) cin>>a[i];
ll q; cin>>q;
ll two; cin>>two;
vector<vector<ll>>queries(q,vector<ll>(2));
for(ll i=0;i<q;i++)
{
ll x,y; cin>>x>>y;
queries[i][0]=x;
queries[i][1]=y;
//cout<<x<<" "<<y<<endl;
}
cout<<solve(n,k,a,q,queries);
return 0;
}
K occurance ✅
👍1
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll mod=1e9+7;
ll C(vector<ll>&a,ll n)
{
ll count=0;
for(ll i=0;i<n-1;i++)
{
if(((a[i]*a[i+1])-(a[i]+a[i+1]))%2==0) count++;
}
return count;
}
void generate(ll n,ll m,ll k,vector<ll>¤t,ll&count)
{
if (current.size()==n)
{
if (C(current,n)==k)
{
count=(count+1)%mod;
}
return;
}
for (ll i=1;i<=m;i++)
{
current.push_back(i);
generate(n,m,k,current,count);
current.pop_back();
}
}
ll solve(ll n,ll m,ll k)
{
vector<ll>current;
ll count=0;
generate(n,m,k,current,count);
return count%mod;
}
signed main()
{
ll n,m,k; cin>>n>>m>>k;
cout<<solve(n,m,k);
return 0;
}
Perfectly even ✅
👍1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main()
{
ll n, i, j;
cin >> n;
ll a[n];
for(i=0; i<n; i++)
{
cin >> a[i];
}
ll maxi = 0;
i=0;
while(i<n-1)
{
ll curr=0;
for(j=i; j<n-1; j++)
{
int diff = a[j+1]-a[j];
if(diff<0)
{
break;
}
else if((diff & (diff-1)) == 0 || diff==0)
{
curr++;
}
else
{
break;
}
}
i=j+1;
maxi = max(maxi, curr+1);
}
if(n==1) maxi=1;
cout<<maxi<<'\n';
return 0;
}
Yet another lies ✅
Infosys
using namespace std;
using ll = long long;
using ld = long double;
int main()
{
ll n, i, j;
cin >> n;
ll a[n];
for(i=0; i<n; i++)
{
cin >> a[i];
}
ll maxi = 0;
i=0;
while(i<n-1)
{
ll curr=0;
for(j=i; j<n-1; j++)
{
int diff = a[j+1]-a[j];
if(diff<0)
{
break;
}
else if((diff & (diff-1)) == 0 || diff==0)
{
curr++;
}
else
{
break;
}
}
i=j+1;
maxi = max(maxi, curr+1);
}
if(n==1) maxi=1;
cout<<maxi<<'\n';
return 0;
}
Yet another lies ✅
Infosys
👍5
Guys pls wait i will try to send...asap maximum answers....❤️
kerp sharing @coding_000✅
Check once above 👆👆👆
kerp sharing @coding_000✅
Check once above 👆👆👆
This media is not supported in your browser
VIEW IN TELEGRAM
#include <bits/stdc++.h>
using namespace std;
int minimumCost(int N, vector<int> A, vector<vector<int>> Cost) {
int minCost = INT_MAX;
vector<int> perm(N);
iota(perm.begin(), perm.end(), 1);
do {
bool valid = true;
for (int i = 0; i < N; ++i) {
if (perm[i] > A[i]) {
valid = false;
break;
} else if (perm[i] < A[i]) {
break;
}
}
if (valid) {
int cost = 0;
for (int i = 0; i < N; ++i) {
cost += Cost[i][perm[i] - 1];
}
minCost = min(minCost, cost);
}
} while (next_permutation(perm.begin(), perm.end()));
return minCost;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> Cost(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> Cost[i][j];
}
}
cout << minimumCost(N, A, Cost) << endl;
return 0;
}
Cheapest permutation ✅
using namespace std;
int minimumCost(int N, vector<int> A, vector<vector<int>> Cost) {
int minCost = INT_MAX;
vector<int> perm(N);
iota(perm.begin(), perm.end(), 1);
do {
bool valid = true;
for (int i = 0; i < N; ++i) {
if (perm[i] > A[i]) {
valid = false;
break;
} else if (perm[i] < A[i]) {
break;
}
}
if (valid) {
int cost = 0;
for (int i = 0; i < N; ++i) {
cost += Cost[i][perm[i] - 1];
}
minCost = min(minCost, cost);
}
} while (next_permutation(perm.begin(), perm.end()));
return minCost;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> Cost(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> Cost[i][j];
}
}
cout << minimumCost(N, A, Cost) << endl;
return 0;
}
Cheapest permutation ✅
👍1
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i;
TreeMap<Long,Integer> map=new TreeMap<>();
long dp[]=new long[n];
long r[]=new long[n];
long h[]=new long[n];
long mod=(long)1e9+7;
for(i=0;i<n;i++) r[i]=sc.nextLong();
for(i=0;i<n;i++) h[i]=sc.nextLong();
for(i=0;i<n;i++){
long volume=(h[i]*r[i]*r[i])%mod;
Long low=map.floorKey(volume);
if(low!=null) dp[i]=(dp[i]+dp[map.get(low)]+volume)%mod;
else dp[i]=(dp[i]+volume)%mod;
map.put(volume,i);
}
long max=0;
for(long it:dp) max=Math.max(max,it);
System.out.println(max);
}
}
Increasing mex counting ✅
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i;
TreeMap<Long,Integer> map=new TreeMap<>();
long dp[]=new long[n];
long r[]=new long[n];
long h[]=new long[n];
long mod=(long)1e9+7;
for(i=0;i<n;i++) r[i]=sc.nextLong();
for(i=0;i<n;i++) h[i]=sc.nextLong();
for(i=0;i<n;i++){
long volume=(h[i]*r[i]*r[i])%mod;
Long low=map.floorKey(volume);
if(low!=null) dp[i]=(dp[i]+dp[map.get(low)]+volume)%mod;
else dp[i]=(dp[i]+volume)%mod;
map.put(volume,i);
}
long max=0;
for(long it:dp) max=Math.max(max,it);
System.out.println(max);
}
}
Increasing mex counting ✅
👍1
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
vector<int> A, P;
vector<vector<int>> tree;
vector<int> ranges;
long long result = 0;
void dfs(int node, int parent, set<int>& values) {
values.insert(A[node]);
set<int> child_values;
int num_ranges = 1, last = -1;
for (int val : values) {
if (last != -1 && val != last + 1) num_ranges++;
last = val;
}
ranges[node] = num_ranges;
for (int child : tree[node]) {
if (child == parent) continue;
child_values = values;
dfs(child, node, child_values);
}
result = (result + ranges[node]) % MOD;
}
int main() {
int N;
cin >> N;
A.resize(N);
P.resize(N);
tree.resize(N);
ranges.resize(N);
for (int i = 0; i < N; ++i) {
cin >> P[i];
if (i > 0) tree[P[i]].push_back(i);
}
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
set<int> values;
dfs(0, -1, values);
cout << result << endl;
return 0;
}
path coverage range ✅
👍3👌1
Coding | EXAMS | IBM ACCENTURE | VIRTUSA | IBM | AMAZON | TCS | EPAM | WILEY EDGE | TECH MAHINDRA | JPMORGAN | HCL | WIPRO
Photo
Colourd ball
Kth palindrome
String container
Play with stacks
Perfectly even
Divisible string
Kth palindrome
String container
Play with stacks
Perfectly even
Divisible string
Kth Palindrome Code
Infosys SP Exam
C++
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int v(int k) {
return (1 << k) - 1;
}
bool p(int n) {
while (n % 2 == 0) n /= 2;
return n == 1;
}
long long s(int n) {
vector<long long> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int k = 1; v(k) <= i; ++k) {
int l = v(k - 1) - 1;
if (i >= 2 * l + 1) {
dp[i] = (dp[i] + dp[i - (2 * l + 1)] * k) % MOD;
}
}
}
return dp[n];
}
int main() {
int n;
cin >> n;
cout << s(n) << endl;
return 0;
}
Kth Palindrome Code
Infosys SP Exam
C++
Infosys SP Exam
C++
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int v(int k) {
return (1 << k) - 1;
}
bool p(int n) {
while (n % 2 == 0) n /= 2;
return n == 1;
}
long long s(int n) {
vector<long long> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int k = 1; v(k) <= i; ++k) {
int l = v(k - 1) - 1;
if (i >= 2 * l + 1) {
dp[i] = (dp[i] + dp[i - (2 * l + 1)] * k) % MOD;
}
}
}
return dp[n];
}
int main() {
int n;
cin >> n;
cout << s(n) << endl;
return 0;
}
Kth Palindrome Code
Infosys SP Exam
C++
👍1
👍1