LeetCode TwoSum solution not working - c++

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int bins(int* p,int lo,int hi,int t)
{//BinarySearch
int mid=0;
int c=0;
if(lo<hi)
{
mid=(lo+hi)/2;
if(p[mid]==t) c=1;
else if(t>p[mid])
bins(p,mid+1,hi,t);
else if(t<p[mid])
bins(p,0,mid,t);
}
return c;
}
int main()
{
clrscr();
int target;
int k;
int count=0;
cout<<"Enter the number of elements:";
cin>>k;
int* numbers=new int(k);
cout<<"Enter the target element"<<endl;
cin>>target;
cout<<"Enter the elements:"<<endl;
for(int i=0;i<k;i++)
cin>>numbers[i];
int* nu=new int(k);
memset(nu,0,sizeof(int));
/*for(i=0;i<k;i++){
cout<<numbers[i]<<endl;
}*/
for(i=0;i<k;i++)
{
int bool=bins(nu,0,k,numbers[i]);
//printf("%d\n",bool);
if(bool)
{
count++;
}
else
{
int tg=target-numbers[i];
cout<<"targetval:"<<tg<<endl;
nu[i]=tg;
}
//bool?count++:(nu[i]=target-numbers[i]);
}
cout<<"Count is:"<<count<<endl;
getch();
return 0;
}
this is the code i have written for TwoSum problem in leetcode,instead of using HashMap whose time complexity is O(n) i used binary search whose complexity is O(logn)
The problem i'm facing is the input i give to the array numbers[i] takes only 3 values and from then it stores junk values even after assigining proper inputs from console
this program has been compiled succesfully on Turbocompiler on system

int* numbers=new int[k];////allocates an array of k adjacent integers. (undefined values)
You are allocating for an array. Check the notations.
Also you are using a keyword as a variable.(bool).
What have you done?
int *numbers=new int(k);////allocates an integer, set to k. (same syntax as constructors)

Leetcode doesn't give exact code. It will have syntax errors or compilation erros. You have to fix them to make it work. In this case, following are the issues,
int* numbers=new int[k]; //not int* numbers=new int(k);
Another thing is that bool is a keyword, that is used to give true or false for variables. You have to use another variable.

Related

Segmentation fault(code dumped) in c++ after several try I cann't get the solution

#include <iostream>
using namespace std;
int main() {
int T,D;
long long int N;
long long int a[N];
long long int b[D];
cin>>T;
for(int i=0;i<T;i++)
{
cin>>N>>D;
for(int i=0;i<N;i++)
{
cin>>a[i];
}
for(int i=0;i<D;i++)
{
b[i]=a[i];
}
for(int i=0;i<(N-D);i++)
{
a[i]=a[i+D];
}
for(int i=0;i<D;i++)
{
a[i+N]=b[i];
}
for(int i=0;i<N;i++)
{
cout<<a[i];
}
cout <<endl;
}
return 0;
}
Why is this coding having segmentation fault? I have seen many solution but cann't get it right.On visual studio or any other application it is not working but on gfg it is working. Please help me solve this problem
There are several things that are wrong.
C-style arrays must be set at compile time. So if you want to use int a[N], N must to known at compile time. If you want a flexible array, one C++ way is to use std::vector.
Then the array a goes from 0 to N-1. So going a[N] is going too far. So a[i+N] is way out if bounds and will be segfault.
you declare array a with N element, but use index out of the array range.
long long int a[N]; // declare here, maximum element is N
for(int i=0;i<D;i++)
{
a[i+N]=b[i]; // use index out of array a
}

Why this code doesn't allow me to receive the whole array?

#include <bits/stdc++.h>
using namespace std;
string bin(int n){
string x="";
while(n!=0)
{
int z=n%2;
x+=to_string(z);
n%=2;
}
return x;
}
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];
string x=bin(a[i]);
int u=x.size();
int cnt=0;
for(int g=0;g<u;g++)
{
if(x[g]=='1')
++cnt;
}
cout<<cnt<<' ';
}
cout<<'\n';
}
}
This code is given several test cases and each test case will have an array of n integers, for each element in the array I should count the number of ones in the binary representation of it. I wrote a function that expects an integer and returns a string containing the binary representation of it. But I wonder why my code does not end, and not allowing me to receive other numbers in array.
For instance, there's one test case and and only array of 2 integers if I inputted 1 and wait for ever to enter the second number, what's happening?
This is your bin function reduced to the bare minimum:
string bin(int n){
while(n!=0)
{
n%=2;
}
return {};
}
If n is even you will set it to 0 on the first iteration, otherwise you set it to 1 and never change it afterwards (1%2==1). Hence you have a endless loop. I won't spoil you the "fun" of completing the exercise, so I will just point you to using a debugger. If you step trough your code line by line you could have observed how n never changes and why the loop wont stop.
PS: (spoiler-alert) you might want to take a look at std::bitset (end of spoiler)

Unable to pass multidimensional array to function

I am writing a program to find if a given sum is possible from a subset of an array. (Subset sum problem). Although many implementations are available on the internet, I have chosen to code it myself. please suggest appropriate corrections. I am doing this using dynamic programming.
#include<iostream>
using namespace std;
bool subsetsum(int a[],int n,int s){
bool T[n][s];
for(int j=0;j<n;j++){
T[j][0]=true;
}
for(int k=1;k<n;k++){
T[0][k]=false;
for(int l=1;l<n;l++){
if((a[k]<s) && ((subsetsum(a,n-1,s-a[k])) || (subsetsum(a,n-1,s)))){
T[k][l]=true;
}
else{
T[k][l]=false;
}
}
}
return T[n][s];
}
int main(){
int n,s;
int a[n];
cout<<"Enter number of elements:\n";
cin>>n;
cout<<"Enter sum required\n";
cin>>s;
cout<<"Enter elements:\n";
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<subsetsum(a,n,s);
}
The output I obtained is below:
Enter number of elements:
3
Enter sum required
5
Enter elements:
1
4
2
0
I do not understand why I obtained 0 as the result when boolean should have been returned.
array is declared as follows:
bool T[n][s];
and return is done as:
return T[n][s];
Probably, you need to do as follows?
return T[n-1][s-1];
Array indexing in C++ starts from 0.

find method giving wrong answer in c++

In the following code we have to first calculate the weights of uniform substrings present in our strings . Uniform sub strings are those which contain just one character like "a" or "aaa". The weight of the character is defined as a-1 b-2......z-26.
After calculating the weights of all the valid uniform substrings we will be given with various queries and we have to check whether the given no. is the array or not.
Here is the link of the code and corresponding output to it:
https://www.ideone.com/pIBPtQ
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=0,k=0;
int arr[10000];
int c=0;
while(s[i]!='\0')
{
int x=(int)s[i];
x=x-96;
arr[c++]=x;
j=i+1;
int sum=x;
while(s[j]==s[i])
{
sum+=x;
arr[c++]=sum;
j++;
}
i=j;
}
int q;
cin>>q;
for(i=0;i<q;i++)
{
int val;
cin>>val;
bool exists=find(begin(arr),end(arr),val)!=end(arr);
if(exists==true)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
cout<<"the elements of the array are:"<<endl;
for(i=0;i<c;i++)
cout<<arr[i]<<" ";
return 0;
}
You forgot to initialize arr.
Change
int arr[1000];
to
int arr[1000] = {0};
https://www.ideone.com/wIj4vp
Also x=x-96; should be better written as x -= 'a';.

output is not what expected

#include<iostream>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int swapper(int n[],int s
)
{
for(int i=0;i<s;i++)
{
int temp=n[i];
n[i]=n[i+1];
n[i+1]=temp;
}
cout<<"\n Array Swapped !!!";
for (int i=0;1<s;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
int main()
{
int n[20]; int s;
cout<<"\n enter array size:";
cin>>s;
cout<<"enter no in array according to size given";
for(int j=0;j<s;j++)
{
cin>>n[j];
}
swapper(n,s);
return 0;
getch();
}
the output to this program does not swap the array elements,
instead it starts producing numbers in plenty
the entire code is written here
all other suggested changes have been made
the function is supposed to take in an integer array and its size as parameters and display an array with its adjacent elements swapped.
The call
swapper(n[],s);
is not right. Assuming the first argument of swapper is a int*, it needs to be:
swapper(n,s);
Remove the [] in your swapper function.