0% found this document useful (0 votes)
46 views9 pages

Workshop 2: Arrays and Pointers

This document contains summaries of 4 questions related to arrays and pointers. Question 1 asks to find elements that occur in two sorted arrays. Question 2 asks to find the contiguous subarray with the largest sum. Question 3 asks to rotate an array by a given number of units. Question 4 asks to check if a string is a palindrome. Solutions are provided for each question.

Uploaded by

Sanjeev Didel
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)
46 views9 pages

Workshop 2: Arrays and Pointers

This document contains summaries of 4 questions related to arrays and pointers. Question 1 asks to find elements that occur in two sorted arrays. Question 2 asks to find the contiguous subarray with the largest sum. Question 3 asks to rotate an array by a given number of units. Question 4 asks to check if a string is a palindrome. Solutions are provided for each question.

Uploaded by

Sanjeev Didel
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/ 9

Workshop 2

Arrays and Pointers


Q.1. Given 2 sorted arrays, find all the Q.2. Find the sum of contiguous subarray within an array
elements which occur in both the arrays. (containing at least one number) which has the largest sum.
Input : Eg.:
A : [1 2 3 3 4 5 6]
B : [3 3 5] Given the array [-2,1,-3,4,-1,2,1,-5,4],
Output : [3 3 5] the contiguous subarray [4,-1,2,1] has the largest sum = 6.

Q.3. Rotate a given array of size ‘n’ by ‘d’ Q.4. Given a string s, check if it’s a palindrome.
units.
Input : E.g. aaabbbb -> NO
A : [1 2 3 7 4 5 6], d=2 caabaac -> YES

Output :[3 7 4 5 6 1 2]

(Try without creating a new array


for storing elements)
Sol-1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, i, j;
cin >> a >> b;
int A[a], B[b];
for(i=0; i<a; i++) cin >> A[i];
for(i=0; i<b; i++) cin >> B[i];
i=0; j=0;
while(i < a && j < b)
{
if(A[i]==B[j])
cout << A[i++] << endl , j++;
else if(A[i] > B[j])
while(j < b && B[j] < A[i])
j++;
else
while(i < a && B[j] > A[i]) i++;
}
return 0;
}
Sol-2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int A[n], i;
for(i=0; i<n; i++) cin >> A[i];
int max_so_far = A[0], curr_max = A[0];
for (i = 1; i < n; i++)
{
curr_max = max(A[i], curr_max+A[i]);
max_so_far = max(max_so_far, curr_max);
}
cout << max_so_far;
return 0;
}
Sol-3:
void reverseArray(int arr[], int start, int
#include <bits/stdc++.h> end)
using namespace std; {
int temp;
void reverseArray(int *, int , int ); while (start < end)
{
int main() temp = arr[start];
{ arr[start] = arr[end];
int n,d,i; arr[end] = temp;
cin>>n>>d; start++;
int a[n]; end--;
for(i=0;i<n;i++) }
cin>>a[i]; }

reverseArray(a,0,d-1);
reverseArray(a,d,n-1);
reverseArray(a,0,n-1);

return 0;
}
Sol-4:
#include <iostream>
using namespace std;

int main()
{
char s[50];
cin >> s;
int l =strlen(s), i=0, j=l-1;
while(s[i++]==s[j--] && i < j);
if(i < j) cout << ”NO\n”;
else cout << "YES\n";
return 0;
}
Q.5. Given a string s, find if any anagram of the string forms a palindrome.

E.g. aaabbbb -> YES


cdefghmnopqrstuvw -> NO

Q.6. Given an array of integers, every element appears twice except for one. Find that single one.
Input : [1 2 2 3 1]
Output : 3
Sol 5
#include <bits/stdc++.h>
using namespace std;

int main()
{
string s;
cin>>s;
int n=s.size(), i, a[26];
memset(a, 0, sizeof(a));
for(i=0;i<n;i++)
{
int cur = s[i] - 'a';
a[cur]++;
}
int odd = 0;
for(i=0; i < 26; i++) odd += (a[i]&1);
if(odd<=1) cout<<"YES\n";
else cout<<"NO\n";

return 0;
}
Sol 6
#include <iostream>
using namespace std;

int main()
{
int i, n, ans=0;
cin >> n;
int A[n];
for(i=0; i<n; i++) cin >> A[i];
for(i=0; i<n; i++) ans = ans ^ A[i];
cout << ans;
return 0;
}

You might also like