PLEASE GIVE THE FULL DETAILS OF THE STEPS ON EACH QUESTIONS SO I COULD FOLLOW UP
ID: 3881774 • Letter: P
Question
PLEASE GIVE THE FULL DETAILS OF THE STEPS ON EACH QUESTIONS SO I COULD FOLLOW UP YOUR WHOLE EXPLANATIONS.
PLEASE WRITE THE STEPS ON EX) STEP 1, STEP 2, ETC
HANDWRITING IS OKAY AS LONG AS IT IS READABLE.
IF YOU HAVE USED THE EQUATIONS OR CONCEPTS, PLEASE STATE IT CLEARLY WHICH ONE YOU HAVE YOU USED SO I COULD FULLY UNDERSTAND THE DETAILS OF THE STEPS YOU HAVE DONE.
PLEASE DO NOT OMIT THE DETAILS OF STEPS AND CONCEPTS YOU HAVE USED, CLEARLY STATE IT!!
PLEASE INCLUDE THE TABLES OR DRAWINGS IF IT CAN BE VISUALIZED (STACK DIAGRAMS, ARRAY (ARROW), ETC)
Reverse an array without affecting special characters.
Convert array into Zig-Zag fashion.
Smallest subarray with sum greater than a given value.
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array.
Explanation / Answer
// C++ program to reverse a string with special characters
#include<bits/stdc++.h>
using namespace std;
// Returns true if x is an aplhabatic character, false otherwise
bool isAlphabet(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}
void reverse(char str[])
{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;
// Traverse string from both ends until
// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlphabet(str[l]))
l++;
else if(!isAlphabet(str[r]))
r--;
else // Both str[l] and str[r] are not spacial
{
swap(str[l], str[r]);
l++;
r--;
}
}
}
// Driver program
int main()
{
char str[] = "a!!!b.c.d,e'f,ghi";
cout << "Input string: " << str << endl;
reverse(str);
cout << "Output string: " << str << endl;
return 0;
}
// C++ program to sort an array in Zig-Zag form
#include <iostream>
using namespace std;
// Program for zig-zag conversion of array
void zigZag(int arr[], int n)
{
// Flag true indicates relation "<" is expected,
// else ">" is expected. The first expected relation
// is "<"
bool flag = true;
for (int i=0; i<=n-2; i++)
{
if (flag) /* "<" relation expected */
{
/* If we have a situation like A > B > C,
we get A > B < C by swapping B and C */
if (arr[i] > arr[i+1])
swap(arr[i], arr[i+1]);
}
else /* ">" relation expected */
{
/* If we have a situation like A < B < C,
we get A < C > B by swapping B and C */
if (arr[i] < arr[i+1])
swap(arr[i], arr[i+1]);
}
flag = !flag; /* flip flag */
}
}
// Driver program
int main()
{
int arr[] = {4, 3, 7, 8, 6, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
zigZag(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
# include <iostream>
using namespace std;
// Returns length of smallest subarray with sum greater than x.
// If there is no subarray with given sum, then returns n+1
int smallestSubWithSum(int arr[], int n, int x)
{
// Initilize length of smallest subarray as n+1
int min_len = n + 1;
// Pick every element as starting point
for (int start=0; start<n; start++)
{
// Initialize sum starting with current start
int curr_sum = arr[start];
// If first element itself is greater
if (curr_sum > x) return 1;
// Try different ending points for curremt start
for (int end=start+1; end<n; end++)
{
// add last element to current sum
curr_sum += arr[end];
// If sum becomes more than x and length of
// this subarray is smaller than current smallest
// length, update the smallest length (or result)
if (curr_sum > x && (end - start + 1) < min_len)
min_len = (end - start + 1);
}
}
return min_len;
}
/* Driver program to test above function */
int main()
{
int arr1[] = {1, 4, 45, 6, 10, 19};
int x = 51;
int n1 = sizeof(arr1)/sizeof(arr1[0]);
int res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1+1)? cout << "Not possible " :
cout << res1 << endl;
int arr2[] = {1, 10, 5, 2, 7};
int n2 = sizeof(arr2)/sizeof(arr2[0]);
x = 9;
int res2 = smallestSubWithSum(arr2, n2, x);
(res2 == n2+1)? cout << "Not possible " :
cout << res2 << endl;
int arr3[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250};
int n3 = sizeof(arr3)/sizeof(arr3[0]);
x = 280;
int res3 = smallestSubWithSum(arr3, n3, x);
(res3 == n3+1)? cout << "Not possible " :
cout << res3 << endl;
return 0;
}
// C++ program to find the smallest positive value that cannot be
// represented as sum of subsets of a given sorted array
#include <iostream>
using namespace std;
// Returns the smallest number that cannot be represented as sum
// of subset of elements from set represented by sorted array arr[0..n-1]
int findSmallest(int arr[], int n)
{
int res = 1; // Initialize result
// Traverse the array and increment 'res' if arr[i] is
// smaller than or equal to 'res'.
for (int i = 0; i < n && arr[i] <= res; i++)
res = res + arr[i];
return res;
}
// Driver program to test above function
int main()
{
int arr1[] = {1, 3, 4, 5};
int n1 = sizeof(arr1)/sizeof(arr1[0]);
cout << findSmallest(arr1, n1) << endl;
int arr2[] = {1, 2, 6, 10, 11, 15};
int n2 = sizeof(arr2)/sizeof(arr2[0]);
cout << findSmallest(arr2, n2) << endl;
int arr3[] = {1, 1, 1, 1};
int n3 = sizeof(arr3)/sizeof(arr3[0]);
cout << findSmallest(arr3, n3) << endl;
int arr4[] = {1, 1, 3, 4};
int n4 = sizeof(arr4)/sizeof(arr4[0]);
cout << findSmallest(arr4, n4) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.