forked from GDSC-JSCOE/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntermediate.txt
More file actions
428 lines (360 loc) · 11.1 KB
/
Intermediate.txt
File metadata and controls
428 lines (360 loc) · 11.1 KB
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
Example :
Solution :
# Python
age=input(int("Age : "))
print(age);
Maintainer : Gaurav-2803
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.
Solution :
2. Write function that reverses a array, preferably in place.
Solution :
#Java
static void reverseArray(int[] intArray,int size){
int i, temp;
for (i = 0; i < size/ 2; i++) {
//store first index value at temp
temp = intArray[i];
//store lastIndex-i-1 value in i'th Index
intArray[i] = intArray[size - i - 1];
//store temp value at lastIndex-i-1
intArray[size - i - 1] = temp;
//Loop will run till the mid position of given array
}
//printing the reversed Array
System.out.println(Arrays.toString(intArray));
}
3. Write a function that tests whether a string is a palindrome.
Solution :
class Main{
public static void main(String[] args){
String str="abba";
String str1="";
StringBuilder ans=new StringBuilder();
ans.append(str);
ans.reverse;
str1=ans.toString();
if(str==str){
System.out.print("Palindromic");
}
Systme.out.print("Not palindromic");
}
}
#CPP
class Solution{
public:
int isPalindrome(string S)
{
int ch=0;
string st;
while(S[ch]!='\0'){
st+=S[ch];
ch++;
}
string newst;
for(int i=ch-1;i>=0;i--){
newst+=S[i];
}
if(newst==st){
return 1;
}
return 0;
}
};
4. Write a function that tests whether a number is a palindrome.
Solution :
#Python
# Python program to check if number is Palindrome
def isPalindrome(n): #user-defined function
# calculate reverse of number
temp=n
reverse = 0
reminder = 0
while(n != 0):
remainder = n % 10
reverse = reverse * 10 + remainder
n = int(n / 10)
if(temp== reverse):
return 1
else:
return 0
5. Write a function on_all that applies a function to every element of a list. Use it to print the first twenty perfect squares. The perfect squares can be found by multiplying each natural number with itself. The first few perfect squares are 1*1= 1, 2*2=4, 3*3=9, 4*4=16. Twelve for example is not a perfect square because there is no natural number m so that m*m=12.
Solution :
6. Write a function that combines two arrays by alternatingly taking elements, e.g. [a,b,c], [1,2,3] → [a,1,b,2,c,3].
Solution :
#CPP
vector<int> mergeArr(int arr1[],int arr2[],m,n){
vector<int> ans(m+n);
int a1=0;
int a2=0;
for(int i=0;i<(m+n);i++){
if(i%2==0){
ans.push_back(arr1[a1];
a1++;
}else{
ans.push_back(arr2[a2]);
a2++;
}
}
return ans;
}
7. Write a function that rotates a array by k elements. For example [1,2,3,4,5,6] rotated by two becomes [3,4,5,6,1,2]. Try solving this without creating a copy of the array. How many swap or move operations do you need?
Solution :
class Main
{
public static void rightRotateByOne(int[] A)
{
int last = A[A.length - 1];
for (int i = A.length - 2; i >= 0; i--) {
A[i + 1] = A[i];
}
A[0] = last;
}
public static void rightRotate(int[] A, int k)
{
if (k < 0 || k >= A.length) {
return;
}
for (int i = 0; i < k; i++) {
rightRotateByOne(A);
}
}
public static void main(String[] args)
{
int[] A = { 1, 2, 3, 4, 5, 6, 7 };
int k = 3;
rightRotate(A, k);
System.out.println(Arrays.toString(A));
}
}
8. Write a function that takes a number and returns a array of its digits. So for 2342 it should return [2,3,4,2].
Solution :
#Java
static void getDigit(int number){
ArrayList<Integer> arr=new ArrayList<Integer>();
// Printing the last digit of the number
while (number > 0) {
// Finding the remainder (Last Digit)
int remainder = number % 10;
// Printing the remainder/current last digit
arr.add(remainder);
// Removing the last digit/current last digit
number = number / 10;
}
Collections.reverse(arr);
System.out.println(arr);
}
9. Write functions that add, subtract, and multiply digits of two numbers.
Solution :
#python
def add(a,b):
return a+b;
def sub(a,b):
return a-b;
def multiply(a,b):
return a*b;
f1=add(5,5.2)
f2=sub(5,4.2)
f3=multiply(5,5.2)
print("Addition: ", f1)
print("substraction: ", f2)
print("multiplication: ", f3)
10. Implement binary search.
Solution :
//java
class Solution {
int binarysearch(int arr[], int n, int k){
int start=0;
int end=n-1;
while(start<=end){
int mid =start +(end-start)/2;
if(arr[mid]<k){
start=mid+1;
}
if(arr[mid]>k){
end=mid-1;
}
if(arr[mid]==k) {
return mid;
}
}
return -1;
}
}
=======
#Java
class binarySearch {
public int search(int[] nums, int target) {
int key, start = 0, end = nums.length - 1;
while (start <= end) {
key = (start+end)/2
if (nums[key] == target){
return key;
}
if (target < nums[key]){
end = key - 1;
}
else {
left = key + 1;
}
}
return -1;
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
binarySearch(arr,30);
}
}
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1
# Initial list1
list1 = [12, 24, 32, 39, 45, 50, 54]
n = 45
# Function call
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list1")
11. Write a function that takes a array of strings an prints them, one per line, in a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"] gets printed as:
*********
* Hello *
* World *
* in *
* a *
* frame *
*********
Solution :
12. Print the following pattern without using print in-built function.
i. ii. iii. iv.
* *** * ***
** ** ** **
*** * *** *
v. ii. iii. iv.
1 123 1 123
23 45 23 45
456 5 456 6
Solution :
13. Convert Age -> Days (Consider leap years in between) take year of birth as input also.
Solution :
14. Create a function that finds the maximum range of a triangle's third edge, where the side lengths are all integers. Note. (side1 + side2) - 1 = maximum range of third edge.
Solution : // C++ implementation of the approach
#include <iostream>
using namespace std;
// Function to find the minimum and the
// maximum possible length of the third
// side of the given triangle
void find_length(int s1, int s2)
{
// Not a valid triangle
if (s1 <= 0 || s2 <= 0) {
cout << -1;
return;
}
int max_length = s1 + s2 - 1;
int min_length = max(s1, s2) - min(s1, s2) + 1;
// Not a valid triangle
if (min_length > max_length) {
cout << -1;
return;
}
cout << "Max = " << max_length << endl;
cout << "Min = " << min_length;
}
// Driver code
int main()
{
int s1 = 8, s2 = 5;
find_length(s1, s2);
return 0;
}
15. Write a function that takes number K where is smaller than size of array, we need to find the Kth smallest and largest elementin the given array.It is given that all array elements are not distinct and If not present return -1.
Solution :
16. Write a function that sort array of 0s, 1s & 2s (inbuilt sort functions are not allowed)
Solution : # Python program to sort an array with
# 0, 1 and 2 in a single pass
# Function to sort array
def sort012(a, arr_size):
lo = 0
hi = arr_size - 1
mid = 0
# Iterate till all the elements
# are sorted
while mid <= hi:
# If the element is 0
if a[mid] == 0:
a[lo], a[mid] = a[mid], a[lo]
lo = lo + 1
mid = mid + 1
# If the element is 1
elif a[mid] == 1:
mid = mid + 1
# If the element is 2
else:
a[mid], a[hi] = a[hi], a[mid]
hi = hi - 1
return a
# Function to print array
def printArray(a):
for k in a:
print(k, end=' ')
# Driver Program
arr = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
arr_size = len(arr)
arr = sort012(arr, arr_size)
printArray(arr)
17. Given Unsorted array check whether elements are in Arithmetic Progression (Differnce between every 2 consecutive is same) if not return -1.
Example : 1,2,3,4,5 are in A.P
Solution :
18. Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.
Solution :
19. Given a string S,of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line.
I/P -> Gaurav O/P -> Gua arv
Solution :
20 .Sort an array of 0s, 1s and 2s (Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.)
/// java
class Solution
{
public static void sort012(int arr[], int n)
{
// code here
int zero =0;
int one =0;
int two =0;
for(int i =0; i<n;i++){
if (arr[i]==0){
zero++;
}
else if(arr[i]==1){
one++;
}
else{
two++ ;
}
}
for(int i=0;i<n;i++){
if (zero>0){
arr[i]=0;
zero--;
}
else if(one>0){
arr[i]=1;
one--;
}
else{
arr[i]=2;
}
}
}
}