Below is the link to problem
https://www.hackerrank.com/challenges/and-xor-or/copy-from/16519519
I have implemented a O(n) solution which seems ok (no TLE and 29 testcaes passed out of 32). But my solution is failing for some testcaes and I am not able to find the error, surely I am missing some corner cases. Any hint would be great help. I have posted my code below which I have ran and submitted.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <stack>
#define ul unsigned long
using namespace std;
ul comp(ul a, ul b){
ul result = ((a&b)^(a|b))&(a^b);
return result;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
vector<ul>ip(n);
for(int i=0;i<n;i++)
cin>>ip[i];
stack<ul>st;
ul result=0;
for(int i=0;i<n;i++){
if(st.empty())
st.push(ip[i]);
else{
result=max(result,comp(st.top(),ip[i]));
while(!st.empty() && st.top()>ip[i]){
result=max(result,comp(st.top(),ip[i]));
st.pop();
}
st.push(ip[i]);
}
}
cout<<result;
return 0;
}
Related
#include <iostream>
#include <bits/stdc++.h>
#include <stack>
#include <algorithm>
#include <vector>
#include <iterator>
#define ll long long
using namespace std;
int main() {
ll t;
cin >> t;
while(t--) {
ll n,k;
cin >>n >>k;
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
vector <ll> freq;
for(int i=1;i<=k;i++) freq.push_back(i);
ll min=n,val,diff=0,max=0;
for(int i=0;i<k;i++){
ll c=count(arr.begin(),arr.end(),freq[i]);
if(min>c)
{min=c;
val=freq[i];
}
}
if(min==0) max=n;
else{
vector<ll> diffarr;
diffarr.push_back(0);
for(int i=0;i<n;i++)
{
if(arr[i]==val){
diffarr.push_back(i);
}
}
for(int i=0;i<min;i++)
{
diff=diffarr[i+1]-diffarr[i];
if(max<diff) max=diff;
}
}
cout <<max <<endl;
}
return 0;
}
The code works fine when input is not taken but as soon as I take input it throws a SIGSEGV error
I have debugged it many times to get rid of all segmentation faults and all kinds of errors. Can someone pls look into it, that would be a gr8 help
the code is correct
No, it isn't. Start with these lines:
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
You declare an empty vector arr, and immediately try to write to element 0 in that vector.
But the vector is still empty, so there is no element 0 in it. On most STL implementations this will crash by trying to write to address 0.
Reading How to debug small programs is likely to benefit you. So will learning how to use a debugger.
I just started working with C++ about a week ago so I don't know much about the language. I am working on creating a program that inputs a text file and then reverses all of the lines.
So an input of:
"abc"
"123"
Would be:
"cba"
"321"
I can figure out how to input the lines from the file, but I am having trouble with my reverse function. I have tried to just print out each individual character as a string using substring, starting from length - 1 and ending at 0. But this does not seem to be working. When I run the program, I get these errors that I don't understand.. Any ideas as to what is going wrong?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void reverse(string input) {
for((int x=input.length()-1); x=0; x--)
cout << input.substr(x);
}
cout << endl;
}
int main()
{
string line;
ifstream myFile;
myFile.open("reverse_input.txt");
while(getline(myFile, line)) {
reverse(line);
}
return 0;
}
There are couple syntax errors in your code that the compiler was complaining
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void reverse(string input) {
for( unsigned int x= (input.length()-1); x>=0; x--) { //Missing {, extra (), >= insteadof = 0
cout << input[x] << flush;
}
cout << endl;
}
int main()
{
string line;
ifstream myFile;
myFile.open("reverse_input.txt");
while(getline(myFile, line)) {
reverse(line);
}
return 0;
}
I meet a problem. http://poj.org/problem?id=1065
The problem is to find the minimum number of ascending subsequences.
I see somebody is to find the length of longest descending subsequences.
I don't know why the two numbers are equal.
#include <iostream>
#include <algorithm>
#include <functional>
#include <memory.h>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
pair<int,int> stick[5000];
int dp[5000];
int main(int argc, char** argv) {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>stick[i].first>>stick[i].second;
}
sort(stick,stick+n);
memset(dp,-1,sizeof(int)*5000);
for(int i=0;i<n;i++){
*lower_bound(dp,dp+n,stick[i].second,greater<int>())=stick[i].second;
}
cout<<lower_bound(dp, dp + n, -1, greater<int>()) - dp<<endl;
}
return 0;
}
It follows immediately from the Dilworth's theorem. It's a standard technique for solving problems like this.
When I debug in Xcode, how can I add the view of picture 1(main()) and the view of picture 2(sort()) at the same time? Or how can I see the value in array: a[10] when debug in function: sort()?
picture 1(main()):
picture 2(sort()):
picture 3(whole picture):
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <limits.h>
void swap(int* a, int* b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void sort(int* a, int l, int r){
int i=0;
if(l!=r){
int change=l;
int move = l+1;
for(; move<r; move++){
if(a[move]<a[l]){
swap(&a[move], &a[++change]);
}
}
swap(&a[l], &a[change]);
if(change>l){
sort(a, l, change-1);
}
if(change<r){
sort(a, change+1, r);
}
}
}
int main(){
int a[10]={1,2,44,22,2,5,6,0,10,99};
sort(a, 0, 9);
int i;
for(i=0; i<10; i++){
printf("%d\n", a[i]);
}
int b=2, c=3;
swap(&b,&c);
printf("b is %d, c is %d", b, c);
return 0;
}
I have made a function for my program that reads from a text file, adds content to a vector and then search in that vector. The Problem is that even if the file is empty it shows that it found something, on the other side if i change return value to 0 it does not give results at all!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
vector<string> contacts;
//This function returns at what index the name is found
int searchContact(string contactToSearch)
{
string entry;
ifstream input;
input.open("contacts.txt");
while (input.good())
{
while (getline(input, entry))
{
contacts.push_back(entry);
}
input.close();
}
for(int i = 0; i < contacts.size(); i++)
{
if(contactToSearch == contacts[i])
{
//Found => Returning index rest of program can see index
return i;
}
}
return 1;
}
I have just refactored your code a little. Further improvements are possible, but to begin with
1) You dont need a while for input.good()
2) You were trying to return 0 and 1 which are indeed valid positions where the string could have been present in the vector
All these aside, I still think your code might not properly populated the array The reasons for this maybe :- case sensitive comparison, reading incorrect file, binary file.. etc..
Here is a refactored code that you could use
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
void readContacts(const string &fileName inputFileName, vector<string> &contacts){
string entry;
ifstream input;
input.open(inputFileName);
if (input.good())
{
while (getline(input, entry))
contacts.push_back(entry);
input.close();
}
}
int searchContact(const string &contactToSearch, vector<string> &contacts)
{
for (int i = 0; i < contacts.size(); i++)
{
if (contactToSearch == contacts[i])
return i;
}
return -1;
}
int main(){
vector<string> contacts;
// This needs to be filled in with the contact name u want to search
string contactToSearch;
readContacts("contacts.txt", contacts);
int index = searchContact(contactToSearch, contacts)
if (index != -1)
cout << "Found Contact " << contactToSearch" at location " << index << endl;
else
cout << "Could Not find contact " << contactToSearch << endl;
}