0% found this document useful (0 votes)
4 views

222

The document contains multiple C++ programs that demonstrate various functionalities. These include finding the next greater element in an array, moving all zeros to the end of an array, calculating the maximum subarray sum, determining the closest multiple of a number, counting pairs of 1s and 0s in an array, and checking if a string is a palindrome. Each program includes a main function that tests its respective functionality.

Uploaded by

Hardik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

222

The document contains multiple C++ programs that demonstrate various functionalities. These include finding the next greater element in an array, moving all zeros to the end of an array, calculating the maximum subarray sum, determining the closest multiple of a number, counting pairs of 1s and 0s in an array, and checking if a string is a palindrome. Each program includes a main function that tests its respective functionality.

Uploaded by

Hardik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

#include<iostream>
#include<vector>
using namespace std;

vector<int> array(vector<int> arr) {


vector<int> arr1;
for (int i = 0; i < arr.size() - 1; i++) {
if (arr[i] < arr[i + 1]) {
arr1.push_back(arr[i + 1]);
} else {
arr1.push_back(-1);
}
}
arr1.push_back(-1);

return arr1;
}

int main() {
vector<int> arr = {1, 2, 1, 5, 3, 10};
vector<int> res = array(arr);

for (int i = 0; i < res.size(); i++) {


cout << res[i] << " ";
}

return 0;
}

#include<iostream>
#include<vector>
using namespace std;

vector<int> array(vector<int> arr){


int index=0;
for(int i=0;i<arr.size();i++){
if(arr[i]!=0){
arr[index]=arr[i];
index+=1;
}
}
for(int i=index;i<arr.size();i++){
arr[i]=0;
}
return arr;

int main(){
vector<int> arr={1,0,0,2,3,3};
vector<int> res=array(arr);
for(int i=0;i<res.size();i++){
cout<<res[i]<<' ';
}
return 0;
}

#include<iostream>
#include<vector>
using namespace std;

int sum(vector<int> nums){


int max1=nums[0];
int current=nums[0];
for(int i=0;i<nums.size();i++){
current=max(nums[i],current+nums[i]);
max1=max(max1,current);
}
return max1;
}

int main(){
vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int res=sum(nums);
cout<<res;
return 0;
}

#include<iostream>
#include<cmath>
using namespace std;

int number(int m,int num){


if(num%m==0){
return 0;
}
else{
int ans=num-(num%m);
int ans1=ans+m;
if(num-ans<ans1-num){
return ans;
}
else if(num-ans>ans1-num){
return ans1;
}
else{
int max1=max(ans,ans1);
return max1;
}

}
int main(){
int m,num;
cout<<"Enter number";
cin>>num;
cout<<"Enter small number";
cin>>m;
int res=number(m,num);
cout<<res;
}

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int number(vector<int> arr){


int count1=0;
int count0=0;
for(int i=0;i<arr.size();i++){
if(arr[i]==1){
count1++;

}
else{
count0++;
}
}

return 2*min(count1,count0);
}

int main(){
int size;
cout<<"Enter the size";
cin>>size;
vector<int> arr;
cout<<"Enter the ele";
for(int i=0;i<size;i++){
int ele;
cin>>ele;
arr.push_back(ele);

}
int res=number(arr);
cout<<res;
}

#include <iostream>
#include <cctype> // for std::isalnum and std::tolower
#include <string>

bool isPalindromeHelper(std::string str, int left, int right) {


// Base case: if left index crosses right index, it's a palindrome
if (left >= right) {
return true;
}

// Move left index to the right if not alphanumeric


while (left < right && !std::isalnum(str[left])) {
left++;
}

// Move right index to the left if not alphanumeric


while (left < right && !std::isalnum(str[right])) {
right--;
}

// Compare characters (case insensitive)


if (std::tolower(str[left]) != std::tolower(str[right])) {
return false; // Not a palindrome
}

// Recursive case: check the next pair of characters


return isPalindromeHelper(str, left + 1, right - 1);
}

bool isPalindrome(std::string str) {


return isPalindromeHelper(str, 0, str.length() - 1);
}

int main() {
std::string input = "A man, a plan, a canal: Panama";

if (isPalindrome(input)) {
std::cout << "The string is a palindrome." << std::endl;
} else {
std::cout << "The string is not a palindrome." << std::endl;
}

return 0;
}

You might also like