-
Notifications
You must be signed in to change notification settings - Fork 0
/
100_hackerrank.cpp
561 lines (501 loc) · 11 KB
/
100_hackerrank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* QUIZ 1 ->Given an array of integers, calculate the ratios of its elements
that are positive, negative, and zero. Print the decimal value of each fraction
on a new line with places after the decimal.
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Explanation
There are 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The proportions of occurrence are positive:3/6=.5 , negative: 2/6 =.333333
and zeros:1/6 = .166667 .
*/
// program ->
/*
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "enter value of n" << endl;
cin >> n;
int arr[n];
cout << "enter the elements" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int positive = 0, negative = 0, zero = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == 0)
{
zero++;
}
if (arr[i] > 0)
{
positive++;
}
if (arr[i] < 0)
{
negative++;
}
}
cout << "positive fraction is : " << (float)positive / n << endl;
cout << "negative fraction is : " << (float)negative / n << endl;
cout << "zero fraction is : " << (float)zero / n;
return 0;
}*/
// size of empty class is always 1 ,bec compi. assigns its address
/*
#include <iostream>
using namespace std;
class A
{
};
int main ()
{
cout<<sizeof(A);
}
*/
/*
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
Output Format
Return the absolute difference between the sums of the matrix's two diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Explanation
The primary diagonal is:
11
5
-12
Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:
4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15
*/
/*
#include<iostream>
#include<cmath>
using namespace std ;
int main ()
{
int n ;
cin>>n;
int arr[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin>>arr[i][j];
}
}
int sum1 = 0 , sum2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i==j)
{
sum1 = sum1 + arr[i][j];
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j == n-i-1)
{
sum2 = sum2 + arr[i][j];
}
}
}
int difference = (sum1-sum2);
cout<<abs(difference);
return 0 ;
}
*/
/*Output Format
Print the size of the vector in the first line and the elements of the vector after the two erase operations in the second line separated by space.
Sample Input
6
1 4 6 2 8 9
2
2 4
Sample Output
3
1 8 9
Explanation
The first query is to erase the 2nd element in the vector, which is 4. Then, modifed vector is {1 6 2 8 9}, we want to remove the range of 2~4, which means the 2nd and 3rd elements should be removed. Then 6 and 2 in the modified vector are removed and we finally get {1 8 9}
*/
/*
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n , pos1 , pos2 , pos3 ;
cin>>n;
vector <int > v(n) ;
for (int i =0 ; i< v.size() ; i++)
{
cin>>v[i]; //1 4 6 2 8 9
}
cin>>pos1; //2
vector <int > :: iterator it = v.begin();
v.erase(it+ (pos1-1)); // 1 6 2 8 9
cin>>pos2>>pos3; //2 4
v.erase(it + (pos2-1), it + (pos3-1)); // 1 8 9
cout<<v.size()<<endl;
for(int i = 0 ; i <v.size() ; i++)
{
cout<<v[i]<<" ";
}
return 0;
}
*/
/*Output Format
The code provided in the code editor will use your class template to add/append elements and give the output.
Sample Input
3
string John Doe
int 1 2
float 4.0 1.5
Sample Output
JohnDoe
3
5.5
Explanation
"Doe" when appended with "John" gives "JohnDoe". 2 added to 1 gives 3, and 1.5 added to 4.0 gives 5.5.
*/
/*
#include <iostream>
#include<string>
using namespace std;
template <class X>
class AddElements
{
X a , b;
public :
X add(X x , X y )
{
a=x , b =y;
return (a+b);
}
};
int main()
{
int n;
cout<<"enter test cases : \n";
cin>>n;
for (int i = 0; i <n; i++)
{
string type ;
cout<<"enter type for test case "<<i+1<<endl;
cin>>type;
if (type == "float")
{
double element1 , element2;
cin>>element1>>element2;
AddElements <float> a1;
cout<<a1.add(element1, element2)<<endl;
}
else if (type == "int")
{
int element1 , element2;
cin>>element1>>element2;
AddElements <int> a1;
cout<<a1.add(element1, element2)<<endl;
}
else if (type == "string")
{
string element1 , element2;
cin>>element1>>element2;
AddElements <string> a1;
cout<<a1.add(element1, element2)<<endl;
}
}
return 0;
}
*/
// Write a C++ program to which replace all the words "dog" with "cat" Go to the editor
// Sample Text: The quick brown fox jumps over the lazy dog. You can assume that the number of characters in a text is less than or equal to 1000.
/*
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
cin.getline(str, 100);
cout << "you entered \n";
cout << str << endl;
for (int i = 0; i < strlen(str); i++)
{
for (int j = i + 1; j < strlen(str); j++)
{
for (int k = i + 2; k < strlen(str); k++)
{
if (str[i] == 'd' && str[j] == 'o' && str[k] == 'g')
{
str[i] = 'c';
str[j] = 'a';
str[k] = 't';
}
}
}
}
cout << "new string is : \n";
cout << str;
return 0;
}
*/
/*
#include<iostream>
#include<cstring>
using namespace std ;
int main ()
{
char str[100];
cout<<"enter string \n";
cin>>str;
for (int i = 0; i !='\0'; i++)
{
cout<<str[i];
}
return 0 ;
}
*/
/*
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100];
cin.getline( str , 100);
cout << "you entered \n";
cout << str << endl;
for (int i = 0; str[i] != '\0' ; i++)
{
for (int j = i + 1; str[j] != '\0' ; j++)
{
for (int k = i + 2; str[k] != '\0'; k++)
{
if (str[i] == 'd' && str[j] == 'o' && str[k] == 'g')
{
str[i] = 'c';
str[j] = 'a';
str[k] = 't';
}
}
}
}
cout << "new string is : \n";
cout << str;
return 0;
}
*/
//arithematic series
/*
#include<iostream>
using namespace std ;
int main ()
{
int n;
cout<<"enter n \n";
cin>>n;
int arr[n];
cout<<"enter series \n";
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
int d = arr[1] - arr[0]; //common difference
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if( (arr[j]-arr[i]) == d )
{
count++; //counts common difference
}
}
}
if (count == n-1) //common difference should be n-1 times
{
cout<<"arithematic series \n";
}
else
{
cout<<"not an arithematic series \n";
}
return 0 ;
}
*/
//geometric series
/*
#include<iostream>
using namespace std ;
int main ()
{
int n;
cout<<"enter n \n";
cin>>n;
int arr[n];
cout<<"enter series \n";
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
int r = arr[1]/arr[0]; //common ratio
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if( (arr[j]/arr[i]) == r )
{
count++; //counts common ratio
}
}
}
if (count == n-1) //common ratio should be n-1 times
{
cout<<"geometric series \n";
}
else
{
cout<<"not an geometric series \n";
}
return 0 ;
}
*/
/*
//to find in ap or gp
#include<iostream>
using namespace std ;
int main ()
{
int n;
cout<<"enter n \n";
cin>>n;
int arr[n];
cout<<"enter series \n";
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
int d = arr[1] - arr[0]; //common difference
int r = arr[1]/arr[0]; //common ratio
int count1 = 0;
int count2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if( (arr[j]-arr[i]) == d )
{
count1++; //counts common difference
}
if( (arr[j]/arr[i]) == r )
{
count2++; //counts common ratio
}
}
}
if (count1 == n-1) //common difference should be n-1 times
{
cout<<"arithematic series \n";
}
else if (count2 == n-1) //common ratio should be n-1 times
{
cout<<"geometric series \n";
}
else
{
cout<<"nor arithematic neither geometric series \n";
}
return 0 ;
}
*/
// Write a program in C++ to find the last prime number occur before the entered number. Go to the editor
// Sample Output:
// Input a number to find the last prime number occurs before the number: 50
// 47 is the last prime number before 50
/*
#include<iostream>
using namespace std ;
int main ()
{
int n ;
cout<<"input a number \n";
cin>>n;
for (int i = n; i >0 ; i--)
{
int count =0;
for (int j = n; j>0; j--)
{
if(i%j==0)
{
count++;
}
}
if (count==2)
{
cout<<i<<" ";
break;
}
}
return 0 ;
}
*/
// . Write a C++ program to check if the first appearance of "a" in a given string is immediately followed by another "a". Go to the editor
// Sample Input:
// "caabb"
// "babaaba"
// "aaaaa"
// Sample Output:
// 1
// 0
// 1
#include<iostream>
using namespace std ;
int check(string s)
{
int count ;
for (int i = 0; i < s.length(); i++)
{
if(s[i] == 'a')
{
count = i;
break;
}
}
return (s[count+1] == 'a' ) ? 1 : 0;
}
int main ()
{
for (int i = 0; i < 3; i++)
{
string str;
cin>>str;
cout << check(str)<<endl;
}
return 0 ;
}