Arrays.dsa
Arrays.dsa
So given the cost of stock on each day in an array A[] of size N. Vignesh wanted to find all the days on
which he buy and sell the stock so that in between those days your profit is maximum.
Coding:
#include <stdio.h>
struct interval
int buy;
int sell;
};
return;
int i=0;
i++;
if(i == n - 1)
break;
sol[count].buy = i++; // index of minima
if(arr[i]>arr[i-1])
i++;
sol[count].sell = i - 1;
count++;
printf("(%d %d)",sol[i].buy,sol[i].sell);
return;
int main()
int t,i,n;
scanf("%d",&t);
while(t)
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
}
if(n==4)
printf("No Profit");
else
stockBS(arr, n);
printf("\n");
t--;
return 0;
2. How many Y’s did a Roman Centurion make a day in cold hard Lira? About a C’s worth! Turns out,
Martians gave Rome the idea for their number system. Use the conversion charts below to help
translate some Martian numbers!
Note, that unlike the Roman Numerals, Martian Numerals reuse symbols to mean different values. B
can either mean ‘1’ or ‘100’ depending on where it appears in the number sequence.
Coding:
#include <bits/stdc++.h>
int num[]={1,4,5,9,10,40,50,90,100,400,500,900,1000};
string sym[]={"B","BW","W","BK","Z","ZP","P","ZB","B","BG","G","BR","R"};
int i=12;
while(number>0)
int div=number/num[i];
number=number%num[i];
while(div--)
cout<<sym[i];
i--;
int main()
int number,n2,n3,n4,n5;
cin>>number>>n2>>n3>>n4>>n5;
print(number);
cout<<endl;
print(n2);
cout<<endl;
print(n3);
cout<<endl;
print(n4);
cout<<endl;
print(n5);
return 0;
He collected previous year’s questions. One of the repeated questions is you need to reverse the
array in C Programming Language.
Coding:
#include <iostream>
int main()
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n/2;i++)
int temp;
temp=arr[i];
arr[i]=arr[n-1-i];
arr[n-1-i]=temp;
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
She wants to modify it such that if a matrix cell mat[m][n] is 1 (or true) then make all the cells of
m_th row and nth column as 1. Can you help Laaysa?
Coding:
#include <bits/stdc++.h>
int main()
int r,c;
cin>>r>>c;
int arr[r][c];
int arrTemp[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>arr[i][j];
arrTemp[i][j]=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(arr[i][j]==1){
for(int i1=0;i1<r;i1++){
arrTemp[i1][j]=1;
for(int i1=0;i1<c;i1++){
arrTemp[i][i1]=1;
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<arrTemp[i][j];
if(j!=c-1)cout<<' ';
cout<<endl;
return 0;
printf("for(m=0;m<r;m++)");
5. Umesh has n mixtures in front of him, arranged in a row. Each mixture has one of 100 different
colors (colors have numbers from 0 to 99).
He wants to mix all these mixtures together. At each step, he is going to take two mixtures that
stand next to each other and mix them together, and put the resulting mixture in their place.
Coding:
#include <stdio.h>
LLU min_smoke[100][100];
int color[100][100];
int i,j,l;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
min_smoke[i][j]=10000000000000000;
}
for(i=0;i<n;i++){
min_smoke[i][i] = 0;
for(l=2;l<=n;l++){
int e = n-1;
for(i=0;i<=e;i++){
int k = i+l-1;
for(j=i;j<k;j++){
int cl = (color[i][j]+color[j+1][k])%100;
if(sm<min_smoke[i][k]){
min_smoke[i][k] = sm;
color[i][k] = cl;
return min_smoke[0][n-1];
int main(void){
int n;
while(scanf("%d",&n)!=EOF){
int i;
for(i=0;i<n;i++)
scanf("%d",&(color[i][i]));
}
printf("%llu\n",smoke(n));
return 0;
printf("scount[100][100] colours[100]");
6. Ravi participated in TCS off campus interview at reputed institution, one of the technical question
he has to complete with in the given time, where you need to sort the array in the waveform. There
might be multiple possible output of the program. The following pattern output is appreciated.
Coding:
#include <stdio.h>
int main()
int i,j,temp,n;
scanf("%d",&n);
int array[n];
for(i=0;i<n;i++)
scanf("%d",&array[i]);
// pattern(array,n);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(array[i]>array[j])
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
for(j=0;j<n;j+=2)
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
printf("%d %d ",array[j],array[j+1]);
//for(j=0;j<n;j++)
if(0)
printf("for(int i=0;i<n;i++)");
return 0;
Theatre staffs insisted the audience to sit in odd row if the seat number is odd and in even row if the
seat number is even.
But the instruction is very confusing for saravanan and his friends.
So help them with the seating layout so that they can sit in correct seats.
Coding:
#include <iostream>
int main()
int i,N,c;
cin>>N;
if(i%2==0)
c=2;
else
c=1;
for(int j=1;j<=i;j++)
cout<<c<<" ";
c+=2;
cout<<endl;
return 0;
He will provide a starting and ending value that describes a range of integers, inclusive of the
endpoints.
Selvan must determine the number of square integers within that range.
Note:
Coding:
#include <bits/stdc++.h>
int count=0;
count+=1;
return count;
//Driver code
int main()
int q,l,r;
cin>>q;
while(q--){
cin>>l>>r;
int result=perfectSquares(l,r);
cout<<result<<endl;
return 0;
As per the blueprint, many questions would come from the Arrays topic.
He collected previous year’s questions. One of the repeated questions is you need to find the pairs in
Array with given sum.
#include <bits/stdc++.h>
int main()
int n;
cin>>n;
int array[n];
int i;
for(i=0;i<n;i++)
cin>>array[i];
int num,count=0;
cin>>num;
vector<int>v;
if(array[i]+array[j]==num)
cout<<"["<<array[i]<<" "<<array[j]<<"]\n";
count++;
return 0;
}