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;