You are on page 1of 6

ISB Algo 2nd Test

Email ID –lavand.aniket@gmail.com
Enrollment ID- ABISB027245

B)
#include <bits/stdc++.h>
using namespace std;
int factorial (int x) //return factorial of arr[i] for 3 rd query
{
int res = 1, i;
for (i = 2; i <= x; i++)
res *= i;
return res;
}

int main()
{
int q=5; //assuming q here is no of elements in array
int arr[q]; //[1,2,0,4,5];
for(i=0->q-1)
cin>>arr[i]; //taking array input
int queries[3];//queries[0]=query type. , queries[1]=l,queries[2]=r;
while( queries input) //executing until query input is there
{
if(queries[0]==1)
{
for(int i=queries[1]-1;i<=queries[2]-1;i++){
arr[i]=arr[i]+(queries[2]-queries[1]);
}
}

else if(queries[0]==2){
arr[queries[1]]=queries[2];
}

else if(queries[0]==3){
int sum=0;
for(int i=queries[1]-1;i<=queries[2]-1;i++){
sum=sum+(factorial(arr[i])%1001);
}

cout<<sum;
}

return 0;

C) most frequent occurrence of wickets


//the crux of the problem is to find the maximum occurring number in the given range
of the array
#include<bits/stdc++.h>
using namespace std;

int main(){
int m; //months

int n; //no of players

cin>>m>>n;

vector<vector<int> > arr(m, vector<int>(n)); //2d array with m rows and n columns

for(vector<int> a : arr){

for(int i=0;i<n;i++)
cin>>a[i];
}

int t=m;

for(int i=0;i<m;i++){
int l,r;
cin>>l>>r;
int freq[101]=0;
for(int j=l-1;i<=r-1;i++){
freq[arr[i][j]]++;
}

int maxm=0;
int ans=0;
for(int i=0;i<101;i++)
{
if(maxm<freq[i]){
maxm=freq[i]; //maxm is the maximum freq
ans=i; //ans is the the wickets count that has maximum freq
}
}
cout<<ans <<endl;
}

D)

#include<bits/stdc++.h>
using namespace std;
int prime[1001];

int div[1001];
void DivisorCount()
{

// finding count of divisors for every number till 1001 so that the algo works
for any number upto 1000
for (int i = 1; i < 1001; i++) {
for (int j = i; j < 1001; j += i) {
divi[j]++;
}
}
}

void SieveOfEratosthenes()
{

for (int i = 0; i < 1001; i++)


prime[i] = 1;

// 0 and 1 is not prime


prime[0] = prime[1] = 0;

for (int p = 2; p * p < 1001; p++) {

// If prime[p] is not changed, then it is a prime


if (prime[p] == 1) {

// Update all multiples of p greater than or equal to the square of it


numbers which are multiple of p and are less than p^2 are already been
marked.
for (int i = p * p; i < 1001; i += p)
prime[i] = 0;
}
}
}

int main(){
DivisorCount(); //pre computation to count divisors of all numbers till 1000
SieveOfEratosthenes(); //pre computation to check if number till 1000 is prime
or not
int n,q; //n is number of elements in array ,q is number of queries
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int l,r;
while(q--){
cin>>l>>r;
int cnt;
for(int i=l;i<=r;i++){
if (prime[div[i]] == 1) {
cnt++; //counting no of elements in the given range having
count of divisors as prime
}
}
cout<<cnt<<endl;
}
return 0;
}

You might also like