Sliding Maximum window brute force method - c++

Given a large array of integers and a window of size 'w', find the current maximum in the window as the window slides through the entire array.
I have made two for loops, the first loops is working correctly but the inner loop doesn't move correctly based on different window sizes.
I tried to draw it on paper, but I still can't get a formula for the inner loop
int main() {
vector<int> arr = { -4, 2, -5, 3, 6 };
int window = 3;
for (int i = 0; i < arr.size() - window; i++)
{
int max = arr[i];
for (int j = i + 1; j < i + window; j++)
{
cout << " i = "<<i << "j = " << j << endl;
if (max < arr[j])
max = arr[j];
}
cout << max << " " << endl;
}

Your inner loop is correct. It's your outer loop's that's off by 1, it should be:
for (int i = 0; i <= arr.size() - window; i++)
With your array of 5 elements and the window size of 3, the last window is array[2] through array[4]. arr.size() is 5. window is 3. 5-3=2, and you need to still iterate for that window starting position.

Try this:
#include <algorithm>
int main() {
std::vector<int> arr = { -4, 2, -5, 3, 6 };
int window = 3;
for (std::size_t i = 0; i < arr.size() - window + 1; i++)
// one of your bugs is here --------------------^
{
int max = 0;
// this way, it's easier to see how the window slides
for (std::size_t j = 0; j < window; j++)
{
std:: cout << " i = "<<i << "j = " << j+i << std::endl;
max=std::max(arr[i+j],max);
// --------------^
// i becomes the window's offset
// j is the offset inside the window
// using std::max eliminates a i+j twice with an if
// ... (and will be inlined anyway)
}
std::cout << max << " " << std::endl;
}
return 0;
}

Related

How does c++ react to int array indexes while performing standard mathematical expressions?

The idea I have is pretty simple. What I'd like to do is just have the user input a series of integers and plug those integers specified into an array. So for example, here's a segment of the program I'm writing.
const int size = 1000;
int array_one[size], array_two[size];
for (int i = 0; i < size; i++)
{
std::cin >> array_one[i];
if (array_one[i] == -1) break;
}
for (int i = 0; i < size; i++)
{
std::cin >> array_two[i];
if (array_two[i] == -1) break;
}
The cut off point is -1. Should the user enter this, one index of the array will be all integers greater than -1 with the following indexes housing 0's in this 1000 sized array.
What I'm doing next is simply replicating how you would write out basic addition on paper. So, lets say for example I plugged in 1 0 0 6 -1 as inputs for the first array and then 2 3 4 for the second array. The whole entire output should look something akin to this
1006
+ 234
------
1240
Here's what I've typed out to get this sort of output
int array_size_one = 0;
int array_size_two = 0;
int space_size = 3;
int cut_off_space = 0;
int count = 0;
for (int i = 0; i < size; ++i)
{
if (array_one[i] < 0) break;
array_size_one += 1;
}
for (int i = 0; i < size; ++i)
{
if (array_two[i] < 0) break;
array_size_two += 1;
}
if (array_size_one > array_size_two) space_size += array_size_one;
else if (array_size_two > array_size_one) space_size += array_size_two;
if (array_size_one < array_size_two) cut_off_space += array_size_one;
else if (array_size_two < array_size_one) cut_off_space += array_size_two;
std::cout << std::setw(space_size - array_size_one);
for (int j = 0; j < size; ++j)
{
if (array_one[j] < 0) break;
std::cout << array_one[j];
}
std::cout << "\n+";
std::cout << std::setw((space_size - 1) - array_size_two);
for (int j = 0; j < size; ++j)
{
if (array_two[j] < 0) break;
std::cout << array_two[j];
}
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
This is the part where I think I'm sure I'm having trouble with.
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
The idea was that I wanted to add up everything inside each index of the arrays to perform standard integer expressions. The for loop above isn't really cutting it. I'm getting outputs like 5 or 15. If going by the example above that has inputs of 1 0 0 6 -1 for the first array and then 2 3 4 -1 for the second array. What could I actually do inside the very last for loop iteration to make sure I get the correct outputs replicating standard integer expressions?

Using Multidimensional Arrays to Keep Track of Indices in C++

I'm working on a project where I need to sort an array from least to greatest, but save the values of the indices. For example, with the array {2, 7, 8, 1, 3}, the sorted indices would be {3, 0, 4, 1, 2}. I thought that I could use a two dimensional array to accomplish this; I would add the index to each component, sort the array, then retrieve the original index from the second element. It hasn't been working for me as well as I hoped though, I have my current code below and it keeps giving me a segmentation fault. I don't know what I'm doing wrong, but I'm assuming its something in my for loops.
#include <iostream>
#include <algorithm>
using namespace std;
const int SIZE = 7;
int main()
{
int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};
int i, j, k, l = 0;
int temp[SIZE][2];
//fills multidimensional array temp with data from intArray or index #
for(i = 0; i < 7; i++){
for(j = 0; j < 2; j++){
switch (j)
{
case '0':
temp[i][j] = *intArray;
break;
case '1':
temp[i][j] = i;
break;
}
}
}
sort(intArray, intArray + SIZE);
//prints each component of temp individually
cout << "Sorted Array looks like this." << endl;
for (k = 0; i < 7; i++){
for (l = 0; j < 2; i++){
cout << &temp[k][l] << endl;
}
}
return 0;
}
The following loop is much simpler and does what it should:
for(i = 0; i < 7; i++){
temp[i][0] = intArray[i];
temp[i][1] = i;
}
One error in your code is the line
temp[i][j] = *intArray;
This always assigns the 1st element of intArray.
The thing that causes the segmentation fault is probably the & in the output statement, just remove it.
Aside from that, I agree with the recommendation in the comment by RyanP.
Use std::map. The code will be the easiest. Test on cpp.sh
#include <iostream>
#include <map>
int main()
{
std::map<int,int>arr = {{5,0}, {3,1}, {32,2}, {-1,3}, {1,4}, {104,5}, {53,6}};
for(auto&x:arr) std::cout << x.first << " - > " << x.second << std::endl;
return 0;
}
I ended up using what Frank Puffer said and chose to use a simple bubble sort instead of using any of the built in functions. Here is a link to what my final program looked like http://ideone.com/cpEgGA
#include <iostream>
#include <algorithm>
using namespace std;
const int SIZE = 7;
int main()
{
int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};
int i, j, k, l, m = 0;
int temp2[SIZE][2];
int indices[SIZE] = {};
for(i = 0; i < 7; i++){
temp2[i][0] = intArray[i];
temp2[i][1] = i;
}
cout << "Unsorted Array looks like this." << endl;
for (j = 0; j < 7; j++){
cout << temp2[j][0];
cout << " : ";
cout << temp2[j][1] << endl;
}
for(k = 0; k < SIZE; k++)
{
for(j = 0; j < SIZE-1-k; j++)
{
if(temp2[j+1][0] < temp2[j][0])
{
l = temp2[j][0];
temp2[j][0] = temp2[j+1][0];
temp2[j+1][0] = l;
l = temp2[j][1];
temp2[j][1] = temp2[j+1][1];
temp2[j+1][1] = l;
}
}
}
cout << "Sorted Array looks like this." << endl;
for (m = 0; m < SIZE; m++)
{
cout << temp2[m][0];
cout << " : ";
cout << temp2[m][1] << endl;
}
for(i = 0; i < SIZE; i++){
indices[i] = temp2[i][1];
}
cout << "Indices of Sorted Array look like this." << endl;
for(i = 0; i < SIZE; i++){
cout << indices[i] << endl;
}
return 0;
}

C++ Cutting Rod Dynamic programming

I am new to dynamic programming and trying to solve an evergreen problem: cutting rod. I have been trying for hours and I am stuck. I am trying to debug it but without success.
Here is my code
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int length = 5;
int peaces[2][4] = {{1, 2, 3, 4}, {2, 5, 7, 8}};
/*
length 1, 2, 3, 4
value: 2, 5, 7, 8
*/
//create working matrix: number of lengths X lengths
int work[4][6];
//fill in the first column with zeros
for( int k = 0; k < 5; k++ )
{
work[k][0] = 0;
}
//fill in the first row with zeroes
for( int k = 0; k <= length; k++ )
{
work[0][k] = 0;
}
for( int i = 1; i < 5; i++ ) // number of lengths
{
cout << endl << " i: " << i << " " << endl;
for( int j = 1; j < 6; j++ )
{
cout << endl << " j: " << j << " " << endl;
if( j >= i )
{
work[i][j] = max( work[i - 1][j], work[i][j - i] + peaces[1][i-1] );
}
else
{
work[i][j] = work[i - 1][j];
}
cout << endl;<< "inserted:" << work[i][j]<< endl;
for( int i = 0; i < 5; i++ )
{
for( int j = 0; j < 6; j++ )
{
cout << work[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
}
for( int i = 0; i < 5; i++ )
{
for( int j = 0; j < 6; j++ )
{
cout << work[i][j] << " ";
}
cout << endl;
}
return 0;
};
Something very strange happens and I can't figure out why: in the last iteration of the for loop I get right solution, but when exit the loop and I print the 2D array, it is changed, not the same as the one in the last iteration. Between two printing no operation is done on the array.
I know that this is not an optimized solution, but that is the first one I have come up with.
Question: Why is not the array filled the right way? What have I done wrong?
The best way to avoid issues like this is to define variables for the dimensions in the first place. For example,
int xlen = 4;
int ylen = 5;
int work[xlen][ylen];
Then, you can use xlen and ylen in your loops and if you want to change the size of the arrays, you just change the xlen and ylen.
Alternatively, you can find the size using answers that you find here: How do I find the length of an array?
And finally, you should read up on the standard template library, which gives you "smarter" objects you can use.

How do I output a 2D char array using nested for loops?

I am trying to output a simple 2D char array using nested for loops
It does not output in a "Square form", and also it is crashing.
#include <iostream>
using namespace std;
int main(){
char array[x][y] = {
"000000",
"0 0",
"0 0",
"000000",
};
for (int i = 1; i <=x; i++) {
for (int j = 1; j <=y; j++)
cout << array[i][j];
}
}
C++ uses Zero-based indexing
your loops should be like
for (int i = 0; i <x; i++)
https://en.wikipedia.org/wiki/Zero-based_numbering
That's why it's crashing
Also you don't print line breaks. Insert cout << std::endl in the outer loop
The arrays are zero based, so you should loop from 0 to x-1 rather than from 1 to x.
You also need a line break, otherwise it will all be written on the same line.
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; i++) {
cout << array[i][j];
}
cout << std::endl;
}
I suppose that x and y are constants defined like
const size_t x = 4;
const size_t y = 7;
C++ does not allow to use non-constant expressions as array sizes though some compilers have their own language extensions that allow to use variable length arrays.
In this case this array
char array[x][y] = {
"000000",
"0 0",
"0 0",
"000000",
};
can be outputed the following way
for ( auto &s : array ) std::cout << s << std::endl;
Take into account that indices of arrays in C++ start from 0. So if to use indices then the loop can look like
for ( size_t i = 0; i < x; i++ ) std::cout << array[i] << std::endl;
Take into account that to output this shape there is no need to declare a 2D array.:)
Here is a demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
size_t n = 4;
size_t m = 6;
char c = '*';
for ( size_t i = 0; i < n; i++ )
{
std::cout << c
<< std::setfill( i % ( n - 1 ) == 0 ? c : ' ' )
<< std::setw( m - 1 )
<< c << std::endl;
}
}
Its output is what you expect:)
******
* *
* *
******
and can someone explain me why the second method outputs the array on other location ? i mean it starts to output on 2nd line while 1st method outputs from 00coord (by coord its mean location in console ,, not array position)
for (int i = 0; i < x; i++)
cout << array[i] << endl;
for (int i = 0; i <x; i++) {
cout << endl;
for (int j = 0; j <y; j++)
cout << map[i][j];
}

Checking duplicated numbers in Array

The function should check if there is any duplicated number but it's not working somehow.
anyone knows why?
#include <iostream>
using namespace std;
int main()
{
int const size = 10;
int arry[size] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
int i = 0;
int k = 1;
for (i = 0; i < size; i++)
{
for (k = 1; k < size; k++)
{
if (arry[i] == arry[k])
{
cout << arry[i] << " " << arry[k] << endl;
cout << "duplicate" << endl;
return 0;
}
}
}
cout << "no duplicate" << endl;
return 0;
}
You should skip when i == k.
for (i = 0; i < size; i++)
{
for (k = i+1; k < size; k++) // <-- either change HERE
{
if (i != k && arry[i] == arry[k])
// ^^^^^^ or HERE
{
cout << arry[i] << " " << arry[k] << endl;
cout << "duplicate" << endl;
return 0;
}
}
}
You're checking every combination twice - for example, you're comparing element 2 with element 5, and then 5 with 2. Aside from the fact that you're also checking an element with itself (which is what timrau's answer is guarding against), you're doing the same work twice.
So try
for (i = 0; i < size; i++)
{
for (k = i + 1; k < size; k++)
instead.
Your approach still doesn't scale very well, it's essentially O(n^2). It would be faster to sort your array and then look for duplicate neighbors.
A more efficient approach should be to sort the array first:
std::vector<int> a = {4, 3, 2, 1, 2, 5, 6};
std::sort(std::begin(a), std::end(a));
for (std::size_t i = 1; i < a.size(); ++i) {
if (a[i - 1] == a[i]) {
std::cout << a[i] << " " << a[i] << std::endl;
std::cout << "duplicate" << std::endl;
return 0;
}
}
return 0;