[go: up one dir, main page]

0% found this document useful (0 votes)
25 views13 pages

Arrays.dsa

The document contains multiple coding problems related to algorithms and data structures, each with a specific task such as tracking stock behavior, converting Martian numbers, reversing arrays, modifying Boolean matrices, mixing colors, sorting arrays in a waveform, creating seating arrangements, counting square integers, and finding pairs with a given sum. Each problem includes a C++ code snippet that implements the solution. The problems are aimed at helping students improve their programming skills in preparation for examinations.

Uploaded by

jayvishal0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

Arrays.dsa

The document contains multiple coding problems related to algorithms and data structures, each with a specific task such as tracking stock behavior, converting Martian numbers, reversing arrays, modifying Boolean matrices, mixing colors, sorting arrays in a waveform, creating seating arrangements, counting square integers, and finding pairs with a given sum. Each problem includes a C++ code snippet that implements the solution. The problems are aimed at helping students improve their programming skills in preparation for examinations.

Uploaded by

jayvishal0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.Rigesh is an electronic shop owner.

Since the number of products he is selling is increasing day by


day we would like to keep track of the buying and selling behavior in his shop

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;

};

void stockBS(int arr[], int n)

if(n==1) //only one element in array

return;

int count = 0; //count of solution pairs

struct interval sol[n/2 + 1];

int i=0;

while(i < n-1)

{ //compare present ele. with next

while((i < n-1) && (arr[i+1] <= arr[i]))

i++;

if(i == n - 1)

break;
sol[count].buy = i++; // index of minima

// compare to previous ele.

while((i < n) && (arr[i] >= arr[i-1]))

if(arr[i]>arr[i-1])

i++;

sol[count].sell = i - 1;

count++;

for(i = 0; i < count; i++)

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];

for(i = 0; i < n; i++)

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>

using namespace std;

void print(int number)

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;

cout<<"char buf[] buf[i++]='R'; while(n>=10)";

3. Sajid is a first year student in reputed institution.

Although he scored well in many subjects, he did not an expert in Algorithms.

But Sajid’s computer examination is scheduled for next week.


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 reverse the
array in C Programming Language.

Can you help him ?

Coding:

#include <iostream>

using namespace std;

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;

4. Tina has given a Boolean matrix mat[P][Q] of size P X Q to Laaysa,

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>

using namespace std;

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>

typedef long long unsigned LLU;

LLU min_smoke[100][100];

int color[100][100];

LLU smoke(int n){

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++){

LLU sm = min_smoke[i][j] + min_smoke[j+1][k] + color[i][j]*color[j+1][k];

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;

7. Saravanan with his friends going to the theatre for a movie.

The seating arrangement is triangular in size.

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>

using namespace std;

int main()

int i,N,c;
cin>>N;

for(i=1; i<=N; i++)

if(i%2==0)

c=2;

else

c=1;

for(int j=1;j<=i;j++)

cout<<c<<" ";

c+=2;

cout<<endl;

return 0;

8. Caleb likes to challenge Selvan’s math ability.

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:

A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25

Coding:

#include <bits/stdc++.h>

using namespace std;

int perfectSquares(float l,float r)


{

int count=0;

for (int i=l;i<=r;i++){ if (sqrt(i)==(int)sqrt(i))

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;

9. Malar is a First year student in reputed institution.

Although he scored well in many subjects, he did not an expert in Algorithms.

But malar’s computer examination is scheduled for next week.

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.

Can you help him ?


Coding:

#include <bits/stdc++.h>

using namespace std;

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;

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

for(int j=i+1; j<n; j++)

if(array[i]+array[j]==num)

cout<<"["<<array[i]<<" "<<array[j]<<"]\n";

count++;

cout<<"Total Number of Pairs:"<<count;

return 0;
}

You might also like