├── C++ ├── 0-1-knapsack.cpp └── mergeSort.cpp ├── C └── Polynomial Addition.c ├── CONTRIBUTING.md ├── CircularLinkedList.cpp ├── DiceMin.cpp ├── LeapYear.java ├── MinMaxElementsOfArray.java ├── PowerOf2.java ├── README.md └── tictactoh.java /C++/0-1-knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int knapSack(int v[], int w[], int n, int W) { 6 | if (W < 0) 7 | return INT_MIN; 8 | if (n < 0 || W == 0) 9 | return 0; 10 | int in = v[n] + knapSack(v, w, n - 1, W - w[n]); 11 | int ex = knapSack(v, w, n - 1, W); 12 | return max (in, ex); 13 | } 14 | 15 | int main() { 16 | int v[] = { 10, 20, 30, 40, 60, 70 }; 17 | int w[] = { 1, 2, 3, 6, 7, 4 }; 18 | int W = 7; 19 | int n = sizeof(v) / sizeof(v[0]); 20 | cout << "Knapsack value is " << knapSack(v, w, n - 1, W); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /C++/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | class Solution { 5 | public: 6 | void merge(int arr[], int l, int mid, int r) 7 | { 8 | int i = l ; // starting index of left half of arr 9 | int j = mid + 1; // starting index of right half of arr 10 | int f = l ; // index used to transfer elements in temporary array 11 | int temp[100000] ; // temporary array 12 | 13 | while (i <= mid && j <= r) { 14 | if (arr[i] < arr[j]) { 15 | temp[f] = arr[i] ; 16 | i++ ; 17 | } 18 | else { 19 | temp[f] = arr[j] ; 20 | j++ ; 21 | } 22 | f++ ; 23 | } 24 | 25 | // if elements on the left half are still left // 26 | 27 | if (i > mid) { 28 | while (j <= r) { 29 | temp[f] = arr[j] ; 30 | f++ ; j++ ; 31 | } 32 | } 33 | else { 34 | // if elements on the right half are still left // 35 | while (i <= mid) { 36 | temp[f] = arr[i] ; 37 | f++ ; i++ ; 38 | } 39 | } 40 | 41 | // transfering all elements from temporary to arr // 42 | for (int f = l ; f <= r; f++) { 43 | arr[f] = temp[f] ; 44 | } 45 | } 46 | void mergeSort(int arr[], int l, int r) 47 | { 48 | if (l < r) { 49 | int mid = (l + r) / 2 ; 50 | mergeSort(arr, l, mid) ; // left half 51 | mergeSort(arr, mid + 1, r) ; // right half 52 | merge(arr, l, mid, r) ; // merging sorted halves 53 | } 54 | } 55 | }; 56 | int main() { 57 | 58 | int arr[] = {9, 4, 7, 6, 3, 1, 5} ; 59 | int n = 7; 60 | 61 | Solution obj ; 62 | cout << "Before Sorting Array: "< 2 | typedef struct 3 | { 4 | float coeff; 5 | int exp; 6 | }poly; 7 | 8 | poly term[100]; 9 | 10 | int main(void) 11 | { 12 | int i=0, j=0, k, m, numOfPoly, numOfTerms; 13 | printf("How many polynomials you want to add? "); 14 | scanf("%d", &numOfPoly); 15 | printf("\n"); 16 | while(numOfPoly) 17 | { 18 | printf("How many terms?"); 19 | scanf("%d", &numOfTerms); 20 | printf("\nEnter the co-eff and exps\n"); 21 | k = 0; 22 | while(numOfTerms) 23 | { 24 | scanf("%f %d", &term[i].coeff, &term[i].exp); 25 | i++; 26 | k++; 27 | numOfTerms--; 28 | } 29 | j++; 30 | k--; 31 | printf("%d-th term is: ",j); 32 | for(m=i-1;m>=i-k-1;m--) 33 | { 34 | printf("%.2fx*%d ", term[m].coeff, term[m].exp); 35 | if(m>i-k-1) printf(" + "); 36 | } 37 | printf("\n"); 38 | numOfPoly--; 39 | } 40 | i--; 41 | for(j=0;j 0) 55 | { printf("+ %.2fx^%d", term[j].coeff, term[j].exp); 56 | } 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## HACKTOBERFEST 2022 2 | # 👍 This is Awesome! How Can I Contribute? 3 | 4 | It's very easy. You don't need to be an expert in coding and programming. Here are the steps you need to follow to create your -(maybe)- EXAMPLE first pull request within few minutes. 5 | 1. **Star this repository.** 6 | 2. **Create a new pull request.** 7 | 3. **Wait for your Pull Request to be reviewed and merged!** 8 | 4. **Enjoy and welcome to Hacktoberfest 2022 and Keep Contributing :)** 9 | -------------------------------------------------------------------------------- /CircularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | Node *next; 7 | Node(int d){ 8 | data =d; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | //Print Circular Linked List 14 | void PrintCLL(Node *head){ 15 | if(head==NULL) return ; 16 | Node *curr = head; 17 | do{ 18 | cout<data<<" "; 19 | curr = curr->next; 20 | }while(curr!=head); 21 | cout<next = temp; 30 | return temp; 31 | } 32 | else{ 33 | temp->next = head->next; 34 | head->next = temp; 35 | int t = head->data; 36 | head->data = temp->data; 37 | temp->data = t; 38 | return head; 39 | } 40 | } 41 | 42 | //Insert at End of Circular Linked List 43 | Node *insertAtEnd(Node *head,int x){ 44 | Node *temp = new Node(x); 45 | if(head==NULL){ 46 | temp->next = temp; 47 | return temp; 48 | } 49 | else{ 50 | temp->next = head->next; 51 | head->next = temp; 52 | int t = head->data; 53 | head->data = temp->data; 54 | temp->data = t; 55 | return temp; 56 | } 57 | } 58 | 59 | //Delete Head of Circular Linked list 60 | Node *deleteHead(Node *head){ 61 | if(head ==NULL) return NULL; 62 | if(head->next == head){ 63 | delete head; 64 | return NULL; 65 | } 66 | head->data = head->next->data; 67 | Node *temp = head->next; 68 | head->next = head->next->next; 69 | delete temp; 70 | return head; 71 | 72 | } 73 | 74 | //Delete Kth Node From a Circular Linked list 75 | Node *deleteKth(Node *head , int k){ 76 | if(head==NULL) return head; 77 | if(k==1) return deleteHead(head); 78 | Node *curr = head; 79 | for(int i=0 ; inext; 80 | Node *temp = curr->next; 81 | curr->next = curr->next->next; 82 | delete temp; 83 | return head; 84 | } 85 | 86 | int main(){ 87 | Node *head = NULL; 88 | head = insertAtBegin(head,3); 89 | head = insertAtBegin(head,7); 90 | head = insertAtBegin(head,5); 91 | cout<<"Insert At Begin Operation : "; 92 | PrintCLL(head); 93 | head = insertAtEnd(head,2); 94 | head = insertAtEnd(head,9); 95 | cout<<"Insert At End Operation : "; 96 | PrintCLL(head); 97 | head = deleteHead(head); 98 | cout<<"Delete Head Operation : "; 99 | PrintCLL(head); 100 | head = deleteKth(head,3); 101 | cout<<"Delete 3rd Node From Begining : "; 102 | PrintCLL(head); 103 | 104 | } 105 | 106 | -------------------------------------------------------------------------------- /DiceMin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | struct queueEntry { 5 | int v; 6 | int dist; 7 | }; 8 | int getMinDiceThrows(int Move[], int N) 9 | { 10 | 11 | 12 | bool* visited = new bool[N]; 13 | for (int i = 0; i < N; i++) 14 | visited[i] = true; 15 | 16 | 17 | queue q; 18 | visited[0] = true; 19 | queueEntry s 20 | = { 0, 0 }; 21 | q.push(s); 22 | 23 | 24 | queueEntry qe; 25 | while (!q.empty()) { 26 | qe = q.front(); 27 | int v = qe.v; 28 | 29 | 30 | 31 | if (v == N - 1) 32 | break; 33 | 34 | 35 | q.pop(); 36 | for (int j = v + 1; j <= (v + 6) && j < N; ++j) { 37 | 38 | if (!visited[j]) { 39 | 40 | 41 | queueEntry a; 42 | a.dist = (qe.dist + 1); 43 | visited[j] = true; 44 | if (Move[j] != -1) 45 | a.v = Move[j]; 46 | else 47 | a.v = j; 48 | q.push(a); 49 | } 50 | } 51 | } 52 | 53 | return qe.dist; 54 | } 55 | 56 | int main() 57 | { 58 | int N = 30; 59 | int Moves[N]; 60 | for (int i = 0; i < N; i++) 61 | Moves[i] = -1;s 62 | Moves[2] = 21; 63 | Moves[4] = 7; 64 | Moves[10] = 25; 65 | Moves[19] = 28; 66 | 67 | 68 | Moves[26] = 0; 69 | Moves[20] = 8; 70 | Moves[16] = 3; 71 | Moves[18] = 6; 72 | 73 | cout << "Min Dice throws required is " 74 | << getMinDiceThrows(Moves, N); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /LeapYear.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class LeapYear { 4 | public static void main(String[] args) { 5 | System.out.print("Enter Year To Check: "); 6 | Scanner sc = new Scanner(System.in); 7 | int year = sc.nextInt(); 8 | if(year%4==0 && year%400==0 || year%100!=0){ 9 | System.out.println(year+" is a leap year"); 10 | } else { 11 | System.out.println(year+" is not a leap year"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MinMaxElementsOfArray.java: -------------------------------------------------------------------------------- 1 | public class MinMaxElementsofArray { 2 | public static void main(String[] args) { 3 | int [] nums = {12,9,5,76,3,42,76,22}; 4 | int max = Integer.MIN_VALUE; 5 | int min = Integer.MAX_VALUE; 6 | for(int i=0; imax){ 8 | max=nums[i]; 9 | } 10 | } 11 | System.out.println("Max is "+max); 12 | for(int i=0; i= brdSize || y < 0 || y >= brdSize || brd[x][y] != ' '){ 29 | return INVALID; 30 | } 31 | 32 | brd[x][y] = symbol; 33 | c++; 34 | // Check Row 35 | if(brd[x][0] == brd[x][1] && brd[x][0] == brd[x][2]){ 36 | return symbol == p1Symbol ? PLAYER_1_WINS : PLAYER_2_WINS; 37 | } 38 | // Check Col 39 | if(brd[0][y] == brd[1][y] && brd[0][y] == brd[2][y]){ 40 | return symbol == p1Symbol ? PLAYER_1_WINS : PLAYER_2_WINS; 41 | } 42 | // First Diagonal 43 | if(brd[0][0] != ' ' && brd[0][0] == brd[1][1] && brd[0][0] == brd[2][2]){ 44 | return symbol == p1Symbol ? PLAYER_1_WINS : PLAYER_2_WINS; 45 | } 46 | // Second Diagonal 47 | if(brd[0][2] != ' ' && brd[0][2] == brd[1][1] && brd[0][2] == brd[2][0]){ 48 | return symbol == p1Symbol ? PLAYER_1_WINS : PLAYER_2_WINS; 49 | } 50 | if(c == brdSize * brdSize){ 51 | return DRAW; 52 | } 53 | return INCOMPLETE; 54 | 55 | } 56 | public void print() { 57 | System.out.println("---------------"); 58 | for(int i =0; i < brdSize; i++){ 59 | for(int j =0; j < brdSize; j++){ 60 | System.out.print("| " + brd[i][j] + " |"); 61 | } 62 | System.out.println( ); 63 | } 64 | System.out.println(); 65 | System.out.println("----------------"); 66 | } 67 | } 68 | 69 | --------------------------------------------------------------------------------