diff --git a/Data Structures/C:C++/duplicateNumber.cpp b/Data Structures/C:C++/duplicateNumber.cpp new file mode 100644 index 00000000..bc8e4f4f --- /dev/null +++ b/Data Structures/C:C++/duplicateNumber.cpp @@ -0,0 +1,35 @@ +//6th +//Find the Duplicate Number +// Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. +// There is only one repeated number in nums, return this repeated number. +// You must solve the problem without modifying the array nums and uses only constant extra space. + + +// Example 1: +// +// Input: nums = [1,3,4,2,2] +// Output: 2 + + +//Rabit and tortoise method. + +class Solution { +public: + int findDuplicate(vector& nums) { + int slow = nums[0]; + int fast = nums[0]; + do{ + slow = nums[slow]; + fast = nums[nums[fast]]; + }while(slow!=fast); + + fast = nums[0]; + while(slow != fast){ + slow = nums[slow]; + fast = nums[fast]; + } + return slow; + } + + +}; diff --git a/Data Structures/C:C++/firstNonRepeatingEle.cpp b/Data Structures/C:C++/firstNonRepeatingEle.cpp new file mode 100644 index 00000000..4c234fdf --- /dev/null +++ b/Data Structures/C:C++/firstNonRepeatingEle.cpp @@ -0,0 +1,35 @@ +// Find the first non-repeating element in a given array arr of N integers. +// Note: Array consists of only positive and negative integers and not zero. +// +// Example 1: +// +// Input : arr[] = {-1, 2, -1, 3, 2} +// Output : 3 +// Explanation: +// -1 and 2 are repeating whereas 3 is +// the only number occuring once. +// Hence, the output is 3. + + + +class Solution{ + public: + int firstNonRepeating(int arr[], int n) + { + unordered_map mpp; + for(int i=0;i mpp; + for(int i=0;i= 2) + { + return i+1; + } + } + return -1; + } +}; diff --git a/Data Structures/C:C++/fourSum.cpp b/Data Structures/C:C++/fourSum.cpp new file mode 100644 index 00000000..7367f98e --- /dev/null +++ b/Data Structures/C:C++/fourSum.cpp @@ -0,0 +1,47 @@ +//4sum + +class Solution { +public: + vector> fourSum(vector& nums, int target) { + vector> res; + if(nums.empty()) + return res; + int n = nums.size(); + sort(nums.begin(), nums.end()); + + for(int i=0; i < n; ++i) + { + for(int j=i+1; j target_2)left--; + else{ + vector quadruple(4,0); + quadruple[0] = nums[i]; + quadruple[1] = nums[j]; + quadruple[2] = nums[right]; + quadruple[3] = nums[left]; + res.push_back(quadruple); + + while(right < left && nums[right] == quadruple[2]) ++right; + + while(right < left && nums[left] == quadruple[3]) --left; + } + } + while((j+1) < n && nums[j+1] == nums[j]) ++j; + } + while((i+1) < n && nums[i+1] == nums[i]) ++i; + + } + + + return res; + } + +}; diff --git a/Data Structures/C:C++/inversionArray.cpp b/Data Structures/C:C++/inversionArray.cpp new file mode 100644 index 00000000..6f9c2e60 --- /dev/null +++ b/Data Structures/C:C++/inversionArray.cpp @@ -0,0 +1,112 @@ +//. Inversion of Array (Using Merge Sort) + +// Inversion Count for an array indicates – how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. +// Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j +// Example: +// +// Input: arr[] = {8, 4, 2, 1} +// Output: 6 +// +// Explanation: Given array has six inversions: +// (8, 4), (4, 2), (8, 2), (8, 1), (4, 1), (2, 1). + +//BRUTE FORCE APPROACH + +// C++ program to Count Inversions +// in an array +#include +using namespace std; + +int getInvCount(int arr[], int n) +{ + int inv_count = 0; + for (int i = 0; i < n - 1; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] > arr[j]) + inv_count++; + + return inv_count; +} + +// Driver Code +int main() +{ + int arr[] = { 1, 20, 6, 4, 5 }; + int n = sizeof(arr) / sizeof(arr[0]); + cout << " Number of inversions are " + << getInvCount(arr, n); + return 0; +} + +//MERGE SORT METHOD +// C++ program to Count +// Inversions in an array +// using Merge Sort +#include +using namespace std; + +int _mergeSort(int arr[], int temp[], int left, int right); +int merge(int arr[], int temp[], int left, int mid, + int right); + +/* This function sorts the +input array and returns the +number of inversions in the array */ +int mergeSort(int arr[], int array_size) +{ + int temp[array_size]; + return _mergeSort(arr, temp, 0, array_size - 1); +} + +/* An auxiliary recursive function +that sorts the input array and +returns the number of inversions in the array. */ +int _mergeSort(int arr[], int temp[], int left, int right) +{ + int mid, inv_count = 0; + if (right > left) { + mid = (right + left) / 2; + inv_count += _mergeSort(arr, temp, left, mid); + inv_count += _mergeSort(arr, temp, mid + 1, right); + inv_count += merge(arr, temp, left, mid + 1, right); + } + return inv_count; +} + +int merge(int arr[], int temp[], int left, int mid, + int right) +{ + int i, j, k; + int inv_count = 0; + + i = left; /* i is index for left subarray*/ + j = mid; /* j is index for right subarray*/ + k = left; /* k is index for resultant merged subarray*/ + while ((i <= mid - 1) && (j <= right)) { + if (arr[i] <= arr[j]) { + temp[k++] = arr[i++]; + } + else { + temp[k++] = arr[j++]; + inv_count = inv_count + (mid - i); + } + } + while (i <= mid - 1) + temp[k++] = arr[i++]; + while (j <= right) + temp[k++] = arr[j++]; + for (i = left; i <= right; i++) + arr[i] = temp[i]; + + return inv_count; +} + +// Driver code +int main() +{ + int arr[] = { 1, 20, 6, 4, 5 }; + int n = sizeof(arr) / sizeof(arr[0]); + int ans = mergeSort(arr, n); + cout << " Number of inversions are " << ans; + return 0; +} diff --git a/Data Structures/C:C++/largestSubarrayWithZero.cpp b/Data Structures/C:C++/largestSubarrayWithZero.cpp new file mode 100644 index 00000000..3f2040aa --- /dev/null +++ b/Data Structures/C:C++/largestSubarrayWithZero.cpp @@ -0,0 +1,43 @@ +// Largest subarray with 0 sum +// +// Given an array having both positive and negative integers. The task is to compute the length of the largest subarray with sum 0. +// +// Example 1: +// +// Input: +// N = 8 +// A[] = {15,-2,2,-8,1,7,10,23} +// Output: 5 +// Explanation: The largest subarray with +// sum 0 will be -2 2 -8 1 7. +// Your Task: +// You just have to complete the function maxLen() which takes two arguments an array A and n, where n is the size of the array A and returns the length of the largest subarray with 0 sum. +// +// Expected Time Complexity: O(N). +// Expected Auxiliary Space: O(N). + +int maxLen(int A[], int n) +{ + // Your code here + int sum=0; + int maxi=0; + unordered_map mpp; + for(int i=0;i&nums){ + int inc = 1; + int n=nums.size(); + int dec = 1; + for (int i = 1; i < n; i++) + { + + if (nums[i] > nums[i - 1]) + { + inc = dec + 1; + } + + else if (nums[i] < nums[i - 1]) + { + dec = inc + 1; + } + } + return max(inc, dec); + } + +};