0% found this document useful (0 votes)
61 views7 pages

C++ Solutions for Array Problems

The document contains multiple C++ class implementations for various algorithms, including finding the largest and second largest numbers in an array, checking if an array is a rotated sorted array, removing duplicates, rotating an array, moving zeroes, finding the union of two arrays, and finding a missing number. Each algorithm is encapsulated within a class named 'Solution' with public methods to execute the respective functionality. The document also includes some incomplete code snippets and comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views7 pages

C++ Solutions for Array Problems

The document contains multiple C++ class implementations for various algorithms, including finding the largest and second largest numbers in an array, checking if an array is a rotated sorted array, removing duplicates, rotating an array, moving zeroes, finding the union of two arrays, and finding a missing number. Each algorithm is encapsulated within a class named 'Solution' with public methods to execute the respective functionality. The document also includes some incomplete code snippets and comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//{ Driver Code Starts

//Initial Template for C++

#include <bits/stdc++.h>

using namespace std;


int print2largest(vector<int> &arr) {

// } Driver Code Ends int n=[Link]();

//User function Template for C++ int maxi=INT_MIN;

int ndmaxi=-1;

class Solution

{ for(int i=0;i<n;i++){

public: if(arr[i]>maxi){

int largest(vector<int> &arr, int n) maxi=arr[i];

{ }

int maxi=INT_MIN; }

for(int i=0;i<n;i++){ // cout<<maxi;

if(arr[i]>maxi){ for(int i=0;i<n;i++){

maxi=arr[i]; if(arr[i]==maxi){

} continue;

} }

return maxi; if(arr[i]>ndmaxi){

ndmaxi=arr[i];

} }

};
}

return ndmaxi;
class Solution {

public:

bool check(vector<int>& nums) {

int n=[Link]();

int count=0;
class Solution {
for(int i=0;i<n-1;i++){
public:
if(nums[i]>nums[i+1]){
int removeDuplicates(vector<int>& nums) {
count++;
int n=[Link]();
}
set<int>st;
}
for(int i=0;i<n;i++){
if(nums[n-1]>nums[0]) count++;
[Link](nums[i]);
return count<=1;
}

[Link]();
}
[Link]([Link](), [Link](), [Link]());
};
return [Link]();
One Break Point: In a rotated sorted array, there
can be at most one place where the order of
elements is broken. If there's more than one such
}
place, the array can't be a rotated version of a
sorted array. This approach directly counts such };
points to determine the result.
Approach 2:

class Solution {

public:

int removeDuplicates(vector<int>& nums) {


int j = 1;

for(int i = 1; i < [Link](); i++){ nums=res;

if(nums[i] != nums[i - 1]){

nums[j] = nums[i]; }

j++; };

} class Solution {

} public:

return j; void rotate(vector<int>& nums, int k) {

}; k=k%[Link]();

reverse([Link](),[Link]()+
([Link]()-k));

reverse([Link]()+([Link]()-
k),[Link]());

reverse([Link](),[Link]());

};
class Solution {

public:

void rotate(vector<int>& nums, int k) {

int n=[Link]();

vector<int>res(n);

k=k%n;

for(int i=0;i<n;i++){

res[(i+k)%n]=nums[i];
class Solution {

public:
}
void moveZeroes(vector<int>& nums) {
int n=[Link]();

stack<int>st;

for(int i=0;i<n;i++){

if(nums[i]==0){

[Link](0);

int index=0;

for(int i=0;i<n;i++){ int searchInSorted(int arr[], int N, int K) {

if(nums[i]!=0){ for(int i=0;i<N;i++){

nums[index]=nums[i]; if(arr[i]==K){

index++;}} return 1;

while(![Link]()){ }

nums[index]=[Link](); }

index++; return -1;

[Link](); // Your code here

}}}}; }

Approach 2:

class Solution {

public:

void moveZeroes(vector<int>& nums) {

int left = 0;

for (int right = 0; right < [Link](); right++) {

if (nums[right] != 0) {

swap(nums[right], nums[left]);

left++;

} }};
vector<int> findUnion(int arr1[], int arr2[], int n, int missingNumber(vector<int>& nums) {
int m)
int n=[Link]();
{
int totalsum=(n*(n+1))/2;
set<int>st;
int missingnum=0;
for(int i=0;i<n;i++){
for(int i=0;i<n;i++){
[Link](arr1[i]);
missingnum+=nums[i];
}
}
for(int j=0;j<m;j++){
missingnum=totalsum-missingnum;
[Link](arr2[j]);
return missingnum;
}

vector<int>res([Link](),[Link]());
}
sort([Link](),[Link]());

return res;

}
int singleNumber(vector<int>& nums) {

int n=[Link]();
class Solution {

public:
unordered_map<int,int>mp;
int findMaxConsecutiveOnes(vector<int>&
nums) { for(int i=0;i<n;i++){
int n=[Link](); mp[nums[i]]++;
int maxi=0; }
int c=0; for(int i=0;i<n;i++){
for(int i=0;i<n;i++){ if (mp[nums[i]] == 1) {
if(nums[i]==1){ return nums[i]; }}
c++; return 0; }

appeoach 2:
maxi=max(c,maxi); int singleNumber(vector<int>& nums) {
}else{ int single = 0;
c=0; // XOR all elements
} for (int num : nums) {
} single ^= num;
return maxi; }

return single;
} }
};
int lenOfLongSubarr(int A[], int N, int K)

// Complete the function

int sum=0, cnt = 0, rem;

unordered_map<int, int> m;

m[0] = -1;

for(int i=0; i<N; i++) {

sum+=A[i];

rem = sum - K;

if([Link](sum) == [Link]()) {

m[sum] = i;

if([Link](rem) != [Link]()) {

cnt = max(cnt, i-m[rem]);

return cnt;

You might also like