Arrays - Sum Of Two numbers as arrays of digits - c++

Take as input N, the size of array. Take N more inputs - digits between 0 and 9 - and store that in an array. Take as input M, the size of second array and take M more input digits and store that in second array. Write a function that returns the sum of the numbers represented by the two arrays. Print the value returned.
Input:
4
1 0 2 9
5
3 4 5 6 7
Output:
3, 5, 5, 9, 6, END
Below is the code I have written for it, but it's not giving any result rather it crashes.
using namespace std;
int main()
{
int tsum,n,m,s,carry=0;
cin>>n;
int a[n],sum[]={0};
for(int i=0; i<n; i++)
{
cin>>a[i];
}
cin>>m;
int b[m];
for(int i=0; i<m; i++)
{
cin>>b[i];
}
s=max(n,m);
sum[s] = {0};
while(n>0 and m>0)
{
tsum=a[n]+b[m]+carry;
sum[s] = tsum%10;
carry = tsum/10;
n--;
m--;
s--;
}
if(n>m)
{
while(n>0)
{
tsum=a[n]+carry;
sum[s] = tsum%10;
carry = tsum/10;
n--;
s--;
}
}
if(m>n)
{
while(m>0)
{
tsum=b[m]+carry;
sum[s] = tsum%10;
carry = tsum/10;
m--;
s--;
}
}
for (int i=1; i<s; i++)
{
cout<<sum[i]<<", ";
}
cout<<"END";
return 0;
}```

Related

Swapping all smallest and largest number in an array/vector

I was trying to make a program that swaps all the biggest and smallest numbers in an array or a vector. I came up with a program but for some reason I'm not able to debug it to get the problem. Its not printing the vector, neither do I know what the issue is. Can anyone please help me.
Desired Input and Output
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n; //size input
vector<int> arr;
for(int i=0;i<n;i++) //filling up the vector
{
int input;
cin>>input;
arr.push_back(input);
}
vector<int> arr1=arr; //copying the vector
sort(arr1.begin(), arr1.end()); //sorting the new vector
int i=0,j=n-1,i1=0,j1=n-1; //i and j are for the vector arr & i1 and j1 are for vector arr1
while(i1<j1)
{
if(arr1[i1]==arr[i] && arr1[j1]==arr[j]) //if the first and last number of the sorted vector is found in arr the swap
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i1++;
j1--;
i=0; // i and j are set to initial value so that it is checked from the start
j=n-1;;
}
else if(arr1[i1]<arr[i] && arr1[j1]==arr[j]) //if only the biggest place element is found the increase i
{
i++;
}
else if(arr1[i1]==arr[i] && arr1[j1]>arr[j]) //if only the smallest place element is found the decrease j
{
j--;
}
else if(arr1[i1]!=arr[i] && arr1[j1]!=arr[j]) //if none of them are found then increase i and decrease j
{
i++;
j--;
}
}
for(int f=0;f<n;f++) //print the vector
cout<<arr[f]<<" ";
return 0;
}
/*
Sample input 1
6
12 34 87 56 38 98
Sample output 1
98 87 34 38 56 12
Sample input 2
6
8 7 9 2 4 6
Sample output 2
4 6 2 9 8 7
*/
Some help would be really appreciated.
A simpler method would be to make a vector of indexes rather than sorting the vector and trying to find the values in the original vector. For example:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int n;
std::cin >> n;
std::vector<int> arr;
std::vector<int> indexes;
for (int i = 0; i < n; i++)
{
int input;
std::cin >> input;
arr.push_back(input);
indexes.push_back(i);
}
std::sort(indexes.begin(), indexes.end(), [&](int a, int b) {return arr[a] < arr[b]; });
for (int i = 0, j = n - 1; i < n / 2; i++, j--)
{
std::swap(arr[indexes[i]], arr[indexes[j]]);
}
for (int f = 0; f < n; f++)
{
std::cout << arr[f] << " ";
}
return 0;
}

Answer changes while deleting the vector display piece of code c++

to resolve a codeforces problem
I have written this c++ code and i have a big problem while displaying the result: in fact,if i add this code :
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
the result will be correct
Otherwise it will be wrong
The code:
#include <bits/stdc++.h>
using namespace std;
void aff (vector<int> v)
{
for (int i=0;i<v.size();i++)
cout<<v[i]<<"|";
}
int main()
{
int n;
cin>>n;
int t1[2][n];
vector <int> t2 ;
vector <int> t2prim ;//flous
vector <int> t2simplifier ;
vector <int> t2primsimplifier ;//flous
vector <int> t3prim ;//flous
for(int i=0;i<n;i++)
cin>>t1[0][i];
for(int i=0;i<n;i++)
cin>>t1[1][i];
for (int i = 0;i<n;i++)
for (int j = i+1 ;j<n ;j++)
{
if(t1[0][i]<t1[0][j])
{
t2.push_back(j);
t2prim.push_back(t1[1][i]+t1[1][j]);
}
}
// cout<<"t2"<<endl;
// aff(t2);
// cout<<endl;
// aff(t2prim);
// cout<<endl;
//pour simplifier t2 et t2prim
int minn;
for (int i = 1;i<n;i++)
{
minn==1000000000;
for (int j = 0 ;j<n ;j++)
{
if((t2[j]==i)&&(t2prim[j]<minn))
{
minn=t2prim[j];
}
}
t2simplifier.push_back(i);
t2primsimplifier.push_back(minn);
}
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
for (int i = 0;i<t2simplifier.size();i++)
for (int j = t2simplifier[i] ;j<n ;j++)
{
if(t1[0][t2simplifier[i]]<t1[0][j])
{
t3prim.push_back(t2primsimplifier[i]+t1[1][j]);
}
}
// cout<<"t3prim";
// aff(t3prim);
if (t3prim.size()==0)
cout<<-1;
else
{
//talla3 min
int k = t3prim[0];
for (int i = 1;i<t3prim.size();i++)
{
if(k>t3prim[i])
k=t3prim[i];
}
// k is the result
cout << k ;
}
return 0;
}
input :
5
2 4 5 4 10
40 30 20 10 40
the result with the part of code is :
t2simplifier
1|2|3|4|
70|50|50|50|
90
but when we delete this piece of code(of vector display)
the result will be wrong :
24
Thanks to Mr "François Andrieux"
in the code there is a typing error
The line minn == 1000000000;is performing a comparison and it should be minn = 1000000000;
this error leads to an undefined behavior from reading from an uninitialized variable.

Finding perfect number

Finding perfect number in an integer range.
Firstly I wrote this code:
#include <stdio.h>
int main() {
int ara[5] = { 6, 28, 496, 2128, 33550336 };
int num, i, l, j, kase;
scanf("%d", &kase);
for (l = 0; l < kase; l++) {
scanf("%d", &num);
j = 1;
for (i = 0; ara[i] <= num; i++) {
printf("%d\n", ara[i]);
}
}
return 0;
}
but when I enter a larger number such as:
2//kase
40000000//input_1
outputs:
6
28
496
2128
33550336
1
0
7
76596795 (input_2) outputs:
6
28
496
2128
33550336
1
1
7
when I use if condition in loops it works perfectly well.
#include <stdio.h>
int main() {
int ara[5] = { 6, 28, 496, 8128, 33550336 };
int i, l, kase;
int num;
scanf("%d", &kase);
for (l = 0; l < kase; l++) {
scanf("%d", &num);
for (i = 0; i < 5; i++) {
if (ara[i] <= num) {
printf("%d\n", ara[i]);
} else {
break;
}
}
}
return 0;
}
as like: 1//kase 40000000//input outputs: 6, 28, 8128, 33550336.
It'll be a great pleasure for me if anyone tell me some details about this.
This is because in the first case, your control stays in the loop even as the value of i reaches and crosses 5.
for(i = 0; ara[i] <= num; i++)
{
printf("%d\n", ara[i]);
}
Suppose you input 40000000. Then, all elements in the array satisfy the condition ara[i] <= num. But this is the sole condition for exiting the loop, due to which you stay in the loop i=5,6,7... and so on. The rest of the output values are garbage values.
In the second case, you are ensuring that i<5, so the problem is resolved. You could do this
for(i = 0; i < 5 && ara[i] <= num; i++)
{
printf("%d\n", ara[i]);
}

getting TLE in Spoj-Hotels

I am getting time limit exceeded in hotels from spoj .
Here 's my code:
int main()
{
long long n,m;
scanf("%lld",&n);
scanf("%lld",&m);
long long a[300005],dp[300005];
for(int i=0;i<n;i++) scanf("%lld",&a[i])
for(int i=1;i<n;i++) a[i]+=a[i-1];
for(int i=n-1;i>=0;i--)
{
if(a[i]<m){
dp[0] = a[i];
break;
}
}
for(int i=1;i<n;i++)
{
long long x = a[i-1];
for(int j=i;j<n;j++)
{
a[j]-=x;
}
for(int j=n-1;j>=i;j--)
{
if(a[j]<=m){
dp[i] = a[j];
break;
}
}
}
long long max_=0;
for(int i=0;i<n;i++)
{
max_ = max(max_,dp[i]);
}
printf("%lld\n",max_);
return 0;
}
Explaination:
First of all, i calculated all the values in the array "a" by summing with previous values.Then, calulated the maximum value less than equal to "m" in dp array.Then, i started subtracting from the previous minus the current value from each element of array and stored value in dp array.So,max of dp array would give me the answer.
for e.g. :
n=5,m=12;
2 1 3 4 5
My array 'a':
2 3 6 10 15
then after 2nd pass :
2 1 4 8 13
then after 3rd pass :
2 1 3 7 12
Here's the code in action:
Ideone
This problem has complexity O(n).
#include <iostream>
using namespace std;
int a[300013];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
long long sumNow = 0;
long long maxAns = 0;
int left=0,right=-1;
while(right!= n-1){
right++;
sumNow+=a[right];
while(sumNow>m){
sumNow-=a[left];
left++;
}
maxAns = max(maxAns,sumNow);
}
printf("%lld", maxAns);
return 0;
}

Repetitive Permutations with summation bounds

I try to generate all permutations with repetition of a number array by putting bound on summation of values.
Example;
I have my array {3,4,5,6} and my bound is 11.
I would like to generate all repetitive permutations reaching and just crossing 11 as:
3 3 3 3 //
3 4 3 3 //
3 3 5 3 //
3 3 3 6 //
3 4 4 3 //
4 4 4 //
6 6 //
6 4 3 //
5 5 5 //
..
So the cardinalty doesnt need to be the same as what we have with array.
Thanks for help in advance
I tried the following conversion from Java code, I got it, but still C++ gave the error "Unhandled exception":
void permute(int array[], int start[]){
int sum=0;
for (int i=0; i< sizeof(start)/sizeof(start[0]); i++) {
sum+= start[i];
}
if (sum >= 11) {
for (int n=0; n< sizeof(start) / sizeof(start[0]); n++)
cout << start[n] << " ";
cout << "\n";
return;
}
for (int i= 0; i < sizeof(array) / sizeof(array[0]) ; i++) {
int* newStart = new int[sizeof(start) / sizeof(start[0]) + 1];
memcpy (newStart, start, sizeof(start) / sizeof(start[0]) + 1);
newStart[sizeof(start) / sizeof(start[0])] = array[i];
permute(array, newStart);
}
}
void main ()
{
int array[] = {3,4,5,6};
int newarray[1];
for (int i=0; i< sizeof(array)/sizeof(array[0]); i++) {
newarray[0]=array[i];
permute(array, newarray);
}
system("pause");}
Additionally I would like to keep the indice numbers of all permutations and positions of each member. Example:
Permutation[1119] = [ 3 3 5 3],
Member[1119][1] = 3,
Member[1119][2] = 3 etc.
This is not so complicated. Because you're so vague about your language requirements, I took the freedom to invent my own pseudocode:
function generate(int[] array, int bound, int[] solution, int sum)
if (sum > bound)
print solution
else
for each elt in array
generate(array, bound, solution ++ [elt], sum + elt)
And call this as
generate([3, 4, 5, 6], 11, [], 0)
This is code for Java:
private static boolean checkConstraint(int[] array) {
int sum=0;
for (int i=0; i<array.length; i++) {
sum+= array[i];
}
//we found it, print
if (sum >= 11) {
System.out.println(Arrays.toString(array));
return true;
}
return false;
}
public static void permute(int[] array, int[] start){
if (checkConstraint(start)) {
return;
}
for (int i= 0; i < array.length; i++) {
int[] newStart= Arrays.copyOf(start, start.length + 1);
newStart[start.length] = array[i];
permute(array, newStart);
}
}
public static void main(String[] args) {
int[] array= {3,4,5,6};
for (int i=0; i<array.length; i++) {
permute(array, new int[] {array[i]});
}
}