├── .vscode └── settings.json ├── C# └── FibonacciDP.cs ├── C ├── ImplementingLinkedList.c ├── LUCKYNUM.c ├── Queue.c ├── Quick_Sort.C ├── TESTSERIES.c ├── bubble_sort.c ├── bubble_sort.exe ├── nqueen.c ├── stack.c └── tolower_string.c ├── CONTRIBUTING.md ├── CPP ├── ADAKING.cpp ├── ADAMAT.cpp ├── AddTwoNumbers.cpp ├── BFS for a Graph.cpp ├── BalancedRemainders.cpp ├── Balanced_Parenthesis.cpp ├── BinarySearch.cpp ├── Binary_search.cpp ├── Brian_kariangam_algo.cpp ├── Bubble_Sort.cpp ├── CACHEHIT.cpp ├── CENS20D.cpp ├── CENS20G.cpp ├── CHEFEZQ.cpp ├── CHEFNWRK.cpp ├── CHEFSTEP.cpp ├── CHEFSTR1.cpp ├── CHFICRM.cpp ├── CHFPARTY.cpp ├── CLPNT.cpp ├── COVID19.cpp ├── CRDGAME.cpp ├── CoinChange.cpp ├── Container_With_Most_Water.cpp ├── DECINC.cpp ├── DetectLoopInLinkedList.cpp ├── Diagonal movement_codechef.cpp ├── DoubleHelix.cpp ├── EVENGAME.cpp ├── EVENM.cpp ├── Euclidean_Algo.cpp ├── FFL.cpp ├── FINXOR.cpp ├── FibonacciSeries.cpp ├── Fixed_number_of_Fixed_Points.cpp ├── GCD_OR_HCF_OF_TWO_NUMBERS_OPTIMIZED.cpp ├── HX.cpp ├── INFIXPOST.cpp ├── J7.cpp ├── KOL15A.cpp ├── Kadane Algorithm.cpp ├── Kth element from end in Linked List.cpp ├── LCH15JAB.cpp ├── LINCHESS.cpp ├── LOSTWKND.cpp ├── LUCKYFR.cpp ├── LongestCommonSubsequence.cpp ├── MCUBES.cpp ├── MEX-OR(MEXOR).md ├── Maximum Number_Of_Distinct_Elements.cpp ├── NUM239.cpp ├── POLYREL.cpp ├── PRICECON.cpp ├── PROB.cpp ├── Print subsets of array recursively ├── Quick Sort Using Recursion ├── Reverse_the_number.cpp ├── Running_Sum_of_1d_Array.cpp ├── ShellSort.cpp ├── Sieve_of_Eratosthenes.cpp ├── TWOSTR.cpp ├── UCL.cpp ├── VILLINE.cpp ├── XYSTR.cpp ├── Zebra.cpp ├── a_cokies_you.cpp ├── ab_pallindrom.cpp ├── alice_bob_candies.cpp ├── alomst_equal.cpp ├── alomst_rectangle.cpp ├── alphabetical.cpp ├── amusing_joke.cpp ├── andrew_strings.cpp ├── anton_currency.cpp ├── aqua_moon.cpp ├── armstrongno.cpp ├── array_rotation.cpp ├── circularqueue.cpp ├── dijkstra.cpp ├── insertionsort.cpp ├── leap_year_finder.cpp ├── linearsearch.cpp ├── lucky_number7_codechef.cpp ├── majority_element.cpp ├── mocha_hiking.cpp ├── n queens.cpp ├── pivot_index.cpp ├── quadrilateral_finder.cpp ├── queueusinfSLL.cpp ├── rat in a maze.cpp ├── roman_to_integer.cpp ├── rotate.cpp ├── selectionsort.cpp ├── stackusingSLL.cpp └── subarray_exists_or_not.cpp ├── Code of conduct.md ├── Dart ├── Fibonacci.dart └── pyramid_pattern.dart ├── Golang └── bubblesort.go ├── Java ├── FoldLinkedList.java ├── Insertion sort.java ├── KadaneAlgorithm.java ├── MergeSort.java ├── NonFibonacciAndFibonacci.java ├── PrintAllPathsInGraph.java ├── Selection Sort.java ├── SelectionSort.java ├── SubArrayWithSumZero.java ├── TowerOfHanoi.java └── dicepath.java ├── Javascript ├── Add 2 numbers linked list ├── Move Zeroes.js ├── Stack.js ├── fractional_knapsack.js ├── linked list cycle ├── max-profits.js ├── queue implentation.js ├── reversed linked list └── subarray with zero.js ├── Maximum_Subarray.cpp ├── Number Guess ├── index.html ├── script.js └── style.css ├── Python ├── CGMN1.py ├── CHEFRECP.py ├── CHEFWED.py ├── CHFNSWPS.py ├── CVDRUN.py ├── DIGITREM.py ├── DRCHEF.py ├── EVENTUAL.py ├── FAIRELCT.py ├── FAKESWAP.py ├── LIKECS01.py ├── MERGE.py ├── MEXOR.py ├── MOVIEWKN.py ├── MergeSort.py ├── ORTHODOX.py ├── POSAND.py ├── PTMSSNG.py ├── SKMP.py ├── Stack menu driven program.py ├── TREE2.py └── ZEBRA.py ├── README.md ├── Recursive Reverse of Linked List.cpp ├── binarysearch tree .js └── subset_sum_recursive.cpp /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "liveServer.settings.port": 5501 3 | } -------------------------------------------------------------------------------- /C#/FibonacciDP.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace FibonacciDP 8 | { 9 | class Program 10 | { 11 | static long FibonacciMaster(int n) 12 | { 13 | Dictionary cache = new Dictionary 14 | { 15 | [0] = 0, 16 | [1] = 1 17 | }; 18 | 19 | if (cache.ContainsKey(n)) 20 | { 21 | return cache[n]; 22 | } 23 | 24 | cache.Add(n, FibonacciMaster(n - 1) + FibonacciMaster(n - 2)); 25 | return cache[n]; 26 | } 27 | 28 | static void Main(string[] args) 29 | { 30 | Console.WriteLine("Enter a number"); 31 | int n = int.Parse(Console.ReadLine()); 32 | long fib = FibonacciMaster(n); 33 | Console.WriteLine("The sum of " + n + " fibonacci numbers is " + fib); 34 | 35 | Console.Read(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /C/ImplementingLinkedList.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Devesh Chandra 3 | Date : 24/10/2021 4 | Description : This Program is to implement Linked lists. 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | struct node{ 11 | int data; 12 | struct node *next; 13 | }; 14 | typedef struct node NODE; 15 | NODE *head=NULL; 16 | NODE *end=NULL; 17 | 18 | void create( int data){ 19 | NODE *temp; 20 | temp =(NODE*)malloc(sizeof(NODE)); 21 | temp->data=data; 22 | if(head==NULL){ 23 | head=temp; 24 | end=temp; 25 | } 26 | else{ 27 | end->next=temp; 28 | end=temp; 29 | temp->next=NULL; 30 | 31 | } 32 | } 33 | void display(){ 34 | NODE *temp; 35 | temp = head; 36 | while(temp!=NULL){ 37 | printf("%d", temp->data); 38 | printf(" \n"); 39 | temp=temp->next; 40 | } 41 | } 42 | 43 | int main(){ 44 | int n; 45 | printf(" Enter the number of nodes in list:"); 46 | scanf(" %d", &n); 47 | for(int i=0; i 2 | 3 | int main(void) { 4 | int n,a,b,c,i; 5 | scanf("%d",&n); 6 | for (i=0;i 2 | #include 3 | # define MAX 100 4 | int array[MAX]; 5 | int Rear = - 1; 6 | int Front = - 1; 7 | void enqueue() 8 | { 9 | int value; 10 | if (Rear == MAX - 1) 11 | printf("Overflow is there \n"); 12 | else 13 | { 14 | if (Front == - 1) 15 | 16 | Front = 0; 17 | printf("Element to be inserted in the Queue\n : "); 18 | scanf("%d", &value); 19 | Rear = Rear + 1; 20 | array[Rear] = value; 21 | } 22 | } 23 | void dequeue() 24 | { 25 | if (Front == - 1 || Front > Rear) 26 | { 27 | printf("Underflow is there\n"); 28 | return ; 29 | } 30 | else 31 | { 32 | printf("Element is deleted from the Queue: %d\n", array[Front]); 33 | Front = Front + 1; 34 | } 35 | } 36 | void show() 37 | { 38 | 39 | if (Front == - 1) 40 | printf("Queue is Empty \n"); 41 | else 42 | { 43 | printf("Queue is : \n"); 44 | printf("............. \n"); 45 | for (int i = Front; i <= Rear; i++) 46 | printf("%d ", array[i]); 47 | printf("\n"); 48 | printf("............. \n"); 49 | 50 | printf("\n"); 51 | } 52 | } 53 | 54 | void main() 55 | { 56 | int choice; 57 | while (1) 58 | { 59 | printf("1.Enqueue Operation\n"); 60 | printf("2.Dequeue Operation\n"); 61 | printf("3.Display the Queue\n"); 62 | printf("4.Exit\n"); 63 | printf("Enter your choiceoice : "); 64 | scanf("%d", &choice); 65 | switch(choice) 66 | { 67 | case 1: 68 | enqueue(); 69 | break; 70 | case 2: 71 | dequeue(); 72 | break; 73 | case 3: 74 | show(); 75 | break; 76 | case 4: 77 | exit(0); 78 | default: 79 | printf("Please Enter a valid option\n"); 80 | } 81 | } 82 | } 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /C/Quick_Sort.C: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Tanya Bansal 3 | Date : 17/10/2021 4 | Description : This Program is to implement quick sort algorithm. 5 | */ 6 | 7 | #include 8 | 9 | void quicksort(int number[25], int first, int last) { 10 | int i, j, pivot, temp; 11 | 12 | if (first < last) { 13 | pivot = first; 14 | i = first; 15 | j = last; 16 | 17 | while (i < j) { 18 | while (number[i] <= number[pivot] && i < last) 19 | i++; 20 | while (number[j] > number[pivot]) 21 | j--; 22 | if (i < j) { 23 | temp = number[i]; 24 | number[i] = number[j]; 25 | number[j] = temp; 26 | } 27 | } 28 | 29 | temp = number[pivot]; 30 | number[pivot] = number[j]; 31 | number[j] = temp; 32 | quicksort(number, first, j - 1); 33 | quicksort(number, j + 1, last); 34 | 35 | } 36 | } 37 | 38 | int main() { 39 | int i, count, number[25]; 40 | 41 | printf("How many elements are u going to enter?: "); 42 | scanf("%d", & count); 43 | 44 | printf("Enter %d elements: ", count); 45 | for (i = 0; i < count; i++) 46 | scanf("%d", & number[i]); 47 | 48 | quicksort(number, 0, count - 1); 49 | 50 | printf("Order of Sorted elements: "); 51 | for (i = 0; i < count; i++) 52 | printf(" %d", number[i]); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C/TESTSERIES.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n,i,j,in,dr,en; 5 | int a[5]; 6 | scanf("%d",&n); 7 | for (i=0;ien) printf("INDIA\n"); 17 | else printf("ENGLAND\n"); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C/bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *x, int *y) 3 | { 4 | int temp = *x; 5 | *x = *y; 6 | *y = temp; 7 | } 8 | void bubbleSort(int arr[], int n) 9 | { 10 | for (int i = 0; i < n - 1; i++) 11 | { 12 | for (int j = 0; j < n - i - 1; j++) 13 | { 14 | if (arr[j] > arr[j + 1]) 15 | { 16 | swap(&arr[j], &arr[j + 1]); 17 | } 18 | } 19 | } 20 | } 21 | void print_array(int arr[],int n) 22 | { 23 | for (int i = 0; i < n; i++) 24 | { 25 | printf("%d ",arr[i]); 26 | } 27 | } 28 | int main() 29 | { 30 | int n; 31 | printf("Enter the size of array:"); 32 | scanf("%d", &n); 33 | printf("Enter the element of array:"); 34 | int arr[n]; 35 | for (int i = 0; i < n; i++) 36 | { 37 | scanf("%d", &arr[i]); 38 | } 39 | printf("After sorting of array:"); 40 | bubbleSort(arr,n); 41 | print_array(arr,n); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /C/bubble_sort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog-harsh/Hacktoberfest2022/97a327e3e41142c2ced54884a5713aab7b08fcc4/C/bubble_sort.exe -------------------------------------------------------------------------------- /C/nqueen.c: -------------------------------------------------------------------------------- 1 | //N-Queen's Problem 2 | #include 3 | #include 4 | 5 | int board[20],count; 6 | 7 | int main() 8 | { 9 | int n,i,j; 10 | void queen(int row,int n); 11 | 12 | printf(" - N Queens Problem Using Backtracking -"); 13 | printf("\n\nEnter number of Queens:"); 14 | scanf("%d",&n); 15 | queen(1,n); 16 | return 0; 17 | } 18 | 19 | //function for printing the solution 20 | void print(int n) 21 | { 22 | int i,j; 23 | printf("\n\nSolution %d:\n\n",++count); 24 | 25 | for(i=1;i<=n;++i) 26 | printf("\t%d",i); 27 | 28 | for(i=1;i<=n;++i) 29 | { 30 | printf("\n\n%d",i); 31 | for(j=1;j<=n;++j) //for nxn board 32 | { 33 | if(board[i]==j) 34 | printf("\tQ"); //queen at i,j position 35 | else 36 | printf("\t-"); //empty slot 37 | } 38 | } 39 | } 40 | 41 | /*funtion to check conflicts 42 | If no conflict for desired postion returns 1 otherwise returns 0*/ 43 | int place(int row,int column) 44 | { 45 | int i; 46 | for(i=1;i<=row-1;++i) 47 | { 48 | //checking column and digonal conflicts 49 | if(board[i]==column) 50 | return 0; 51 | else 52 | if(abs(board[i]-column)==abs(i-row)) 53 | return 0; 54 | } 55 | 56 | return 1; //no conflicts 57 | } 58 | 59 | //function to check for proper positioning of queen 60 | void queen(int row,int n) 61 | { 62 | int column; 63 | for(column=1;column<=n;++column) 64 | { 65 | if(place(row,column)) 66 | { 67 | board[row]=column; //no conflicts so place queen 68 | if(row==n) //dead end 69 | print(n); //printing the board configuration 70 | else //try queen with next position 71 | queen(row+1,n); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /C/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define CAPACITY 10 4 | int stack[CAPACITY],top=-1; 5 | int* sp = stack; 6 | 7 | void push(int); 8 | void pop(); 9 | void display(); 10 | 11 | void main(){ 12 | int ch,element; 13 | printf("\n1.Push element \n2.Pop Element \n3.Display \n4.Exit\n"); 14 | 15 | while(1){ 16 | printf("\nEnter your choice : "); 17 | scanf("%d",&ch); 18 | switch(ch){ 19 | case 1 : 20 | printf("\nEnter the element to be pushed : "); 21 | scanf("%d",&element); 22 | push(element); 23 | break; 24 | 25 | case 2 : 26 | pop(); 27 | break; 28 | 29 | case 3 : 30 | display(); 31 | break; 32 | 33 | case 4 : 34 | exit(0); 35 | 36 | default : 37 | printf("\nWrong choice!"); 38 | } 39 | } 40 | } 41 | 42 | void push(int element){ 43 | 44 | if(top==CAPACITY-1) 45 | printf("\nStack Overflow!"); 46 | else { 47 | top++; 48 | *(stack+top)=element; 49 | printf("%d is pushed to the stack",element); 50 | display(); 51 | } 52 | } 53 | 54 | void pop(){ 55 | if(top==-1) 56 | printf("\nStack Underflow!"); 57 | else { 58 | printf("\n%d is popped from the stack",*(stack+top)); 59 | top--; 60 | display(); 61 | } 62 | } 63 | 64 | void display(){ 65 | if(top==-1) 66 | printf("\nStack is empty"); 67 | else{ 68 | printf("\nThe stack now contains\n"); 69 | for(int i=top ; i>=0 ; i--) 70 | printf("%d\n",*(stack+i)); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /C/tolower_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ft_tolower(int c) 5 | { 6 | unsigned char a; 7 | 8 | a = (unsigned char)c; 9 | if ((c >= 256) || (c <= 0)) 10 | return (c); 11 | if ((a < 65) || (a > 90)) 12 | return (a); 13 | return (a + 32); 14 | } 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int i; 19 | 20 | i = 0; 21 | if (argc != 2) 22 | { 23 | printf("You must introduce executable plus one string between quotes (example: ./a.out \"I WAnt THIS STRING IN LOWer LETTERS\")\n"); 24 | exit (0); 25 | } 26 | printf("Lets do it --> "); 27 | while(argv[1][i] != '\0') 28 | { 29 | printf("%c", ft_tolower(argv[1][i++])); 30 | } 31 | printf("\n"); 32 | return (0); 33 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | CONTRIBUTING.md 2 | 3 | ## 🤝 Wanna Contribute to this project 4 | You can contribute to this project by following these simple steps: 5 | 6 | ### Step-1: 7 | Fork this repo by clicking on fork button at top-right corner. 8 | 9 | ### Step-2: 10 | Clone this repo 11 | ```bash 12 | https://github.com/your_username/repo_name.git 13 | ``` 14 | 15 | ### Step-3: 16 | Make the changes that you want. 17 | 18 | ### Step-4: 19 | Create a new branch 20 | ```bash 21 | git branch branch_name 22 | ``` 23 | ```bash 24 | git checkout branch_name 25 | ``` 26 | 27 | ### Step-4: 28 | Save changes and Commit changes 29 | ```bash 30 | git add . 31 | ``` 32 | ```bash 33 | git commit -m "add message" 34 | ``` 35 | ### Step-5: 36 | Make PR 37 | ```bash 38 | git push origin branch_name 39 | ``` 40 | Then go to github repo, there will be automatically a button generated to create pull request, just click that button. 41 | 42 | Congratulations🎉, you have successfully created the PR. 43 | 44 | [![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://jatiinyadav.github.io/) 45 | -------------------------------------------------------------------------------- /CPP/ADAMAT.cpp: -------------------------------------------------------------------------------- 1 | // Question : https://www.codechef.com/SEPT20B/problems/ADAMAT 2 | 3 | #include 4 | using namespace std; 5 | //Templates start here 6 | #define pb push_back 7 | #define mp make_pair 8 | #define F first 9 | #define S second 10 | #define ll long long 11 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 12 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 13 | #define rep(i, j) fo(i, 0, j, 1) 14 | #define rerep(i, j) fo(i, j, 0, 1) 15 | #define all(cont) cont.begin(), cont.end() 16 | #define reall(cont) cont.end(), cont.begin() 17 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 18 | #define in(A, B, C) assert(B <= A && A <= C) 19 | #define MOD (int)1e9 20 | #define MOD7 1000000007 21 | #define PI 3.1415926535897932384626433832795 22 | typedef pair PII; 23 | typedef pair PLL; 24 | typedef vector VI; 25 | typedef vector VL; 26 | typedef vector VS; 27 | typedef vector VII; 28 | typedef vector VLL; 29 | typedef vector VVI; 30 | typedef vector VVL; 31 | typedef map MPII; 32 | typedef map MPLL; 33 | typedef set SETI; 34 | typedef multiset MSETI; 35 | int main() { 36 | long long t,n; 37 | cin>>t; 38 | while(t--) 39 | { 40 | cin>>n; 41 | ll a[n][n],b[n][n]; 42 | fo(i,0,n,1) 43 | { 44 | fo(j,0,n,1) 45 | { 46 | cin>>a[i][j]; 47 | } 48 | } 49 | ll counter=0; 50 | for(int l=n-1 ; l>=1 ; l--) 51 | { 52 | if(a[0][l]!=(l+1)) 53 | { 54 | counter+=1; 55 | fo(i,0,l,1) 56 | { 57 | fo(j,0,l,1) 58 | { 59 | b[i][j]=a[j][i]; 60 | } 61 | } 62 | fo(i,0,l,1) 63 | { 64 | fo(j,0,l,1) 65 | { 66 | a[i][j]=b[i][j]; 67 | } 68 | } 69 | } 70 | } 71 | cout<0) 19 | { 20 | 21 | if(l1!=NULL) 22 | { 23 | sum+=l1->val; 24 | l1=l1->next; 25 | } 26 | if(l2!=NULL) 27 | { 28 | sum+=l2->val; 29 | l2=l2->next; 30 | } 31 | (*node)=new ListNode(sum%10); 32 | sum/=10; 33 | node = &((*node)->next); 34 | 35 | } 36 | return result; 37 | } 38 | }; -------------------------------------------------------------------------------- /CPP/BFS for a Graph.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog-harsh/Hacktoberfest2022/97a327e3e41142c2ced54884a5713aab7b08fcc4/CPP/BFS for a Graph.cpp -------------------------------------------------------------------------------- /CPP/BalancedRemainders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | int c[3] = {0,0,0}; 11 | 12 | for(int i=0;i>x; 16 | 17 | c[x%3]++; 18 | } 19 | int step = 0; 20 | while(true){ 21 | for(int i=0;i<3;i++){ 22 | if(c[i] > n/3){ 23 | int diff = c[i] - n/3; 24 | c[(i+1)%3] += diff; 25 | c[i] -= diff; 26 | step++; 27 | } 28 | } 29 | 30 | int stop =1; 31 | 32 | for(int i =0;i<3;i++){ 33 | if(c[i] != n/3){ 34 | stop =0; 35 | break; 36 | } 37 | } 38 | 39 | if(stop) 40 | break; 41 | } 42 | cout<>t; 48 | 49 | while(t--) 50 | solve(); 51 | } -------------------------------------------------------------------------------- /CPP/Balanced_Parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | cout.tie(NULL); 9 | 10 | string s; 11 | 12 | bool result = true; 13 | cout << "Enter a string containing "; 14 | cout << "(" 15 | << ")" 16 | << "{" 17 | << "}" 18 | << "[" 19 | << "]" << endl; 20 | 21 | cin >> s; 22 | stack st; 23 | 24 | for (int i = 0; i < s.length(); i++) 25 | { 26 | if (s[i] == '(' || s[i] == '{' || s[i] == '[') 27 | st.push(s[i]); 28 | else 29 | { 30 | 31 | if (s[i] == ')') 32 | { 33 | 34 | if (st.empty() || st.top() != '(') 35 | result = false; 36 | else 37 | st.pop(); 38 | } 39 | else if (s[i] == '}') 40 | { 41 | 42 | if (st.empty() || st.top() != '{') 43 | result = false; 44 | else 45 | st.pop(); 46 | } 47 | else if (s[i] == ']') 48 | { 49 | if (st.empty() || st.top() != '[') 50 | result = false; 51 | else 52 | st.pop(); 53 | } 54 | } 55 | } 56 | if (result && (!st.empty())) 57 | result = false; 58 | 59 | if (result) 60 | cout << "Valid String" << endl; 61 | 62 | else 63 | cout << "Invalid String" << endl; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /CPP/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int BinarySearch(int a[],int n,int data){ 6 | int l,r; 7 | int mid=0; 8 | l = 0; 9 | r = n -1; 10 | while(l < r){ 11 | mid = (l + r)/2; 12 | if(data == a[mid]) 13 | return mid; 14 | else if(data < a[mid]) 15 | r = mid - 1; 16 | else 17 | l = mid + 1; 18 | } 19 | return -1; 20 | } 21 | 22 | int main(){ 23 | int arraySize, data, result; 24 | cout<<"\nEnter Array Size :- "; 25 | cin>>arraySize; 26 | int a[arraySize]; 27 | cout<<"\nEnter Array Elements :- "; 28 | for(int i = 0; i < arraySize; i++){ 29 | cin>>a[i]; 30 | } 31 | cout<<"\nYour Array elements are :- \n"; 32 | for(int i = 0; i < arraySize; i++){ 33 | cout<<" "<>data; 38 | result = BinarySearch(a,arraySize,data); 39 | cout<<"\n"< 2 | using namespace std; 3 | 4 | //define array globally 5 | const int N = 1e6 +4; 6 | 7 | int a[N]; 8 | int n;//array size 9 | 10 | //element to be searched in array 11 | int k; 12 | 13 | bool check(int dig) 14 | { 15 | //element at dig position in array 16 | int ele=a[dig]; 17 | 18 | //if k is less than 19 | //element at dig position 20 | //then we need to bring our higher ending to dig 21 | //and then continue further 22 | if(k<=ele) 23 | { 24 | return 1; 25 | } 26 | else 27 | { 28 | return 0; 29 | } 30 | } 31 | void binsrch(int lo,int hi) 32 | { 33 | while(lo>n; 57 | for(int i=0; i>a[i]; 60 | } 61 | cin>>k; 62 | 63 | //it is being given array is sorted 64 | //if not then we have to sort it 65 | 66 | //minimum possible point where our k can be is starting index 67 | //so lo=0 68 | //also k cannot be outside of array so end point 69 | //hi=n 70 | 71 | binsrch(0,n); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /CPP/Brian_kariangam_algo.cpp: -------------------------------------------------------------------------------- 1 | // Brian Kariangam's Algorithm to calculate set bits 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int countBits(int number) 10 | { 11 | int res = 0; 12 | while(number>0) 13 | { 14 | number = (number&(number-1)); 15 | res++; 16 | } 17 | return res; 18 | } 19 | 20 | #define ll long long 21 | 22 | int main(){ 23 | int number; 24 | cin>>number; 25 | cout< 3 | using namespace std; 4 | int main() 5 | { 6 | int n,i,j,temp; 7 | cout<<"Enter the size of the array : "; 8 | cin>>n; 9 | int a[n]; 10 | cout<<"Enter array elements : "; 11 | for(i=0;i>a[i]; 14 | } 15 | cout<<"Array before sorting : "; 16 | for(i=0;ia[j+1]) 27 | { 28 | temp=a[j]; 29 | a[j]=a[j+1]; 30 | a[j+1]=temp; 31 | } 32 | } 33 | } 34 | cout<<"Array after sorting : "; 35 | for(i=0;i 5 | #include 6 | #include 7 | using namespace std; 8 | int main() 9 | { 10 | int t; 11 | cin >> t; 12 | while (t--) 13 | { 14 | int n, b, m, temp, count = 0, prev = -1, now = 0; 15 | cin >> n >> b >> m; 16 | vector x; 17 | for (int i = 0; i < m; i++) 18 | { 19 | cin >> temp; 20 | x.push_back(temp); 21 | if ((temp + 1) % b != 0) 22 | now = ((temp + 1) / b) + 1; 23 | else 24 | now = ((temp + 1) / b); 25 | if (prev != now) 26 | { 27 | prev=now; 28 | count += 1; 29 | } 30 | } 31 | cout << (count) << "\n"; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /CPP/CENS20D.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/CENS2020/problems/CENS20D 2 | 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define pb push_back 8 | #define mp make_pair 9 | #define F first 10 | #define S second 11 | #define ll long long 12 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 13 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 14 | #define rep(i, j) fo(i, 0, j, 1) 15 | #define rerep(i, j) fo(i, j, 0, 1) 16 | #define all(cont) cont.begin(), cont.end() 17 | #define reall(cont) cont.end(), cont.begin() 18 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 19 | #define in(A, B, C) assert(B <= A && A <= C) 20 | #define MOD (int)1e9 21 | #define MOD7 1000000007 22 | #define PI 3.1415926535897932384626433832795 23 | typedef pair PII; 24 | typedef pair PLL; 25 | typedef vector VI; 26 | typedef vector VL; 27 | typedef vector VS; 28 | typedef vector VII; 29 | typedef vector VLL; 30 | typedef vector VVI; 31 | typedef vector VVL; 32 | typedef map MPII; 33 | typedef map MPLL; 34 | typedef set SETI; 35 | typedef multiset MSETI; 36 | bool grt(long long int x, long long int y) 37 | { 38 | return x > y; 39 | } 40 | long exponentiation(long base, long exponent) 41 | { 42 | long t = 1L; 43 | while (exponent > 0) 44 | { 45 | if (exponent % 2 != 0) 46 | t = (t * base) % MOD7; 47 | base = (base * base) % MOD7; 48 | exponent /= 2; 49 | } 50 | return t % MOD7; 51 | } 52 | int fact(int n) 53 | { 54 | int result = 1; 55 | fo(i, 2, n + 1, 1) 56 | result = result * i; 57 | return result; 58 | } 59 | int nPr(int n, int r) 60 | { 61 | return fact(n) / fact(n - r); 62 | } 63 | int nCr(int n, int r) 64 | { 65 | return fact(n) / (fact(r) * fact(n - r)); 66 | } 67 | VL generateSubArray(int arr[], int n) 68 | { 69 | VL tot_subarr; 70 | rep(i, n) 71 | { 72 | fo(j, i, n, 1) 73 | { 74 | fo(k, i, j + 1, 1) 75 | tot_subarr.pb(arr[k]); 76 | } 77 | } 78 | return tot_subarr; 79 | } 80 | VL generateSubsequences(int arr[], int n) 81 | { 82 | VL tot_subsequence; 83 | unsigned int opsize = pow(2, n); 84 | for (int counter = 1; counter < opsize; counter++) 85 | { 86 | for (int j = 0; j < n; j++) 87 | { 88 | if (counter & (1 << j)) 89 | tot_subsequence.pb(arr[j]); 90 | } 91 | } 92 | return tot_subsequence; 93 | } 94 | 95 | int main() 96 | { 97 | ll i,n, j, k, t, p, v, x, y, z; 98 | cin >> t; 99 | while (t--) 100 | { 101 | cin>>n; 102 | ll a[n],count=0; 103 | fo(i,0,n,1) 104 | { 105 | cin>>a[i]; 106 | } 107 | if(n==1) 108 | { 109 | count=0; 110 | } 111 | else 112 | { 113 | fo(i,0,n,1) 114 | { 115 | fo(j,i+1,n,1) 116 | { 117 | if((a[i]&a[j])==a[i]) 118 | { 119 | count+=1; 120 | } 121 | } 122 | } 123 | } 124 | cout< 6 | using namespace std; 7 | 8 | #define pb push_back 9 | #define mp make_pair 10 | #define F first 11 | #define S second 12 | #define ll long long 13 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 14 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 15 | #define rep(i, j) fo(i, 0, j, 1) 16 | #define rerep(i, j) fo(i, j, 0, 1) 17 | #define all(cont) cont.begin(), cont.end() 18 | #define reall(cont) cont.end(), cont.begin() 19 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 20 | #define in(A, B, C) assert(B <= A && A <= C) 21 | #define MOD (int)1e9 22 | #define MOD7 1000000007 23 | #define PI 3.1415926535897932384626433832795 24 | typedef pair PII; 25 | typedef pair PLL; 26 | typedef vector VI; 27 | typedef vector VL; 28 | typedef vector VS; 29 | typedef vector VII; 30 | typedef vector VLL; 31 | typedef vector VVI; 32 | typedef vector VVL; 33 | typedef map MPII; 34 | typedef map MPLL; 35 | typedef set SETI; 36 | typedef multiset MSETI; 37 | bool grt(long long int x, long long int y) 38 | { 39 | return x > y; 40 | } 41 | long exponentiation(long base, long exponent) 42 | { 43 | long t = 1L; 44 | while (exponent > 0) 45 | { 46 | if (exponent % 2 != 0) 47 | t = (t * base) % MOD7; 48 | base = (base * base) % MOD7; 49 | exponent /= 2; 50 | } 51 | return t % MOD7; 52 | } 53 | int fact(int n) 54 | { 55 | int result = 1; 56 | fo(i, 2, n + 1, 1) 57 | result = result * i; 58 | return result; 59 | } 60 | int nPr(int n, int r) 61 | { 62 | return fact(n) / fact(n - r); 63 | } 64 | int nCr(int n, int r) 65 | { 66 | return fact(n) / (fact(r) * fact(n - r)); 67 | } 68 | VL generateSubArray(int arr[], int n) 69 | { 70 | VL tot_subarr; 71 | rep(i, n) 72 | { 73 | fo(j, i, n, 1) 74 | { 75 | fo(k, i, j + 1, 1) 76 | tot_subarr.pb(arr[k]); 77 | } 78 | } 79 | return tot_subarr; 80 | } 81 | VL generateSubsequences(int arr[], int n) 82 | { 83 | VL tot_subsequence; 84 | unsigned int opsize = pow(2, n); 85 | for (int counter = 1; counter < opsize; counter++) 86 | { 87 | for (int j = 0; j < n; j++) 88 | { 89 | if (counter & (1 << j)) 90 | tot_subsequence.pb(arr[j]); 91 | } 92 | } 93 | return tot_subsequence; 94 | } 95 | //Templates end here 96 | 97 | int main() 98 | { 99 | ios::sync_with_stdio(0); 100 | cin.tie(0); 101 | cout.tie(0); 102 | ll t; 103 | cin >> t; 104 | while (t--) 105 | { 106 | ll q,x1,y1; 107 | int hash_map[128]={0}; 108 | string stri; 109 | cin>>stri; 110 | fo(i,0,stri.length(),1) 111 | { 112 | hash_map[stri[i]]++; 113 | } 114 | cin>>x1>>y1; 115 | cin>>q; 116 | while(q--) 117 | { 118 | ll x2,y2; 119 | bool temp=false; 120 | cin>>x2>>y2; 121 | ll x_d=x2-x1; 122 | ll y_d=y2-y1; 123 | if(x_d<0) 124 | { 125 | if((hash_map['L']-abs(x_d))<0) 126 | temp=true; 127 | } 128 | if(x_d>=0) 129 | { 130 | if((hash_map['R']-abs(x_d))<0) 131 | temp=true; 132 | } 133 | if(y_d<0) 134 | { 135 | if((hash_map['D']-abs(y_d))<0) 136 | temp=true; 137 | } 138 | if(y_d>=0) 139 | { 140 | if((hash_map['U']-abs(y_d))<0) 141 | temp=true; 142 | } 143 | if(temp) 144 | cout<<"NO\n"; 145 | else 146 | cout<<"YES "<<(abs(x_d)+abs(y_d))<<"\n"; 147 | } 148 | } 149 | return 0; 150 | } -------------------------------------------------------------------------------- /CPP/CHEFEZQ.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/OCT20B/problems/CHEFEZQ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | long long t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | vector arr; 12 | long long n,k; 13 | cin>>n>>k; 14 | long long temp=0,diff=0,ans=0,flag=-1,sum=0; 15 | for(int i=0 ; i>temp; 18 | temp+=diff; 19 | sum+=temp; 20 | arr.push_back(temp); 21 | if(temp>k) 22 | diff=temp-k; 23 | else 24 | diff=0; 25 | } 26 | for(int i=0 ; i 4 | using namespace std; 5 | 6 | //Templates start here 7 | #define pb push_back 8 | #define mp make_pair 9 | #define F first 10 | #define S second 11 | #define ll long long 12 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 13 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 14 | #define rep(i, j) fo(i, 0, j, 1) 15 | #define rerep(i, j) fo(i, j, 0, 1) 16 | #define all(cont) cont.begin(), cont.end() 17 | #define reall(cont) cont.end(), cont.begin() 18 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 19 | #define in(A, B, C) assert(B <= A && A <= C) 20 | #define MOD (int)1e9 21 | #define MOD7 1000000007 22 | #define PI 3.1415926535897932384626433832795 23 | typedef pair PII; 24 | typedef pair PLL; 25 | typedef vector VI; 26 | typedef vector VL; 27 | typedef vector VS; 28 | typedef vector VII; 29 | typedef vector VLL; 30 | typedef vector VVI; 31 | typedef vector VVL; 32 | typedef map MPII; 33 | typedef map MPLL; 34 | typedef set SETI; 35 | typedef multiset MSETI; 36 | bool grt(long long int x, long long int y) 37 | { 38 | return x > y; 39 | } 40 | long exponentiation(long base, long exponent) 41 | { 42 | long t = 1L; 43 | while (exponent > 0) 44 | { 45 | if (exponent % 2 != 0) 46 | t = (t * base) % MOD7; 47 | base = (base * base) % MOD7; 48 | exponent /= 2; 49 | } 50 | return t % MOD7; 51 | } 52 | int fact(int n) 53 | { 54 | int result = 1; 55 | fo(i, 2, n + 1, 1) 56 | result = result * i; 57 | return result; 58 | } 59 | int nPr(int n, int r) 60 | { 61 | return fact(n) / fact(n - r); 62 | } 63 | int nCr(int n, int r) 64 | { 65 | return fact(n) / (fact(r) * fact(n - r)); 66 | } 67 | VL generateSubArray(int arr[], int n) 68 | { 69 | VL tot_subarr; 70 | rep(i, n) 71 | { 72 | fo(j, i, n, 1) 73 | { 74 | fo(k, i, j + 1, 1) 75 | tot_subarr.pb(arr[k]); 76 | } 77 | } 78 | return tot_subarr; 79 | } 80 | VL generateSubsequences(int arr[], int n) 81 | { 82 | VL tot_subsequence; 83 | unsigned int opsize = pow(2, n); 84 | for (int counter = 1; counter < opsize; counter++) 85 | { 86 | for (int j = 0; j < n; j++) 87 | { 88 | if (counter & (1 << j)) 89 | tot_subsequence.pb(arr[j]); 90 | } 91 | } 92 | return tot_subsequence; 93 | } 94 | //Templates end here 95 | 96 | int main() 97 | { 98 | ll i, j, k,n, t, p, v, x, y, z; 99 | cin >> t; 100 | while (t--) 101 | { 102 | cin>>n>>k; 103 | ll arr[n]; 104 | fo(i,0,n,1) 105 | { 106 | cin>>arr[i]; 107 | } 108 | ll count=0,sum=0,j=-1; 109 | fo(i,0,n,1) 110 | { 111 | if((sum+arr[i])<=k) 112 | { 113 | sum+=arr[i]; 114 | j=i; 115 | } 116 | else 117 | { 118 | if(j!=-1) 119 | { 120 | count+=1; 121 | j=i; 122 | } 123 | sum=arr[i]; 124 | } 125 | if(arr[i]>k) 126 | { 127 | break; 128 | j=-1; 129 | } 130 | } 131 | if(j==-1 || j 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | long long n,k,temp; 14 | cin>>n>>k; 15 | string ans=""; 16 | for(int i=0 ; i>temp; 19 | if(temp%k==0) 20 | ans+='1'; 21 | else 22 | ans+='0'; 23 | } 24 | cout< 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | long long t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | long long n,sum=0; 14 | cin>>n; 15 | long long a[n]; 16 | for(int i=0 ; i>a[i]; 18 | for(int i=0 ; i 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int t=0; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n,five=0,ten=0,fifteen=0; 15 | cin>>n; 16 | bool ans=true; 17 | int a[n]; 18 | 19 | for(int i=0 ; i>a[i]; 21 | 22 | if(a[0]==5) 23 | { 24 | for(int i=0 ; i0) 31 | { 32 | ten+=1; 33 | five-=1; 34 | } 35 | else 36 | { 37 | cout<<"NO"<<"\n"; 38 | ans=false; 39 | break; 40 | } 41 | } 42 | else if(a[i]==15) 43 | { 44 | if(ten>0) 45 | { 46 | fifteen+=1; 47 | ten-=1; 48 | } 49 | else if(five>1) 50 | { 51 | fifteen+=1; 52 | five-=2; 53 | } 54 | else 55 | { 56 | cout<<"NO"<<"\n"; 57 | ans=false; 58 | break; 59 | } 60 | } 61 | } 62 | if(ans) 63 | cout<<"YES"<<"\n"; 64 | } 65 | else 66 | { 67 | cout<<"NO"<<"\n"; 68 | } 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /CPP/CHFPARTY.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/CHFPARTY 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | int arr[n]; 14 | for(int i=0; i>arr[i]; 17 | } 18 | int c=0; 19 | sort(arr, arr+n); 20 | for(int i=0; i 5 | using namespace std; 6 | int main() 7 | { 8 | ios::sync_with_stdio(0); cin.tie(0); 9 | cout.tie(0); 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | long long n; 15 | cin>>n; 16 | vector a; 17 | long long temp; 18 | for (int i = 0; i < n; i++) 19 | { 20 | cin>>temp; 21 | a.push_back(temp); 22 | } 23 | long long query; 24 | cin>>query; 25 | while(query--) 26 | { 27 | long long x_c,y_c; 28 | cin>>x_c>>y_c; 29 | vector::iterator ptr; 30 | ptr = lower_bound(a.begin(),a.end(),(x_c+y_c)); 31 | if((x_c+y_c) 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n,max=-1,min=9,count=0; 15 | cin>>n; 16 | int arr[n]; 17 | vectorx; 18 | for(int i=0 ; i>arr[i]; 20 | for(int i=1 ; icount) 32 | min=count; 33 | count=0; 34 | } 35 | if(max 5 | using namespace std; 6 | long long getSum(long long n) 7 | { 8 | long long sum; 9 | for (sum = 0; n > 0; sum += n % 10, n /= 10); 10 | return sum; 11 | } 12 | int main() 13 | { 14 | long long t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | long long n,a,b,point_a=0,point_b=0,a_try,b_try; 19 | cin>>n; 20 | 21 | for(int i=0 ; i>a>>b; 24 | a_try = getSum(a); 25 | b_try = getSum(b); 26 | if(a_try > b_try) 27 | point_a+=1; 28 | else if(a_try < b_try) 29 | point_b+=1; 30 | else if(a_try == b_try) 31 | { 32 | point_a+=1; 33 | point_b+=1; 34 | } 35 | } 36 | 37 | if(point_a>point_b) 38 | cout<<"0 "< 4 | 5 | using namespace std; 6 | 7 | int count(int coins[], int n, int sum) 8 | { 9 | int i, j, x, y; 10 | 11 | // We need sum+1 rows as the table 12 | // is constructed in bottom up 13 | // manner using the base case 0 14 | // value case (sum = 0) 15 | int table[sum + 1][n]; 16 | 17 | // Fill the entries for 0 18 | // value case (sum = 0) 19 | for (i = 0; i < n; i++) 20 | table[0][i] = 1; 21 | 22 | // Fill rest of the table entries 23 | // in bottom up manner 24 | for (i = 1; i < sum + 1; i++) { 25 | for (j = 0; j < n; j++) { 26 | // Count of solutions including coins[j] 27 | x = (i - coins[j] >= 0) ? table[i - coins[j]][j] 28 | : 0; 29 | 30 | // Count of solutions excluding coins[j] 31 | y = (j >= 1) ? table[i][j - 1] : 0; 32 | 33 | // total count 34 | table[i][j] = x + y; 35 | } 36 | } 37 | return table[sum][n - 1]; 38 | } 39 | 40 | // Driver Code 41 | int main() 42 | { 43 | int coins[] = { 1, 2, 3 }; 44 | int n = sizeof(coins) / sizeof(coins[0]); 45 | int sum = 4; 46 | cout << count(coins, n, sum); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /CPP/Container_With_Most_Water.cpp: -------------------------------------------------------------------------------- 1 | //Container with most water 2 | 3 | #include 4 | using namespace std; 5 | 6 | int container_with_most_water(int n,vector h){ 7 | int most_water=-1; 8 | for(int i=0;imost_water){ 11 | most_water=min(h[i],h[j])*(j-i); 12 | } 13 | } 14 | } 15 | return most_water; 16 | } 17 | 18 | int main(){ 19 | int n; 20 | vector h; 21 | cout<<"Enter the length of the array: "; 22 | cin>>n; 23 | cout<<"Enter elements:\n"; 24 | for(int i=0;i>temp_height; 27 | h.push_back(temp_height); 28 | } 29 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | long long n; 8 | cin>>n; 9 | if(n%4==0) 10 | cout<<(n+1); 11 | else 12 | cout<<(n-1); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /CPP/DetectLoopInLinkedList.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int key; 7 | struct Node* next; 8 | }; 9 | 10 | Node* newNode(int key) 11 | { 12 | Node* temp = new Node; 13 | temp->key = key; 14 | temp->next = NULL; 15 | return temp; 16 | } 17 | 18 | void printList(Node* head) 19 | { 20 | while (head != NULL) { 21 | cout << head->key << " "; 22 | head = head->next; 23 | } 24 | cout << endl; 25 | } 26 | 27 | int distance(Node* first, Node* last) 28 | { 29 | int counter = 0; 30 | 31 | Node* curr; 32 | curr = first; 33 | 34 | while (curr != last) { 35 | counter += 1; 36 | curr = curr->next; 37 | } 38 | 39 | return counter + 1; 40 | } 41 | 42 | bool detectLoop(Node* head) 43 | { 44 | 45 | Node* temp = new Node; 46 | 47 | Node *first, *last; 48 | 49 | first = head; 50 | last = head; 51 | 52 | int current_length = 0; 53 | 54 | int prev_length = -1; 55 | 56 | while (current_length > prev_length && last != NULL) { 57 | 58 | prev_length = current_length; 59 | 60 | current_length = distance(first, last); 61 | 62 | last = last->next; 63 | } 64 | 65 | if (last == NULL) { 66 | return false; 67 | } 68 | else { 69 | return true; 70 | } 71 | } 72 | int main() 73 | { 74 | Node* head = newNode(1); 75 | head->next = newNode(2); 76 | head->next->next = newNode(3); 77 | head->next->next->next = newNode(4); 78 | head->next->next->next->next = newNode(5); 79 | 80 | head->next->next->next->next->next = head->next->next; 81 | 82 | bool found = detectLoop(head); 83 | if (found) 84 | cout << "Loop Found"; 85 | else 86 | cout << "No Loop Found"; 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /CPP/Diagonal movement_codechef.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Platform: CodeChef 3 | Problem name: Diagonal movement 4 | Problem Code: DIAGMOVE 5 | 6 | 7 | Given the coordinates (x,y) of a point in 2-D plane. Find if it is possible to reach (x,y) from (0,0). The only possible moves from any coordinate (i,j) are as follows: 8 | 9 | Go to the point with coordinates (i+1,j+1). 10 | Go to the point with coordinates (i+1,j−1) 11 | Go to the point with coordinates (i−1,j+1). 12 | Go to the point with coordinates (i−1,j−1). 13 | 14 | 15 | Input Format: 16 | 17 | First line will contain T, number of testcases. Then the testcases follow. 18 | Each testcase contains of a single line of input, two integers x,y. 19 | 20 | Output Format: 21 | 22 | For each test case, print YES if it is possible to reach (x,y) from (0,0), otherwise print NO. 23 | 24 | You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). 25 | 26 | Constraints 27 | 1≤T≤2⋅104 28 | −109≤x,y≤109 29 | */ 30 | 31 | 32 | #include 33 | using namespace std; 34 | 35 | void solve() 36 | { 37 | int x, y; 38 | cin >> x >> y; 39 | int sum = x+y; 40 | if((x+y)%2 == 0) 41 | { 42 | cout << "YES" << "\n"; 43 | } 44 | else 45 | cout << "NO" << "\n"; 46 | return; 47 | } 48 | 49 | int main() 50 | { 51 | int t; 52 | cin >> t; 53 | while(t--) 54 | { 55 | solve(); 56 | } 57 | return 0; 58 | } 59 | 60 | 61 | /* 62 | 63 | Sample Input 1 64 | 65 | 6 66 | 0 2 67 | 1 2 68 | -1 -3 69 | -1 0 70 | -3 1 71 | 2 -1 72 | 73 | 74 | Sample Output 1 75 | 76 | YES 77 | NO 78 | YES 79 | NO 80 | YES 81 | NO 82 | 83 | */ 84 | -------------------------------------------------------------------------------- /CPP/DoubleHelix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int i,j,k; 8 | int x,y,z; 9 | int n1,n2; 10 | int result1,result2,result; 11 | 12 | cout<<"Enter the number of elements in the first sequence "; 13 | cin>>n1; 14 | 15 | result=result1=result2=0; 16 | 17 | vector a(n1); 18 | cout<<"Enter the values of the first sequence"<>a[i]; 22 | } 23 | 24 | j=0; 25 | 26 | cout<<"Enter the number of elements in the second sequence "; 27 | cin>>n2; 28 | vector b(n2); 29 | 30 | cout<<"Enter the values of the second sequence"<>b[i]; 34 | result2+=b[i]; 35 | 36 | while(j 4 | using namespace std; 5 | 6 | int main() { 7 | long long t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | long long n, temp, sum=0; 12 | cin>>n; 13 | for(int i=0 ; i>temp; 16 | sum+=temp; 17 | } 18 | if(sum%2==0) 19 | cout<<"1\n"; 20 | else 21 | cout<<"2\n"; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /CPP/EVENM.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/EVENM 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n, c=1; 12 | cin>>n; 13 | int arr[n][n]; 14 | for(int i=0 ; i=0 ; j--) 27 | { 28 | arr[i][j]=c; 29 | c+=1; 30 | } 31 | } 32 | } 33 | for(int i=0 ; i 4 | #include 5 | using namespace std; 6 | 7 | int gcd(int a, int b) 8 | { 9 | if(b == 0) 10 | return a; 11 | else 12 | return gcd(b, a % b); 13 | } 14 | 15 | #define ll long long 16 | 17 | int main(){ 18 | ios::sync_with_stdio(false) ; 19 | cin.tie(NULL) ; 20 | int a, b; 21 | cin>>a>>b; 22 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | int n,s; 14 | bool ans=false; 15 | cin>>n>>s; 16 | int p[n],a[n]; 17 | vectorforward,defender; 18 | for(int i=0 ; i>p[i]; 20 | for(int i=0 ; i>a[i]; 22 | for(int i=0 ; i 6 | using namespace std; 7 | 8 | #define pb push_back 9 | #define mp make_pair 10 | #define F first 11 | #define S second 12 | #define ll long long 13 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 14 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 15 | #define rep(i, j) fo(i, 0, j, 1) 16 | #define rerep(i, j) fo(i, j, 0, 1) 17 | #define all(cont) cont.begin(), cont.end() 18 | #define reall(cont) cont.end(), cont.begin() 19 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 20 | #define in(A, B, C) assert(B <= A && A <= C) 21 | #define MOD (int)1e9 22 | #define MOD7 1000000007 23 | #define PI 3.1415926535897932384626433832795 24 | #define FIO1 ios_base::sync_with_stdio(false) 25 | #define FIO2 cin.tie(NULL) 26 | #define twenty 20 27 | #define twenty_one 21 28 | #define twenty_two 22 29 | typedef pair PII; 30 | typedef pair PLL; 31 | typedef vector VI; 32 | typedef vector VL; 33 | typedef vector VS; 34 | typedef vector VII; 35 | typedef vector VLL; 36 | typedef vector VVI; 37 | typedef vector VVL; 38 | typedef map MPII; 39 | typedef map MPLL; 40 | typedef set SETI; 41 | typedef multiset MSETI; 42 | bool grt(long long int x, long long int y) 43 | { 44 | return x > y; 45 | } 46 | long exponentiation(long base, long exponent) 47 | { 48 | long t = 1L; 49 | while (exponent > 0) 50 | { 51 | if (exponent % 2 != 0) 52 | t = (t * base) % MOD7; 53 | base = (base * base) % MOD7; 54 | exponent /= 2; 55 | } 56 | return t % MOD7; 57 | } 58 | int fact(int n) 59 | { 60 | int result = 1; 61 | fo(i, 2, n + 1, 1) 62 | result = result * i; 63 | return result; 64 | } 65 | int nPr(int n, int r) 66 | { 67 | return fact(n) / fact(n - r); 68 | } 69 | int nCr(int n, int r) 70 | { 71 | return fact(n) / (fact(r) * fact(n - r)); 72 | } 73 | VL generateSubArray(int arr[], int n) 74 | { 75 | VL tot_subarr; 76 | rep(i, n) 77 | { 78 | fo(j, i, n, 1) 79 | { 80 | fo(k, i, j + 1, 1) 81 | tot_subarr.pb(arr[k]); 82 | } 83 | } 84 | return tot_subarr; 85 | } 86 | VL generateSubsequences(int arr[], int n) 87 | { 88 | VL tot_subsequence; 89 | unsigned int opsize = pow(2, n); 90 | for (int counter = 1; counter < opsize; counter++) 91 | { 92 | for (int j = 0; j < n; j++) 93 | { 94 | if (counter & (1 << j)) 95 | tot_subsequence.pb(arr[j]); 96 | } 97 | } 98 | return tot_subsequence; 99 | } 100 | 101 | int main() 102 | { 103 | ll i, answer, j, k, t, p, q, r, m, n, v, x, y, z; 104 | ll arr[22]; 105 | arr[0]=1; 106 | fo(i,1,twenty_two,1) 107 | { 108 | arr[i]=0; 109 | } 110 | fo(i,1,twenty_one,1) 111 | { 112 | arr[i]=(2*arr[i-1]); 113 | } 114 | cin >> t; 115 | ll value,grader_output,summation,temp_sum,validity; 116 | while (t--) 117 | { 118 | cin>>n; 119 | cout<<"1 "<>grader_output; 121 | summation=grader_output-(arr[twenty]*n); 122 | answer=0; 123 | if(summation%2!=0) 124 | { 125 | answer+=1; 126 | } 127 | fo(i,1,twenty,1) 128 | { 129 | temp_sum=summation+(arr[i]*n); 130 | cout<<"1 "<>grader_output; 132 | value=(temp_sum-grader_output)/(arr[i]*2); 133 | if(value%2!=0) 134 | { 135 | answer+=arr[i]; 136 | } 137 | } 138 | cout<<"2 "<>validity; 140 | if(validity==-1) 141 | { 142 | break; 143 | } 144 | } 145 | return 0; 146 | } -------------------------------------------------------------------------------- /CPP/FibonacciSeries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int i,n; 6 | cin>>n; 7 | int a[n]; 8 | for (i=0;i 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int t; 9 | cin>>t; 10 | while(t--){ 11 | int n, k; 12 | cin>>n>>k; 13 | int z=k; 14 | if(n-k==1){ 15 | cout<<"-1"; 16 | } 17 | else{ 18 | int i=1; 19 | while(k-- and i<=n){ 20 | cout< 2 | using namespace std; 3 | 4 | int gcd(int a, int b) 5 | { 6 | if(b==0) 7 | return a; 8 | 9 | return gcd(b, a % b); 10 | } 11 | 12 | int main() { 13 | 14 | int a,b; 15 | cin>>a>>b; 16 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | long long n; 8 | cin>>n; 9 | int arr[n][n]; 10 | for(int i=0 ; i>arr[i][j]; 13 | } 14 | } 15 | long long score=0; 16 | vector left, right; 17 | for(int i=0 ; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int precedence(char c){ 7 | if(c=='^'){ 8 | return 3; 9 | } 10 | else if(c=='*'||c=='/'){ 11 | return 2; 12 | } 13 | else if(c=='+'||c=='-'){ 14 | return 1; 15 | } 16 | else{ 17 | return 0; 18 | } 19 | } 20 | 21 | int isOperator(char c){ 22 | if(c=='^'||c=='*'||c=='/'||c=='+'||c=='-'||c==')'||c=='('){ 23 | return 1; 24 | } 25 | else{ 26 | return 0; 27 | } 28 | } 29 | 30 | int rtol(char c){ 31 | if(c=='^'){ 32 | return 1; 33 | } 34 | else{ 35 | return 0; 36 | } 37 | } 38 | 39 | string infixtopostfix(string s){ 40 | stackst; 41 | string res; 42 | for(int i=0;i=precedence(s[i]))){ 65 | if (s[i]=='^'&&st.top()!='^') 66 | { 67 | break; 68 | } 69 | else{ 70 | res+=st.top(); 71 | st.pop(); 72 | } 73 | } 74 | st.push(s[i]); 75 | 76 | 77 | 78 | 79 | } 80 | 81 | 82 | 83 | } 84 | // while(!st.empty()&&(precedence(st.top())>precedence(s[i]))){ 85 | // res+=st.top(); 86 | // st.pop(); 87 | // } 88 | // st.push(s[i]); 89 | 90 | 91 | 92 | while(!st.empty()) 93 | { 94 | res +=st.top(); 95 | st.pop(); 96 | } 97 | 98 | return res; 99 | 100 | } 101 | 102 | 103 | 104 | int main(){ 105 | string res; 106 | cout<<"Enter the infix expression"; 107 | cin>>res; 108 | cout< 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int t; 9 | cin >> t; 10 | while(t--) 11 | { 12 | double p, s, length, volume; 13 | cin >> p >> s; 14 | length = (p - sqrt(p*p - 24*s)) / 12; 15 | volume = (length*s)/2 - (length*length)*(p/4 - length); 16 | cout << fixed << setprecision(2) << volume << "\n"; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | cout.tie(NULL); 25 | solve(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /CPP/KOL15A.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/KOL15A 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string str; 13 | cin>>str; 14 | int sum=0; 15 | for(int i=0 ; i 2 | # include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | int a[n]; 11 | for(int i = 0; i < n; i++){ 12 | cin>>a[i]; 13 | } 14 | 15 | int currSum = 0; 16 | int maxSum = a[0]; 17 | for(int i = 0; i < n; i++){ 18 | currSum += a[i]; 19 | maxSum = max(maxSum,currSum); 20 | if (currSum < 0){ 21 | currSum = 0; 22 | } 23 | } 24 | 25 | cout< 40 | using namespace std; 41 | 42 | 43 | class node 44 | { 45 | public: 46 | 47 | int data; 48 | node * next; 49 | 50 | node(int d) 51 | { 52 | data = d; 53 | next = NULL; 54 | } 55 | }; 56 | 57 | 58 | void insertAtTail(node *&head,int data) 59 | { 60 | if(head==NULL) 61 | { 62 | head=new node(data); 63 | return; 64 | } 65 | 66 | 67 | node *t=head; 68 | 69 | 70 | while(t->next!=NULL) 71 | { 72 | t=t->next; 73 | } 74 | 75 | 76 | t->next=new node(data); 77 | return; 78 | } 79 | 80 | 81 | node* take_input(node*&head) 82 | { 83 | int data; 84 | cin >> data; 85 | 86 | // pass by reference toh no need 87 | //node* head = NULL; 88 | while(data!= -1) 89 | { 90 | 91 | insertAtTail(head, data); 92 | cin >> data; 93 | } 94 | 95 | return head; 96 | 97 | } 98 | 99 | 100 | 101 | void print (node* temp) 102 | { 103 | 104 | while(temp!=NULL) 105 | { 106 | cout << temp -> data << " --> "; 107 | temp = temp -> next; 108 | } 109 | 110 | cout << "NULL" << endl; 111 | 112 | } 113 | 114 | 115 | 116 | node* KthNodefromEnd(node* head, int k) 117 | { 118 | node* fast = head; 119 | node* slow = head; 120 | 121 | for(int i = 0; i < k; i++) 122 | { 123 | // traversal of N nodes 124 | fast = fast -> next; 125 | } 126 | 127 | while(fast!=NULL) 128 | { 129 | // N-K from begin and K at end is same 130 | fast = fast -> next; 131 | slow = slow -> next; 132 | } 133 | 134 | return slow; 135 | 136 | } 137 | 138 | 139 | 140 | int main() 141 | { 142 | node *head=NULL; 143 | 144 | take_input(head); 145 | 146 | int position; 147 | cin >> position; 148 | 149 | node* answer = KthNodefromEnd(head,position); 150 | 151 | cout << answer -> data; 152 | 153 | return 0; 154 | 155 | } 156 | -------------------------------------------------------------------------------- /CPP/LCH15JAB.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/LCH15JAB 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | string st; 12 | cin>>st; 13 | int arr[26]={0}; 14 | for(int i=0 ; i 4 | using namespace std; 5 | 6 | int main() { 7 | long long t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | long long n,k,temp,minm_p=LONG_MAX,minm_quotient=LONG_MAX; 12 | cin>>n>>k; 13 | bool flag=true; 14 | for(int i=0 ; i>temp; 17 | if(k%temp==0) 18 | { 19 | if((k/temp) 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int t=0; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int tot_available=120,work=0; 15 | int arr[6]; 16 | for(int i=0 ; i<6 ; i++) 17 | cin>>arr[i]; 18 | for(int i=0 ; i<5 ; i++) 19 | { 20 | arr[i]=arr[i]*arr[5]; 21 | work+=arr[i]; 22 | } 23 | if(work>tot_available) 24 | cout<<"Yes\n"; 25 | else 26 | cout<<"No\n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /CPP/LUCKYFR.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/LUCKYFR 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | long long t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | long long n, c=0; 13 | cin>>n; 14 | while(n>0) 15 | { 16 | if(n%10==4) 17 | c+=1; 18 | n/=10; 19 | } 20 | cout< 35 | using namespace std; 36 | 37 | // } Driver Code Ends 38 | class Solution { 39 | public: 40 | int LcW5MfbLwHayuHRL2jJeQN8AXGWC4Bv6Xk(string str){ 41 | int n = str.length(); 42 | 43 | // Create and initialize DP table 44 | int dp[n+1][n+1]; 45 | for (int i=0; i<=n; i++) 46 | for (int j=0; j<=n; j++) 47 | dp[i][j] = 0; 48 | 49 | // Fill dp table (similar to LCS loops) 50 | for (int i=1; i<=n; i++) 51 | { 52 | for (int j=1; j<=n; j++) 53 | { 54 | // If characters match and indexes are 55 | // not same 56 | if (str[i-1] == str[j-1] && i != j) 57 | dp[i][j] = 1 + dp[i-1][j-1]; 58 | 59 | // If characters do not match 60 | else 61 | dp[i][j] = max(dp[i][j-1], dp[i-1][j]); 62 | } 63 | } 64 | return dp[n][n]; 65 | } 66 | 67 | }; 68 | 69 | // { Driver Code Starts. 70 | int main(){ 71 | 72 | string str; 73 | cin >> str; 74 | Solution obj; 75 | int ans = obj.LongestRepeatingSubsequence(str); 76 | cout << ans << "\n"; 77 | 78 | return 0; 79 | } // } Driver Code Ends 80 | -------------------------------------------------------------------------------- /CPP/MCUBES.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/MCUBES 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | long long n,temp,res=0; 8 | cin>>n; 9 | vector a; 10 | 11 | for(int i=0 ; i>temp; 14 | a.push_back(temp); 15 | } 16 | bool flag=true; 17 | for(int i=n-1 ; i>0 ; i--) 18 | { 19 | if(a[i]!=a[i-1]) 20 | { 21 | res = i+1; 22 | flag=false; 23 | break; 24 | } 25 | } 26 | if(flag) 27 | cout<<"1\n"; 28 | else 29 | cout<Problem Name : MEX-OR
4 | >Problem Code : MEXOR
5 | 6 | ## QUESTION LINK : https://www.codechef.com/OCT21C/problems/MEXOR/ 7 | 8 | ### Solution : 9 | ``` 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int tc ; cin>>tc ; 16 | while(tc--) 17 | { 18 | int m=0, n; 19 | cin>>n ; 20 | 21 | if(n==0) cout<<"1\n"; 22 | else if(n>0) 23 | { 24 | while(n>=2*m+1) 25 | { 26 | m=2*m+1; 27 | } 28 | cout< 4 | using namespace std; 5 | #define ll long long 6 | struct myc{ 7 | bool operator()(pair const &p1, pair const &p2){ 8 | return p1.second>t; 15 | while(t--){ 16 | int n; 17 | cin>>n; 18 | vector> m; 19 | for(ll i=0; i>x; 22 | m.push_back({x, i}); 23 | } 24 | sort(m.begin(), m.end()); 25 | ll k =0; 26 | bool t=true; 27 | for(ll i=0; i k){ 29 | m[i].first=k; 30 | k++; 31 | } 32 | else if(m[i].first==k){ 33 | m[i].first=k; 34 | 35 | } 36 | } 37 | sort(m.begin(), m.end(), myc()); 38 | for(ll i=0; i 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int l, r, lRes, rRes; 12 | cin>>l>>r; 13 | lRes = ((l/10)*3)+(l%10==1?0:0)+(l%10==2?1:0)+((l%10>=3 && l%10<=8)?2:0)+(l%10==9?3:0); 14 | rRes = ((r/10)*3)+(r%10==1?0:0)+(r%10==2?1:0)+((r%10>=3 && r%10<=8)?2:0)+(r%10==9?3:0)+((l%10==2 || l%10==3 || l%10==9)?1:0); 15 | // cout< 4 | using namespace std; 5 | 6 | #define pb push_back 7 | #define mp make_pair 8 | #define F first 9 | #define S second 10 | #define ll long long 11 | #define fo(i, j, k, in) for (int i = j; i < k; i += in) 12 | #define refo(i, j, k, in) for (int i = j; i >= k; i -= in) 13 | #define rep(i, j) fo(i, 0, j, 1) 14 | #define rerep(i, j) fo(i, j, 0, 1) 15 | #define all(cont) cont.begin(), cont.end() 16 | #define reall(cont) cont.end(), cont.begin() 17 | #define foreach(it, l) for (auto it = l.begin() l it != l.end(); it++) 18 | #define in(A, B, C) assert(B <= A && A <= C) 19 | #define MOD (int)1e9 20 | #define MOD7 1000000007 21 | #define PI 3.1415926535897932384626433832795 22 | typedef pair PII; 23 | typedef pair PLL; 24 | typedef vector VI; 25 | typedef vector VL; 26 | typedef vector VS; 27 | typedef vector VII; 28 | typedef vector VLL; 29 | typedef vector VVI; 30 | typedef vector VVL; 31 | typedef map MPII; 32 | typedef map MPLL; 33 | typedef set SETI; 34 | typedef multiset MSETI; 35 | bool grt(long long int x, long long int y) 36 | { 37 | return x > y; 38 | } 39 | long exponentiation(long base, long exponent) 40 | { 41 | long t = 1L; 42 | while (exponent > 0) 43 | { 44 | if (exponent % 2 != 0) 45 | t = (t * base) % MOD7; 46 | base = (base * base) % MOD7; 47 | exponent /= 2; 48 | } 49 | return t % MOD7; 50 | } 51 | int fact(int n) 52 | { 53 | int result = 1; 54 | fo(i, 2, n + 1, 1) 55 | result = result * i; 56 | return result; 57 | } 58 | int nPr(int n, int r) 59 | { 60 | return fact(n) / fact(n - r); 61 | } 62 | int nCr(int n, int r) 63 | { 64 | return fact(n) / (fact(r) * fact(n - r)); 65 | } 66 | VL generateSubArray(int arr[], int n) 67 | { 68 | VL tot_subarr; 69 | rep(i, n) 70 | { 71 | fo(j, i, n, 1) 72 | { 73 | fo(k, i, j + 1, 1) 74 | tot_subarr.pb(arr[k]); 75 | } 76 | } 77 | return tot_subarr; 78 | } 79 | VL generateSubsequences(int arr[], int n) 80 | { 81 | VL tot_subsequence; 82 | unsigned int opsize = pow(2, n); 83 | for (int counter = 1; counter < opsize; counter++) 84 | { 85 | for (int j = 0; j < n; j++) 86 | { 87 | if (counter & (1 << j)) 88 | tot_subsequence.pb(arr[j]); 89 | } 90 | } 91 | return tot_subsequence; 92 | } 93 | 94 | int main() 95 | { 96 | ll i, j, k,n, t, p, v, x1, y1, z; 97 | cin >> t; 98 | while (t--) 99 | { 100 | cin>>n; 101 | ll ans=0; 102 | for(int i=0 ; i>x1>>y1; 105 | } 106 | if(n<6) 107 | { 108 | ans=n; 109 | } 110 | else 111 | { 112 | while(n>=3) 113 | { 114 | ans+=(n); 115 | n=n/2; 116 | } 117 | } 118 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int t=0; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n,k,count=0,sum=0; 15 | cin>>n>>k; 16 | int p[n]={0}; 17 | for(int i=0 ; i>p[i]; 20 | if(p[i]>k) 21 | { 22 | count+=1; 23 | sum+=p[i]; 24 | } 25 | } 26 | cout <<(sum-(k*count))<<"\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /CPP/PROB.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/PROB 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | float t,t1,t2,t3,t4; 8 | cin>>t; 9 | for(int i=1;i<=t;i++) 10 | { 11 | cin>>t1>>t2>>t3>>t4; 12 | float a=t1+t2; 13 | cout< 18 | using namespace std; 19 | void printsubsethelper(int input[],int n, int output[], int m){ 20 | if(n==0) 21 | { 22 | for (int i = 0;i> length; 57 | for(int i=0; i < length; i++) 58 | cin >> input[i]; 59 | printSubsetsOfArray(input, length); 60 | } 61 | -------------------------------------------------------------------------------- /CPP/Quick Sort Using Recursion: -------------------------------------------------------------------------------- 1 | /*Sort an array A using Quick Sort.*/ 2 | 3 | 4 | 5 | int partion(int input[],int si,int ei) 6 | { 7 | int count=0; 8 | int x=input[si]; 9 | 10 | for(int i=si+1 ; i<=ei ; i++) 11 | { 12 | if(input[i]<=x) 13 | count++; 14 | } 15 | 16 | count=si+count; //placing element of partion index at right index 17 | int temp=input[count]; 18 | input[count]=input[si]; 19 | input[si]=temp; 20 | 21 | int i=si; 22 | int j=ei; 23 | 24 | while(i=count) //swaping elements such that element less than element of partion index is on left n others on right. 25 | { 26 | if(input[i]<=input[count]) 27 | i++; 28 | 29 | else if (input[j]>input[count]) 30 | j--; 31 | else 32 | { 33 | int temp=input[i]; 34 | input[i]=input[j]; 35 | input[j]=temp; 36 | i++; 37 | j--; 38 | } 39 | 40 | } 41 | 42 | return count; 43 | 44 | 45 | } 46 | 47 | 48 | 49 | void quickSort(int input[],int si,int ei) 50 | { 51 | if(si>ei) 52 | return; 53 | 54 | int pi=partion(input,si,ei); // recursive call to get the index of partion point 55 | 56 | quickSort(input ,si,pi-1); //recusion call on partion-1 part of array 57 | quickSort(input,pi+1,ei); //recursion call on partion+1 part of array 58 | 59 | } 60 | 61 | 62 | 63 | void quickSort(int input[], int size) 64 | { 65 | 66 | quickSort( input,0,size-1); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /CPP/Reverse_the_number.cpp: -------------------------------------------------------------------------------- 1 | //Problem Statement: 2 | /*Reverse The Number Problem Code: FLOW007SolvedSubmit 3 | Given an Integer N, write a program to reverse it. 4 | 5 | Input 6 | The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N. 7 | 8 | Output 9 | For each test case, display the reverse of the given number N, in a new line. 10 | 11 | Constraints 12 | 1 ≤ T ≤ 1000 13 | 1 ≤ N ≤ 1000000 14 | Example 15 | Input 16 | 4 17 | 12345 18 | 31203 19 | 2123 20 | 2300 21 | Output 22 | 54321 23 | 30213 24 | 3212 25 | 32*/ 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | //Declaring the testcase and taking input 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | //Taking the input of a number to reverse it 36 | int n; 37 | cin>>n; 38 | int sum=0; 39 | for (int i=0;n>0;i++) { 40 | int lastdigit=n%10; 41 | sum=sum*10+lastdigit; 42 | n=n/10; 43 | } 44 | cout< 2 | 3 | using namespace std; 4 | int main() { 5 | int arr[4]; 6 | cout << "Enter Four Numbers = "; 7 | for (int i = 0; i < 4; i++) { 8 | cin >> arr[i]; 9 | } 10 | int new_sum[4]; 11 | for (int i = 0; i < 4; i++) { 12 | if (i < 1) { 13 | new_sum[i] = arr[i]; 14 | } else { 15 | new_sum[i] = new_sum[i - 1] + arr[i]; 16 | } 17 | } 18 | 19 | cout << endl << endl << "Running Sum of 1d Array now wil be :: " << endl << endl; 20 | for (int i = 0; i < 4; i++) { 21 | cout << new_sum[i] << " "; 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /CPP/ShellSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void shellSort(int arr[], int n){ 5 | for(int gap = n/2; gap > 0; gap /=2){ 6 | for(int i = gap; i= gap && arr[j-gap] > temp; j -= gap){ 10 | arr[j] = arr[j-gap]; 11 | } 12 | arr[j] = temp; 13 | } 14 | } 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | int arr[n]; 21 | for(int i = 0; i>arr[i]; 23 | } 24 | //Printing before sorting 25 | for(int i = 0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Declare the variables 7 | int a, b, i, j; 8 | 9 | // Ask user to enter lower value of interval please not 10 | // interval < 0 cannot be prime numbers 11 | cout << "Enter lower bound of the interval: "; 12 | cin >> a; // Take input 13 | 14 | // Ask user to enter upper value of interval 15 | cout << "\nEnter upper bound of the interval: "; 16 | cin >> b; // Take input 17 | 18 | // Print display message 19 | cout << "\nPrime numbers between " << a << " and " << b 20 | << " are: "; 21 | 22 | // Explicitly handling the cases when a is less than 2 23 | // since 0 and 1 are not prime numbers 24 | if (a <= 2) 25 | { 26 | a = 2; 27 | if (b >= 2) 28 | { 29 | cout << a << " "; 30 | a++; 31 | } 32 | } 33 | 34 | // MAKING SURE THAT a IS ODD BEFORE WE BEGIN 35 | // THE LOOP 36 | if (a % 2 == 0) 37 | a++; 38 | 39 | // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY 40 | for (i = a; i <= b; i = i + 2) 41 | { 42 | 43 | // flag variable to tell 44 | // if i is prime or not 45 | bool flag = 1; 46 | 47 | // WE TRAVERSE TILL SQUARE ROOT OF j only. 48 | // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR) 49 | for (j = 2; j * j <= i; ++j) 50 | { 51 | if (i % j == 0) 52 | { 53 | flag = 0; 54 | break; 55 | } 56 | } 57 | 58 | // flag = 1 means i is prime 59 | // and flag = 0 means i is not prime 60 | if (flag == 1) 61 | cout << i << " "; 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /CPP/TWOSTR.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codechef.com/problems/TWOSTR 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | string X; 12 | cin>>X; 13 | string Y; 14 | cin>>Y; 15 | bool ans=true; 16 | for(int i=0 ; i 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | unordered_map mp,points,goal_diff; 15 | string s1,s2,vs; 16 | int a,b; 17 | for(int i=0 ; i<12 ; i++) 18 | { 19 | cin>>s1>>a>>vs>>b>>s2; 20 | if(points.find(s1)==points.end() && points.find(s2)==points.end()) 21 | { 22 | if(a>b) 23 | { 24 | points[s1]=3; 25 | points[s2]=0; 26 | } 27 | else if(ab) 43 | { 44 | points[s1]+=3; 45 | points[s2]=0; 46 | } 47 | else if(ab) 63 | { 64 | points[s1]=3; 65 | points[s2]+=0; 66 | } 67 | else if(ab) 83 | { 84 | points[s1]+=3; 85 | points[s2]+=0; 86 | } 87 | else if(a::iterator itr; 103 | int first_point=-1,second_point=-1; 104 | for(itr=points.begin() ; itr!=points.end() ; itr++) 105 | { 106 | if(first_pointsecond) 107 | { 108 | first_point=itr->second; 109 | first_name=itr->first; 110 | } 111 | if(first_point==itr->second) 112 | { 113 | if(goal_diff.at(first_name) < goal_diff.at(itr->first)) 114 | { 115 | first_point=itr->second; 116 | first_name=itr->first; 117 | } 118 | } 119 | } 120 | points.erase(first_name); 121 | goal_diff.erase(first_name); 122 | for(itr=points.begin() ; itr!=points.end() ; itr++) 123 | { 124 | if(second_pointsecond) 125 | { 126 | second_point=itr->second; 127 | second_name=itr->first; 128 | } 129 | if(second_point==itr->second) 130 | { 131 | if(goal_diff.at(second_name) < goal_diff.at(itr->first)) 132 | { 133 | second_point=itr->second; 134 | second_name=itr->first; 135 | } 136 | } 137 | } 138 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int n,m,c,eq=0,v1=0,v2=0,x,y,p; 8 | cin>>n; 9 | cin>>m>>c; 10 | for(int i=0 ; i>x>>y>>p; 13 | eq=y-c-(m*x); 14 | if(eq>0) 15 | v1+=p; 16 | else 17 | v2+=p; 18 | } 19 | if(v1>=v2) 20 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int t=0; 11 | cin>>t; 12 | while(t--) 13 | { 14 | string s; 15 | cin>>s; 16 | int s_len=s.length(),pairs=0; 17 | for(int i=0 ; i 4 | using namespace std; 5 | 6 | /*---------------------------------------------------------------*/ 7 | #define int long long int 8 | #define fl(i, a, b) for(int i = a; i 10 | #define PB push_back 11 | #define mp make_pair 12 | #define tc int t; cin>>t; while(t--) 13 | #define aa(arr,n) int arr[n]; fl(i,0,n) cin>>arr[i]; 14 | #define av(arr, n) vector arr(n); fl(i,0,n) cin>>arr[i]; 15 | #define vvi vector> 16 | #define all(x) (x).begin(), (x).end() 17 | #define pii pair 18 | #define mii map 19 | #define R return 20 | #define B break 21 | #define ff first 22 | #define ss second 23 | #define rmin(a,b) (a=min((a), (b))) 24 | #define rmax(a,b) (a=max((a), (b))) 25 | #define C continue 26 | #define lcm(a,b) (a/__gcd(a, b))*b 27 | #define mod 1000000007 28 | /*--------------------------------------------------------------*/ 29 | 30 | 31 | void solve(){ 32 | int n, k; 33 | cin>>n>>k; 34 | string clr; 35 | cin>>clr; 36 | char curr = clr[0]; 37 | int islands = 0; 38 | 39 | for(int i = 0; i=0; i--){ 55 | if(clr[i]!=curr){ 56 | cout< 11 | using namespace std; 12 | #ifndef ONLINE_JUDGE 13 | #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) 14 | #else 15 | #define dbg(x...) 16 | #endif 17 | #define ll long long 18 | #define lf long double 19 | #define mp make_pair 20 | #define vi vector 21 | #define pii pair 22 | #define ff first 23 | #define ss second 24 | #define fx(x) cout<=a;i--) 32 | #define lpz(i,a,b) for(int i=0;i isPrime(1000001,true); 44 | void sieve(){ 45 | isPrime[1]=false; 46 | for(int i=2;(i*i)<=1000001;i++){ 47 | if(isPrime[i]){ 48 | for(int j=i;i*j<=1000001;j++){ 49 | isPrime[i*j]=false; 50 | } 51 | } 52 | } 53 | } 54 | */ 55 | ll modulo(ll a, ll b) 56 | { 57 | ll r = a % b; 58 | return r < 0 ? r + b : r; 59 | } 60 | void solve(){ 61 | ll a,b,n,m; 62 | cin>>a>>b>>n>>m; 63 | ll mx; 64 | if(a>b){mx=a;a=b; b=mx;} 65 | if(a> t; 76 | while (t--) 77 | { 78 | solve(); 79 | } 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /CPP/ab_pallindrom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define pb push_back 3 | #define endl "\n"; 4 | #define mp make_pair 5 | #define mod 1000000007 6 | #define ll long long 7 | #define all(x) (x).begin(), (x).end() 8 | #define lp(i,a,b) for(int i=a;i<=b;i++) 9 | #define lpz(i,a,b) for(int i=a;i>a>>b; 30 | string s; 31 | cin>>s; 32 | int n=s.length(); 33 | int cnt1=0,cnt0=0; 34 | lpz(i,0,n){ 35 | if(s[i]=='1')cnt1++; 36 | else if(s[i]=='0')cnt0++; 37 | 38 | } 39 | if(cnt0>a||cnt1>b){ 40 | cout<<"-1\n"; 41 | return; 42 | } 43 | a-=cnt0; 44 | b-=cnt1; 45 | lpz(i,0,n/2){ 46 | if(s[i]=='?'){ 47 | if(s[n-1-i]=='?') continue; 48 | if(s[n-1-i]=='0') a--; 49 | else b--; 50 | s[i]=s[n-1-i]; 51 | } 52 | else{ 53 | if(s[n-1-i]=='?'){ 54 | if(s[i]=='1') b--; 55 | else a--; 56 | s[n-1-i]=s[i]; 57 | } 58 | else if(s[i]!=s[n-1-i]){ 59 | cout<<"-1\n"; 60 | return; 61 | } 62 | } 63 | if(a<0||b<0){ 64 | cout<<"-1\n"; 65 | return; 66 | 67 | } 68 | 69 | } 70 | lpz(i,0,n/2){ 71 | if(s[i]=='?'){ 72 | if(a>1){ 73 | s[i]='0'; 74 | s[n-1-i]='0'; 75 | a=a-2; 76 | } 77 | else if(b>1){ 78 | s[i]='1'; s[n-1-i]='1'; 79 | b=b-2; 80 | //continue; 81 | } 82 | else{ 83 | cout<<"-1\n"; 84 | return; 85 | } 86 | } 87 | } 88 | 89 | if(n%2!=0 && s[n/2]=='?'){ 90 | if(a>0){ 91 | s[n/2]='0'; 92 | } 93 | else 94 | s[n/2]='1'; 95 | 96 | } 97 | cout<> t; 106 | while (t--) 107 | { 108 | solve(); 109 | } 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /CPP/alice_bob_candies.cpp: -------------------------------------------------------------------------------- 1 | // CODE by: ############ 2 | // ## 3 | // ## # #### 4 | // ## ## # 5 | // ## ## 6 | // ## ## ## # ## ## 7 | // ## ## ## # # ## # ## 8 | // ## ## # ## ## ## ## # ## 9 | // ###### ########## #### # ####### ## ### 10 | #include 11 | using namespace std; 12 | #ifndef ONLINE_JUDGE 13 | #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) 14 | #else 15 | #define dbg(x...) 16 | #endif 17 | #define ll long long 18 | #define lf long double 19 | #define mp make_pair 20 | #define vi vector 21 | #define pii pair 22 | #define ff first 23 | #define ss second 24 | #define fx(x) cout<=a;i--) 32 | #define lpz(i,a,b) for(int i=0;i isPrime(1000001,true); 44 | void sieve(){ 45 | isPrime[1]=false; 46 | for(int i=2;(i*i)<=1000001;i++){ 47 | if(isPrime[i]){ 48 | for(int j=i;i*j<=1000001;j++){ 49 | isPrime[i*j]=false; 50 | } 51 | } 52 | } 53 | } 54 | */ 55 | void solve(){ 56 | int n; 57 | cin>>n; 58 | int arr[n]; 59 | lpz(i,0,n)cin>>arr[i]; 60 | int i=1,j=n-1; 61 | int as=arr[0]; 62 | int bs=0; 63 | int a=as,b=0; 64 | int mv=1; 65 | t: 66 | bool ch=false; 67 | while(bs<=as && j>=i){ 68 | bs+=arr[j]; 69 | j--; 70 | ch=true; 71 | } 72 | b+=bs; 73 | as=0; 74 | if(ch)mv++; 75 | ch=false; 76 | while(as<=bs && i<=j){ 77 | as+=arr[i]; 78 | i++; 79 | ch=true; 80 | } 81 | a+=as; 82 | bs=0; 83 | if(ch)mv++; 84 | ch=false; 85 | if(i>j){ 86 | cout<> t; 99 | while (t--) 100 | { 101 | solve(); 102 | } 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /CPP/alomst_equal.cpp: -------------------------------------------------------------------------------- 1 | 2 | // CODE by: ############ 3 | // ## 4 | // ## # #### 5 | // ## ## # 6 | // ## ## 7 | // ## ## ## # ## ## 8 | // ## ## ## # # ## # ## 9 | // ## ## # ## ## ## ## # ## 10 | // ###### ########## #### # ####### ## ### 11 | #include 12 | using namespace std; 13 | #ifndef ONLINE_JUDGE 14 | #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) 15 | #else 16 | #define dbg(x...) 17 | #endif 18 | #define ll long long 19 | #define lf long double 20 | #define mp make_pair 21 | #define vi vector 22 | #define set set 23 | #define pii pair 24 | #define ff first 25 | #define ss second 26 | #define fx(x) cout<=a;i--) 34 | #define lpz(i,a,b) for(int i=a;i isPrime(1000001,true); 45 | void sieve(){ 46 | isPrime[1]=false; 47 | for(int i=2;(i*i)<=1000000;i++){ 48 | if(isPrime[i]){ 49 | for(int j=i;i*j<=1000000;j++){ 50 | isPrime[i*j]=false; 51 | } 52 | } 53 | } 54 | } 55 | */ 56 | void solve(){ 57 | int n; 58 | cin>>n; 59 | if(n&1){ 60 | cout<<"YES\n"; 61 | lp(i,1,n){ 62 | cout<<2*i-i%2<<" "; 63 | } 64 | lp(i,1,n){ 65 | cout<<2*i+i%2-1<<" "; 66 | } 67 | cout<> t; 82 | while (t--) 83 | { 84 | solve(); 85 | } 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /CPP/alomst_rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(){ 4 | int n; 5 | cin>>n; 6 | char arr[n][n]; 7 | for(int i=0;i>arr[i][j]; 11 | } 12 | 13 | 14 | } 15 | int t1=0,t2=0,t3=0,t4=0,k=1; 16 | for(int i=0;i>T; 70 | while(T--){ 71 | solve(); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /CPP/alphabetical.cpp: -------------------------------------------------------------------------------- 1 | // CODE by: ############ 2 | // ## 3 | // ## # #### 4 | // ## ## # 5 | // ## ## 6 | // ## ## ## # ## ## 7 | // ## ## ## # # ## # ## 8 | // ## ## # ## ## ## ## # ## 9 | // ###### ########## #### # ####### ## ### 10 | #include 11 | using namespace std; 12 | #ifndef ONLINE_JUDGE 13 | #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) 14 | #else 15 | #define dbg(x...) 16 | #endif 17 | #define ll long long 18 | #define lf long double 19 | #define mp make_pair 20 | #define vi vector 21 | #define pii pair 22 | #define ff first 23 | #define ss second 24 | #define fx(x) cout<=a;i--) 32 | #define lpz(i,a,b) for(int i=0;i isPrime(1000001,true); 44 | void sieve(){ 45 | isPrime[1]=false; 46 | for(int i=2;(i*i)<=1000001;i++){ 47 | if(isPrime[i]){ 48 | for(int j=i;i*j<=1000001;j++){ 49 | isPrime[i*j]=false; 50 | } 51 | } 52 | } 53 | } 54 | */ 55 | 56 | void solve(){ 57 | string s; 58 | cin>>s; 59 | char t = 'a' + (sz(s)-1); 60 | while (sz(s)) 61 | { 62 | if (s.back()==t) 63 | { 64 | s.pop_back(); 65 | } 66 | else if (s[0] == t) 67 | { 68 | s.erase(s.begin()); 69 | } 70 | else 71 | { 72 | cout << "NO\n"; 73 | return; 74 | } 75 | t--; 76 | } 77 | cout << "YES\n"; 78 | /* vi v; 79 | lpz(i,0,sz(s)){ 80 | v.pb(s[i]-'0'); 81 | } 82 | sort(all(v)); 83 | if(v[0]!='a'-'0'){ 84 | cout<<"NO\n"; 85 | return; 86 | } 87 | int k=49; 88 | for(int i=0;i> t; 105 | while (t--) 106 | { 107 | solve(); 108 | } 109 | return 0; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /CPP/amusing_joke.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define pb push_back 3 | #define lp(i,a,b) for(int i=a;i<=b;i++) 4 | #define lpz(i,a,b) for(int i=a;i>s>>s1>>s2; 29 | s3=s+s1; 30 | sort(s3.begin(),s3.end()); 31 | sort(s2.begin(),s2.end()); 32 | if(s3.length()!=s2.length())cout<<"NO\n"; 33 | else { 34 | lpz(i,0,s2.length()){ 35 | if(s3[i]!=s2[i]){ 36 | cout<<"NO\n"; 37 | return; 38 | } 39 | } 40 | cout<<"YES\n"; 41 | } 42 | 43 | 44 | } 45 | 46 | signed main() 47 | { 48 | ios::sync_with_stdio(0); 49 | cin.tie(NULL); cout.tie(NULL); 50 | //long long t; 51 | //cin >> t; 52 | //while (t--) 53 | //{ 54 | solve(); 55 | //} 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /CPP/andrew_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define long long int; 4 | 5 | 6 | void solve(){ 7 | string s; 8 | cin>>s; 9 | int n; 10 | n=s.length(); 11 | map mp; 12 | vector v; 13 | for(int i=0;i>T; 36 | while(T--){ 37 | solve(); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /CPP/anton_currency.cpp: -------------------------------------------------------------------------------- 1 | // CODE by: ############ 2 | // ## 3 | // ## # #### 4 | // ## ## # 5 | // ## ## 6 | // ## ## ## # ## ## 7 | // ## ## ## # # ## # ## 8 | // ## ## # ## ## ## ## # ## 9 | // ###### ########## #### # ####### ## ### 10 | #include 11 | using namespace std; 12 | #ifndef ONLINE_JUDGE 13 | #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) 14 | #else 15 | #define dbg(x...) 16 | #endif 17 | #define ll long long 18 | #define lf long double 19 | #define mp make_pair 20 | #define vi vector 21 | #define pii pair 22 | #define ff first 23 | #define ss second 24 | #define fx(x) cout<=a;i--) 32 | #define lpz(i,a,b) for(int i=0;i isPrime(1000001,true); 44 | void sieve(){ 45 | isPrime[1]=false; 46 | for(int i=2;(i*i)<=1000001;i++){ 47 | if(isPrime[i]){ 48 | for(int j=i;i*j<=1000001;j++){ 49 | isPrime[i*j]=false; 50 | } 51 | } 52 | } 53 | } 54 | */ 55 | ll modulo(ll a, ll b) 56 | { 57 | ll r = a % b; 58 | return r < 0 ? r + b : r; 59 | } 60 | void solve(){ 61 | string s; 62 | cin>>s; 63 | int n=sz(s)-1; 64 | lpz(i,0,n-1){ 65 | if(s[i]%2==0 && s[i]> t; 88 | while (t--) 89 | { 90 | solve(); 91 | } 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /CPP/aqua_moon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define pb push_back 3 | #define endl "\n"; 4 | #define mp make_pair 5 | #define mod 1000000007 6 | #define ll long long 7 | #define all(x) (x).begin(), (x).end() 8 | #define lp(i,a,b) for(int i=a;i<=b;i++) 9 | #define lpz(i,a,b) for(int i=a;i>n; 31 | int a[n],b[n]; 32 | lpz(i,0,n) cin>>a[i]; 33 | lpz(j,0,n){ 34 | cin>>b[j]; 35 | sum+=b[j]-a[j]; 36 | } 37 | vector v1,v2; 38 | if(sum!=0) { 39 | cout<<"-1\n"; 40 | return; 41 | } 42 | 43 | else { 44 | lpz(i,0,n){ 45 | while(b[i]-a[i]>0){ 46 | v1.pb(i+1); 47 | a[i]++; 48 | } 49 | while(b[i]-a[i]<0){ 50 | v2.pb(i+1); 51 | a[i]--; 52 | } 53 | } 54 | cout<> t; 69 | while (t--) 70 | { 71 | solve(); 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /CPP/armstrongno.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,originalno; 8 | cin>>n; 9 | originalno=n; 10 | int lastdigit; 11 | int sum=0; 12 | while(n>0) 13 | { 14 | lastdigit=n%10; 15 | sum+=pow(lastdigit,3); 16 | n/=10; 17 | } 18 | 19 | if(sum==originalno) 20 | { 21 | printf("armstrong no"); 22 | } 23 | else 24 | { 25 | printf("not an armstrong no"); 26 | } 27 | 28 | 29 | 30 | } -------------------------------------------------------------------------------- /CPP/array_rotation.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program to rotate an array by 3 | // d elements 4 | #include 5 | using namespace std; 6 | 7 | /*Function to left Rotate arr[] of 8 | size n by 1*/ 9 | void leftRotatebyOne(int arr[], int n) 10 | { 11 | int temp = arr[0], i; 12 | for (i = 0; i < n - 1; i++) 13 | arr[i] = arr[i + 1]; 14 | 15 | arr[n-1] = temp; 16 | } 17 | 18 | /*Function to left rotate arr[] of size n by d*/ 19 | void leftRotate(int arr[], int d, int n) 20 | { 21 | for (int i = 0; i < d; i++) 22 | leftRotatebyOne(arr, n); 23 | } 24 | 25 | /* utility function to print an array */ 26 | void printArray(int arr[], int n) 27 | { 28 | for (int i = 0; i < n; i++) 29 | cout << arr[i] << " "; 30 | } 31 | 32 | /* Driver program to test above functions */ 33 | int main() 34 | { 35 | int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; 36 | int n = sizeof(arr) / sizeof(arr[0]); 37 | 38 | // Function calling 39 | leftRotate(arr, 2, n); 40 | printArray(arr, n); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /CPP/circularqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define n 5 3 | using namespace std; 4 | 5 | int queue[n],front=-1,rear=-1; 6 | 7 | bool isFull(){ 8 | if (front == 0 && rear == n - 1) { 9 | return true; 10 | } 11 | else if(front == rear + 1){ 12 | return true; 13 | } 14 | return false; 15 | } 16 | 17 | bool isEmpty(){ 18 | if (front == -1){ 19 | return true; 20 | } 21 | return false; 22 | } 23 | 24 | void Insert(){ 25 | int val; 26 | if (isFull()){ 27 | cout<<"Queue Overflow"<>val; 34 | rear= (rear+1)%n; 35 | queue[rear]=val; 36 | } 37 | } 38 | void Delete(){ 39 | if(isEmpty()){ 40 | cout<<"Queue Underflow"<>ch; 65 | switch (ch) { 66 | case 1: Insert(); 67 | break; 68 | case 2: Delete(); 69 | break; 70 | case 3: Display(); 71 | break; 72 | case 4: cout<<"Exit"< 7 | using namespace std; 8 | 9 | const int INF = 1e9; 10 | vector>> adj; 11 | 12 | void dijkstra(int s, vector & d, vector & p) { 13 | int n = adj.size(); 14 | d[s] = 0; 15 | using pii = pair; 16 | priority_queue, greater> q; 17 | q.push({0, s}); 18 | while (!q.empty()) { 19 | int v = q.top().second; 20 | int d_v = q.top().first; 21 | q.pop(); 22 | if (d_v != d[v]) 23 | continue; 24 | 25 | for (auto edge : adj[v]) { 26 | int to = edge.first; 27 | int len = edge.second; 28 | 29 | if (d[v] + len < d[to]) { 30 | d[to] = d[v] + len; 31 | p[to] = v; 32 | q.push({d[to], to}); 33 | } 34 | } 35 | } 36 | } 37 | 38 | void solve() { 39 | int n; cin >> n; // number of vertices; 40 | int m; cin >> m; // numner of edges; 41 | adj.resize(n + 1); 42 | for(int i = 0; i < m; i++) { 43 | int a, b, c; // edge from a -> b with distance as c; 44 | cin >> a >> b >> c; 45 | adj[a].push_back({b, c}); 46 | adj[b].push_back({a, c}); 47 | } 48 | vector distance, predecessors; 49 | distance.assign(n + 1, INF); 50 | predecessors.assign(n + 1, -1); 51 | dijkstra(1, distance, predecessors); 52 | // distance vector now contains shortest distance from node 1 to kth node (where 2 <= k <= n); 53 | } 54 | 55 | int main() { 56 | solve(); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /CPP/insertionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void insersort(int arr[]){ 4 | int i,j=0,key=0; 5 | for(i=1;i<5;i++){ 6 | j=i-1; 7 | key = arr[i]; 8 | while(j>=0 && arr[j]>key){ 9 | arr[j+1]= arr[j]; 10 | j=j-1; 11 | } 12 | arr[j+1]=key; 13 | } 14 | } 15 | int main(){ 16 | int myarr[5]; 17 | int i; 18 | 19 | for(i=0;i<5;i++){ 20 | cout<<"Enter ("<>myarr[i]; 22 | } 23 | cout<<"Before Sorting: "; 24 | for(i=0;i<5;i++){ 25 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | // function that ask an integer and returns it 7 | int ask_number(){ 8 | 9 | // declaring variable named number as an integer 10 | int number ; 11 | 12 | // ask for input and save it in number 13 | std::cin >> number; 14 | 15 | // loop that check wether number is an integer 16 | while (cin.fail() || number < 0 ) { 17 | 18 | std::cout << "Enter a positive integer please" << '\n'; 19 | 20 | // clearing cin.fail() status 21 | cin.clear(); 22 | // clearing the iostream buffer 23 | cin.ignore(256,'\n'); 24 | // ask for input and save it in number 25 | std::cin >> number; 26 | 27 | } 28 | 29 | // returning the number for the function ask_number() 30 | return number; 31 | } 32 | 33 | int main () { 34 | 35 | // declaring integer to store starting , ending and total years 36 | int fromYear , toYear, total_years =0 ; 37 | 38 | std::cout << "Enter the year you wanna start" << '\n'; 39 | // run the function ask_number() and store it's return fromYear 40 | fromYear = ask_number(); 41 | 42 | std::cout << "Enter the year you wanna end" << '\n'; 43 | // run the function ask_number() and store it's return toYear 44 | toYear = ask_number(); 45 | 46 | // loop that keep running until to years are greater than from years 47 | while (toYear<=fromYear) { 48 | std::cout << "ending year should be smaller then starting year" << '\n'; 49 | // run the function ask_number() and store it's return toYear 50 | toYear = ask_number(); 51 | } 52 | 53 | // loop that run from starting years to ending years 54 | for (int i = fromYear-1; i < toYear+1; i++) { 55 | 56 | // check if the year is divisble by four and not divisble by 100 57 | // or if its divisible by both four hunder and one hundred 58 | if ((i%100)!=0 ? (i%4==0) : i%400==0 ) { 59 | std::cout << i << '\n'; 60 | // incrementing the total leap years counter 61 | total_years++; 62 | } 63 | 64 | } 65 | 66 | // displaying total leaps years 67 | std::cout << "total number o fleap years in betweeen :"<< total_years << '\n'; 68 | 69 | return 0; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /CPP/linearsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define x 1000 3 | using namespace std; 4 | 5 | int main(){ 6 | int arr[x]; 7 | int n,i,j,a; 8 | cout<<"Enter the length of the array: "; 9 | cin>>n; 10 | for(i=0;i>arr[i]; 13 | } 14 | cout<<"Enter the number you want to search: "; 15 | cin>>a; 16 | for(j=0;j 2 | using namespace std; 3 | 4 | int main() { 5 | // your code goes here 6 | int n; 7 | cin>>n; 8 | while(n--){ 9 | int a,b,c; 10 | cin>>a>>b>>c; 11 | if(a==7 || b==7 || c==7){ 12 | cout<<"YES"< 11 | using namespace std; 12 | 13 | int majority_element(int n, vector v){ 14 | int max_freq=0,maj_ele; 15 | map m; 16 | for(auto x:v){ 17 | m[x]++; 18 | if(m[x]>max_freq) { 19 | maj_ele=x; 20 | max_freq=m[x]; 21 | } 22 | } 23 | return maj_ele; 24 | } 25 | 26 | int main(){ 27 | int n; 28 | vector v; 29 | cout<<"Enter the size of the array:\n"; 30 | cin>>n; 31 | cout<<"Enter the elements:\n"; 32 | for(int i=0;i>temp; 35 | v.push_back(temp); 36 | } 37 | cout< 12 | using namespace std; 13 | #define mp make_pair 14 | #define vi vector 15 | #define pii pair 16 | #define ff first 17 | #define ss second 18 | #define endl "\n"; 19 | #define pb push_back 20 | #define mod 1000000007 21 | #define ll long long 22 | #define lf long double 23 | #define all(x) (x).begin(), (x).end() 24 | #define allr(x) (x).rbegin(), (x).rend() 25 | #define lp(i,a,b) for(int i=a;i<=b;i++) 26 | #define lpz(i,a,b) for(int i=a;i isPrime(1000001,true); 39 | void sieve(){ 40 | isPrime[1]=false; 41 | for(int i=2;(i*i)<=1000000;i++){ 42 | if(isPrime[i]){ 43 | for(int j=i;i*j<=1000000;j++){ 44 | isPrime[i*j]=false; 45 | } 46 | } 47 | } 48 | } 49 | */ 50 | void solve(){ 51 | int n; 52 | cin>>n; 53 | int arr[10000]; 54 | lp(i,1,n){ 55 | cin>>arr[i]; 56 | } 57 | if(arr[1]){ 58 | cout<<(n+1)<<" "; 59 | lp(i,1,n){ 60 | cout<> t; 91 | while (t--) 92 | { 93 | solve(); 94 | } 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /CPP/n queens.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | bool isSafe(int row, int col, vector Board, int n) 9 | { 10 | int rowcopy = row; 11 | int colcopy = col; 12 | 13 | while (row >= 0 && col >= 0) 14 | { 15 | if (Board[row][col] == 'Q') 16 | return false; 17 | row--; 18 | col--; 19 | } 20 | row = rowcopy; 21 | col = colcopy; 22 | while (col >= 0) 23 | { 24 | if (Board[row][col] == 'Q') 25 | return false; 26 | col--; 27 | } 28 | row = rowcopy; 29 | col = colcopy; 30 | while (row < n && col >= 0) 31 | { 32 | if (Board[row][col] == 'Q') 33 | return false; 34 | row++; 35 | col--; 36 | } 37 | return true; 38 | } 39 | void solve(int col, int n, vector &Board, vector> &ans) 40 | { 41 | if (col == n) 42 | { 43 | ans.push_back(Board); 44 | return; 45 | } 46 | for (int row = 0; row < n; row++) 47 | { 48 | if (isSafe(row, col, Board, n)) 49 | { 50 | Board[row][col] = 'Q'; 51 | solve(col + 1, n, Board, ans); 52 | Board[row][col] = '.'; 53 | } 54 | } 55 | } 56 | vector> solveNQueens(int n) 57 | { 58 | vector> ans; 59 | string s(n, '.'); 60 | vector Board(n); 61 | for (int i = 0; i < n; i++) 62 | Board[i] = s; 63 | solve(0, n, Board, ans); 64 | return ans; 65 | } 66 | }; 67 | 68 | int main() 69 | { 70 | int n; 71 | cin >> n; 72 | Solution ob; 73 | vector> ans = ob.solveNQueens(n); 74 | for (int i = 0; i < n; i++) 75 | { 76 | for (int j = 0; j < n; j++) 77 | { 78 | cout << ans[i][j] << "\n"; 79 | } 80 | cout << "\n"; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /CPP/pivot_index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int flag=0; 8 | int arr[] = {1,4,6,6,5,6}; 9 | int n = sizeof(arr)/sizeof(arr[0]); 10 | /* //uncomment this if you need to get your own input 11 | int n; 12 | cout << "Enter the no of elements : " ; 13 | cin >> n; 14 | int arr[n]; 15 | for(int i=0;i> arr[i]; 19 | } 20 | */ 21 | int suml,sumr; // finding size or no of elements in the array 22 | for(int i=0;ij) //if the index is greater we add all elements to sum on left 29 | suml += arr[j]; 30 | else //if the index is greater we add all elements to sum on left 31 | sumr += arr[j]; 32 | } 33 | if(suml == sumr) 34 | { 35 | cout << "Pivot point : " << i ; 36 | flag=1; 37 | break; 38 | } 39 | } 40 | if(flag == 0) 41 | cout <<"Pivot point not found -1"; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /CPP/quadrilateral_finder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // programm should be run and compile on GNU/linux platform with g++ compiler 8 | // cause unistd.h header is specific to GNU/linux platform 9 | 10 | // function that will ask to enter yes or no depending what he want 11 | char ask_option(){ 12 | 13 | // declaring character for storing user choice 14 | char choice; 15 | 16 | // asking for input and saving it in choice 17 | std::cin >> choice; 18 | 19 | // loop that will keep asking for input unless it is a Y,y,N or n 20 | while (choice != 'n' && choice != 'y' && choice != 'Y' && choice != 'N') { 21 | std::cout << "wrong input entered try again" << '\n'; 22 | // asking for inout and saving it in choice 23 | std::cin >> choice; 24 | } 25 | 26 | // returning choice for the function ask_option() 27 | return choice; 28 | 29 | } 30 | 31 | int main () { 32 | 33 | // declaring variable for storing user choice 34 | char choice ; 35 | 36 | // telling user what the programm is for 37 | cout << "Think of a quadrilateral from square, rectangle, kite, rhombus, trapezium and parallelogram and I will guess it... \n" ; 38 | cout << "press any key to continue " < 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int key; 6 | int data; 7 | Node * next; 8 | Node(){ 9 | key = 0; 10 | data = 0; 11 | next = NULL; 12 | } 13 | Node(int k ,int d){ 14 | key = k; 15 | data = d; 16 | next = NULL; 17 | } 18 | }; 19 | 20 | class Queue{ 21 | public: 22 | Node * front; 23 | Node * rear; 24 | 25 | Queeu(){ 26 | front = NULL; 27 | rear = NULL; 28 | } 29 | bool isEmpty(){ 30 | if(front==NULL && rear==NULL){ 31 | return true; 32 | }else{ 33 | return false; 34 | } 35 | } 36 | bool CheckExistedNode(Node * n){ 37 | Node * temp = front; 38 | bool check = false; 39 | while(temp!=NULL){ 40 | if(temp->key==n->key){ 41 | check = true; 42 | break; 43 | } 44 | temp=temp->next; 45 | } 46 | return check; 47 | } 48 | void enqueue(Node * n){ 49 | if (isEmpty()==true){ 50 | front = n; 51 | rear = n; 52 | cout<<"Node Inserted"<next = n; 59 | rear=n; 60 | cout<<"Node inserted at rear"<next; 78 | return temp; 79 | } 80 | 81 | } 82 | } 83 | int count() { 84 | int count = 0; 85 | Node * temp = front; 86 | while (temp != NULL) { 87 | count++; 88 | temp = temp -> next; 89 | } 90 | return count; 91 | } 92 | 93 | void display(){ 94 | if(isEmpty()) 95 | { 96 | cout << "Queue is Empty" << endl; 97 | } 98 | else 99 | { 100 | cout << "All values in the Queue are :" << endl; 101 | Node *temp=front; 102 | while(temp!=NULL) 103 | { 104 | cout<<"("<key<<","<data<<")"<<" -> "; 105 | temp=temp->next; 106 | } 107 | cout<> option; 126 | 127 | //Node n1 = new Node(); 128 | Node * new_node = new Node(); 129 | 130 | 131 | switch (option) { 132 | case 0: 133 | break; 134 | case 1: 135 | cout << "ENQUEUE Function Called -" <> key; 140 | cin >> data; 141 | new_node->key = key; 142 | new_node->data = data; 143 | q.enqueue(new_node); 144 | break; 145 | case 2: 146 | cout << "DEQUEUE Function Called - " <key<<"," 149 | <data<<")"; 150 | delete new_node; 151 | cout< 3 | 4 | // Maze size 5 | #define N 4 6 | 7 | bool solveMazeUtil( 8 | int maze[N][N], int x, 9 | int y, int sol[N][N]); 10 | 11 | /* A utility function to print solution matrix sol[N][N] */ 12 | void printSolution(int sol[N][N]) 13 | { 14 | for (int i = 0; i < N; i++) { 15 | for (int j = 0; j < N; j++) 16 | printf(" %d ", sol[i][j]); 17 | printf("\n"); 18 | } 19 | } 20 | 21 | /* A utility function to check if x, y is valid index for N*N maze */ 22 | bool isSafe(int maze[N][N], int x, int y) 23 | { 24 | // if (x, y outside maze) return false 25 | if ( 26 | x >= 0 && x < N && y >= 0 27 | && y < N && maze[x][y] == 1) 28 | return true; 29 | 30 | return false; 31 | } 32 | 33 | /* This function solves the Maze problem using Backtracking. It mainly uses solveMazeUtil() to solve the problem. 34 | It returns false if no path is possible, otherwise return true and prints the path in the form of 1s. Please note that there 35 | may be more than one solutions, this function prints one of the feasible solutions.*/ 36 | 37 | bool solveMaze(int maze[N][N]) 38 | { 39 | int sol[N][N] = { { 0, 0, 0, 0 }, 40 | { 0, 0, 0, 0 }, 41 | { 0, 0, 0, 0 }, 42 | { 0, 0, 0, 0 } }; 43 | 44 | if (solveMazeUtil( 45 | maze, 0, 0, sol) 46 | == false) { 47 | printf("Solution doesn't exist"); 48 | return false; 49 | } 50 | 51 | printSolution(sol); 52 | return true; 53 | } 54 | 55 | /* A recursive utility function to solve Maze problem */ 56 | 57 | bool solveMazeUtil( 58 | int maze[N][N], int x, 59 | int y, int sol[N][N]) 60 | { 61 | // if (x, y is goal) return true 62 | if ( 63 | x == N - 1 && y == N - 1 64 | && maze[x][y] == 1) { 65 | sol[x][y] = 1; 66 | return true; 67 | } 68 | 69 | // Check if maze[x][y] is valid 70 | if (isSafe(maze, x, y) == true) { 71 | // Check if the current block is already part of solution path. 72 | if (sol[x][y] == 1) 73 | return false; 74 | 75 | // mark x, y as part of solution path 76 | sol[x][y] = 1; 77 | 78 | /* Move forward in x direction */ 79 | if (solveMazeUtil( 80 | maze, x + 1, y, sol) 81 | == true) 82 | return true; 83 | 84 | /* If moving in x direction doesn't give solution then Move down in y direction */ 85 | if (solveMazeUtil( 86 | maze, x, y + 1, sol) 87 | == true) 88 | return true; 89 | 90 | /* If none of the above movements 91 | work then BACKTRACK: unmark 92 | x, y as part of solution path */ 93 | sol[x][y] = 0; 94 | return false; 95 | } 96 | 97 | return false; 98 | } 99 | 100 | // driver program to test above function 101 | int main() 102 | { 103 | int maze[N][N] = { { 1, 0, 0, 0 }, 104 | { 1, 1, 0, 1 }, 105 | { 0, 1, 0, 0 }, 106 | { 1, 1, 1, 1 } }; 107 | 108 | solveMaze(maze); 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /CPP/roman_to_integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int romanToInt(string s) { 8 | const static string M[] = {"", "M", "MM", "MMM"}; 9 | const static string C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; 10 | const static string X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; 11 | const static string I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; 12 | 13 | const static int l[] = {0, 1, 2, 3, 2, 1, 2, 3, 4, 2}; 14 | 15 | int num=0, v, len, pos = 0; 16 | for (int i=0; i<4; ++i) if (s.find(M[i]) == 0) {v = i; len=l[i];} 17 | num = v; pos = pos + len; 18 | for (int i=0; i<10; ++i) if (s.find(C[i], pos) == pos) {v = i; len=l[i];} 19 | num = 10*num + v; pos = pos + len; 20 | for (int i=0; i<10; ++i) if (s.find(X[i], pos) == pos) {v = i; len=l[i];} 21 | num = 10*num + v; pos = pos + len; 22 | for (int i=0; i<10; ++i) if (s.find(I[i], pos) == pos) {v = i; len=l[i];} 23 | num = 10*num + v; 24 | return num; 25 | } 26 | }; 27 | int main(){ 28 | string s; 29 | cin>>s; 30 | Solution ss; 31 | int k; 32 | k=ss.romanToInt(s); 33 | cout< 2 | using namespace std; 3 | 4 | void swap(int*a, int*b){ 5 | int temp=*a; 6 | *a=*b; 7 | *b=temp; 8 | } 9 | 10 | void reverse(int*arr,int l,int r){ 11 | while(l 2 | #define x 1000 3 | using namespace std; 4 | void selecsort(int arr[]){ 5 | int i,j; 6 | for(i=0;i<4;i++){ 7 | int min = i; 8 | for(j=i+1;j<5;j++){ 9 | if(arr[j]>n; 26 | for(i=0;i>myarr[i]; 29 | } 30 | cout<<"Before Sorting: "; 31 | for(i=0;i 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int key; 6 | int data; 7 | Node * next; 8 | Node(){ 9 | key = 0; 10 | data = 0; 11 | next = NULL; 12 | } 13 | Node(int k ,int d){ 14 | key = k; 15 | data = d; 16 | next = NULL; 17 | } 18 | }; 19 | class Stack{ 20 | public: 21 | Node * top; 22 | 23 | Stak(){ 24 | top = NULL; 25 | } 26 | bool isEmpty(){ 27 | if(top==NULL){ 28 | return true; 29 | }else{ 30 | return false; 31 | } 32 | } 33 | bool CheckExistedNode(Node * n){ 34 | Node * temp = top; 35 | bool check = false; 36 | while(temp!=NULL){ 37 | if(temp->key==n->key){ 38 | check = true; 39 | break; 40 | } 41 | temp=temp->next; 42 | } 43 | return check; 44 | } 45 | void push(Node * n){ 46 | if(top == NULL){ 47 | top = n; 48 | cout<<"Pushed on first"<next = temp; 55 | cout<<"Node Pushed"<next; 66 | return temp; 67 | } 68 | } 69 | Node * peek(){ 70 | Node * temp = NULL; 71 | if(isEmpty()){ 72 | cout <<"Stack Underflow"< next; 84 | } 85 | return count; 86 | } 87 | void display(){ 88 | Node * temp = top; 89 | while(temp!=NULL){ 90 | cout << "(" << temp -> key << "," << temp -> data << ")" << " -> " < next; 92 | } 93 | } 94 | }; 95 | int main(){ 96 | Stack s1; 97 | int option, key, data; 98 | 99 | do { 100 | cout << "What operation do you want to perform?" << 101 | "Select Option number. Enter 0 to exit." << endl; 102 | cout << "1. Push()" << endl; 103 | cout << "2. Pop()" << endl; 104 | cout << "3. isEmpty()" << endl; 105 | cout << "4. peek()" << endl; 106 | cout << "5. count()" << endl; 107 | cout << "6. display()" << endl; 108 | cout << "7. Clear Screen" << endl << endl; 109 | cin >> option; 110 | 111 | //Node n1 = new Node(); 112 | Node * new_node = new Node(); 113 | 114 | switch (option) { 115 | case 0: 116 | break; 117 | case 1: 118 | cout << "Enter KEY and VALUE of NODE to push in the stack" << 119 | endl; 120 | cin >> key; 121 | cin >> data; 122 | new_node -> key = key; 123 | new_node -> data = data; 124 | s1.push(new_node); 125 | break; 126 | case 2: 127 | cout << "Pop Function Called - Poped Value: " << endl; 128 | new_node = s1.pop(); 129 | cout << "TOP of Stack is: (" << new_node -> key << "," << new_node -> data << ")"; 130 | delete new_node; 131 | cout << endl; 132 | 133 | break; 134 | case 3: 135 | if (s1.isEmpty()) 136 | cout << "Stack is Empty" << endl; 137 | else 138 | cout << "Stack is not Empty" << endl; 139 | break; 140 | case 4: 141 | if (s1.isEmpty()) { 142 | cout << "Stack is Empty" << endl; 143 | } else { 144 | cout << "PEEK Function Called : " << endl; 145 | new_node = s1.peek(); 146 | cout << "TOP of Stack is: (" << new_node -> key << "," << new_node -> data << ")" << 147 | endl; 148 | } 149 | break; 150 | case 5: 151 | cout << "Count Function Called: " << endl; 152 | cout << "No of nodes in the Stack: " << s1.count() << endl; 153 | break; 154 | 155 | case 6: 156 | cout << "Display Function Called - " << endl; 157 | s1.display(); 158 | cout << endl; 159 | break; 160 | case 7: 161 | system("cls"); 162 | break; 163 | default: 164 | cout << "Enter Proper Option number " << endl; 165 | } 166 | 167 | } while (option != 0); 168 | 169 | return 0; 170 | } 171 | 172 | -------------------------------------------------------------------------------- /CPP/subarray_exists_or_not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool subarray(int arr[], int n) 7 | { 8 | unordered_set ss; 9 | int sum = 0; 10 | for (int i = 0; i < n; i++) 11 | { 12 | sum += arr[i]; 13 | if (sum == 0 || ss.find(sum)!= ss.end()){ 14 | return true; 15 | } 16 | ss.insert(sum); 17 | } 18 | return false; 19 | } 20 | 21 | int main() 22 | { 23 | int arr[] = { 3, 4, -7, 3, 1, 3, 1, -4, -2, -2 }; 24 | int n; 25 | n = sizeof(arr) / sizeof(arr[0]); 26 | if (subarray(arr, n)){ 27 | cout << "Subarray with zero-sum exists"; 28 | } 29 | else{ 30 | cout << "Subarray with zero-sum does not exists"; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Dart/Fibonacci.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | int fibonacci(int n) { 4 | if (n <= 1) { 5 | return n; 6 | } 7 | 8 | var prev = 1; 9 | var current = 1; 10 | for (var i = 2; i < n; i++) { 11 | var next = prev + current; 12 | prev = current; 13 | current = next; 14 | } 15 | 16 | return current; 17 | } 18 | 19 | main() { 20 | print('Enter an integer:'); 21 | int input = int.parse(stdin.readLineSync()).abs(); 22 | print(fibonacci(input)); 23 | } 24 | -------------------------------------------------------------------------------- /Dart/pyramid_pattern.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | void main(){ 4 | int rows = 6; 5 | for(int i = 0 ; i< rows; i++) 6 | { 7 | for(int j=(rows-i);j>1;j--){ 8 | stdout.write(" "); 9 | } 10 | for(int j = 0;j<=i;j++) 11 | { 12 | stdout.write("* "); 13 | } 14 | stdout.writeln(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Golang/bubblesort.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func BubbleSort(array[] int)[]int { 4 | for i:=0; i< len(array)-1; i++ { 5 | for j:=0; j < len(array)-i-1; j++ { 6 | if (array[j] > array[j+1]) { 7 | array[j], array[j+1] = array[j+1], array[j] 8 | } 9 | } 10 | } 11 | return array 12 | } 13 | func main() { 14 | array:= []int{11, 14, 3, 8, 18, 17, 43}; 15 | fmt.Println(BubbleSort(array)) 16 | } -------------------------------------------------------------------------------- /Java/FoldLinkedList.java: -------------------------------------------------------------------------------- 1 | //Java Implementation of folding a linked list 2 | 3 | public class FoldLinkedList{ 4 | public static void main(String[] args) throws Exception { 5 | LinkedList ll = new LinkedList(); 6 | int n = 6; 7 | for (int i = 1; i <= n; i++) { 8 | ll.addLast(i); 9 | } 10 | // LinkedList --> [1,2,3,4,5,6] for n = 6 11 | 12 | System.out.print("Linked List before folding --> "); 13 | ll.display(); 14 | ll.fold(); 15 | System.out.print("Linked List after folding --> "); 16 | ll.display(); 17 | } 18 | 19 | } 20 | class Node { 21 | int data; 22 | Node next; 23 | public Node(int data){ 24 | this.data = data; 25 | this.next = null; 26 | } 27 | } 28 | 29 | class LinkedList { 30 | Node head; 31 | Node tail; 32 | int size; 33 | 34 | public void display() { 35 | for (Node temp = head; temp != null; temp = temp.next) { 36 | System.out.print(temp.data + " "); 37 | } 38 | System.out.println(); 39 | } 40 | 41 | public void addFirst(int val) { 42 | Node temp = new Node(val); 43 | temp.next = head; 44 | head = temp; 45 | if (size == 0) { 46 | tail = temp; 47 | } 48 | size++; 49 | } 50 | 51 | void addLast(int val) { 52 | Node temp = new Node(val); 53 | temp.next = null; 54 | if (size == 0) { 55 | head = tail = temp; 56 | } else { 57 | tail.next = temp; 58 | tail = temp; 59 | } 60 | size++; 61 | } 62 | 63 | Node fleft; 64 | private void foldhelper(Node right , int floor){ 65 | if(right == null){ 66 | return; 67 | } 68 | foldhelper(right.next , floor+1); 69 | if(floor>size/2){ 70 | Node val = fleft.next; 71 | fleft.next = right; 72 | right.next = val; 73 | fleft = val; 74 | } 75 | else if(floor==size/2){ 76 | this.tail = right; 77 | tail.next = null; 78 | } 79 | else{ 80 | // do nothing 81 | } 82 | } 83 | 84 | public void fold() { 85 | fleft = this.head; 86 | foldhelper(this.head, 0); 87 | } 88 | } 89 | 90 | 91 | /* 92 | Output:- 93 | Linked List before folding --> 1 2 3 4 5 6 94 | Linked List after folding --> 1 6 2 5 3 4 95 | */ 96 | -------------------------------------------------------------------------------- /Java/Insertion sort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main { 3 | public static void main(String[] args) { 4 | //declare an array and print the original contents 5 | int[] numArray = {10,6,15,4,1,45}; 6 | System.out.println("Original Array:" + Arrays.toString(numArray)); 7 | //apply insertion sort algorithm on the array 8 | for(int k=1; k=0 && temp <= numArray[j]) { 12 | numArray[j+1] = numArray[j]; 13 | j = j-1; 14 | } 15 | numArray[j+1] = temp; 16 | } 17 | //print the sorted array 18 | System.out.println("Sorted Array:" + Arrays.toString(numArray)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Java/KadaneAlgorithm.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KadaneAlgorithm{ 4 | public static void main(String[] args){ 5 | int[] arr={2,4,3,-6,-4,1,7,-2,6,4,-3,2,3,-9,-6,-4,4,8}; 6 | 7 | int ci=0; 8 | int cj=0; 9 | int cs=arr[0]; 10 | int bi=0; 11 | int bj=0; 12 | int bs=arr[0]; 13 | 14 | for(int i=1;i0) 17 | { 18 | cj++; 19 | cs+=arr[i]; 20 | } 21 | else 22 | { 23 | ci=i; 24 | cj=i; 25 | cs=arr[i]; 26 | } 27 | if(cs>bs) 28 | { 29 | bi=ci; 30 | bj=cj; 31 | bs=cs; 32 | } 33 | } 34 | for(int i=bi;i<=bj;i++) 35 | { 36 | System.out.print(arr[i]+ " "); 37 | } 38 | System.out.println(); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Java/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | // Merge sort in Java 4 | 5 | class MergeSort { 6 | 7 | // Merge two sub arrays L and M into array 8 | void merge(int array[], int p, int q, int r) { 9 | 10 | int n1 = q - p + 1; 11 | int n2 = r - q; 12 | 13 | int L[] = new int[n1]; 14 | int M[] = new int[n2]; 15 | 16 | // fill the left and right array 17 | for (int i = 0; i < n1; i++) 18 | L[i] = array[p + i]; 19 | for (int j = 0; j < n2; j++) 20 | M[j] = array[q + 1 + j]; 21 | 22 | // Maintain current index of sub-arrays and main array 23 | int i, j, k; 24 | i = 0; 25 | j = 0; 26 | k = p; 27 | 28 | // Until we reach either end of either L or M, pick larger among 29 | // elements L and M and place them in the correct position at A[p..r] 30 | // for sorting in descending 31 | // use if(L[i] >= <[j]) 32 | while (i < n1 && j < n2) { 33 | if (L[i] <= M[j]) { 34 | array[k] = L[i]; 35 | i++; 36 | } else { 37 | array[k] = M[j]; 38 | j++; 39 | } 40 | k++; 41 | } 42 | 43 | // When we run out of elements in either L or M, 44 | // pick up the remaining elements and put in A[p..r] 45 | while (i < n1) { 46 | array[k] = L[i]; 47 | i++; 48 | k++; 49 | } 50 | 51 | while (j < n2) { 52 | array[k] = M[j]; 53 | j++; 54 | k++; 55 | } 56 | } 57 | 58 | // Divide the array into two sub arrays, sort them and merge them 59 | void mergeSort(int array[], int left, int right) { 60 | if (left < right) { 61 | 62 | // m is the point where the array is divided into two sub arrays 63 | int mid = (left + right) / 2; 64 | 65 | // recursive call to each sub arrays 66 | mergeSort(array, left, mid); 67 | mergeSort(array, mid + 1, right); 68 | 69 | // Merge the sorted sub arrays 70 | merge(array, left, mid, right); 71 | } 72 | } 73 | 74 | public static void main(String args[]) { 75 | 76 | // created an unsorted array 77 | int[] array = { 6, 5, 12, 10, 9, 1 }; 78 | 79 | MergeSort ob = new MergeSort(); 80 | 81 | // call the method mergeSort() 82 | // pass argument: array, first index and last index 83 | ob.mergeSort(array, 0, array.length - 1); 84 | 85 | System.out.println("Sorted Array:"); 86 | System.out.println(Arrays.toString(array)); 87 | } 88 | } -------------------------------------------------------------------------------- /Java/NonFibonacciAndFibonacci.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class NonFibonacci{ 3 | public static void main(String args[]){ 4 | Scanner sc=new Scanner(System.in); 5 | System.out.print("Enter the max Range:"); 6 | int high=sc.nextInt(); 7 | System.out.println("The fibonacci series upto "+high+" is");fibo(high); 8 | System.out.println(); 9 | System.out.println("The Non-fibonacci series upto "+high+" is");nonFibo(high); 10 | } 11 | private static void fibo(int high){ 12 | int a=0,b=1,c=1; 13 | while(a<=high){ 14 | System.out.print(a+" "); 15 | a=b; 16 | b=c; 17 | c=a+b; 18 | } 19 | } 20 | private static void nonFibo(int high){ 21 | int a=0,b=1,c=1; 22 | while(a<=high){ 23 | if(b>high){ 24 | prnt(a,high); 25 | break; 26 | } 27 | prnt(a,b); 28 | a=b; 29 | b=c; 30 | c=a+b; 31 | } 32 | } 33 | private static void prnt(int a,int b){ 34 | for(int i=a+1;i[] graph = new ArrayList[vtces]; 52 | for (int i = 0; i < vtces; i++) { 53 | graph[i] = new ArrayList<>(); 54 | } 55 | 56 | int edges = Integer.parseInt(br.readLine()); 57 | for (int i = 0; i < edges; i++) { 58 | String[] parts = br.readLine().split(" "); 59 | int v1 = Integer.parseInt(parts[0]); 60 | int v2 = Integer.parseInt(parts[1]); 61 | int wt = Integer.parseInt(parts[2]); 62 | graph[v1].add(new Edge(v1, v2, wt)); 63 | graph[v2].add(new Edge(v2, v1, wt)); 64 | } 65 | 66 | int src = Integer.parseInt(br.readLine()); 67 | int dest = Integer.parseInt(br.readLine()); 68 | 69 | 70 | String psf=src+""; 71 | boolean[] visited=new boolean[vtces]; 72 | printPaths(graph,src,dest,visited,psf); 73 | } 74 | 75 | // recursive function to print all paths in a graph 76 | public static void printPaths(ArrayList[] graph,int src,int dest,boolean[] visited,String psf){ 77 | 78 | // Base Case , when src and dest vertex are equal , print path so far(psf) and return 79 | if(src==dest){ 80 | System.out.println(psf); 81 | return; 82 | } 83 | 84 | visited[src]=true; // mark visited as soon as we come to a vertex 85 | for(Edge e:graph[src]){ 86 | int nbr=e.nbr; 87 | // only make call to unvisited neigbour of a vertex 88 | if(visited[nbr]==false){ 89 | printPaths(graph,nbr,dest,visited,psf+nbr); 90 | } 91 | } 92 | 93 | visited[src]=false; // mark unvisited at every vertex while returning from the function 94 | 95 | } 96 | 97 | } 98 | 99 | -------------------------------------------------------------------------------- /Java/Selection Sort.java: -------------------------------------------------------------------------------- 1 | //Selection Sort 2 | 3 | import java.util.*; 4 | class Main 5 | { 6 | static void sel_sort(int numArray[]) 7 | { 8 | int n = numArray.length; 9 | 10 | // traverse unsorted array 11 | for (int i = 0; i < n-1; i++) 12 | { 13 | // Find the minimum element in unsorted array 14 | int min_idx = i; 15 | for (int j = i+1; j < n; j++) 16 | if (numArray[j] < numArray[min_idx]) 17 | min_idx = j; 18 | 19 | // swap minimum element with compared element 20 | int temp = numArray[min_idx]; 21 | numArray[min_idx] = numArray[i]; 22 | numArray[i] = temp; 23 | } 24 | } 25 | 26 | public static void main(String args[]) 27 | { 28 | //declare and print the original array 29 | int numArray[] = {7,5,2,20,42,15,23,34,10}; 30 | System.out.println("Original Array:" + Arrays.toString(numArray)); 31 | //call selection sort routine 32 | sel_sort(numArray); 33 | //print the sorted array 34 | System.out.println("Sorted Array:" + Arrays.toString(numArray)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Java/SelectionSort.java: -------------------------------------------------------------------------------- 1 | public class SelectionSort { 2 | public static void selectionSort(int[] arr){ 3 | for (int i = 0; i < arr.length - 1; i++) 4 | { 5 | int index = i; 6 | for (int j = i + 1; j < arr.length; j++){ 7 | if (arr[j] < arr[index]){ 8 | index = j;//searching for lowest index 9 | } 10 | } 11 | int smallerNumber = arr[index]; 12 | arr[index] = arr[i]; 13 | arr[i] = smallerNumber; 14 | } 15 | } 16 | 17 | public static void main(String a[]){ 18 | int[] arr1 = {56,45,87,65,98,1,2,84}; 19 | System.out.println("Before Selection Sort"); 20 | for(int i:arr1){ 21 | System.out.print(i+" "); 22 | } 23 | System.out.println(); 24 | 25 | selectionSort(arr1);//sorting array using selection sort 26 | 27 | System.out.println("After Selection Sort"); 28 | for(int i:arr1){ 29 | System.out.print(i+" "); 30 | } 31 | } 32 | } 33 | // MADE BY LOVE - KunaL :) -------------------------------------------------------------------------------- /Java/SubArrayWithSumZero.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubArrayWithSumZero { 4 | 5 | public static void main( String[] args ) { 6 | int[] array = new int[]{ 3, 4, -7, 3, 1, 3, 1, -4, -2, -2 }; 7 | List subArraysWithSumZero = calculateSubArraysWithSumZero( array ); 8 | for ( int[] subArray : subArraysWithSumZero ) { 9 | System.out.println( Arrays.toString( subArray ) ); 10 | } 11 | } 12 | 13 | /** 14 | * Efficiently calculates the sub arrays of an array which have a sum of zero 15 | * 16 | *

17 | * We calculate the sum of all sub arrays from index 0 18 | * and map each sum to the end indices which produce the sum.
19 | * Every pair of sub arrays from index 0 which produce the same sum 20 | * generate a sub array with sum zero by subtracting the smaller interval from the larger one. 21 | *

22 | * 23 | * @param array the array 24 | * @return a list of all sub arrays with a sum of zero 25 | */ 26 | public static List calculateSubArraysWithSumZero( int[] array ) { 27 | List results = new ArrayList<>(); 28 | Map> sumEndMap = new HashMap<>(); 29 | // We have to employ a little trick to cover cases where the beginning of the array has a sum of zero 30 | insert( sumEndMap, 0, -1 ); 31 | 32 | int cummulativeSum = 0; 33 | for ( int i = 0; i < array.length; i++ ) { 34 | cummulativeSum += array[ i ]; 35 | List endList = sumEndMap.get( cummulativeSum ); 36 | 37 | if ( sumEndMap.containsKey( cummulativeSum ) ) { 38 | // Sub array with sum zero between the end points 39 | for ( int end : endList ) { 40 | int[] subArray = new int[ i - end ]; 41 | System.arraycopy( array, end + 1, subArray, 0, i - end ); 42 | results.add( subArray ); 43 | } 44 | } 45 | 46 | insert( sumEndMap, cummulativeSum, i ); 47 | } 48 | 49 | return results; 50 | } 51 | 52 | private static void insert( Map> sumEndMap, int sum, int end ) { 53 | List endList = sumEndMap.getOrDefault( sum, new ArrayList<>() ); 54 | endList.add( end ); 55 | sumEndMap.put( sum, endList ); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Java/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.util.*; 4 | class TowerOfHanoi { 5 | static void towerOfHanoi(int n, char from_rod, 6 | char to_rod, char aux_rod) 7 | { 8 | if (n == 0) { 9 | return; 10 | } 11 | towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); 12 | System.out.println("Move disk " + n + " from rod " 13 | + from_rod + " to rod " 14 | + to_rod); 15 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); 16 | } 17 | 18 | public static void main(String args[]) 19 | { 20 | int N = 3; 21 | towerOfHanoi(N, 'A', 'C', 'B'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java/dicepath.java: -------------------------------------------------------------------------------- 1 | public class dicepath { 2 | 3 | public static int dice(int sp, int ep, int[] dp){ 4 | for(sp = ep; sp >= 0; sp--){ 5 | if(sp==ep){ 6 | dp[sp] = 1; 7 | continue; 8 | } 9 | int count = 0; 10 | for(int dice = 1; sp+dice<=ep && dice<=6;dice++){ 11 | count+=dp[sp+dice]; 12 | } 13 | dp[sp] = count; 14 | } 15 | return dp[0]; 16 | } 17 | public static void main(String[] args){ 18 | int[] dp = new int[11]; 19 | System.out.println(dice(0,10,dp)); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Javascript/Add 2 numbers linked list: -------------------------------------------------------------------------------- 1 | Add Two Numbers 2 | 3 | You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. 4 | 5 | You may assume the two numbers do not contain any leading zero, except the number 0 itself. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: l1 = [2,4,3], l2 = [5,6,4] 13 | Output: [7,0,8] 14 | Explanation: 342 + 465 = 807. 15 | Example 2: 16 | 17 | Input: l1 = [0], l2 = [0] 18 | Output: [0] 19 | 20 | 21 | /** 22 | * Definition for singly-linked list. 23 | * function ListNode(val) { 24 | * this.val = val; 25 | * this.next = null; 26 | * } 27 | */ 28 | /** 29 | * @param {ListNode} l1 30 | * @param {ListNode} l2 31 | * @return {ListNode} 32 | */ 33 | var addTwoNumbers = function(l1, l2) { 34 | var List = new ListNode(0); 35 | var head = List; 36 | var sum = 0; 37 | var carry = 0; 38 | 39 | while(l1!==null||l2!==null||sum>0){ 40 | 41 | if(l1!==null){ 42 | sum = sum + l1.val; 43 | l1 = l1.next; 44 | } 45 | if(l2!==null){ 46 | sum = sum + l2.val; 47 | l2 = l2.next; 48 | } 49 | if(sum>=10){ 50 | carry = 1; 51 | sum = sum - 10; 52 | } 53 | 54 | head.next = new ListNode(sum); 55 | head = head.next; 56 | 57 | sum = carry; 58 | carry = 0; 59 | 60 | } 61 | 62 | return List.next; 63 | }; 64 | -------------------------------------------------------------------------------- /Javascript/Move Zeroes.js: -------------------------------------------------------------------------------- 1 | function moveZeroes(n) { 2 | for (let i = 0; i < n.length; i++) { 3 | if (n[i] === 0) { 4 | n.splice(i, 1); 5 | n.push(0); 6 | } 7 | } 8 | 9 | return n; 10 | } -------------------------------------------------------------------------------- /Javascript/Stack.js: -------------------------------------------------------------------------------- 1 | // program to implement stack data structure in js 2 | class Stack { 3 | constructor() { 4 | this.items = []; 5 | } 6 | 7 | // add element to the stack 8 | add(element) { 9 | return this.items.push(element); 10 | } 11 | 12 | // remove element from the stack 13 | remove() { 14 | if(this.items.length > 0) { 15 | return this.items.pop(); 16 | } 17 | } 18 | 19 | // view the last element 20 | peek() { 21 | return this.items[this.items.length - 1]; 22 | } 23 | 24 | // check if the stack is empty 25 | isEmpty(){ 26 | return this.items.length == 0; 27 | } 28 | 29 | // the size of the stack 30 | size(){ 31 | return this.items.length; 32 | } 33 | 34 | // empty the stack 35 | clear(){ 36 | this.items = []; 37 | } 38 | } 39 | 40 | let stack = new Stack(); 41 | stack.add(1); 42 | stack.add(2); 43 | stack.add(4); 44 | stack.add(8); 45 | console.log(stack.items); 46 | 47 | stack.remove(); 48 | console.log(stack.items); 49 | 50 | console.log(stack.peek()); 51 | 52 | console.log(stack.isEmpty()); 53 | 54 | console.log(stack.size()); 55 | 56 | stack.clear(); 57 | console.log(stack.items); 58 | -------------------------------------------------------------------------------- /Javascript/fractional_knapsack.js: -------------------------------------------------------------------------------- 1 | // This function solves the bounded fractional knapsack with given target wih given values and weights 2 | // Time complexity: O(2 ^ n) 3 | // Space complexity: O(1) 4 | 5 | const knapSack = (values, weights, n, target) => { 6 | //base case: when we cannot have take more items 7 | if (target < 0) { 8 | return Number.MIN_SAFE_INTEGER; 9 | } 10 | 11 | //base case: when no items are left or capacity becomes 0 12 | if (n < 0 || target === 0) { 13 | return 0; 14 | } 15 | 16 | // pick current item n in knapSack and recur for 17 | // remaining items (n - 1) with reduced capacity (weight - weights[n]) 18 | let include = 19 | values[n] + knapSack(values, weights, n - 1, target - weights[n]); 20 | 21 | // leave the current item n from knapSack and recur for 22 | // remaining items (n - 1) 23 | let exclude = knapSack(values, weights, n - 1, target); 24 | 25 | // return maximum value we get by picking or leaving the current item 26 | return Math.max(include, exclude); 27 | }; 28 | 29 | const values = [12, 2, 1, 4, 1]; 30 | const weights = [4, 2, 1, 10, 2]; 31 | const target = 15; 32 | 33 | console.log(knapSack(values, weights, values.length - 1, target)); 34 | -------------------------------------------------------------------------------- /Javascript/linked list cycle: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @param {ListNode} head 11 | * @return {boolean} 12 | */ 13 | var hasCycle = function(head) { 14 | const set = new Set(); 15 | let node = head; 16 | // return true when node has been visited and saved in the set 17 | while (node !== null) { 18 | if (set.has(node)) { 19 | return true; 20 | } 21 | set.add(node); 22 | node = node.next 23 | } 24 | return false; 25 | }; 26 | -------------------------------------------------------------------------------- /Javascript/max-profits.js: -------------------------------------------------------------------------------- 1 | function getMaxProfit(prices) { 2 | let maxProfit = 0; 3 | let min = prices[0]; 4 | for(let i = 1; i < prices.length; i++) { 5 | min = Math.min(prices[i], min); 6 | maxProfit = Math.max(maxProfit, prices[i] - min); 7 | } 8 | return maxProfit; 9 | }; 10 | -------------------------------------------------------------------------------- /Javascript/queue implentation.js: -------------------------------------------------------------------------------- 1 | class Queue { 2 | constructor() { 3 | this.items = {}; 4 | this.headIndex = 0; 5 | this.tailIndex = 0; 6 | } 7 | enqueue(item) { 8 | this.items[this.tailIndex] = item; 9 | this.tailIndex++; 10 | } 11 | dequeue() { 12 | const item = this.items[this.headIndex]; 13 | delete this.items[this.headIndex]; 14 | this.headIndex++; 15 | return item; 16 | } 17 | peek() { 18 | return this.items[this.headIndex]; 19 | } 20 | get length() { 21 | return this.tailIndex - this.headIndex; 22 | } 23 | } 24 | const queue = new Queue(); 25 | queue.enqueue(7); 26 | queue.enqueue(2); 27 | queue.enqueue(6); 28 | queue.enqueue(4); 29 | queue.dequeue(); // => 7 30 | queue.peek(); // => 2 31 | queue.length; // => 3 32 | -------------------------------------------------------------------------------- /Javascript/reversed linked list: -------------------------------------------------------------------------------- 1 | Reverse Linked List 2 | 3 | Given the head of a singly linked list, reverse the list, and return the reversed list. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | 10 | Input: head = [1,2,3,4,5] 11 | Output: [5,4,3,2,1] 12 | Example 2: 13 | 14 | 15 | Input: head = [1,2] 16 | Output: [2,1] 17 | 18 | 19 | // Time Complexity: O(n), Linear - traverse linked list only once 20 | // Space Complexity: O(1), Constant - we will only have 2 pointers regardless of size of input; prev and temp 21 | 22 | var reverseList = function(head) { 23 | // End of the reversed linked list set to null 24 | let prev = null; 25 | 26 | // Traverse through the given linked list 27 | while (head) { 28 | const temp = head.next; // References the next Node of given linked list 29 | head.next = prev; // head.next point to previous Node, instead of the next Node of the given linked list 30 | prev = head; // Move the prev Node pointer up to head 31 | head = temp; // Move the head up to next Node of the given linked list 32 | } 33 | 34 | // Prev is the reversed linked list 35 | return prev; 36 | }; 37 | -------------------------------------------------------------------------------- /Javascript/subarray with zero.js: -------------------------------------------------------------------------------- 1 | // Given an integer array, check if it contains a subarray having zero - sum. 2 | 3 | // For example: 4 | 5 | // Input: { 3, 4, -7, 3, 1, 3, 1, -4, -2, -2 } 6 | 7 | // Output: Subarray with zero - sum exists 8 | 9 | // The subarrays with a sum of 0 are: 10 | 11 | // { 3, 4, -7 } 12 | // { 4, -7, 3 } 13 | // { -7, 3, 1, 3 } 14 | // { 3, 1, -4 } 15 | // { 3, 1, 3, 1, -4, -2, -2 } 16 | // { 3, 4, -7, 3, 1, 3, 1, -4, -2, -2 } 17 | 18 | let checkSubbary = (number) => { 19 | let data = 0 20 | number.forEach(item => data = data + parseInt(item)) 21 | if (data == 0) { 22 | return "Subarray with zero - sum exists" 23 | } else { 24 | return "Subarray with zero - sum not exists" 25 | } 26 | } 27 | 28 | console.log(checkSubbary([3, 4, - 7])) -------------------------------------------------------------------------------- /Maximum_Subarray.cpp: -------------------------------------------------------------------------------- 1 | //Application of KADANE'S ALGORITHM 2 | //Leetcode Problem 3 | #include 4 | using namespace std; 5 | int maxSubArray(vector& nums) { 6 | int cursum,maxsum; 7 | cursum=maxsum=nums[0]; 8 | for(int i=1;i>n; 20 | vectorarray; 21 | for(int i=0;i>x; 24 | array.push_back(x); 25 | } 26 | cout< 2 | 3 | 4 | 5 | Guess the Number! 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 |

I am thinking of a number between 1-100.

18 |

Can you guess it?

19 | 21 |
22 | 23 | 24 |

No. Of Guesses: 0

25 |

Guessed Numbers are: None

26 | 27 | 28 |

29 |

30 |
31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Number Guess/script.js: -------------------------------------------------------------------------------- 1 | var msg1 = document.getElementById("message1"); 2 | var msg2 = document.getElementById("message2"); 3 | var msg3 = document.getElementById("message3"); 4 | var msg4 = document.getElementById("message4"); 5 | 6 | var answer = Math.floor(Math.random() * 100) + 1; 7 | var no_of_guesses = 0; 8 | var guessed_nums = []; 9 | 10 | function play() { 11 | var user_guess = document.getElementById("guess").value; 12 | if (user_guess < 1 || user_guess > 100) { 13 | alert("Please enter a number between 1 and 100."); 14 | } else { 15 | guessed_nums.push(user_guess); 16 | no_of_guesses += 1; 17 | if (no_of_guesses >= 7) { 18 | msg1.textContent = "Your chance is over"; 19 | msg2.textContent = "No. of guesses: " + no_of_guesses; 20 | msg3.textContent = "Guessed numbers are: " + guessed_nums; 21 | msg4.textContent = "Guessed left : " + (7-no_of_guesses); 22 | document.getElementById("my_btn").disabled = true; 23 | } else if (user_guess < answer) { 24 | msg1.textContent = "Your guess is too low."; 25 | msg2.textContent = "No. of guesses: " + no_of_guesses; 26 | msg3.textContent = "Guessed numbers are: " + guessed_nums; 27 | msg4.textContent = "Guessed left : " + (7-no_of_guesses); 28 | } else if (user_guess > answer) { 29 | msg1.textContent = "Your guess is too high."; 30 | msg2.textContent = "No. of guesses: " + no_of_guesses; 31 | msg3.textContent = "Guessed numbers are: " + guessed_nums; 32 | msg4.textContent = "Guessed left : " + (7-no_of_guesses); 33 | } else if (user_guess == answer) { 34 | msg1.textContent = "Yippie You Win!!"; 35 | msg2.textContent = "The number was: " + answer; 36 | msg3.textContent = "You guessed it in " + no_of_guesses + " guesses"; 37 | document.getElementById("my_btn").disabled = true; 38 | 39 | 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Number Guess/style.css: -------------------------------------------------------------------------------- 1 | *, 2 | *:before, 3 | *:after{ 4 | padding: 0; 5 | margin: 0; 6 | box-sizing: border-box; 7 | } 8 | body{ 9 | height: 100vh; 10 | background: linear-gradient( 11 | to right, 12 | #c099fa, 13 | #f99dd0 14 | ); 15 | } 16 | .container{ 17 | position: absolute; 18 | width: 50%; 19 | min-width: 580px; 20 | transform: translate(-50%,-50%); 21 | top: 50%; 22 | left: 50%; 23 | background-color: #ffffff; 24 | padding: 50px 10px; 25 | border-radius: 5px; 26 | display: grid; 27 | justify-items: center; 28 | font-family: 'Poppins',sans-serif; 29 | } 30 | h3{ 31 | font-size: 16px; 32 | font-weight: 600; 33 | } 34 | input[type="text"]{ 35 | width: 90px; 36 | font-weight: 600; 37 | color: #663399; 38 | padding: 20px 0; 39 | text-align: center; 40 | margin-top: 30px; 41 | border-radius: 5px; 42 | border: 2px solid #202020; 43 | font-size: 28px; 44 | } 45 | button{ 46 | width: 160px; 47 | padding: 15px 0; 48 | border-radius: 5px; 49 | background-color: #663399; 50 | color: #ffffff; 51 | border: none; 52 | font-size: 18px; 53 | font-weight: 600; 54 | margin-bottom: 30px; 55 | outline: none; 56 | cursor: pointer; 57 | } 58 | p{ 59 | font-weight: 400; 60 | } -------------------------------------------------------------------------------- /Python/CGMN1.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/CGRL2020/problems/CGMN1 2 | 3 | try: 4 | n, m=map(int,input().split()) 5 | sum1=((n*(n+1))/2) 6 | prev_last=n 7 | prev_first=1 8 | sum_mid=sum1-(prev_first+prev_last) 9 | while m: 10 | k=int(input()) 11 | if (k>=2 and k<=n-1) or (k==prev_first) or (k==prev_last): 12 | prev_last,prev_first=prev_first,prev_last 13 | else: 14 | prev_last=k 15 | print(int(sum_mid+prev_last+prev_first)) 16 | m-=1 17 | except: 18 | pass -------------------------------------------------------------------------------- /Python/CHEFRECP.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/problems/CHEFRECP 2 | 3 | def main(): 4 | t=int(input()) 5 | while t: 6 | n=int(input()) 7 | arr = list(map(int, input().split())) 8 | arr.append(-1) 9 | temp = [] 10 | res = True 11 | freq=dict() 12 | for i in range(len(arr) - 1): 13 | if arr[i] == arr[i + 1]: 14 | continue 15 | else: 16 | if arr[i] in temp: 17 | res = False 18 | break 19 | else: 20 | temp.append(arr[i]) 21 | 22 | for item in arr: 23 | freq[item]=freq.get(item,0)+1 24 | 25 | k=list() 26 | for key,value in freq.items(): 27 | if key!=-1: 28 | k.append(value) 29 | 30 | if res and len(set(k)) == len(k): 31 | print("YES") 32 | else: 33 | print("NO") 34 | 35 | '''print("freq : \n",freq) 36 | print("k : \n", k)''' 37 | 38 | t-=1 39 | 40 | main() -------------------------------------------------------------------------------- /Python/CHEFWED.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/AUG20B/problems/CHEFWED 2 | 3 | # Solution 4 | 5 | def main(): 6 | t=int(input()) 7 | while t: 8 | n,k=map(int,input().split()) 9 | f=list(map(int,input().split())) 10 | f_dict=dict() 11 | inefficiency1=0 #one table only 12 | inefficiency2=list() #multiple tables allowed 13 | inefficiency3=2*k #two tables only 14 | ineff_mult_tables=list() 15 | x=0 16 | ans=0 17 | uniques=list() 18 | for item in f: 19 | f_dict[item]=f_dict.get(item, 0)+1 20 | freq=list(f_dict.values()) 21 | summ=0 22 | for key,val in f_dict.items(): 23 | if val>1: 24 | summ+=val 25 | uniques.append(key) 26 | maxm=max(freq) 27 | for key,val in f_dict.items(): 28 | if maxm==val: 29 | maxm_ele=key 30 | 31 | tempo=0 32 | if maxm==1: 33 | ans = int(k) 34 | else: 35 | inefficiency1=k+summ 36 | 37 | for i in range(len(f)): 38 | if i==0: 39 | ineff_mult_tables.append(f[i]) 40 | else: 41 | if f[i] not in ineff_mult_tables: 42 | ineff_mult_tables.append(f[i]) 43 | else: 44 | inefficiency2.append(ineff_mult_tables) 45 | ineff_mult_tables=list() 46 | ineff_mult_tables.append(f[i]) 47 | 48 | inefficiency2.append(ineff_mult_tables) 49 | arg=0 50 | min_arg=summ 51 | first=[f[0]] 52 | second = f[1:] 53 | list_set = set(second) 54 | unique_list = (list(list_set)) 55 | first=f[:1] 56 | second = f[1:] 57 | list_set = set(first) 58 | unique_list = (list(list_set)) 59 | for item in unique_list: 60 | if first.count(item)>1: 61 | arg+=first.count(item) 62 | list_set = set(second) 63 | unique_list = (list(list_set)) 64 | for item in unique_list: 65 | if second.count(item)>1: 66 | arg+=second.count(item) 67 | if arg1: 75 | arg+=1 76 | if second_freq==2: 77 | arg-=2 78 | elif second_freq>2: 79 | arg-=1 80 | return_val=second.pop(0) 81 | first.append(return_val) 82 | if arg0: 15 | num,dig=map(str,input().split()[:2]) 16 | le=len(num) 17 | if dig not in num: 18 | print(0) 19 | else: 20 | print(coreL2Q_Mex(0,num,dig,le)) 21 | t-=1 22 | -------------------------------------------------------------------------------- /Python/DRCHEF.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/JULY20B/problems/DRCHEF 2 | 3 | # Solution 4 | def main(): 5 | t = int(input()) 6 | while t: 7 | n1 = list(map(int, input().split())) 8 | n = int(n1[0]) 9 | x = int(n1[1]) 10 | a = list(map(int, input().split())) 11 | a.sort() 12 | 13 | days=0 14 | index=-2 15 | 16 | for i in range(len(a)): 17 | if 2 * a[i] < x: 18 | days+=1 19 | else: 20 | index=i 21 | break 22 | 23 | if index!=-2: 24 | while x < a[i]: 25 | x = x * 2 26 | days += 1 27 | days+=1 28 | for i in range(index+1,len(a)): 29 | if a[i-1]==a[i]: 30 | days+=1 31 | else: 32 | while a[i] > a[i-1]: 33 | a[i-1] = a[i-1] * 2 34 | days += 1 35 | print(days) 36 | else: 37 | print(days) 38 | t-=1 39 | 40 | if __name__ == '__main__': 41 | main() -------------------------------------------------------------------------------- /Python/EVENTUAL.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/COOK120B/problems/EVENTUAL 2 | 3 | def main(): 4 | t = int(input()) 5 | while t: 6 | n = int(input()) 7 | s = input() 8 | ans = "YES" 9 | alphas = dict() 10 | for ch in s: 11 | alphas[ch]=alphas.get(ch, 0)+1 12 | for key, val in alphas.items(): 13 | if val%2!=0: 14 | ans = "NO" 15 | break 16 | print(ans) 17 | 18 | t-=1 19 | 20 | if __name__ == '__main__': 21 | main() -------------------------------------------------------------------------------- /Python/FAIRELCT.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/JAN21B/problems/FAIRELCT 2 | 3 | try: 4 | t = int(input()) 5 | while t: 6 | n, m = map(int,input().split()) 7 | a = list(map(int,input().split())) 8 | b = list(map(int,input().split())) 9 | a_sum = sum(a) 10 | b_sum = sum(b) 11 | a_min = min(a) 12 | b_max = max(b) 13 | swaps=0 14 | while a_sum<=b_sum and b_max>a_min: 15 | a_min = min(a) 16 | b_max = max(b) 17 | a_sum = a_sum - a_min + b_max 18 | b_sum = b_sum - b_max + a_min 19 | a_min_index = a.index(a_min) 20 | b_max_index = b.index(b_max) 21 | a[a_min_index] = b_max 22 | b[b_max_index] = a_min 23 | # a.remove(a_min) 24 | # a.append(b_max) 25 | # b.remove(b_max) 26 | # b.append(a_min) 27 | a_min = min(a) 28 | b_max = max(b) 29 | swaps+=1 30 | if sum(a)>sum(b): 31 | print(swaps) 32 | else: 33 | print("-1") 34 | t-=1 35 | except: 36 | pass -------------------------------------------------------------------------------- /Python/FAKESWAP.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | for _ in range(t): 3 | n = int(input()) 4 | s = input() 5 | p = input() 6 | os=0 7 | zs=0 8 | for i in range(n): 9 | if p[i]=='0': 10 | zs+=1 11 | else: 12 | os+=1 13 | if s==p: 14 | print("YES") 15 | elif zs>=2 and os>=1: 16 | print("YES") 17 | elif zs>=1 and os>=2: 18 | print("YES") 19 | else: 20 | print("NO") 21 | -------------------------------------------------------------------------------- /Python/LIKECS01.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/problems/LIKECS01 2 | 3 | test=int(input()) 4 | while test: 5 | s=input() 6 | s_list=list(s) 7 | s_len=len(s_list) 8 | output="" 9 | dic=dict() 10 | fake_list=list(s) 11 | for i in range(0,s_len): 12 | alpha=s_list[i] 13 | fake_list.remove(alpha) 14 | if alpha in fake_list: 15 | output="yes" 16 | break 17 | else: 18 | output="no" 19 | print(output) 20 | test-=1 -------------------------------------------------------------------------------- /Python/MERGE.py: -------------------------------------------------------------------------------- 1 | def mergeSort(values): 2 | if len(values) > 1: 3 | mid = len(values) // 2 4 | left = values[:mid] 5 | right = values[mid:] 6 | mergeSort(left) 7 | mergeSort(right) 8 | i = 0 9 | j = 0 10 | k = 0 11 | 12 | while i < len(left) and j < len(right): 13 | if left[i] <= right[j]: 14 | values[k] = left[i] 15 | i += 1 16 | else: 17 | values[k] = right[j] 18 | j += 1 19 | k += 1 20 | while i < len(left): 21 | values[k] = left[i] 22 | i += 1 23 | k += 1 24 | 25 | while j < len(right): 26 | values[k]=right[j] 27 | j += 1 28 | k += 1 29 | 30 | values = [] 31 | values = [int(item) for item in input("Enter the values : ").split()] 32 | mergeSort(values) 33 | print(values) 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Python/MEXOR.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | import math 3 | for i in range(int(input())): 4 | n = int(input()) 5 | pow = int(math.log2(n+1)) 6 | print(2**pow) 7 | -------------------------------------------------------------------------------- /Python/MOVIEWKN.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/problems/MOVIEWKN 2 | 3 | t=int(input()) 4 | while(t): 5 | n=int(input()) 6 | l = list(map(int, input().split())) 7 | r = list(map(int, input().split())) 8 | prod=[] 9 | indexes=[] 10 | r1=[] 11 | r1_index=[] 12 | for i in range(n): 13 | prod.append(l[i]*r[i]) 14 | maxm=max(prod) 15 | for i in range(n): 16 | if prod[i]==maxm: 17 | indexes.append(i) 18 | len_indexes=len(indexes) 19 | if len_indexes==1: 20 | print(indexes[0]+1) 21 | elif len_indexes>1: 22 | for i in range(len_indexes): 23 | r1.append(r[indexes[i]]) 24 | r1_max=max(r1) 25 | for i in range(len(r1)): 26 | if r1_max==r1[i]: 27 | r1_index.append(i) 28 | if len(r1_index)==1: 29 | for i in range(len(r1)): 30 | if r1_max==r1[i]: 31 | print(indexes[i]+1) 32 | elif len(r1_index)>1: 33 | for i in range(len(r1)): 34 | if r1_max==r1[i]: 35 | print(indexes[i]+1) 36 | break 37 | t-=1 -------------------------------------------------------------------------------- /Python/MergeSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of MergeSort 2 | def mergeSort(arr): 3 | if len(arr) > 1: 4 | 5 | # Finding the mid of the array 6 | mid = len(arr)//2 7 | 8 | # Dividing the array elements 9 | L = arr[:mid] 10 | 11 | # into 2 halves 12 | R = arr[mid:] 13 | 14 | # Sorting the first half 15 | mergeSort(L) 16 | 17 | # Sorting the second half 18 | mergeSort(R) 19 | 20 | i = j = k = 0 21 | 22 | # Copy data to temp arrays L[] and R[] 23 | while i < len(L) and j < len(R): 24 | if L[i] < R[j]: 25 | arr[k] = L[i] 26 | i += 1 27 | else: 28 | arr[k] = R[j] 29 | j += 1 30 | k += 1 31 | 32 | # Checking if any element was left 33 | while i < len(L): 34 | arr[k] = L[i] 35 | i += 1 36 | k += 1 37 | 38 | while j < len(R): 39 | arr[k] = R[j] 40 | j += 1 41 | k += 1 42 | 43 | # Code to print the list 44 | 45 | 46 | def printList(arr): 47 | for i in range(len(arr)): 48 | print(arr[i], end=" ") 49 | print() 50 | 51 | 52 | # Driver Code 53 | if __name__ == '__main__': 54 | arr = list(map(int,input("\nEnter the array numbers : ").strip().split())) 55 | print("Given array is", end="\n") 56 | printList(arr) 57 | mergeSort(arr) 58 | print("Sorted array is: ", end="\n") 59 | printList(arr) -------------------------------------------------------------------------------- /Python/ORTHODOX.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/COOK120B/problems/ORTHODOX 2 | 3 | # Solution 4 | 5 | def Distinct_Subsequential_OR(original_a): 6 | result = set() 7 | before_OR = {0} 8 | for item in original_a: 9 | before_OR = {item | val for val in before_OR} | {item} 10 | result |= before_OR 11 | return len(result) 12 | 13 | def main(): 14 | t = int(input()) 15 | while t: 16 | n = int(input()) 17 | a = list(map(int, input().split())) 18 | ans = "" 19 | total_possible = int((n*(n+1))/2) 20 | distinct_OR_s=Distinct_Subsequential_OR(a) 21 | if distinct_OR_s==total_possible: 22 | ans = "YES" 23 | else: 24 | ans = "NO" 25 | print(ans) 26 | t-=1 27 | 28 | if __name__ == '__main__': 29 | main() -------------------------------------------------------------------------------- /Python/POSAND.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/OCT20B/problems/POSAND 2 | 3 | def check(num): 4 | return (num and (not(num&(num-1)))) 5 | 6 | try: 7 | t=int(input()) 8 | while t: 9 | n=int(input()) 10 | ans=list() 11 | minus=-1 12 | if n==1: 13 | print("1") 14 | elif n==2: 15 | print("-1") 16 | elif n==3: 17 | print("1 3 2") 18 | else: 19 | if check(n): 20 | print(minus) 21 | else: 22 | flag=0 23 | ans=[2,3,1] 24 | for i in range(4,n+1): 25 | if flag==1: 26 | flag=0 27 | continue 28 | if check(i): 29 | ans.append(i+1) 30 | ans.append(i) 31 | flag=1 32 | # i+=1 33 | else: 34 | ans.append(i) 35 | st='' 36 | for i in ans: 37 | st+=str(i)+" " 38 | print(st) 39 | t-=1 40 | except: 41 | pass -------------------------------------------------------------------------------- /Python/PTMSSNG.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/JULY20B/problems/PTMSSNG 2 | 3 | # Solution 4 | def main(): 5 | t=int(input()) 6 | while t: 7 | n = int(input()) 8 | x=0 9 | y=0 10 | 11 | first_vertex = dict() 12 | second_vertex = dict() 13 | 14 | for i in range(0,(4*n)-1): 15 | vertex = list(map(int, input().split())) 16 | first_vertex[vertex[0]] = first_vertex.get(vertex[0],0)+1 17 | second_vertex[vertex[1]] = second_vertex.get(vertex[1],0)+1 18 | 19 | for key, val in first_vertex.items(): 20 | if val%2!=0: 21 | x = key 22 | 23 | for key, val in second_vertex.items(): 24 | if val%2!=0: 25 | y = key 26 | ans = str(x)+" "+str(y) 27 | print(ans) 28 | 29 | t-=1 30 | 31 | main() 32 | -------------------------------------------------------------------------------- /Python/SKMP.py: -------------------------------------------------------------------------------- 1 | # https://www.codechef.com/AUG20B/problems/SKMP 2 | 3 | def main(): 4 | t = int(input()) 5 | while t: 6 | s = input() 7 | p = input() 8 | s_dict=dict() 9 | p_dict=dict() 10 | s_len=len(s) 11 | p_len=len(p) 12 | p_first=p[0] 13 | p_last=p[p_len-1] 14 | 15 | for i in range(s_len): 16 | s_dict[s[i]]=s_dict.get(s[i],0)+1 17 | for i in range(p_len): 18 | p_dict[p[i]]=p_dict.get(p[i],0)+1 19 | for key,val in p_dict.items(): 20 | s_dict[key]-=val 21 | st="" 22 | for key,val in s_dict.items(): 23 | st=st+(key*val) 24 | st_list=list(st) 25 | st_list.sort() 26 | st="" 27 | ans="" 28 | seperator='' 29 | st=seperator.join(st_list) 30 | 31 | flag=False 32 | flag1=False 33 | 34 | for i in range(len(st)): 35 | if st[i]==p_first: 36 | flag1=True 37 | for j in range(1,len(p)): 38 | if st[i]>p[j]: 39 | ans=st[:i]+p+st[i:] 40 | flag=True 41 | break 42 | elif st[i]p_first: 52 | flag1=True 53 | ans=st[:i]+p+st[i:] 54 | break 55 | 56 | if flag1==False: 57 | ans=st+p 58 | 59 | 60 | print(ans) 61 | 62 | t-=1 63 | 64 | if __name__=="__main__": 65 | main() -------------------------------------------------------------------------------- /Python/Stack menu driven program.py: -------------------------------------------------------------------------------- 1 | lim=100 2 | s=[] 3 | def add(s): 4 | global lim 5 | n=int(input("Enter the value:")) 6 | l=len(s) 7 | if l==lim: 8 | print("Overflow") 9 | else: 10 | s.append(n) 11 | print("Number added in stack") 12 | 13 | def delete(s): 14 | if len(s)==0: 15 | print("Underflow") 16 | else: 17 | a=s.pop() 18 | print("Deleted {} from stack".format(a)) 19 | 20 | def stacklim(): 21 | global lim 22 | lim=int(input("Enter the stack limit")) 23 | print("Limit set as",lim) 24 | 25 | def view(s): 26 | for i in range(len(s)-1,-1,-1): 27 | print(s[i]) 28 | 29 | def top(s): 30 | if len(s)==0: 31 | print("Stack is empty") 32 | else: 33 | print("Top element is",s[len(s)-1]) 34 | 35 | def main(): 36 | c=1 37 | while c!=0: 38 | print("""Menu 39 | 1.Add elements to stack 40 | 2.Delete elements from stack 41 | 3.Set stack limit(By default stack limit is 100) 42 | 4.View stack 43 | 5.View Top element 44 | 6.Exit""") 45 | a=int(input("Enter your choice:")) 46 | if a==1: 47 | add(s) 48 | elif a==2: 49 | delete(s) 50 | elif a==3: 51 | stacklim() 52 | elif a==4: 53 | view(s) 54 | elif a==5: 55 | top(s) 56 | elif a==6: 57 | c=0 58 | else: 59 | print("Invalid Input") 60 | main() 61 | -------------------------------------------------------------------------------- /Python/TREE2.py: -------------------------------------------------------------------------------- 1 | # Question : https://www.codechef.com/SEPT20B/problems/TREE2 2 | 3 | # Solution 4 | 5 | try: 6 | t=int(input()) 7 | while t: 8 | n=int(input()) 9 | arr=list(map(int,input().split())) 10 | fin=list() 11 | for item in arr: 12 | if item!=0: 13 | fin.append(item) 14 | if len(fin)>0: 15 | arr_set=set(fin) 16 | print(len(arr_set)) 17 | else: 18 | print("0") 19 | t-=1 20 | except: 21 | pass -------------------------------------------------------------------------------- /Python/ZEBRA.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | for _ in range(int(input())): 3 | n, k = map(int, input().strip().split()) 4 | s = input() 5 | s1, c = s[0], 0 6 | for i in range(1, n): 7 | if s1 != s[i]: 8 | c += 1 9 | s1 = s[i] 10 | if c < k: 11 | print(-1) 12 | continue 13 | if s[0] == '0': 14 | if k % 2: 15 | for i in range(len(s)-1, -1, -1): 16 | if s[i]=='1': 17 | print(i+1) 18 | break 19 | else: 20 | for i in range(len(s) - 1, -1, -1): 21 | if s[i] == '0': 22 | print(i + 1) 23 | break 24 | else: 25 | if k % 2: 26 | for i in range(len(s)-1, -1, -1): 27 | if s[i]=='0': 28 | print(i+1) 29 | break 30 | else: 31 | for i in range(len(s) - 1, -1, -1): 32 | if s[i] == '1': 33 | print(i + 1) 34 | break 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EXCLUDED FROM HACKTOBERFEST 2 | 3 | ![image](https://user-images.githubusercontent.com/62786689/193422113-cea8df62-7528-47de-afd6-cb643b830431.png) 4 | 5 |

Hacktoberfest 2022

6 | 7 | *** 8 |

9 | 10 | Link To HacktoberFest 2022 11 | 12 |

13 | 14 | ## Event details : 15 | 16 | - Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge. 17 | 18 | - Hacktoberfest is a celebration open to everyone in our global community. 19 | - Pull requests can be made in any GitHub-hosted repositories/projects. 20 | - You can sign up anytime between October 1 and October 31. 21 | 22 | ## HacktoberFest Rules : 23 | 24 | To earn your Hacktoberfest tee or tree reward, you must register and make four valid pull requests (PRs) between October 1-31 (in any time zone). Pull requests can be made in any participating GitHub or GitLab hosted repository/project. Look for the 'hacktoberfest' topic to know if a repository/project is participating in Hacktoberfest. Pull requests must be approved by a maintainer of the repository/project to count. If a maintainer reports your pull request as spam or behavior not in line with the project’s code of conduct, you will be ineligible to participate. This year, the first 55,000 participants who successfully complete the challenge will be eligible to receive a prize. 25 | *** 26 |

Whether it’s your first or fiftieth pull request, there’s always more to learn! We’ve put together a few resources that can help you create quality pull requests, keep your repositories pristine, and build on your open source knowledge.

27 | 28 | 29 | ## Rules To Contribute To This Repo 30 | 31 | - Use any language. 32 | - C, C++, JAVA, Data Structure and Algorithms. 33 | - Go to the particular folder or (make a folder for your language if it doesnt pre-exist) 34 | - Contribute only logical and Algorithm based solution. 35 | - Problems of CodeChef, LeetCode, etc 36 | 37 | ## How to do contributions 38 | 39 | ### 1. Fork the Project 40 | Fork this repository and make changes in code as required. You can change it online or by cloning it in your device. Then Pust it on your Forked Repo for furteher Actions. Do not use special characters in the template above. 41 | 42 | ### 2. Write a Good Commit Message 43 | You have written some code in your branch, and are ready to commit. So, make sure to written good, clean commit messages. Let's review the anatomy of a commit message. 44 | 45 | ``` 46 | --- 47 | First line, no more than 50 characters 48 | 49 | Details section, as long as you want. Not always necessary, but 50 | available if you need it. Wrapped at 72 characters. Present imperative 51 | tense is preferred for commits. That means "fix bug", not "fixes bug" or 52 | "fixed bug". 53 | 54 | - Use bullets if you need 55 | - Bullets are a good way to summarize a few things 56 | 57 | If you have too much info here, it might be a good candidate to break 58 | down into multiple commits. You can use emoji here too :sparkles: 59 | 60 | --- 61 | ``` 62 | 63 | ### 3. Lastly, submit your Pull Request 64 | Go through the checklist on the pull request template to guarantee your submission is valid. Our team will review your application, approve and merge your submission if everything is correct. Otherwise, you will get notified of the changes requested in the pull request comment section. 65 | 66 | **Note:** Don't Forget to add `hacktoberfest-accepted` Label. 67 | 68 | 69 | Please check first and then send your codes with discription. 70 | 71 | **All the best for the event ** 72 | 73 | 74 | ### Show some ❤ by starring the repository. 75 | 76 |

77 |

78 | Thank You 79 |

80 |

81 | -------------------------------------------------------------------------------- /binarysearch tree .js: -------------------------------------------------------------------------------- 1 | class Node{ 2 | constructor(data) { 3 | this.data = data; 4 | this.left = null; 5 | this.right = null; 6 | }; 7 | }; 8 | class BinarySearchTree{ 9 | constructor(){ 10 | this.root = null; 11 | } 12 | insert(data){ 13 | var newNode = new Node(data); 14 | if(this.root === null){ 15 | this.root = newNode; 16 | }else{ 17 | this.insertNode(this.root, newNode); 18 | }; 19 | }; 20 | insertNode(node, newNode){ 21 | if(newNode.data < node.data){ 22 | if(node.left === null){ 23 | node.left = newNode; 24 | }else{ 25 | this.insertNode(node.left, newNode); 26 | }; 27 | } else { 28 | if(node.right === null){ 29 | node.right = newNode; 30 | }else{ 31 | this.insertNode(node.right,newNode); 32 | }; 33 | }; 34 | }; 35 | }; 36 | const BST = new BinarySearchTree(); 37 | BST.insert(1); 38 | BST.insert(3); 39 | BST.insert(2); 40 | -------------------------------------------------------------------------------- /subset_sum_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countSubsets(int arr[], int n, int sum) 5 | { 6 | if(n==0) 7 | return sum==0? 1 : 0; 8 | 9 | return countSubsets(arr, n-1, sum) + countSubsets(arr, n-1, sum - arr[n-1]); 10 | } 11 | 12 | 13 | int main() { 14 | int n,sum; 15 | cin>>n>>sum; 16 | int a[n]; 17 | for(int i=0;i>a[i]; 19 | cout<