I'm trying to print all increasing subsequence with following.
But it is not working accordingly.
Please explain what actually my code doing.
I'm stuck since last 1 week.
But unable to figure out.
#include <bits/stdc++.h>
using namespace std;
int temp[1000];
int ans = 1;
int cnt = 0;
void solve(int *arr, int n, int k)
{
if(k == n){
cnt++;
for(int j = 0; j < n; j++){
cout<<temp[j]<<" ";
}
cout<<endl;
return;
}
for(int i = k; i < n; ++i){
if(arr[k] <= arr[i]){
temp[k] = -2;
solve(arr, n, i+1);
temp[k] = 2;
}
}
}
int main()
{
int arr[] = {4, 1, 13, 7, 0, 2, 8, 11, 3};
//int arr[] = {-1, 1 ,2, 3, 4};
//int arr[] = {-1,1,2,3,4,11, 5,6, 2, 9};
memset(temp, -1, sizeof(temp));
solve(arr, 9, 0);
cout<<cnt<<endl;
return 0;
}
Output should be total number of enumerating increasing subsequence.
This prints all increasing subsequences:
void print(const vector<int> v) {
for (size_t i = 0; i < v.size() - 1; ++i)
cout << v[i] << ", ";
cout << v.back() << endl;
}
int solve(const vector<int>& v, size_t start, vector<int>& sequence) {
print(sequence);
int count = 1;
for (size_t i = start; i < v.size(); ++i) {
if (v[i] >= sequence.back()) {
sequence.push_back(v[i]);
count += solve(v, i + 1, sequence);
sequence.pop_back();
}
}
return count;
}
size_t solve(const vector<int>& v) {
int count = 0;
for (size_t i = 0; i < v.size(); ++i) {
vector<int> sequence{v[i]};
count += solve(v, i + 1, sequence);
}
return count;
}
Usage:
vector<int> v{4, 1, 13, 7, 0, 2, 8, 11, 3};
auto count = solve(v);
cout << "Count: " << count << endl;
Output:
4
4, 13
4, 7
4, 7, 8
4, 7, 8, 11
4, 7, 11
4, 8
4, 8, 11
4, 11
1
1, 13
1, 7
1, 7, 8
1, 7, 8, 11
1, 7, 11
1, 2
1, 2, 8
1, 2, 8, 11
1, 2, 11
1, 2, 3
1, 8
1, 8, 11
1, 11
1, 3
13
7
7, 8
7, 8, 11
7, 11
0
0, 2
0, 2, 8
0, 2, 8, 11
0, 2, 11
0, 2, 3
0, 8
0, 8, 11
0, 11
0, 3
2
2, 8
2, 8, 11
2, 11
2, 3
8
8, 11
11
3
Count: 48
Related
void Data::Paramters()
{
for (int i = 0; i < I; i++)
{
mc[i] = new int[K];
for (int k = 0; k < K; k++)
{
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
}
}
}
getting "Too Many Initializer Values" error in starting of { 4, 2, 2, 1, 3 }
where I=5 and K=4
The initializer is for a 4x5 2D matrix, so you should just write:
enum { I = 4, K = 5 };
int mc[I][K] = { { 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 } };
The expression mc[i][k] has the type int. It is not an array.
So this assignment statement
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
does not make a sense.
If K is a constant expression then you can allocate and initialize the two-dimensional array the following way
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
Here is a demonstration program.
#include <iostream>
int main()
{
const size_t K = 5;
size_t I = 4;
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
for ( size_t i = 0; i < I; i++ )
{
for (const auto &item : mc[i])
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
The program output is
1 0 2 3 5
4 2 2 1 3
4 3 4 1 3
3 5 6 4 2
This is my code which task is to check how many times does the number appear in my array.
Error message tells me that vector subscript is out of range.
int main() {
vector<int> numbers(1,-1);
int x;
int z;
bool exit;
int digits[] = { 2, 4, 5, 3, 2, 5, 6, 3, 5, 7, 9, 2, 1, 2, 3, 4, 5, 6, 4, 3, 2, 6, 3, 4, 4, 1, 3, 7, 9, 5, 9, 2, 3, 1, 2, 3, 4, 5, 6, 2, 1, 2, 3, 4, 5, 3, 2, 7, 7, 7 };
for (int i = 0; i < (sizeof(digits) / sizeof(int)); i++) {
z = 0;
bool exit = 1;
for (int j = 0; j < (sizeof(digits) / sizeof(int)); j++) {
x = digits[i];
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
if (x == numbers[y]) {
bool exit = 0;
}
}
if (digits[j] == x && exit) {
z++;
}
}
if (exit) {
cout << "Liczba: " << x << " wystepuje: " << z << " razy" << endl;
numbers.push_back(x);
}
}
}
Visual studio tells me that my code is valid but then during compilation , cmd shows me the error message which tells me that Vector subscript out of range . I just can't figure it out how to fix it exactly , bcs i have not found that i would initialize the vector which would not exist .
At least this statement
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
does not make sense because the expression sizeof(numbers) / sizeof(int) does not yield the number of elements in the vector. Instead use
for (int y = 0; y < numbers.size(); y++) {
If I have correctly understood your task then instead of the container std::vector it is better to use either std::map or std::unordered_map.
For example
#include <iostream>
#include <map>
int main()
{
int digits[] =
{
2, 4, 5, 3, 2, 5, 6, 3, 5, 7,
9, 2, 1, 2, 3, 4, 5, 6, 4, 3,
2, 6, 3, 4, 4, 1, 3, 7, 9, 5,
9, 2, 3, 1, 2, 3, 4, 5, 6, 2,
1, 2, 3, 4, 5, 3, 2, 7, 7, 7
};
std::map<int, size_t> frequency;
for ( const auto &item : digits )
{
++frequency[item];
}
for ( const auto &p : frequency )
{
std::cout << p.first << ": " << p.second << '\n';
}
return 0;
}
The program output is
1: 4
2: 10
3: 10
4: 7
5: 7
6: 4
7: 5
9: 3
Is this linear complexity implementation of circular array rotation correct?
n = number of elements
k = number of rotations
int write_to = 0;
int copy_current = 0;
int copy_final = a[0];
int rotation = k;
int position = 0;
for (int i = 0; i < n; i++) {
write_to = (position + rotation) % n;
copy_current = a[write_to];
a[write_to] = copy_final;
position = write_to;
copy_final = copy_current;
}
No.
Consider this example.
#include <iostream>
int main(void) {
int n = 6;
int k = 2;
int a[] = {1, 2, 3, 4, 5, 6};
int write_to = 0;
int copy_current = 0;
int copy_final = a[0];
int rotation = k;
int position = 0;
for (int i = 0; i < n; i++) {
write_to = (position + rotation) % n;
copy_current = a[write_to];
a[write_to] = copy_final;
position = write_to;
copy_final = copy_current;
}
for (int i = 0; i < n; i++) {
std::cout << a[i] << (i + 1 < n ? ' ' : '\n');
}
return 0;
}
Expected result:
5 6 1 2 3 4
Actual result:
3 2 1 4 1 6
Using stl::rotate on std::array, you can left rotate by, say 2, as:
std::array<int, 6> a{1, 2, 3, 4, 5, 6};
std::rotate(begin(a), begin(a) + 2, end(a)); // left rotate by 2
to yield: 3 4 5 6 1 2, or right-rotate by, say 2, as:
std::rotate(begin(a), end(a) - 2, end(a)); // right rotate by 2
to yield: 5 6 1 2 3 4, with linear complexity.
Rotate an Array of length n for k times in left or right directions.
The code is in Java
I define a Direction Enum:
public enum Direction {
L, R
};
Rotation with times and direction:
public static final void rotate(int[] arr, int times, Direction direction) {
if (arr == null || times < 0) {
throw new IllegalArgumentException("The array must be non-null and the order must be non-negative");
}
int offset = arr.length - times % arr.length;
if (offset > 0) {
int[] copy = arr.clone();
for (int i = 0; i < arr.length; ++i) {
int j = (i + offset) % arr.length;
if (Direction.R.equals(direction)) {
arr[i] = copy[j];
} else {
arr[j] = copy[i];
}
}
}
}
Complexity: O(n).
Example:
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Rotate 3 times left
Output: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
Input: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
Rotate 3 times right
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I'm attempting to cout the elements of input array "arr" that was used to determine max sum of a subarray, hereinafter named "maxSum" (which is determined elsewhere, and confirmed to be correct). The function showSubArray() accepts as parameters the array arr, the length of the array n, and maxSum. Input array is positive and negative ints. Below is a set of test arrays with the result. Fail means that arr[0] is printed to the screen with a space separating them INFINITELY. I can't see any discernable pattern in the input that would cause this. Any help greatly appreciated and I am not beholden to the unordered_map approach. Getting the indices from the function that determined the maxSum is not an acceptable solution.
#include <unordered_map>
#include <iostream>
using std::cout;
int main() {
//int arr[] = { 1, 4, -9, 8, 1, 3, 3, 1, -1, -4, -6, 2, 8, 19, -10, -11 };
// runs ok, inputs: n=16, maxSum = 34
//int arr[] = { 2, 9, 8, 6, 5, -11, 9, -11, 7, 5, -1, -8, -3, 7, -2 };
// ***fails, inputs: n=15, maxSum = 30
//int arr[] = { 10, -11, -1, -9, 33, -45, 23, 24, -1, -7, -8, 19 };
// runs ok, n=12, maxSum = 50
//int arr[] = { 31, -41, 59, 26, -53, 58, 97, -93, -23, 84 };
// runs ok n=10 maxSum = 187
//int arr[] = { 3, 2, 1, 1, -8, 1, 1, 2, 3 };
// ***fails, inputs: n=9 maxSum = 7
int arr[] = { 12, 99, 99, -99, -27, 0, 0, 0, -3, 10 };
// ***fails, n=10 maxSum = 210
//int arr[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
// runs ok, inputs: n=9 maxSum = 6
showSubArray(arr, n, maxSum);
return 0;
}
void showSubArray(int arr[], int n, int maxSum) {
std::unordered_map<int, int> aMap;
int accumulator = 0;
for (int i = 0; i < n; i++) {
accumulator += arr[i];
if (accumulator == maxSum) {
for(int j = 0; j <= i; j++) {
// ACB found error here ^ (I had it as "i")
cout << arr[j];
cout << " ";
}
cout << '\n';
return;
}
if (aMap.find(accumulator - maxSum) != aMap.end()) {
for (int j = aMap[accumulator - maxSum] + 1; j <= i; j++) {
cout << arr[j];
cout << " ";
}
cout << '\n';
return;
}
aMap[accumulator] = i;
}
cout << "Subarray not found!\n";
}
if (accumulator == maxSum) {
for(int j = 0; j <= i; i++) {
you are incrementing i here but you want to increment j cause 0 will always be smaller i for i > 0 until it overflows
The questions pretty much sums it up. Here's what I have so far:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
for(int i = 0; i < 8; i++) {
}
}
I'm planning on assigning the sum of each value from sum1 to sum8 to a row, then order each row by its total sum and display it to the user.
However, I keep getting stuck and can't find any good documentation. Can anyone help me write an effective function I can loop through in my for loop to add each of the rows to a sum, then return the sum and average all the rows out? (this will likely have to be more than one function but...whatever.)
If you want the sum of each row displayed in ascending order, the following code will do that...
// #include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int sums[8] = {0};
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
// calculate the sums
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
sums[i] += a[i][j];
}
}
sort(begin(sums), end(sums)); // sort the sums
copy(begin(sums), end(sums), ostream_iterator<int>(cout, " ")); // display the sums
return 0;
}
I see you are a cool programmer. You do not seek an easy way.
Of course you could use an array of sums but it seems it is too easy.
Well, then try the following
#include <iostream>
#include <numeric>
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
size_t i = 0;
for ( auto p : { &sum1, &sum2, &sum3, &sum4, &sum5, &sum6, &sum7, &sum8 } )
{
*p = std::accumulate( a[i], a[i] + 7, 0 );
++i;
}
for ( int x : { sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8 } ) std::cout << x << std::endl;
}
The output is
34
28
20
31
32
28
37
41
Having the sums you can find any average. I hope you will do this yourself.