Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Data Structures/C:C++/duplicateNumber.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& 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;
}


};
35 changes: 35 additions & 0 deletions Data Structures/C:C++/firstNonRepeatingEle.cpp
Original file line number Diff line number Diff line change
@@ -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<int,int> mpp;
for(int i=0;i<n;i++)
{
mpp[arr[i]]++;
}
for(int i=0;i<n;i++)
{
if(mpp[arr[i]] == 1)
{
return arr[i];
}
}
return 0;

}

};
40 changes: 40 additions & 0 deletions Data Structures/C:C++/firstRepeatingEle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Given an array arr[] of size n, find the first repeating element. The element should occurs more than once and the index of its first occurrence should be the smallest.
//
//
//
// Example 1:
//
// Input:
// n = 7
// arr[] = {1, 5, 3, 4, 3, 5, 6}
// Output: 2
// Explanation:
// 5 is appearing twice and
// its first appearence is at index 2
// which is less than 3 whose first
// occuring index is 3.






class Solution {
public:
// Function to return the position of the first repeating element.
int firstRepeated(int arr[], int n) {
unordered_map<int,int> mpp;
for(int i=0;i<n;i++)
{
mpp[arr[i]]++;
}
for(int i=0;i<n;i++)
{
if(mpp[arr[i]] >= 2)
{
return i+1;
}
}
return -1;
}
};
47 changes: 47 additions & 0 deletions Data Structures/C:C++/fourSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//4sum

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> 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<n; ++j)
{
int target_2 = target - nums[i] - nums[j];
int right = j+1;
int left = n-1;
while(right < left)
{
int two_sum = nums[right] + nums[left];
if(two_sum < target_2) right++;
else if(two_sum > target_2)left--;
else{
vector<int> 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;
}

};
112 changes: 112 additions & 0 deletions Data Structures/C:C++/inversionArray.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
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 <bits/stdc++.h>
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;
}
43 changes: 43 additions & 0 deletions Data Structures/C:C++/largestSubarrayWithZero.cpp
Original file line number Diff line number Diff line change
@@ -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<int,int> mpp;
for(int i=0;i<n;++i)
{
sum += A[i];
if(sum == 0)
{
maxi = i + 1;
}
else {
if(mpp.find(sum) != mpp.end())
{
maxi = max(maxi, i - mpp[sum]);
}
else{
mpp[sum] = i;
}
}
}
return maxi;
}
23 changes: 23 additions & 0 deletions Data Structures/C:C++/longestAlternating.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int AlternatingaMaxLength(vector<int>&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);
}

};