0% found this document useful (0 votes)
0 views5 pages

Coding

The document contains multiple code snippets in C++ that implement various algorithms. These include finding the k-th node from the end of a linked list, calculating the maximum area of a histogram, identifying the first negative integer in every subarray of size k, and checking if a number is a Shadow Twin. Additionally, there are implementations for counting even and odd subarray sums and checking for palindromic substrings.

Uploaded by

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

Coding

The document contains multiple code snippets in C++ that implement various algorithms. These include finding the k-th node from the end of a linked list, calculating the maximum area of a histogram, identifying the first negative integer in every subarray of size k, and checking if a number is a Shadow Twin. Additionally, there are implementations for counting even and odd subarray sums and checking for palindromic substrings.

Uploaded by

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

7.

Node* fast = head;

Node* slow = head;

for(int i = 0; i < k; i++)

if(fast->next)

fast = fast->next;

else if( i == k - 1 && !fast->next)

return head->data;

else

return -1;

while(fast)

fast = fast->next;

slow = slow->next;

return slow->data;

3. int n = arr.size();

stack<int> st;

st.push(0);

int ans = 0;

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

while(st.empty() == false && arr[i] < arr[st.top()]){

int currElement = arr[st.top()];

st.pop();

int nextSmaller = i;

int prevSmaller = st.empty() == true ? -1 : st.top();

ans = max(ans, currElement * (nextSmaller - prevSmaller - 1));

st.push(i);

while(st.empty() == false){

int currElement = arr[st.top()];


st.pop();

int prevSmaller = st.empty() ? -1 : st.top();

ans = max(ans, currElement * (n - prevSmaller - 1));

return ans;

4. vector<int> firstNegInt(vector<int>& arr, int k) {

// write code here

vector<int>v;

int i=0,j=0,l=0;

while(j<arr.size()){

int flag=0,l=i;

if(j-i+1<k){

j++;

else{

while(l<=j){

if(arr[l]<0){

flag=1;

v.push_back(arr[l]);

break;

l++;

if(flag==0){

v.push_back(0);

i++;

j++;

}
}

return v;

5. #include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

int square = num * num;

int copy = num;

int lastDigits = 1;

while (copy > 0) {

lastDigits *= 10;

copy /= 10;

if (square % lastDigits == num)

cout << num << " is a Shadow Twin!" << endl;

else

cout << num << " is not a Shadow Twin." << endl;

return 0;

1.

#include <iostream>

using namespace std;

int main() {

int n;

cin >> n;

int arr[n];

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

cin >> arr[i];


long long count = 0;

int even = 1, odd = 0;

int sum = 0;

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

sum += arr[i];

if (sum % 2 == 0) {

count += odd;

even++;

} else {

count += even;

odd++;

cout << count << endl;

2.

#include <iostream>

using namespace std;

bool isPalindrome(string str, int start, int end) {

while (start < end) {

if (str[start] != str[end])

return false;

start++;

end--;

return true;

int main() {

string s;
cin >> s;

int n = s.length();

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

// First part: s[0..i]

if (isPalindrome(s, 0, i)) {

for (int j = i + 1; j < n - 1; j++) {

// Second part: s[i+1..j]

// Third part: s[j+1..n-1]

if (isPalindrome(s, i + 1, j) && isPalindrome(s, j + 1, n - 1)) {

// Print the 3 palindromes

cout << s.substr(0, i + 1) << endl;

cout << s.substr(i + 1, j - i) << endl;

cout << s.substr(j + 1) << endl;

return 0;

cout << "Impossible" << endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy