merge overlapping sub-intervals - c++

I actually tried this question in some different way
#include"bits/stdc++.h"
using namespace std;
vector<vector<int> > overlap(vector<vector<int> > arr,int n){
deque<int> q;
q.push_back(arr[0][0]);
q.push_back(arr[0][1]);
int i=1;
while(i<n){
int j=0;
if(q.back()>arr[i+1][j]){
if(q.back()<arr[i+1][j+1]){
q.pop_back();
q.push_back(arr[i+1][j+1]);
}
}
else{
q.push_back(arr[i+1][j]);
q.push_back(arr[i+1][j+1]);
}
i++;
}
vector<vector<int> > out(n,vector<int>(n,0));
int k=0;
while(!q.empty()){
int l=0;
out[k][l]=q.front();
q.pop_front();
out[k][l+1]=q.front();
q.pop_front();
k++;
}
return out;
}
but while running it isn't showing any output. Is there something wrong in my code or my approach is wrong

Related

Why is my code to detect a cycle in a graph not printing anything?

I have written the code to check if a cycle exists or not in a graph using breadth-first traversal.
If I declare the adjacency list as vectoradj[n]; then it's okay.
But the problem arises when I use vector<vector>adj(n)
Can someone tell me what is the error? and how can I use the latter option to run my code?
#include <bits/stdc++.h>
using namespace std;
bool checkForCycle(int s, int V, vector<vector<int>>adj, vector<int> &visited)
{
queue<pair<int, int>> q;
visited[s] = true;
q.push({s, -1});
while (!q.empty())
{
int node = q.front().first;
int par = q.front().second;
q.pop();
for (auto it : adj[node])
{
if (!visited[it])
{
visited[it] = true;
q.push({it, node});
}
else if (par != it)
return true;
}
}
return false;
}
bool isCycle(int V,vector<vector<int>>adj)
{
vector<int> vis(V - 1, 0);
for (int i = 1; i <= V; i++)
{
if (!vis[i])
{
if (checkForCycle(i, V, adj, vis))
return true;
}
}
}
int main() {
int n, m;
cout<<"Enter number of vertices :\n";
cin>>n;
cout<<"Enter number of edges :\n";
cin>>m;
vector<vector<int>>adj(n);
for(int i=0; i<m; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<endl;
if(isCycle(n,adj))
cout<<"cycle present int the graph";
else
cout<<"No cycle present";
return 0;
}
okay so the error resolves if I use
vector<vector<int>>adj(n+1);
instead of
vector<vector<int>>adj(n);

What is wrong with my code in finding the length of Maximum Bitonic Subarray

Logic used is simple if a triplet x,y,z occurs such that x>y<z then give the length of the array to m and continue and it gives right output in all the test cases I can think of
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0;
int m=0;
for(int i=0;i<n;i++)
{
l++;
if(i>0&&i<n-1)
{
if(a[i]<a[i-1]&&a[i]<a[i+1])
{
l=1;
}
}
if(m<l)
m=l;
}
if(m)
cout<<m<<endl;
else
cout<<l<<endl;
}
return 0;
}

Memory issue implementing quick sort

I am trying to implement quick sort to sort a sequence of integers.
I am getting a segmentation default with the following code:
I implemented partition and quick sort recursive calls.But for some c++ reason I am getting an access to memory or an infinite loop I cant understand why.
#include <fstream>
#include<vector>
#include<iostream>
using namespace std;
int pivotSelection(vector<int> A){
return 0;
}
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=0; j< A.size(); j++) {
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i;
}
vector<int> readArray(char* file){
ifstream inFile;
vector<int> A;
inFile.open(file);
int x;
while (inFile >>x ) {
A.push_back(x);
}
return A;
}
void quickSort(vector<int> &A, int l,int r){
if(r==1) {
return ;
}
if(r>l){
int p= partition(A,l,r);
quickSort(A,l,p-1);
quickSort(A,p+1,r);
}
}
int main(){
vector<int> A;//= readArray((char*)"/home/brunoeducsantos/AlgorithmFoundation/quicksort/data.txt");
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(1);
int length = A.size();
quickSort(A,0,length-1);
for(int i=0;i<length;i++) cout<<A[i]<<endl;
return 0;
};
The expected result is: 1 3 5 7
Fixes noted in comments:
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=i; j<=r; j++) { // fix
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i-1; // fix
}

Intersection of 2 bidimensional arrays

I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?

TIME_LIMIT_EXCEEDED: Codeforces 686D

i was trying to solve this problem on codeforces http://codeforces.com/contest/686/problem/D, but i got TLE. could you tell me what takes so much time in my solution? is it data structures that i use or is the algorithm inefficient? how can i improve it?
#include <iostream>
#include <vector>
using namespace std;
int assignweights(int index, int weights[], vector<vector<int> > &v){
if(v[index].size()==0){
return 0;
}else{
vector<int> children=v[index];
int result=0;
result+=children.size();
for(int i=0; i<children.size(); ++i){
result+=assignweights(children[i], weights, v);
}
weights[index]=result;
}
return weights[index];
}
void getcentroid(int index, vector<vector<int> > &v, int weights[], int & result, int n){
vector<int> children=v[index];
bool b=false;
for(int i=0; i<children.size(); ++i){
if(weights[children[i]]+1>n/2){
if(!result)getcentroid(children[i], v, weights, result, n);
else break;
b=true;
}
}
if(!b){
result=index+1;
}
}
int main(){
int n, q;
cin>>n>>q;
vector<vector<int> > v;
for(int i=0; i<n; ++i){
vector<int> vv;
v.push_back(vv);
}
for(int i=1; i<=n-1; ++i){
int a;
cin>>a;
v[a-1].push_back(i);
}
int weights[n];
for(int i=0; i<n; ++i){
weights[i]=0;
}
assignweights(0, weights, v);
for(int i=0; i<q; ++i){
int index;
cin>>index;
index--;
int result=0;
getcentroid(index, v, weights, result, weights[index]);
cout<<result<<endl;
}
return 0;
}
In the worse test case, your solution my have complexity of O(n*q).
I think the best solution for this problem is for each node of the tree, you create a vector of its descendants at index 1,2,4,...2^x upon it.
With that vector, for each query, it takes O(log(n)) to find the answer.