Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment 2 19BCE10140

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Assignment 2

Name: Shekhar Sharma

Reg No: 19BCE10140

Slot: A21+A22+A23

1. Spiral Pattern

Given an integer matrix of size N x N. Traverse it in a spiral form.

CODE:

#include<bits/stdc++.h>

#define ll long long

#define dd double

using namespace std;

const ll MOD = 1e9 + 7;

#define R 100

#define C 100

void spiralPrint(int m, int n, int a[R][C])

int i, k = 0, l = 0;

while (k < m && l < n)

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

printf("%d ", a[k][i]);

k++;
for (i = k; i < m; ++i)

printf("%d ", a[i][n-1]);

n--;

if ( k < m)

for (i = n-1; i >= l; --i)

printf("%d ", a[m-1][i]);

m--;

if (l < n)

for (i = m-1; i >= k; --i)

printf("%d ", a[i][l]);

l++;

int main()

int a[R][C];
int n;

cin>>n;

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

for(int j=0;j<n;j++){

cin>>a[i][j];

spiralPrint(n, n, a);

return 0;

}
OUTPUT:
2. Segregate 0's and 1's

Given a pattern that consists of 0’s and 1’s. Write a program to move all 1’s to the
beginning and 0’s to the end.

CODE:

#include<bits/stdc++.h>

#define ll long long

#define dd double

using namespace std;

const ll MOD = 1e9 + 7;

int main()

ios_base::sync_with_stdio(false);

cin.tie(NULL);

string s;

cin>>s;

// sort(s.begin(),s.end());

// reverse(s.begin(),s.end());

// cout<<s<<endl;

int n = s.size();

char brr[n];

int counter=0;

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

if(s[i]=='1'){

brr[counter]=s[i];

counter++;

}
}

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

if(s[i]=='0'){

brr[counter]=s[i];

counter++;

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

cout<<brr[i];

cout<<endl;

return 0;

}
OUTPUT:
3. Leaders in an array

Given an array of positive integers. Your task is to find the leaders in the array.

Note: An element of an array is a leader if it is greater than or equal to all the


elements to its right side. Also, the rightmost element is always a leader.

CODE:

#include<bits/stdc++.h>

#define ll long long

#define dd double

using namespace std;

const ll MOD = 1e9 + 7;

int main()

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin>>t;

while(t--){

int n;

cin>>n;

int arr[n];

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

cin>>arr[i];
}

for(int i=0;i<n-1;i++){

int check_no = *max_element(arr+i+1,arr+n);

if(arr[i]>=check_no){

cout<<arr[i]<<" ";

cout<<arr[n-1]<<endl;

return 0;

}
OUTPUT:
4. count 1's in sorted binary array

Given a sorted binary array in non-increasing order. Write a program to count the
number of 1’s in it.

CODE:

#include<bits/stdc++.h>

#define ll long long

#define dd double

using namespace std;

const ll MOD = 1e9 + 7;

int main()

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int n;

cin>>n;

int arr[n];

int count=0;

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

cin>>arr[i];

if(arr[i]==1){

count++;

}
cout<<count<<endl;

return 0;

}
OUTPUT:
5. Bitonic Generator Sort

Given an array of N distinct numbers, the task is to sort all even-placed numbers in
increasing and odd-placed numbers in decreasing order. The modified array should
contain, all sorted even-placed numbers followed by reverse sorted odd-placed
numbers.

CODE:

#include<bits/stdc++.h>

#define ll long long

#define dd double

using namespace std;

const ll MOD = 1e9 + 7;

int main()

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin>>t;

while(t--){

int n;

cin>>n;

int arr[n];

vector<int> odd;
vector<int> even;

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

cin>>arr[i];

if(i%2==0){

even.push_back(arr[i]);

}else{

odd.push_back(arr[i]);

sort(even.begin(),even.end());

sort(odd.begin(),odd.end(), greater<int>());

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

if(i<even.size()){

arr[i]=even[i];

}else{

if(n%2==0){

arr[i]=odd[i-odd.size()];

}else{

arr[i]=odd[i-odd.size()-1];

}
}

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

cout<<arr[i]<<" ";

cout<<endl;

return 0;

}
OUTPUT:

You might also like