Assignment 2 19BCE10140
Assignment 2 19BCE10140
Assignment 2 19BCE10140
Slot: A21+A22+A23
1. Spiral Pattern
CODE:
#include<bits/stdc++.h>
#define dd double
#define R 100
#define C 100
int i, k = 0, l = 0;
k++;
for (i = k; i < m; ++i)
n--;
if ( k < m)
m--;
if (l < n)
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 dd double
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.
CODE:
#include<bits/stdc++.h>
#define dd double
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++){
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 dd double
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 dd double
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: