-
Notifications
You must be signed in to change notification settings - Fork 96
/
Intermediate.txt
1808 lines (1530 loc) · 39.9 KB
/
Intermediate.txt
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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 :
#Python
import getpass
print("THE GUESSING GAME!\n")
no = int(getpass.getpass("Enter any Number for Friend to Guess: "))
i = 1
while (True):
temp = int(input("\nEnter Your Guess "))
if temp == no:
print("Correct Guess!")
print("You WON!! \n")
if i==1:
print("You did it in", i, "Guess! Yay!")
elif i>=2 and i<=4:
print("You did it in only", i, "Guesses!")
elif i>=5 and i<=8:
print("You did it in", i, "Guesses!")
else:
print("You required", i,"Guesses :(")
break
elif temp<no:
print("Your guess is Less!")
print("Try Again. \n")
else:
print("Your guess is Large!")
print("Try Again. \n")
if i==1:
prev_no=temp
i+=1
elif prev_no==temp:
pass
else:
prev_no=temp
i += 1
#CPP
//Solution Begins
#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;
void guess(int num){
cout << "GUESS THE NUMBER!!\n\n";
int i=1;
int a=1;
while (a){
int temp;
cout<<"\nEnter your guess : ";
cin >> temp;
if (temp==num){
cout << "Correct Guess!\nYou WON!!\n";
if (i==1){
cout << "You did it in "<< i<< " Guess! Yay!";
}
else if (i>=2 && i<=4){
cout <<"\nYou did it in only "<< i<< " Guesses!";
}
else if (i>=5 && i<=8)
cout<<"\nYou did it in "<<i<<" Guesses!";
else
cout <<"\nYou required "<<i<<" Guesses :(";
a=0;
}
else if (temp<num){
cout << "Your guess is Less!\n";
cout << "Try Again. \n";
}
else{
cout << "Your guess is Large!\n";
cout << "Try Again. \n";
}
int prev_no;
if (i==1){
prev_no=temp;
i+=1;
}
else if (prev_no==temp){
continue;
}
else{
prev_no=temp;
i += 1;
}
}
}
int main(){
cout << "THE GAME BEGINS!!\n";
cout << "Enter any Number for your friend to guess :- ";
int b;
cin >> b;
system("clear");
cout << "GAME IS ON!!\n\n";
guess(b);
}
2. Write function that reverses a array, preferably in place.
Solution :
#Python
def reverseArray(arr):
start, end = 0, len(arr)-1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
#CPP
//Function
void reverse_array(vector<int> &arr)
{
//Calculate size of array
int n=arr.size();
//loop will run till the mid position of given array
for(int i=0;i<n/2;i++)
{
//swap elements
swap(arr[i],arr[n-i-1]);
}
return;
}
#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 Solution {
int isPalindrome(String S) {
char [] ans=new char[S.length()];
for(int i=0;i<S.length();i++){
ans[i]=S.charAt(i);
}
int start=0;
int end=S.length()-1;
while(start<end){
if(ans[start] != ans[end]){
return 0;
}
start++;
end--;
}
return 1;
}
};
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(str1==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;
}
};
#solved Q no 3 using python
#python
my_str =input("Enter string: ")
my_str = my_str.casefold()
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
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
n=str(n)
l=[]
for i in n:
l.append(i)
rev_l=l[::-1]
if l==rev_l:
return 1
else:
return 0
# CPP
class Solution{
public:
int isPalindrome(int n) {
int rev = 0;
int temp = n;
while(temp != 0) {
int rem = temp % 10;
rev = rev * 10 + rem;
temp = temp / 10;
}
if(rev == n){
return 1;
}
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[],int m,int 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;
}
#Java
import java.util.Scanner;
public class Merge{
static Scanner scanner = new Scanner(System.in);
public static String[] merge(String[] list1, String[] list2){
String[] pattern = new String[list1.length+list2.length];
boolean flag = false;
int count1 = 0, count2 = 0, count = 0;
for (int i = 0; i < pattern.length; i++) {
if (!flag){
pattern[count] = list1[count1];
count1++;
count++;
flag = true;
}
else {
pattern[count] = list2[count2];
count2++;
count++;
flag = false;
}
}
return pattern;
}
public static void getData(String list[]){
for (int i = 0; i < list.length; i++) {
System.out.println("Enter element no. "+(i+1) +" : ");
list[i] = scanner.next();
}
}
public static void main(String[] args) {
System.out.print("Enter the number of elements of list 1 :");
int n1 = scanner.nextInt();
String list1[] = new String[n1];
getData(list1);
System.out.print("Enter the number of elements of list 2 :");
int n2 = scanner.nextInt();
String list2[] = new String[n2];
getData(list2);
String[] merged = merge(list1,list2);
for (int i = 0; i < merged.length; i++) {
System.out.print(merged[i]+ " ");
}
}
}
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 :
//java
class Solution
{
//Function to rotate an array by d elements in counter-clockwise direction.
static void rotateArr(int arr[], int d, int n)
{
int []ans=new int[n];
int i=0;
while(i<n-d){
ans[i]=arr[i+d];
i++;
}
int j=0;
while(j<d){
ans[i+j]=arr[j];
j++;
}
for(int k=0;k<n;k++){
arr[k]=ans[k];
}
}
}
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 :
#CPP
//Function
vector<int> digit_to_vector(int n)
{
//STL to convert Integer to String
string s=to_string(n);
vector<int> ans;
//Interating string
for(int i=0;i<s.size();i++)
ans.push_back(s[i]-'0');
return ans;
}
#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 :
p=input("words?")
def frame(words):
size = len(max(words, key=len))
print('*' * (size + 4))
for word in words:
print(f'* {word} *')
print('*' * (size + 4))
frame(p.split(" "))
12. Print the following pattern without using print in-built function.
i. ii. iii. iv.
* *** * ***
** ** ** **
*** * *** *
v. vi. vii. viii.
1 123 1 123
23 45 23 45
456 5 456 6
Solution :
// i)
void printPat(int n)
{
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j<=i){
cout<<"*"<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// ii)
void printPat(int n)
{
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j<=(n/2-i-1)){
cout<<"*"<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// iii)
void printPat(int n)
{
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j>=(n/2-i-1)){
cout<<"*"<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// iv)
void printPat(int n)
{
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j>=i){
cout<<"*"<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
//v)
void printPat(int n)
{
int count=1;
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j<=i){
cout<<count++<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// vi)
void printPat(int n)
{
int count=1;
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j<=(n/2-i-1)){
cout<<count++<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// vii)
void printPat(int n)
{
int count=1;
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j>=(n/2-i-1)){
cout<<count++<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
// viii)
void printPat(int n)
{
int count=1;
for(int i=0;i<n/2;i++){
for(int j=0;j<n/2;j++){
if(j>=i){
cout<<count++<<" ";
}else{
cout<<" ";
}
}
cout<<"\n";
}
}
13. Convert Age -> Days (Consider leap years in between) take year of birth as input also.
Solution :
#python
from datetime import datetime, timedelta
my_DOB = datetime(2002, 4, 30)
todays_date = datetime.today()
age_in_days = todays_date - my_DOB
print("The age in days and time for the Given DOB = ", age_in_days)
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 :
//cpp
int findnum(int arr[],int n){
int k;
cin>>k;
if(k>=n)
return -1;
sort(arr,arr+n);
cout<<arr[k-1];
cout<<arr[n-k];
return 0;
}
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 :
#Python
class Solution:
def checkIsAP(self, arr, n):
arr.sort()
diff=arr[1]-arr[0]
for i in range(2,n):
if(arr[i]-arr[i-1]!=diff):
return 0
return 1
//cpp
int ap(int arr[],int n){
sort(arr,arr+n);
int d=arr[1]-arr[0];
for(int i=1;i<n;i++){
if(arr[i]-arr[i-1])!=d){
return -1;
}
}
return 1;
}
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 :
#Python
def binarySearch(arr, low, high, k):
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == k:
return mid
elif arr[mid] < k:
low = mid + 1
else:
high = mid - 1
return low
//java
int binarysearch(int arr[], int n, int k) {
int start=0;
int end=n-1;
int ans=0;
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;
}
ans=mid;
}
return ans+1;
}
#CPP
class Solution{
public:
int binarysearch(int arr[], int n, int k){
int first=0;
int last=n-1;
if(arr[0]>k){
return 0;
}
int mid=(first+last)/2;
while(first<last){
if(arr[mid]==k){
return mid;
}
if(arr[first]==k){
return first;
}
if(arr[last]==k){
return last;
}
if(k<arr[mid]){
last=mid-1;
mid=(first+last)/2;
}else{
first=mid+1;
mid=(first+last)/2;
}
}
return mid+1;
}
};
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 :
class Main{
public static void main(String[] args){
String s="SlamanBhaijaan"
String ans="";
for(int i=0; i<s.length(); i++){
if(i%2==0){
ans=ans+s.charAt(i);
}
}
ans=ans+" ";
for(int i=0; i<s.length(); i++){
if(i%2!=0){
ans=ans+s.charAt(i);
}
}
return ans;
}
solution;
#python
n=int(input("enter the length of string"))
s=input("enter the string")
eve=[]
odd=[]
result=[]
for i in range (0,n):
if i%2==0:
eve.append(s[i])
else:
odd.append(s[i])
d=' '.join(eve)
e=' '.join(odd)
result.append(d)
result.append(e)
print(' '.join(result))
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;
}
}
}
}
21. Remove duplicates in small prime array
Solution :
class Solution
{
ArrayList<Integer> removeDuplicate(int arr[], int n)
{
// code here
ArrayList<Integer>a=new ArrayList<>();
for(int i=0;i<arr.length;i++){
if(!a.contains(arr[i])){
a.add(arr[i]);
}
}
return a;
}
}
22. Print all permutation of String both iterative and Recursive way?
Solution :
#CPP
//Using Recursion//
void permute(string str, int l, int r)
{
// Base case
if (l == r)
cout<< str <<endl;
else
{
// Permutations made
for (int i = l; i <= r; i++)
{
// Swapping done
swap(str[l], str[i]);