Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2cb13a

Browse files
author
Jinendra Jain
authored
Add files via upload
1 parent 7caba91 commit c2cb13a

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Sum of Two Arrays\n",
8+
"Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M.<br>\n",
9+
"You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index.<br>\n",
10+
"Note:The sizes N and M can be different. <br>\n",
11+
"<br>\n",
12+
"Output array/list(of all 0s) has been provided as a function argument. Its size will always be one more than the size of the bigger array/list. Place 0 at the 0th index if there is no carry. <br>\n",
13+
"<br>\n",
14+
"No need to print the elements of the output array/list.<br>\n",
15+
"<br>\n",
16+
"Input format :<br>\n",
17+
"The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.<br>\n",
18+
"<br>\n",
19+
"First line of each test case or query contains an integer 'N' representing the size of the first array/list.<br>\n",
20+
"<br>\n",
21+
"Second line contains 'N' single space separated integers representing the elements of the first array/list.<br>\n",
22+
"<br>\n",
23+
"Third line contains an integer 'M' representing the size of the second array/list.<br>\n",
24+
"<br>\n",
25+
"Fourth line contains 'M' single space separated integers representing the elements of the second array/list.<br>\n",
26+
"<br>\n",
27+
"Output Format :<br>\n",
28+
"For each test case, print the required sum of the arrays/list in a row, separated by a single space.<br>\n",
29+
"<br>\n",
30+
"Output for every test case will be printed in a separate line.<br>\n",
31+
"<br>\n",
32+
"Constraints :<br>\n",
33+
"1 <= t <= 10^2<br>\n",
34+
"0 <= N <= 10^5<br>\n",
35+
"0 <= M <= 10^5<br>\n",
36+
"Time Limit: 1 sec <br>\n",
37+
"Sample Input 1:<br>\n",
38+
"1<br>\n",
39+
"3<br>\n",
40+
"6 2 4<br>\n",
41+
"3<br>\n",
42+
"7 5 6<br>\n",
43+
"Sample Output 1:<br>\n",
44+
"1 3 8 0<br>\n",
45+
"Sample Input 2:<br>\n",
46+
"2<br>\n",
47+
"3<br>\n",
48+
"8 5 2<br>\n",
49+
"2<br>\n",
50+
"1 3<br>\n",
51+
"4<br>\n",
52+
"9 7 6 1<br>\n",
53+
"3<br>\n",
54+
"4 5 9<br>\n",
55+
"Sample Output 2:<br>\n",
56+
"0 8 6 5<br>\n",
57+
"1 0 2 2 0"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"def sumArray(arr1,n,arr2,m,output):\n",
67+
" i=n-1\n",
68+
" j=m-1\n",
69+
" carry=0\n",
70+
" l=max(n,m)+1\n",
71+
" while i>=0 and j>=0:\n",
72+
" num=arr1[i]+arr2[j]+carry\n",
73+
" s=num%10\n",
74+
" carry=num//10\n",
75+
" output[l-1]=s\n",
76+
" l=l-1\n",
77+
" i=i-1\n",
78+
" j=j-1\n",
79+
" while i>=0:\n",
80+
" num=arr1[i]+carry\n",
81+
" s=num%10\n",
82+
" carry=num//10\n",
83+
" output[l-1]=s\n",
84+
" l=l-1\n",
85+
" i=i-1\n",
86+
" while j>=0:\n",
87+
" num=arr2[j]+carry\n",
88+
" s=num%10\n",
89+
" carry=num//10\n",
90+
" output[l-1]=s\n",
91+
" l=l-1\n",
92+
" j=j-1\n",
93+
" if carry!=0:\n",
94+
" output[0]=carry\n",
95+
" \n",
96+
" \n",
97+
" \n",
98+
"t=int(input())\n",
99+
"for i in range(t):\n",
100+
" n=int(input())\n",
101+
" if n==0:\n",
102+
" print(0)\n",
103+
" break\n",
104+
" arr1=[int(x) for x in input().split()]\n",
105+
" m=int(input())\n",
106+
" arr2=[int(x) for x in input().split()]\n",
107+
" size=max(n,m)+1\n",
108+
" output=[0 for i in range(size)]\n",
109+
" sumArray(arr1,n,arr2,m,output)\n",
110+
" print(*output)\n",
111+
" print()"
112+
]
113+
}
114+
],
115+
"metadata": {
116+
"kernelspec": {
117+
"display_name": "Python 3",
118+
"language": "python",
119+
"name": "python3"
120+
},
121+
"language_info": {
122+
"codemirror_mode": {
123+
"name": "ipython",
124+
"version": 3
125+
},
126+
"file_extension": ".py",
127+
"mimetype": "text/x-python",
128+
"name": "python",
129+
"nbconvert_exporter": "python",
130+
"pygments_lexer": "ipython3",
131+
"version": "3.8.6"
132+
}
133+
},
134+
"nbformat": 4,
135+
"nbformat_minor": 4
136+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /