Find unique element in sorted array using c++ - c++

I am trying to find unique element from the array these is question
Input : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output : arr[] = {1, 2, 3, 4, 5}
They give me correct output but why they give 0 at the end in output:
these is my output:
{1,2,3,4,5,0}
Code:
#include<iostream>
using namespace std;
int main(){
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0;j<=n;j++){
if(arr[j]!=arr[j+1]){
cout<<arr[j];
}
}
}

Except for std::cout, you code is much more C than ++.
std::unique of the C++ Standard Library does exactly what you want. There is no need to re-implement this.
Next there is the erase-remove idiom to delete the superfluous elements.
For the output, you can use std::for_each() or at least a range-based for loop.
And also, you don't want using namespace std;
A more modern solution looks like this:
#include <iostream>
#include <algorithm>
#include <vector>
int main(){
std::vector<int> arr {1, 2, 2, 3, 4, 4, 4, 5, 5};
auto last = std::unique(arr.begin(), arr.end());
arr.erase(last, arr.end());
std::for_each(arr.begin(), arr.end(), [](int n){std::cout << n << std::endl;} );
}
Try it on Godbolt.

problem is in for loop becouse you use less or equal to n.
you need to use just less then n becouse array starts from zero that means you asking for sixth element that do not have alocalizated in memory
now it should be correct
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0; j<n; j++) {
if (arr[j]!=arr[j+1]) {
cout<<arr[j];
}
}
}

Related

Index of vector containing the global minimum

Given a vector of vectors, is there an optimal way to determine the index of the vector which holds the global minimum?
What is the complexity in Big-O notation?
#include <algorithm>
#include <iostream>
#include <vector>
unsigned getMinimumIndex(std::vector<std::vector<unsigned>> const& a) {
if (!a.size())
return 0;
unsigned ret = 0; unsigned temp; unsigned global = 1 << 31;
for (std::size_t idx = 0; idx < a.size(); ++idx) {
if ((temp = *std::min_element(std::begin(a[idx]), std::end(a[idx]))) < global) {
global = temp;
ret = idx;
}
}
return ret;
}
int main() {
std::vector<std::vector<unsigned>> a = {{2, 4, 6, 8}, {3, 9, 5, 7},
{3, 4, 4, 3}, {2, 8, 3, 2},
{4, 4, 4, 0}, {1, 2, 3, 4}};
std::cout << getMinimumIndex(a); // 4-th vector posseses the value '0'
return 0;
}
Since neither your vectors nor the numbers inside a vector are sorted, you have to check every number to be the smallest value.
Thus you get a complexity of O(n).
You can either use iterators like you did or simply use 2 for loops and access the vector with a[i][j] (which should be minor faster because of the missing overhead from iterators).
Also - since you only have unsigned int, you can break as soon as you find 0.

Find difference between two lists using Eigen

I would like to find the difference between two lists. For example:
// two lists:
A = [ 0, 1, 2, 3, 4, 5, 6 ];
B = [ 1, 4, 5 ];
// difference between the lists:
C = [ 0, 2, 3, 6 ];
I have done this using the STL-library of C++ as follows:
#include <iostream>
#include <vector>
int main()
{
std::vector<size_t> A = {0, 1, 2, 3, 4, 5, 6};
std::vector<size_t> B = {1, 4, 5};
std::vector<size_t> C;
std::set_difference(A.begin(),A.end(), B.begin(),B.end(), std::inserter(C,C.begin()));
return 0;
}
However, because my application uses mostly Eigen, I now would like to do also this using Eigen. I couldn't find what I was looking for in the documentation nor online.
Note that I specifically want to avoid writing my own function.
Here you go:
#include <iostream>
#include <Eigen/Dense>
int main()
{
using namespace Eigen;
VectorXd a(3), b(1);
VectorXd c(a.size());
a << 1,2,3;
b << 1;
auto it = std::set_difference(a.data(), a.data() + a.size(),
b.data(), b.data() + b.size(),
c.data());
c.conservativeResize(std::distance(c.data(), it)); // resize the result
std::cout << c;
}
The key here is to use Eigen::VectorXd::data() member function, which returns a pointer to the underlying storage, which is itself an iterator that can be passed around to C++ standard library functions.

How to make space between array values in printing?

I'm interested, how could i get same result in C++. For this C code:
for(i=0;i<n;i++)
printf("%4d",array[i]);
This will create 4 space gap between my values from array.
Is there something similar in C++?
The same code works in C++:
const int n = 10;
int array[n] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i=0;i<n;i++)
printf("%4d",array[i]);
But if you're looking for more a C++-esque way of doing things, you can use std::cout and std::setw:
#include <iostream> // cout
#include <iomanip> // setw
int main()
{
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto n : array)
{
std::cout << std::setw(4) << n;
}
}

printing a 2D array inside a function in c++

I have made the following program in c++. I just want to print an array elements. My code is as follows:
#include <iostream>
#include <cstdio>
using namespace std;
#define n 5
double dist[n][n];
void read_distances()
{
for(int i = 0; i < n ; i++)
{
for (int j = 0 ;j < n; j++)
{
cout<<dist[i][j]<<" ";
}
cout<<"\n";
}
}
main()
{
double dist[n][n] =
{
{0, 20, 30, 10, 11},
{15, 0, 16, 4, 2},
{3, 5, 0, 2, 4},
{19, 6, 18, 0, 3},
{16, 4, 7, 16, 0}
};
read_distances();
}
I just wanted to print the dist[][] array inside the read_distances() function. But here I am getting all the values 0 as output. what's the reason of this?
The reason is because you declared the dist array in main(), and you initialized its contents, but the function read_distances() prints the values of a global array called dist. It happens to have the same name as the dist array in main()'s scope, but is a completely different array, and it is never initialized.
EDIT: you asked how to make it work. The easiest way is to pass it as a parameter. After removing the global declaration:
void read_distances(double dist[n][n])
{
// ...
}
and then in your main():
read_distances(dist);
(technically, the parameter to read_distances() is actually double (*)[n], but that's going to be a topic for another day and I didn't want to make this too confusing).
How about this:
#include <iostream>
#include <cstdio>
using namespace std;
#define n 5
void read_distances(double dist[n][n])
{
for(int i = 0; i < n ; i++)
{
for (int j = 0 ;j < n; j++)
{
cout<<dist[i][j]<<" ";
}
cout<<"\n";
}
}
int main()
{
double dist[n][n] =
{
{0, 20, 30, 10, 11},
{15, 0, 16, 4, 2},
{3, 5, 0, 2, 4},
{19, 6, 18, 0, 3},
{16, 4, 7, 16, 0}
};
read_distances(dist);
}
Your code doesn't work because you overshadowed the global version of dist with the local one you created at main. So you either not use a global one, like I do here, and pass the one you create at main() to your printing function; or you fill that array without creating a new array in main().

concat std::vector and initializer list

In c++11 you can do this wonderful syntax:
vector<int> numbers = {1, 2, 3};
Is there a way to concatenate a further initializer list onto an existing vector?
numbers.??? ({4, 5, 6});
or
std::??? (numbers, {4, 5, 6});
You can use std::vector::insert for that:
#include <vector>
vector<int> numbers = {1, 2, 3};
numbers.insert( numbers.end(), {4, 5, 6} );
Use std::vector::insert:
numbers.insert(numbers.end(), {4, 5, 6});
You can use std::vector::insert. Link to example code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1,2,3};
a.insert(a.end(), {4,5,6});
for(int &i : a) {
cout << i << " ";
}
cout << endl;
return 0;
}