diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Detection&Removal_of_Loop_in_LL_(Floyd's_Algorithm).cpp b/Detection&Removal_of_Loop_in_LL_(Floyd's_Algorithm).cpp new file mode 100644 index 0000000..d57487b --- /dev/null +++ b/Detection&Removal_of_Loop_in_LL_(Floyd's_Algorithm).cpp @@ -0,0 +1,115 @@ +#include +using namespace std; + +/* Link list node */ +struct Node { + int data; + struct Node* next; +}; + +/* Function to remove loop. */ +void removeLoop(struct Node*, struct Node*); + +/* This function detects and removes loop in the list +If loop was there in the list then it returns 1, +otherwise returns 0 */ +int detectAndRemoveLoop(struct Node* list) +{ + struct Node *slow_p = list, *fast_p = list; + + // Iterate and find if loop exists or not + while (slow_p && fast_p && fast_p->next) { + slow_p = slow_p->next; + fast_p = fast_p->next->next; + + /* If slow_p and fast_p meet at some point then there + is a loop */ + if (slow_p == fast_p) { + removeLoop(slow_p, list); + + /* Return 1 to indicate that loop is found */ + return 1; + } + } + + /* Return 0 to indicate that there is no loop*/ + return 0; +} + + +/* Function to remove loop. +loop_node --> Pointer to one of the loop nodes +head --> Pointer to the start node of the linked list */ +void removeLoop(struct Node* loop_node, struct Node* head) +{ + struct Node* ptr1 = loop_node; + struct Node* ptr2 = loop_node; + + // Count the number of nodes in loop + unsigned int k = 1, i; + while (ptr1->next != ptr2) { + ptr1 = ptr1->next; + k++; + } + + // Fix one pointer to head + ptr1 = head; + + // And the other pointer to k nodes after head + ptr2 = head; + for (i = 0; i < k; i++) + ptr2 = ptr2->next; + + /* Move both pointers at the same pace, + they will meet at loop starting node */ + while (ptr2 != ptr1) { + ptr1 = ptr1->next; + ptr2 = ptr2->next; + } + + // Get pointer to the last node + while (ptr2->next != ptr1) + ptr2 = ptr2->next; + + /* Set the next node of the loop ending node + to fix the loop */ + ptr2->next = NULL; +} + +/* Function to print linked list */ +void printList(struct Node* node) +{ + // Print the list after loop removal + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +struct Node* newNode(int key) +{ + struct Node* temp = new Node(); + temp->data = key; + temp->next = NULL; + return temp; +} + +// Driver Code +int main() +{ + struct Node* head = newNode(50); + head->next = newNode(20); + head->next->next = newNode(15); + head->next->next->next = newNode(4); + head->next->next->next->next = newNode(10); + + /* Create a loop for testing */ + head->next->next->next->next->next = head->next->next; + + detectAndRemoveLoop(head); + + cout << "Linked List after removing loop \n"; + printList(head); + return 0; +} + diff --git a/Doubly Linked List Traversal.cpp b/Doubly Linked List Traversal.cpp new file mode 100644 index 0000000..2376808 --- /dev/null +++ b/Doubly Linked List Traversal.cpp @@ -0,0 +1,70 @@ +#include +#include + +struct Node{ + int data; + struct Node *prev; + struct Node *next; +}; + +void DoublylinkedListTraversal(struct Node *ptr){ + while(ptr != NULL){ + printf("Elements: %d\n",ptr->data); + ptr=ptr->next; +} +}/* +void DoublylinkedListTraversal(struct Node *head) +{ + struct Node * ptr = head; + while (ptr->next != NULL) + { + printf("Elements: %d\n",ptr->data); + ptr = ptr->next; + } + printf("Elements: %d\n",ptr->data); + printf("\n"); + while (ptr != NULL) + { + printf("Elements: %d\n",ptr->data); + ptr = ptr->prev; + } +} +*/ +int main(){ + struct Node *head; + struct Node *second; + struct Node *third; + struct Node *fourth; + + //Allocate memory to Nodes in heap + head=(struct Node*)malloc(sizeof(struct Node)); + second=(struct Node*)malloc(sizeof(struct Node)); + third=(struct Node*)malloc(sizeof(struct Node)); + fourth=(struct Node*)malloc(sizeof(struct Node)); + + //Link first to second as well prev to NULL + head->data=23; + head->prev=NULL; + head->next=second; + + //Link second to third as well prev to first + second->data=34; + second->prev=head; + second->next=third; + + //Link third to fourth as well prev to second + third->data=56; + third->prev=second; + third->next=fourth; + + //Link fourth to NULL as well prev to third + fourth->data=52; + fourth->prev=third; + fourth->next=NULL; + + DoublylinkedListTraversal(head); + //printf("\n"); + //DoublylinkedListTraversal(fourth); + + return 0; +} diff --git a/Insertion_Sort.cpp b/Insertion_Sort.cpp new file mode 100644 index 0000000..769a5f7 --- /dev/null +++ b/Insertion_Sort.cpp @@ -0,0 +1,61 @@ + +// This is a program on the insertion sort. It's a sorting algorithm. +// -----------Time complexity of the insertion sort algorithm---------- +// 1. Best complexity: n +// 2. Average complexity: n^2 +// 3. Worst complexity: n^2 +// -----------Space complexity of the insertion sort algorithm--------- +// 1.Space complexity: 1 + + +#include +using namespace std; + +// function to print the elements of the array +void display(int arr[], int n) { + + for(int i=0;i= 0) { + arr[j + 1] = arr[j]; + --j; + } + arr[j + 1] = temp; + } +} + +int main() { + + int n; + cout<<"Enter the number of elements : "; + cin>>n; + + int arr[n]; + cout<<"Enter the array elements :\n"; + for(int i=0;i>arr[i]; + } + + cout<<"\nBefore Sort the array is :\n"; + display(arr,n); + + insertionSort(arr, n); + + cout<<"\nAfter Insertion Sort the array is :\n"; + display(arr,n); + + return 0; + +} diff --git a/Kadanes_Algo b/Kadanes_Algo new file mode 100644 index 0000000..d788079 --- /dev/null +++ b/Kadanes_Algo @@ -0,0 +1,20 @@ +#include +using namespace std; +int main() +{ + int n; + cin >> n; + int ms = 0, cs = 0; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + cs += arr[i]; + if (cs < 0) + cs = 0; + else if (cs >= ms) + ms = cs; + } + cout << ms << endl; + return 0; +} diff --git a/LongestCommonSubsequence.cpp b/LongestCommonSubsequence.cpp new file mode 100644 index 0000000..6b6a574 --- /dev/null +++ b/LongestCommonSubsequence.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +//finding max value +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +//finding LCS +int LongestCommonSubsequence(char *str1, char *str2, int x, int y) +{ + if (x == 0 || y == 0) + { + return 0; + } + if (str1[x - 1] == str2[y - 1]) + { + return (1 + LongestCommonSubsequence(str1, str2, x - 1, y - 1)); + } + else + { + return max(LongestCommonSubsequence(str1, str2, x, y - 1), LongestCommonSubsequence(str1, str2, x - 1, y)); + } +} + +//main function +int main() +{ + char str1[] = "cantonment"; + char str2[] = "longatone"; + int x = strlen(str1); + int y = strlen(str2); + cout << "\nLength of Longest Common Subsequence From Both The Strings Is " << LongestCommonSubsequence(str1, str2, x, y) << endl; + return 0; +} \ No newline at end of file diff --git a/Merge sort in cpp b/Merge sort in cpp new file mode 100644 index 0000000..b806565 --- /dev/null +++ b/Merge sort in cpp @@ -0,0 +1,69 @@ +#include +using namespace std; +void swapping(int &a, int &b) { //swap the content of a and b + int temp; + temp = a; + a = b; + b = temp; +} +void display(int *array, int size) { + for(int i = 0; i> n; + int arr[n]; //create an array with given number of elements + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Array before Sorting: "; + display(arr, n); + mergeSort(arr, 0, n-1); //(n-1) for last index + cout << "Array after Sorting: "; + display(arr, n); +} diff --git a/Quick_Sort.cpp b/Quick_Sort.cpp new file mode 100644 index 0000000..d49e04f --- /dev/null +++ b/Quick_Sort.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +int partition(int *a, int start, int end){ + + int pivot=a[end]; + //P-index indicates the pivot value index + + int P_index=start; + int i,t; //t is temporary variable + + //Here We will check if array value is less than pivot + //Then we will place it at left side by swapping + + for(i=start;i>n; + + int a[n]; + cout<<"Enter the array elements :\n"; + for(int i=0;i>a[i]; + } + + Quicksort(a,0,n-1); + + cout<<"After Quick Sort the array is :\n"; + for(int i=0;i +using namespace std; + +// Recursive function to sort an array using +// insertion sort +void insertionSortRecursive(int arr[], int n) +{ + // Base case + if (n <= 1) + return; + + // Sort first n-1 elements + insertionSortRecursive( arr, n-1 ); + + int last = arr[n-1]; + int j = n-2; + + while (j >= 0 && arr[j] > last) + { + arr[j+1] = arr[j]; + j--; + } + arr[j+1] = last; +} + +void display(int arr[], int n) +{ + for (int i=0; i < n; i++) + cout << arr[i] <<" "; +} + +int main() +{ + int n; + cout<<"Enter number of elements : "; + cin>>n; + + int arr[n]; + cout<<"Enter the array elements :\n"; + for(int i=0;i>arr[i]; + } + + cout<<"\nBefore Sort the array is :\n"; + display(arr,n); + + insertionSortRecursive(arr, n); + + cout<<"\nAfter Insertion Sort the array is :\n"; + display(arr, n); + + return 0; +} diff --git a/Stack using linked list.c b/Stack using linked list.c new file mode 100644 index 0000000..c3f9864 --- /dev/null +++ b/Stack using linked list.c @@ -0,0 +1,106 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}; + +struct Node *head = NULL; + +void push(int value) +{ + struct Node *n = (struct Node *)malloc(sizeof(struct Node *)); + if (head == NULL) + { + head = n; + n->data = value; + n->next = NULL; + return; + } + + n->next = head; + n->data = value; + head = n; +} + +void pop() +{ + struct Node *temp = head; + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + + head = head->next; + temp->next = NULL; +} + +void peek() +{ + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + + printf("The top element is: %d\n", head->data); +} + +void display() +{ + struct Node *temp = head; + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + printf("\nElements of stack are:\n"); + while (temp != NULL) + { + printf("%d\n", temp->data); + temp = temp->next; + } +} + +int main() +{ + while (1) + { + printf("\n=======Menu=======\n"); + printf("1. Push\n"); + printf("2. Pop\n"); + printf("3. Peek\n"); + printf("4. Display\n"); + printf("5. Exit\n\n"); + int c, n; + scanf("%d", &c); + switch (c) + { + case 1: + printf("Enter the element to be inserted: "); + scanf("%d", &n); + push(n); + break; + case 2: + pop(); + break; + case 3: + peek(); + break; + case 4: + display(); + break; + case 5: + exit(0); + break; + + default: + printf("Please select a valid option."); + } + } + + return 0; +} \ No newline at end of file diff --git a/Swap Numbers.cpp b/Swap Numbers.cpp new file mode 100644 index 0000000..a956a99 --- /dev/null +++ b/Swap Numbers.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() +{ + int a = 5, b = 10, temp; + + cout << "Before swapping." << endl; + cout << "a = " << a << ", b = " << b << endl; + + temp = a; + a = b; + b = temp; + + cout << "\nAfter swapping." << endl; + cout << "a = " << a << ", b = " << b << endl; + + return 0; +} diff --git a/ZigZagPattern.cpp b/ZigZagPattern.cpp new file mode 100644 index 0000000..e345f7f --- /dev/null +++ b/ZigZagPattern.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +int main() +{ + + int n; + cin >> n; + + for (int i = 1; i <= 3; i++) + { + if (i == 1) + { + int c = 0; + for (int j = 1; j <= n; j++) + { + if (j == 1 || j == 2) + { + cout << " "; + } + else + { + if (c % 4 == 0) + { + cout << "* "; + } + else + { + cout << " "; + } + c++; + } + } + } + else if (i == 2) + { + for (int j = 1; j <= n; j++) + { + if (j % 2 == 0) + { + cout << "* "; + } + else + { + cout << " "; + } + } + } + else + { + for (int j = 1; j <= n; j++) + { + if (j % 4 == 1) + { + cout << "* "; + } + else + { + cout << " "; + } + } + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/binary_search.cpp b/binary_search.cpp new file mode 100644 index 0000000..1d8ab11 --- /dev/null +++ b/binary_search.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; +#define ll long long int +#define endl '\n' +#define MOD 1000000007 +#define fastio() \ + ios_base::sync_with_stdio(false); \ + cin.tie(NULL); \ + cout.tie(NULL); + +int binarySearch(vector &nums, int target) +{ + int start = 0, end = nums.size() - 1; + + while (start <= end) + { + int mid = start + (end - start) / 2; + + if (nums[mid] == target) + { + return mid; + } + else if (nums[mid] < target) + { + start = mid + 1; + } + else + { + end = mid - 1; + } + } + + return -1; +} +int main() +{ + int noOfElements; + cout << "Please enter number of elements : "; + cin >> noOfElements; + vector elements; + + cout << "Please enter the elements one by one" << endl; + for (int i = 0; i < noOfElements; i++) + { + int a; + cin >> a; + elements.push_back(a); + } + sort(elements.begin(), elements.end()); + + int noToFind; + cout << "Please enter the number to search : "; + cin >> noToFind; + + int index = binarySearch(elements, noToFind); + if (index != -1) + { + cout << noToFind << " found in the array" << endl; + } + else + { + cout << noToFind << " is not present" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/binary_search.exe b/binary_search.exe new file mode 100644 index 0000000..46c40f5 Binary files /dev/null and b/binary_search.exe differ diff --git a/dnfSortArrays.cpp b/dnfSortArrays.cpp new file mode 100644 index 0000000..d496a0c --- /dev/null +++ b/dnfSortArrays.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +void swap(int arr[], int i, int j){ + int temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; +} + +void dnfSort(int arr[], int n){ + int low=0; + int mid=0; + int high=n-1; + + while(mid<=high){ + if(arr[mid]==0){ + swap(arr,low,mid); + low++; + mid++; + } + else if(arr[mid]==1){ + mid++; + } + else{ + swap(arr,mid,high); + high--; + } + } +} + +int main(){ + int arr[]={1,0,2,1,0,1,2,1,2}; + dnfSort(arr,9); + + for(int i=0;i<9;i++){ + cout< + +using namespace std; + +int main() { + + // Begin our main loop of 1-50 + for (int i = 1; i <= 50; i++) { + + string output = ""; + + if (i % 3 == 0) output += "Fizz"; // If our current number is a multiple of 3, add "Fizz to the output" + if (i % 5 == 0) output += "Buzz"; // If our current number is a multiple of 5, add "Buzz to the output" + + if (output == "") output = to_string(i); // If the output is empty (our number isn't a multiple of 3 or 5), we simply set it to our number + + cout << output << endl; // Show the user the output + + } + +} diff --git a/insertion_sort.cpp b/insertion_sort.cpp new file mode 100644 index 0000000..37a691a --- /dev/null +++ b/insertion_sort.cpp @@ -0,0 +1,80 @@ +#include +#include +using namespace std; +#define int long long + +// bool compareQ(pair a, pair b) +// { +// if (a.first < b.first) +// return (a.second > b.second); + + +// return false; +// // } +// int N = 1e9 + 2; +void insertion_sort(int a[], int n) +{ + for (int i = 0; i < n; i++) + { + int key = a[i]; + int j = i - 1; + while (j >= 0 && a[j] > key) + { + a[j + 1] = a[j]; + j--; + } + a[j + 1] = key; + + } +} + +void solve() +{ + + int n; + cin >> n; + int a[n]; + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + insertion_sort(a, n); + + for (int i = 0; i < n; i++) + { + cout << a[i] << " "; + } +} + + + + +int32_t main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); +#endif + + long long t = 1; + + // cin >> t; + while (t--) + { + + solve(); + } + + + +} + + + + + + diff --git a/longest_common_subsequence_using_dp.cpp b/longest_common_subsequence_using_dp.cpp new file mode 100644 index 0000000..cf862e8 --- /dev/null +++ b/longest_common_subsequence_using_dp.cpp @@ -0,0 +1,73 @@ +//here i am providing two solutions to the following problems using dynamic programming +//one is Top-Down DP and another is Bottom-Up DP +// this paticular questions corresponds to the folloing question on leetcode +//leetcode-1143 https://leetcode.com/problems/longest-common-subsequence/ +//here i have explained the question in a gradual optimal manner +//------------------------------------------------------------------------------------------------------------------ +//brute force approach +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + return longestCommonSubsequence(text1, text2, 0, 0); + } + + private int longestCommonSubsequence(String text1, String text2, int i, int j) { + if (i == text1.length() || j == text2.length()) + return 0; + if (text1.charAt(i) == text2.charAt(j)) + return 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1); + else + return Math.max( + longestCommonSubsequence(text1, text2, i + 1, j), + longestCommonSubsequence(text1, text2, i, j + 1) + ); + } +} +//------------------------------------------------------------------------------------------------------------------ +/* Top-down DP +We might use memoization to overcome overlapping subproblems. +Since there are two changing values, i.e. i and j in the recursive +function longestCommonSubsequence, we might apply a two-dimensional array.*/ +class Solution { + private Integer[][] dp; + public int longestCommonSubsequence(String text1, String text2) { + dp = new Integer[text1.length()][text2.length()]; + return longestCommonSubsequence(text1, text2, 0, 0); + } + + private int longestCommonSubsequence(String text1, String text2, int i, int j) { + if (i == text1.length() || j == text2.length()) + return 0; + + if (dp[i][j] != null) + return dp[i][j]; + + if (text1.charAt(i) == text2.charAt(j)) + return dp[i][j] = 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1); + else + return dp[i][j] = Math.max( + longestCommonSubsequence(text1, text2, i + 1, j), + longestCommonSubsequence(text1, text2, i, j + 1) + ); + } +} +//------------------------------------------------------------------------------------------------------------------ +/*Bottom-up DP +For every i in text1, j in text2, we will choose one of the following two options: + +if two characters match, length of the common subsequence would be 1 plus the +length of the common subsequence till the i-1 andj-1 indexes +if two characters doesn't match, we will take the longer by either skipping i or j indexes*/ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int[][] dp = new int[text1.length() + 1][text2.length() + 1]; + for (int i = 1; i <= text1.length(); i++) { + for (int j = 1; j <= text2.length(); j++) { + if (text1.charAt(i - 1) == text2.charAt(j - 1)) + dp[i][j] = 1 + dp[i - 1][j - 1]; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[text1.length()][text2.length()]; + } +} \ No newline at end of file diff --git a/permutations.cpp b/permutations.cpp new file mode 100644 index 0000000..ae51194 --- /dev/null +++ b/permutations.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector> ans; + + void allper(vector& nums,int pos,vector&used,vector& cur){ + if(nums.size()==cur.size()){ + ans.push_back(cur); + return; + } + for(int i = 0 ; i> permute(vector& nums) { + vector used(nums.size(),false); + vector cur; + allper(nums,0,used,cur); + return ans; + } +}; diff --git a/rat_in_maze.cpp b/rat_in_maze.cpp new file mode 100644 index 0000000..808a791 --- /dev/null +++ b/rat_in_maze.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +bool isSafe(int** arr, int x, int y, int n){ + + if( x>n; + + int** arr = new int*[n]; + for(int i=0; i>arr[i][j]; + } + } + + int** solArr = new int*[n]; + for(int i=0; i maxSlidingWindow(vector& nums, int k) { + int n = nums.size(); + if (n == 0 || k == 0) return {}; + + vector v(n-k+1); + deque d; + + for (int i = 0; i < n; ++i) { + while (!d.empty() && d.front() <= i-k) + d.pop_front(); + + while (!d.empty() && nums[d.back()] < nums[i]) + d.pop_back(); + + d.push_back(i); + + if (i >= k-1) + v[i+1-k] = nums[d.front()]; + } + return v; + } + +}; diff --git a/stack_linkedlist.cpp b/stack_linkedlist.cpp new file mode 100644 index 0000000..5022a8b --- /dev/null +++ b/stack_linkedlist.cpp @@ -0,0 +1,82 @@ +/* C++ program to implement basic stack +operations */ +#include + +using namespace std; + +#define MAX 1000 + +class Stack { + int top; + +public: + int a[MAX]; // Maximum size of Stack + + Stack() { top = -1; } + bool push(int x); + int pop(); + int peek(); + bool isEmpty(); +}; + +bool Stack::push(int x) +{ + if (top >= (MAX - 1)) { + cout << "Stack Overflow"; + return false; + } + else { + a[++top] = x; + cout << x << " pushed into stack\n"; + return true; + } +} + +int Stack::pop() +{ + if (top < 0) { + cout << "Stack Underflow"; + return 0; + } + else { + int x = a[top--]; + return x; + } +} +int Stack::peek() +{ + if (top < 0) { + cout << "Stack is Empty"; + return 0; + } + else { + int x = a[top]; + return x; + } +} + +bool Stack::isEmpty() +{ + return (top < 0); +} + +// Driver program to test above functions +int main() +{ + class Stack s; + s.push(10); + s.push(20); + s.push(30); + cout << s.pop() << " Popped from stack\n"; + //print all elements in stack : + cout<<"Elements present in stack : "; + while(!s.isEmpty()) + { + // print top element in stack + cout< +using namespace std; + +int main(){ + int n; + cin>>n; + + int arr[n]; + for(int i=0;i>arr[i]; + } + + int sumfinal=0; + for(int i=0;i + +#define ll long long int +#define ld long double + +using namespace std; + +void file_io() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif +} + +class ThreeVal +{ +public: + bool True, False, None; + + ThreeVal() + { + True = false; + False = false; + None = false; + } + + void setTrue() + { + True = true; + False = false; + None = false; + } + + void setFalse() + { + False = true; + True = false; + None = false; + } + + void setNone() + { + None = true; + True = false; + False = false; + } +}; + +void addPerson(vector *arr, ll n, vector *currentElements, ll noOfSelectedElements, vector *solution, ll *minimumDifference, ll sum, ll currentSum, ll currentPosition) +{ + if (currentPosition == n) + { + return; + } + + if (((n / 2) - noOfSelectedElements) > (n - currentPosition)) + { + return; + } + addPerson(arr, n, currentElements, noOfSelectedElements, solution, minimumDifference, sum, currentSum, currentPosition + 1); + noOfSelectedElements++; + currentSum += (*arr)[currentPosition]; + (*currentElements)[currentPosition].setTrue(); + + if (noOfSelectedElements == n / 2) + { + if ((abs(sum / 2) - currentSum) < *minimumDifference) + { + *minimumDifference = abs(sum / 2 - currentSum); + for (ll i = 0; i < n; i++) + { + (*solution)[i] = (*currentElements)[i]; + } + } + } + else + { + addPerson(arr, n, currentElements, noOfSelectedElements, solution, minimumDifference, sum, currentSum, currentPosition + 1); + } + + (*currentElements)[currentPosition].setFalse(); +} + +void tugOfWar(vector *arr, ll n) +{ + vector *currentElements = new vector(); + for (ll i = 0; i < n; i++) + { + ThreeVal tv; + tv.setNone(); + currentElements->push_back(tv); + } + + vector *solution = new vector(); + for (ll i = 0; i < n; i++) + { + ThreeVal tv; + tv.setNone(); + solution->push_back(tv); + } + + ll minimumDifference = INT_MAX; + + ll sum = 0; + for (ll i = 0; i < n; i++) + { + sum += (*arr)[i]; + (*currentElements)[i].setFalse(); + (*solution)[i].setFalse(); + } + + addPerson(arr, n, currentElements, 0, solution, &minimumDifference, sum, 0, 0); + + ll sum1 = 0, sum2 = 0; + + for (ll i = 0; i < n; i++) + { + // cout << (*solution)[i].True << " " << (*solution)[i].False << " " << (*solution)[i].None << endl; + if ((*solution)[i].True) + { + sum1 += (*arr)[i]; + } + + if ((*solution)[i].False) + { + sum2 += (*arr)[i]; + } + } + + if (sum1 < sum2) + { + cout << sum1 << " " << sum2 << endl; + } + else + { + cout << sum2 << " " << sum1 << endl; + } +} + +int main() +{ + file_io(); + ll testCases; + cin >> testCases; + ll n, no; + vector arr; + while (testCases--) + { + arr.clear(); + cin >> n; + for (ll i = 0; i < n; i++) + { + cin >> no; + arr.push_back(no); + } + tugOfWar(&arr, n); + } + + return 0; +} \ No newline at end of file diff --git a/valid_sudoku.cpp b/valid_sudoku.cpp new file mode 100644 index 0000000..031e1e7 --- /dev/null +++ b/valid_sudoku.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + bool isValidSudoku(vector>& b) { + vector> row(9); + vector> col(9); + vector> box(9); + + for(int i=0 ; i<9 ; i ++){ + for(int j=0 ; j<9 ; j++){ + char val=b[i][j]; + if(val=='.')continue; + if(row[i].find(val)!=row[i].end())return false; + row[i].insert(val); + if(col[j].find(val)!=col[j].end())return false; + col[j].insert(val); + int id=(i/3)*3+j/3; + if(box[id].find(val)!=box[id].end())return false; + box[id].insert(val); + } + } + return true; + } +}; diff --git a/waveSortArrays.cpp b/waveSortArrays.cpp new file mode 100644 index 0000000..c8567c1 --- /dev/null +++ b/waveSortArrays.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +void swap(int arr[],int i,int j){ + int temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; +} + +void waveSort(int arr[],int n){ + for(int i=1;iarr[i-1]){ + swap(arr,i,i-1); + } + if(arr[i]>arr[i+1] && i