Vous êtes sur la page 1sur 9

Rishabh Chhabra, O7, 13116055

Ans 1
#include<iostream>
using namespace std;

void swap (int* a, int* b) {


int temp = *a;
*a=*b;
*b=temp;
}
int main() {
//Any random array a and b
int a[] = {5,7,3,6,8,1,4,7,9,3,5,8,0,3,5,6,7,6,25,54,7};
int n = sizeof(a)/sizeof(int);
int b[n];
for (int i=0;i<n-1;i++) {
if(i%2==0) {
if(a[i]>a[i+1]) swap(&a[i],&a[i+1]); //Because an even element
should be less than an element to its right
b[i]=a[i];
}
else {
if(a[i]<a[i+1]) swap(&a[i],&a[i+1]); //Because an odd element
should be greater than an element to its right
b[i]=a[i];
}
}
b[n-1]=a[n-1];
for(int i=0;i<n;i++) {
cout << b[i] <<endl; //Print the wiggly array
}
return 0;
}

Rishabh Chhabra, O7, 13116055

Ans 2
#include<iostream>
using namespace std;
int main () {
//Array of numbers and its size
int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20,25,28,29,34,37,48};
int n = sizeof(a)/sizeof(int);
//Take input from the user of the sum to be found
int sum;
cin >> sum;
int j=0;
int k=n-1;
while(k>j) {
if(a[j]+a[k]>sum) k--;
else if(a[j]+a[k]<sum) j++;
else {
cout << "Found, the numbers are" << a[j] << " and " <<a[k] <<
endl;
break;
}
}
return 0;
}

Ans 3
#include<iostream>

Rishabh Chhabra, O7, 13116055


using namespace std;
void swap (int* a, int* b) {
int temp = *a;
*a=*b;
*b=temp;
}
int main() {
//Any random binary array a
int a[] = {0,1,0,1,0,1,0,1};
int n = sizeof(a)/sizeof(int);
int numOfZero;
for(int i=0;i<n;i++) {
if(a[i]==0) numOfZero++;
}
for(int i=0;i<numOfZero;i++) {
a[i]=0;
}
for(int i=numOfZero;i<n;i++) {
a[i]=1;
}
for(int i=0;i<n;i++) {
cout << a[i] <<endl;
}
return 0;
}

Ans 4
#include<iostream>
using namespace std;
void swap (int* a, int* b) {

Rishabh Chhabra, O7, 13116055


int temp = *a;
*a=*b;
*b=temp;
}
int main() {
int* a;
int n;
cin >>n;
a = new int[n];
int j = n-1;
//Input of values
for (int i =0;i<n;i++) {
cin >> a[i];
}
//Reversing the array
for (int i=0;i<n/2;i++) {
swap(&a[i],&a[j]);
j--;
}
//Printing the reversed array
for (int i=0;i<n;i++) {
cout << a[i] <<endl;
}
}

Ans 5
#include<iostream>
using namespace std;
int num = 0; //number of combinations

Rishabh Chhabra, O7, 13116055


void combinations (int v[], int start, int n, int k, int r) {
int i;
/* k here takes care of the maximum number of digits in v.
* if k > r, then the v is complete and we can use it.
*/
if (k > r) {
for (i=1; i<=r; i++) cout << v[i] << " ";
cout << endl;
num++;
return;
}
/* the for loop below adds digits to v in the kth position, recursively.
* When k becomes greater than r, it returns to the loop and adds the
* next number to the kth position
*/
for (i=start; i<=n; i++) {
v[k] = i;
/* recursively generate combinations of integers
* from i+1..n
*/
combinations (v, i+1, n, k+1, r);
}
}
int main () {
cout << "Enter n of nCr" << endl;
int n;
cin >> n;
cout << "Enter r of nCr" <<endl;
int r;
cin >> r;
int v[n];
/* generate all combinations of n elements taken
* r at a time, starting with combinations containing 1

Rishabh Chhabra, O7, 13116055


* in the first position, then moving onto combinations
* containing 2 in the first position and so on.
*/
combinations (v, 1, n, 1, r);
cout << "The number of combinations are " << num <<endl;
return 0;
}

Ans 6
#include<iostream>
using namespace std;
int main () {
int n;
cin >> n;
int *a = new int[n];
a[0]=0;
int j = 1;

Rishabh Chhabra, O7, 13116055


int num=0;
for (int i=1;i<n;i++) {
int temp = j;
for (int k=1;;k*=10) {
if (temp%5 == 0 && temp%10!=0) {
i--;
break;
}
if(temp/k == 0) {
a[i] = j;
num++;
break;
}
temp = temp/k;
}
j++;
if (j>n) break;
}

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


cout << a[i] <<endl;
}
return 0;
}

Ans 7
#include<iostream>
#include<string.h>
using namespace std;
string insert (string str, char c, int k, int n) {
string newStr = "";
for (int i=0;i<k-1;i++) {
newStr += str[i];
}

Rishabh Chhabra, O7, 13116055


newStr += c;
for(int i = k;i<n+1;i++) {
newStr += str[i-1];
}
return newStr;
}
int main() {
string str = "DTA STRCURES IT ROKE";
str = insert(str,'U',8,20);
str = insert(str,'A',2,21);
str = insert(str,'T',11,22);
str = insert(str,'I',17,23);
str = insert(str,'O',22,24);
str = insert(str,'R',24,25);
str = insert(str,'E',26,26);
cout << string(str) <<endl;
return 0;
}

Ans 8
The space complexity of the function is O(nlogn).

Ans 9
#include<iostream>
using namespace std;
int main() {
//Assuming the array is sorted
int a[] = {3,3,5,5,5,7,8,8,9,9,10,10,10,10,11,12,13,13,13};
int n = sizeof(a)/sizeof(int);
int j=0;

Rishabh Chhabra, O7, 13116055


for(int i=0;i<n;i++) {
if (a[i]!=a[i+1]) {
a[j] = a[i];
j++;
}
}
for(int i=j;i<n;i++) {
a[i]=NULL;
}
for (int i=0;i<n;i++) {
cout << a[i] <<endl;
}
return 0;
}

Vous aimerez peut-être aussi