Array
Array
#include<iostream>
using namespace std;
#Linear Search
#include<iostream>
using namespace std;
#Reverse an Array
#include<iostream>
using namespace std;
while(start<=end)
{
swap(arr[start],arr[end]);
start++;
end--;
}
}
int main()
{
int A[6] = {1,2,3,4,5,6};
int B[5] = {4,5,6,7,1};
reverse(A,6);
reverse(B,5);
printArr(A,6);
printArr(B,5);
return 0;
}
#Swap adjacent elements of an array
#include<iostream>
using namespace std;
int main()
{
int A[6] = {6,4,8,10,12,14};
int B[5] = {1,2,3,4,5};
swapadj(A,6);
swapadj(B,5);
printArr(A,6);
printArr(B,5);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int A[7] = {1,2,4,4,2,1,3};
int a = 0;
for(int i = 0; i < n; i++)
{
a = a^A[i+1];
}
}
#Duplicate In Array
You are given an array ‘ARR’ of size ‘N’ containing each number between 1 and ‘N’ -
1 at least once. There is a single integer value that is present in the array
twice. Your task is to find the duplicate integer value present in the array.
Consider ARR = [1, 2, 3, 4, 4], the duplicate integer value present in the array is
4. Hence, the answer is 4 in this case.
int N = arr.size();
int ans = 0;
for(int i=0;i<N;i++)
{
ans = ans^arr[i]^i; // XOR to find the duplicate element
}
return ans;
}
Approach 2 Frquency
int N = arr.size();
int f[N+1] = {0};
for(int i = 0; i<N; i++)
{
f[arr[i]]++;
}
for(int i = 0; i<N; i++)
{
if(f[arr[i]]==2)
{
return arr[i];
}
}
#Binary Search
#include <iostream>
using namespace std;
if(k>arr[mid])
{
start = mid+1;
}
else
{
end = mid-1;
}
mid = start + (end-start)/2;
}
return -1;
}
int main() {
// your code goes here
int even[6] = {10,20,30,40,50,60};
int odd[5] = {10,20,30,40,50};
int k;cin>>k;
int index = binarysearch(even,6,k);
cout<<index<<endl;
index = binarysearch(odd,5,k);
cout<<index<<endl;
return 0;
}
O(N)
Find maximum element of the array
O(logN)
class Solution {
public:
int peakIndexInMountainArray(vector<int>& arr) {
int s = 0;
int e = arr.size()-1;
int mid = s+(e-s)/2;
while(s<e)
{
if(arr[mid]<arr[mid+1])
{
s = mid+1;
}
else
{
e = mid;
}
mid = s+(e-s)/2;
}
return s;
}
};
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int tsum = 0;int ans = -1;
for(int i =0;i<nums.size();i++)
{
tsum+=nums[i];
}
int lsum = 0;
for(int i=0;i<nums.size();i++)
{
if(lsum == tsum-nums[i]-lsum)
{
ans= i;
break;
}
lsum+=nums[i];
}
return ans;
}
};
Approach 1 Create a copy array and update the elements of the initial array using
copy array
O(n) and extra space O(n)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int A[]={2,3,4,5,6};
int n = sizeof(A)/sizeof(A[0]);
modify(A,n);
print(A,n);
return 0;
}
Approach 2 Store the value of prev element in loop and update array using prev
element
O(n) and O(1) space
#include<bits/stdc++.h>
using namespace std;
int main()
{
int A[]={2,3,4,5,6};
int n = sizeof(A)/sizeof(A[0]);
modify(A,n);
print(A,n);
return 0;
}
-----------------------------------------------------------------------------------
--------------
#include<bits/stdc++.h>
using namespace std;
for(int i=0;i<n;i++)
{
cout<<B[i]<<" ";
}
int main()
{
int A[]={2,3,4,5,6};
int n = sizeof(A)/sizeof(A[0]);
evenodd(A,n);
return 0;
}
Approach 2 Pending
-----------------------------------------------------------------------------------
---
Approach Swap elements at 0th and (n-1)th and similar using s and e.
O(n)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int A[]={1,2,3,4,5,6};
int n=sizeof(A)/sizeof(A[0]);
reverse(A,n);
print(A,n);
return 0;
}
---------------------------------------------------------------------------
Approach here we don't need copy array just keeping index of non zero elements and
0's and updating array.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int A[]={1, 2, 0, 4, 3, 0, 5, 0};
int n=sizeof(A)/sizeof(A[0]);
zero(A,n);
print(A,n);
return 0;
}
-----------------------------------------------------------------------------------
-----
1 2 0 1 0 2
0 0 1 1 2 2
for(auto x: a)
{
++b[x];
}
2 2 2
b 0 1 2 3 4 5