Swapping ALternate Element in Array
#include<bits/stdc++.h>
using namespace std;
int main()
{
int arr[100],n;
cout<<"Enter the size"<<endl;
cin>>n;
cout<<"Enter elements in array"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Swaping Alternates"<<endl;
for(int i=0;i<n;i=i+2)
{
if(i+1<n)
{
swap(arr[i],arr[i+1]);
}
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
int findUnique(int *arr, int size)
{ int ans;
for(int i=0;i<size;i++) { (use concept of xor a^a=0 , 0^a=a){
ans=ans^arr[i];
}
return ans;
}
int findDuplicate(vector<int> &arr)
{ int ans=0;
for(int i=0;i<[Link]();i++)
{
ans=ans^arr[i];
}
for(int i=1;i<[Link]();i++)
{
ans=ans^i;
}
return ans;
vector<int> findArrayIntersection(vector<int> &arr1, int n, vector<int> &arr2, int m)
{
vector<int> arr3;
int i=0,j=0;
while(i<n && j<m)
{
if(arr1[i]==arr2[j])
{
arr3.push_back(arr1[i]);
i++,j++;
}
else if(arr1[i]<arr2[j])
{
i++;
}
else{
j++;
}
}
return arr3;
}
vector<vector<int>> pairSum(vector<int> &arr, int s){
vector<vector<int>> ans; //contain vector under vector
for(int i=0;i<[Link]();i++)
{
for(int j=i+1;j<[Link]();j++)
{
if((arr[i]+arr[j])==s)
{ vector<int> temp;
temp.push_back(min(arr[i],arr[j]));
temp.push_back(max(arr[i],arr[j]));
ans.push_back(temp);
}
}
}
sort([Link](),[Link]());
return ans;
}
442. Find All Duplicates in an
Array
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]Output: [2,3]
Example 2:
Input: nums = [1,1,2]Output: [1]
Example 3:
Input: nums = [1]Output: []
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
for(int n: nums)
{
n=abs(n);
if(nums[n-1]>0)
{
nums[n-1] *=-1;
}
else
{
result.push_back(n);
}
}
return result;
}
};
75. Sort Colors
Medium
9607411Add to ListShare
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of
the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]Output: [0,1,2]
#DUTCH NATIONAL FLAG ALGORITHM
class Solution {
public:
void sortColors(vector<int>& nums) {
int high=[Link]()-1;
int low=0;
int mid=0;
while(mid<=high)
{
if(nums[mid]==0)
{
swap(nums[low],nums[mid]);
low++;
mid++;
}
else if(nums[mid]==1)
{
mid++;
}
else
{
swap(nums[mid],nums[high]);
high--;
}
}
}
};