Next Greater Element I

Easy

The next greater element of a number x in an array is the first number to the right of x in that array that is bigger than x.

You are given two arrays of distinct integers, nums1 and nums2. Every number in nums1 also appears somewhere in nums2.

For each nums1[i], find that number in nums2 and look for its next greater element there. If nothing to its right in nums2 is bigger, the answer for it is -1.

Return an array ans of the same length as nums1, where ans[i] is the answer for nums1[i].

Examples

Example 1

Input:  nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Example 1: look to the right in nums2nums2answer1334-12-1Shaded numbers are the ones in nums1.

In nums2, the only number after 4 is 2, which is smaller, so the answer for 4 is -1. The first number after 1 is 3, which is bigger. 2 is the last number in nums2, so nothing comes after it.

Example 2

Input:  nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Example 21234answer3-14 is the last number in nums2, so nothing comes after it.nums2

3 comes right after 2. 4 is the last number, so its answer is -1.

Example 3

Input:  nums1 = [1,5,3], nums2 = [6,5,4,3,2,1,7]
Output: [7,7,7]
Example 3: everyone waits for the 76543217answer777Everything before the 7 is going down, so all three answers are 7.nums2

nums2 keeps going down until the 7 at the very end, so every one of them has to wait for the 7.

Example 4

Input:  nums1 = [3], nums2 = [3]
Output: [-1]

Example 5

Input:  nums1 = [5,2], nums2 = [2,5,3,8,6]
Output: [8,5]
Example 5: the first bigger numbernums2answer2558386The 5 skips the smaller 3 and stops at the first bigger number, 8.

After 5 comes 3, which is smaller, so keep looking: the first bigger number is 8. The answer is the first bigger number, not the biggest one: for 2 it's 5, even though 8 is bigger. The answers follow the order of nums1, not nums2.

Constraints

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 10⁴
  • All the integers in nums1 are unique, and so are all the integers in nums2.
  • Every integer in nums1 also appears in nums2.

Follow-up

Can you find a solution that runs in O(nums1.length + nums2.length) time?

TypeScript
Loading editor…
Test cases32
Case 1
nums1 = [4,1,2]
nums2 = [1,3,4,2]
Case 2
nums1 = [2,4]
nums2 = [1,2,3,4]
Case 3
nums1 = [1,5,3]
nums2 = [6,5,4,3,2,1,7]
Case 4
nums1 = [3]
nums2 = [3]
Case 5
nums1 = [5,2]
nums2 = [2,5,3,8,6]
Case 6
nums1 = [1,2,3]
nums2 = [1,2,3]
Case 7
nums1 = [3,2,1]
nums2 = [3,2,1]
Case 8
nums1 = [2]
nums2 = [1,2]
Case 9
nums1 = [1]
nums2 = [1,2]
Case 10
nums1 = [0]
nums2 = [0,10000]
Case 11
nums1 = [10000,0]
nums2 = [0,10000]
Case 12
nums1 = [4,9]
nums2 = [9,4,5,1]
Case 13
nums1 = [7,3,5,1]
nums2 = [1,7,3,5]
Case 14
nums1 = [2,6,4]
nums2 = [6,2,4,1,3,5,7]
Case 15
nums1 = [5]
nums2 = [5,1,2,3,4,6]
Case 16
nums1 = [8,6,4]
nums2 = [8,6,4,9,5]
Case 17
nums1 = [1,3,5,2,4]
nums2 = [6,5,4,3,2,1,7]
Case 18
nums1 = [33,30]
nums2 = [30,17,42,33,57,22]
Case 19
nums1 = [23,29,51,30]
nums2 = [23,30,17,41,51,29,44,38]
Case 20
nums1 = [3,36,23,28,12,13,52,4,21]
nums2 = [28,23,10,21,13,3,36,52,12,4]
Case 21
nums1 = [42,35,7,20,44,34,8,27,39]
nums2 = [28,7,42,27,8,34,20,39,35,10,44,3]
Case 22
nums1 = [41,24,58,1,46,16,42,34,38,22,50]
nums2 = [38,24,34,1,41,46,5,12,50,42,16,22,23,44,58]
Case 23
nums1 = [6,33,17,11,55,14,27,43,24,4,38,53,1,13,35,49]
nums2 = [2,33,1,14,43,27,59,24,13,49,38,6,35,56,11,4,17,53,54,55]
Case 24
nums1 = [15,57,9,2,20,38,55,21,34,12,30,59,39,42,0,44,14,43,32,11,23]
nums2 = [11,12,49,47,14,38,0,17,57,21,59,15,53,32,2,48,39,7,23,34,46,9,1,20,52,42,55,43,30,44]
Case 25
nums1 = [6886,9229,9124,3260,2267,8056,9505,7729,3472,6631,5113,9173,2636,5693,5786,2158]
nums2 = [446,2158,9142,4355,6886,2267,3952,3281,7131,7729,963,9173,9342,4225,642,6842,7064,9640,1521,6631,3260,9505,9124,5693,6037,6308,9229,2636,280,2546,506,621,1195,6807,8764,3279,6217,1610,5786,3095,5113,4435,8873,8056,2043,3472,8753,9814,6457,9513]
Case 26
nums1 = [5279,4679,8190,5033,8208,8460,8203,7539,5903,806,4633,9735,5112,8542,3298,5258,7612,278,8917,3697,7992,3727,4850,7269,3426,6757,5364,3675,2226,2791,6374,7085,5912,1587,2203,2428,8103,6476,9948,6200,4093,9345,5976,6370,856,7530,752,7486,2659,4582,6004,3610,3905]
nums2 = [3426,7992,7269,7952,2938,928,9735,5591,7486,3905,5364,1587,3675,126,5903,6004,4582,3298,7085,4850,3610,8460,2403,8208,5447,2659,8203,7612,6370,9937,8206,4093,7539,7530,806,3727,6677,8814,6476,8542,7207,856,1741,9074,9345,5112,5738,278,5279,8190,5912,5033,2791,2226,7816,3697,8917,2203,4126,8103,752,7030,5976,4679,6693,9810,4633,6374,9948,5258,2428,2061,4248,533,9969,6200,6757,6944,4312,5567]
Case 27
nums1 = [4571,7946,8053,1166,6856,6252,6993,5281,9718,1022,8232,5572,1688,1676,6939,3796,5813,205,1854,3384,2540,9615,3358,1380,924,7789,7214,9956,2556,4088,5592,6931,7142,7535,3744,6143,1953,4885,5412,2018,2328,5526,9618]
nums2 = [7656,5831,6364,6797,8464,461,4563,336,2540,8053,6003,1688,6993,8991,6649,9956,6143,1176,2118,5708,1022,3796,1771,6931,1380,924,137,4088,507,1854,9253,459,9718,3734,9618,7789,3384,7551,5516,6939,8898,5176,3960,6402,1428,9942,5526,962,5934,2328,4885,2006,7955,9832,2239,4551,205,3235,3744,2018,7904,7142,5572,6640,5205,5565,9732,6252,7946,5612,3191,8780,5925,1054,3776,5592,6313,4366,9247,2078,8862,7214,4571,4130,5626,6856,1676,1953,9615,8068,2831,5942,8979,2556,9762,9456,4525,6199,5500,5076,6133,5813,5412,8232,9802,7535,1166,2834,6549,3358,5281,6015,5732,2324,4863,5510,5445,6508,701,8449]
Case 28
nums1 = [2746,3338,1049,8391,7422,6013,3349,4695,3397,2393,8516,9163,8524,6547,2763,737,9876,5344,4245,696,1993,8440,6388,1526,8029,8746,8427,4384,6093,1996,5148,7105,9850,1539,5163,5328,7164,403,561,1105,3659,3453,8032,2937,2197,5864,5816,4271,6220,319,9039,4935,7864,2775,1779,4123,3036,8019,9456,6124,5037,7142,5820,1344,2239,9672,2749,5034,3587,5525,3736,4749,7945,3918,4628,8646,947,7849,7466,657,352,848,943,1774,7058,398,4698,2305,5786,736,8680,2686,9089,6450,5826,6537,5952,339,2900,8618,5187,3919,6895,4784,169,6674,2596,6224,4024,8935,5578,6595,8912,5884,1080,2462,1392,1217,9038,3090,1190,4893,7505,3602,4793,3376,8951,5508,8263,2873,8950,1431,5210,3219,6799,5105]
nums2 = [3219,3918,9670,8618,7945,8217,5564,5820,6537,5022,2239,403,1217,4784,6394,4749,7877,2482,8524,6895,9456,5148,2937,4245,6924,6779,848,2329,826,7422,1914,8702,5034,4024,2596,3453,1996,9039,6220,8391,8032,5037,9850,1526,3154,2393,3736,6224,8663,4200,8657,6013,3090,2061,3397,9038,1774,3916,4271,561,8744,8950,7267,1779,8019,8516,3036,9307,947,7142,4935,352,8094,6292,5884,6555,1392,1273,8951,8552,7454,9202,9005,6547,8912,132,8263,6103,6799,2315,398,5495,1190,8646,4537,5105,3602,4908,6674,4585,8440,6450,8588,1323,736,4695,5952,4698,5679,1344,2020,696,1881,2305,1049,9876,2462,2197,8249,8728,2746,2900,8983,5786,3659,6595,319,2368,4628,7505,9327,7466,5344,2749,4123,169,4676,7084,7105,3376,2775,339,5816,5864,4793,1540,8029,6093,6797,5826,5328,657,9277,4958,8827,8680,9089,157,4893,9163,5163,2282,943,5210,3919,8935,3349,1993,1431,1080,9988,2873,7058,5578,4384,737,7164,3587,6124,5187,5525,843,7864,3338,3593,1105,1539,7849,8085,2686,8427,3316,7666,8746,6388,5508,3130,2208,9672,2763]
Case 29
nums1 = [2223,1326,123,7104,1501,5377,9817,1015,5184,5148,7627,3395,4044,5652,4682,4600,7310,8771,361,7831,582,6796,8031,9475,1919,91,8173,6516,1629,841,2312,5427,462,6538,1418,2351,1694,1922,5933,6075,7450,971,3778,5728,3651,8415,3984,6292,2919,1998,3629,6128,8032,9228,3743,8135,9741,3744,6480,6882,5630,2920,9504,1488,7893,8801,6863,2401,4926,707,1293,7971,3359,7743,3407,9860,8005,1242,96,8869,4202,712,7002,9992,5166,3828,2682,6916,163,6246,2245,488,2816,9684,7889,2038,119,3212,3715,3662,7732,2375,5404,1440,976,9186,2209,1019,211,2164,15,8662,7758,4480,2025,8858,8618,5126,8134,7171,8567,1654,9783,39,5823,3630,1084,4546,3268,7100,6334,7935,5391,7800,3090,983,1811,2715,5243,2206,4749,7095,8970,3031,8835,3403,3446,5505,9011,6295,6777,5109,8951,1858,1929,977,770,2591,6025,4008,5078,166,6733,9015,9022,1517,3220,2993,4585,5150,7118,1779,7480,1769,4397,8086,6188,2807,5539,1949,610,3078,3207,7331,2466,1185,2339,8218,3928,3046,6785,3155,6045,2000,4357,9139,3385,7004,6643,2145,3869,3868,2869,2878,7323,9405,5527,5514,4362,7163,6739,7083,5116,926,9874,6636,8147,450,4862,7809,8781,2605,8193,838,8270,7168,6434,2370,5025,7573,2849,7297,1871,1711,713,2009,9734,9502,1581,5216,7179,7157,9287,9610,433,1783,1091]
nums2 = [6556,119,2591,3744,7179,8173,8801,6434,6334,1103,1517,2572,9958,7297,2754,7357,5116,9027,4882,3868,770,39,3212,5166,8086,6429,9186,5126,2849,2218,1084,6777,2145,1293,5391,6246,96,5216,7573,4519,5823,1242,5078,5539,5630,338,1769,5514,6943,3869,4362,3194,977,6785,2209,3385,163,3715,4749,1783,841,2351,354,6045,8771,1440,6025,3446,2821,7118,1922,1488,1326,9169,5770,7831,3031,2245,2919,8147,5527,7002,1654,3046,2466,4997,7287,1858,1015,462,7758,940,838,8415,3943,5184,211,5099,2715,8951,6910,6796,1185,5377,2038,667,7971,1639,983,7732,6916,9502,5074,8662,7893,8158,7009,9475,3984,8970,7800,6636,2164,3220,9610,123,7627,5404,2807,3828,7889,4600,8618,9741,1187,3395,2401,2993,361,2793,8078,7004,3662,9783,6739,7157,2682,2920,5652,9011,8134,2816,7171,2878,348,8835,1694,4044,4202,6733,9223,4653,7410,5505,7480,7104,91,1998,8567,1929,8781,8193,8869,512,2375,3207,8727,1019,433,2025,7083,6292,7168,488,7323,6295,707,2881,5728,976,3268,6538,1711,4414,9022,4585,6188,9139,3078,6421,3407,5243,4008,1779,3403,8005,9874,7450,3155,2605,1871,8858,971,1501,9405,2406,7095,6161,6075,2000,8270,6281,9817,9015,8135,9992,7100,926,3629,2370,4357,7935,7331,1949,5427,4682,15,3359,3928,8218,6882,2052,1418,610,6643,450,8429,4480,5025,3778,2437,713,166,7310,9860,7809,712,5670,1629,168,2869,4546,9684,8032,2206,8031,6128,9228,9734,582,1706,5933,5150,6516,3651,9287,6867,5148,1919,3630,4862,7743,2223,6480,4926,2312,1063,3743,1091,3090,2339,9553,6863,2009,5109,1811,9504,1581,4397,7163]
Case 30
nums1 = [400,393,386,379,372,365,358,351,344,337,330,323,316,309,302,295,288,281,274,267,260,253,246,239,232,225,218,211,204,197,190,183,176,169,162,155,148,141,134,127,120,113,106,99,92,85,78,71,64,57,50,43,36,29,22,15,8,1]
nums2 = [400,399,398,397,396,395,394,393,392,391,390,389,388,387,386,385,384,383,382,381,380,379,378,377,376,375,374,373,372,371,370,369,368,367,366,365,364,363,362,361,360,359,358,357,356,355,354,353,352,351,350,349,348,347,346,345,344,343,342,341,340,339,338,337,336,335,334,333,332,331,330,329,328,327,326,325,324,323,322,321,320,319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,299,298,297,296,295,294,293,292,291,290,289,288,287,286,285,284,283,282,281,280,279,278,277,276,275,274,273,272,271,270,269,268,267,266,265,264,263,262,261,260,259,258,257,256,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,9999]
Case 31
nums1 = [4907,4852,1064,4976,1130,1056,1149,1135,4882,4869,4982,1128,1108,1093,1142,1043,4918,4901,1013,4873,4991,1136,1110,4864,4925,4972,4998,1091,1025,4909,1079,4934,4860,4926,1058,1087,1076,1107,4978,4898,4930,4912,4975,1065,1020,1071,1067,1063,4987,4890,1090,1010,5000,4957,4941,4894,4915,4899,4996,1008,4854,1057,4903,1000,4871,4960,1082,1095,1148,1042,1073,1120,4862,1096,1016,4940,1140,4943,1109,1123,1112,1102,1048,4905,1115,1022,4945,1003,4928,4979,4923,1146,1014,4971,1009,1046,4952,1129,4994,4989,1039,4868,4920,1147,1029,4897,1033,1099,4954,4904,4949,1006,1023,1037,1012,1104,4872,1127,1074,4968]
nums2 = [1000,5000,1001,4999,1002,4998,1003,4997,1004,4996,1005,4995,1006,4994,1007,4993,1008,4992,1009,4991,1010,4990,1011,4989,1012,4988,1013,4987,1014,4986,1015,4985,1016,4984,1017,4983,1018,4982,1019,4981,1020,4980,1021,4979,1022,4978,1023,4977,1024,4976,1025,4975,1026,4974,1027,4973,1028,4972,1029,4971,1030,4970,1031,4969,1032,4968,1033,4967,1034,4966,1035,4965,1036,4964,1037,4963,1038,4962,1039,4961,1040,4960,1041,4959,1042,4958,1043,4957,1044,4956,1045,4955,1046,4954,1047,4953,1048,4952,1049,4951,1050,4950,1051,4949,1052,4948,1053,4947,1054,4946,1055,4945,1056,4944,1057,4943,1058,4942,1059,4941,1060,4940,1061,4939,1062,4938,1063,4937,1064,4936,1065,4935,1066,4934,1067,4933,1068,4932,1069,4931,1070,4930,1071,4929,1072,4928,1073,4927,1074,4926,1075,4925,1076,4924,1077,4923,1078,4922,1079,4921,1080,4920,1081,4919,1082,4918,1083,4917,1084,4916,1085,4915,1086,4914,1087,4913,1088,4912,1089,4911,1090,4910,1091,4909,1092,4908,1093,4907,1094,4906,1095,4905,1096,4904,1097,4903,1098,4902,1099,4901,1100,4900,1101,4899,1102,4898,1103,4897,1104,4896,1105,4895,1106,4894,1107,4893,1108,4892,1109,4891,1110,4890,1111,4889,1112,4888,1113,4887,1114,4886,1115,4885,1116,4884,1117,4883,1118,4882,1119,4881,1120,4880,1121,4879,1122,4878,1123,4877,1124,4876,1125,4875,1126,4874,1127,4873,1128,4872,1129,4871,1130,4870,1131,4869,1132,4868,1133,4867,1134,4866,1135,4865,1136,4864,1137,4863,1138,4862,1139,4861,1140,4860,1141,4859,1142,4858,1143,4857,1144,4856,1145,4855,1146,4854,1147,4853,1148,4852,1149,4851]
Case 32
nums1 = [5085,9550,4554,6080,4477,9524,2490,2603,4719,5867,2866,5309,6589,4857,4261,8151,6344,3602,7295,7061,1275,8620,2123,184,8333,6171,4512,534,2715,3387,341,5896,7865,5370,2599,5848,1113,3174,17,1866,2293,3621,1094,9085,2245,1390,4343,9865,1883,3616,9475,7320,2624,8136,5937,1799,1040,9893,6494,8294,4787,9625,2121,1200,1462,6428,6581,1782,4922,4183,8254,5446,1160,8371,3363,2925,3913,1695,466,7308,4625,4536,6107,3485,5428,8947,8101,4062,8409,4084,8882,7562,9465,7629,8396,5035,3364,1276,2275,3398,9778,5968,9748,3252,6554,2124,3091,9476,1752,8250,8934,7834,824,115,5205,4965,9023,8300,8722,5378,4203,5582,3709,7156,9232,3229,1195,2920,4925,2976,8487,8711,5379,4946,8763,4112,4187,9057,1835,8689,2054,4305,2955,7982,7999,7529,7803,2532,6688,4265,5791,8679,8991,90,6882,1043,425,4214,1770,948,4496,2364,6632,9398,2810,6510,3320,7577,7194,1556,4697,5495,6368,4208,2494,8325,7018,5751,8671,2027,6292,7134,4702,7005,4131,1135,4704,5552,675,5864,8048,4407,9064,2489,705,9223,3727,3736,9006,8943,4993,4601,3082,1524,5991,8210,9520,3908,7323,3623,1735,1957,3772,1516,2938,1486,6582,937,4120,796,8229,4167,2268,8823,2865,66,1726,5842,7,9564,3289,8555,9194,7147,4144,6170,7817,3048,8461,6703,1509,3021,9980,5849,9658,9570,3317,8377,8953,4049,7944,4498,3173,706,6502,7066,8029,1045,2143,4072,4284,3147,1518,4002,3318,8464,5744,9740,3275,1810,4058,5593,6049,1162,2726,9821,4803,5043,6153,5778,4577,4664,9350,1864,1997,9271,8172,4696,4412,8081,2420,7693,7297,8274,6872,3862,6638,8498,763,1745,3065,5633,7930,9017,942,3568,733,4116,4443,1964,140,3014,3928,3592,5289,21,141,1681,9376,8774,6610,8776,2558,5625,1463,9934,8789,2727,7402,1863,2235,7431,6238,4769,3892,6008,5402,7143,4709,5466,2696,8603,145,4468,4321,350,5313,8257,1145,1029,5116,9257,3811,6774,1614,4814,2477,3427,7559,7261,8994,8908,3220,7933,2911,1109,9659,4762,9501,399,9377,1644,3205,2330,7627,5635,4920,8321,2015,3430,5900,3329,5019,5575,6131,3346,3411,246,6540,9091,8004,9195,4139,3511,279,2990,567,6865,678,2289,5838,479,4195,8438,2923,8379,3980,2310,7634,3418,2527,9358,1346,7887,6928,3897,5042,4124,4825,4599,1664,5758,2522,8182,1044,6100,368,4141,4560,9941,9897,2944,4649,8471,5441,8619,4306,2806,504,6118,7704,9529,239,120,1058,5311,9198,845,4714,7211,8936,9773,7960,1528,7768,6827,5472,7424,1628,9016,1679,3268,4458,2138,9241,9839,340,8400,4256,7601,2878,8621,3041,413,9959,4387,2637,3190,5307,2414,1143,4804,6372,6133,4767,5306,9656,1542,8748,6380,5081,9951,5488,2226,2018,1377,6847,6222,1737,9669,3159,402,8139,6602,6661,2593,8648,847,4109,2498,1505,5668,1118,335,1804,2244,9781,5972,5150,258,8244,6628,7804,2391,2676,3302,8189,851,8380,3110,5529,7971,682,4614,5291,8001,595,9322,6780,1205,8847,1157,816,8433,8731,7787,8996,4337,4500,3906,2786,2669,5964,3740,7335,5276,770,4372,2732,3954,4735,6490,4295,9338,372,9974,9964,8610,4398,5041,6921,4912,1604,6164,1423,8741,8310,8658,30,6911,2317,521,1739,2965,6018,889,6765,3593,850,6046,6395,515,6014,1819,6900,3477,9129,5253,9484,8669,1347,7709,5525,4185,2683,2666,5384,6054,7776,1688,507,8350,5739,8317,4277,1122,4522,2913,8770,5084,3796,9914,1971,2876,2356,5653,1874,3813,9985,6897,7544,748,4915,6365,3903,6751,6383,9517,3347,9151,8326,3661,1537,8822,6699,6020,8421,5451,5829,584,6445,7460,5027,6916,2598,165,1076,1629,6464,606,7824,3804,2634,1676,5260,2075,5527,2811,9859,1168,1832,5216,4486,2351,1723,3767,9674,5444,63,2367,5770,477,5646,3079,2515,822,6941,8223,2514,5237,3468,4158,4189,8627,7907,4616,9811,3339,4194,4811,473,3249,3907,4489,1361,6413,7203,1878,3417,9796,2323,7959,6452,5574,8959,3614,325,611,69,8238,1879,551,3725,5455,1896,4353,4220,1577,3365,4445,6301,1257,532,7661,4707,2243,5909,9884,1489,1292,6999,1184,8415,2130,8041,8612,4053,9633,7506,8104,4621,8276,2145,9133,5745,6841,1882,1948,191,8644,1924,2450,6980,2347,229,2652,9630,4218,4197,4679,9749,6061,2691,4262,7843,3169,4994,9372,541,6777,9117,8594,1624,1453,2954,1196,5015,1431,3496,1678,5062,2151,5807,5515,3379,8587,9670,4294,3589,1787,6439,9412,7445,4737,469,2568,3555,9913,196,6082,8163,6371,2784,175,4561,9317,1694,8874,3717,151,6590,6563,7354,3069,2820,4039,9862,544,9600,5091,3995,2982,5248,909,4778,5550,2125,1446,7795,6036,6147,7929,1101,3910,6967,9435,1571,6979,9705,2107,7358,7238,7863,2645,5732,9299,1715,840,4979,9911,4233,6461,2390,3770,6208,1643,1593,3434,6219,5500,2873,8387,5986,5586,8083,6433,4184,927,9095,4632,5146,918,9197,3981,8277,6819,2176,2608,6829,3293,5573,823,9168,2674,2901,9947,3580,2183,2544,4349,3579,7209,8097,3384,4718,1493,9469,4909,6486,2883,9530,7331,7947,5229,6904,8708,9526,5893,3919,5982,3198,3479,8240,6521,9967,1115,3263,9413,5475,7814,9205,5138,2829,681,4961,4734,5477,4845,3541,7090,9614,1055,405,4097,6813,8564,1784,887,7816,231,8881,4060,3201,9047,8359,3061,2041,311,2834,5437,1800,8538,6833,808,7363,2929,8126,3211,2361,4565,285,1020,2433,4615,9518,6042,1078,2392,802,5397,5261,9355,1178,1686,5923,3077,1759,3833,955,2358,9161,6363,1853,4699,3206,7954,2759,5118,4779,2639]
nums2 = [8433,5116,2391,9913,3173,5770,3593,9271,3981,5909,850,6827,8644,1664,611,1879,2514,8126,8421,7824,4139,9232,5306,8048,8238,4167,3811,8464,4343,3903,1694,1195,3511,8671,3061,8947,5261,5668,5586,6921,69,6363,2121,9656,9947,551,8136,5964,3302,7297,4445,6445,2243,2289,9350,7865,7544,3021,9564,2925,5205,5062,1745,1800,1787,8936,4814,7817,9197,1819,5744,1695,8438,285,3069,5477,5732,4825,4702,9376,3384,3275,2637,7134,5042,1804,2392,9476,6841,2944,8380,9475,2820,8325,6368,7445,6452,2130,7843,2361,246,5146,477,8996,2245,823,6131,3329,4920,6581,7295,2715,4256,4965,8350,1275,9299,4779,1784,17,4925,2598,1629,4946,8953,9435,8415,1874,8139,6383,5084,2323,4443,1196,8277,5015,4599,8400,2990,7320,2829,141,5428,1604,3862,3190,8083,2176,5937,2433,824,7834,8619,9967,4185,9821,2018,3709,6301,2683,1957,847,8774,2666,3980,8172,2075,796,1423,7559,3205,7424,8882,3048,6208,1832,3041,2358,5758,5455,3347,368,1145,8763,2876,5807,6082,6699,4214,2639,2490,504,3621,3740,7261,2544,4554,1446,5739,887,8934,8461,9057,196,5313,8244,8081,1101,8789,6153,2124,1505,4277,4349,5635,7629,1883,5035,2691,1835,4769,5019,7211,2494,507,4486,1759,2674,8594,1094,7018,7562,6882,2938,541,9941,7,3434,3541,4284,6632,4616,8621,6170,4305,9129,770,2883,372,9530,706,8943,4993,9465,7930,1377,2414,9133,2123,8371,5466,6036,4218,5437,9241,4561,6428,6380,1866,4707,3479,7005,4778,5515,9658,3892,6107,8603,7816,2558,6413,851,2913,1157,2151,2293,1162,9501,8555,151,521,120,6765,5778,1115,5751,3364,5085,4719,2527,1486,5446,1678,4961,2965,4116,3293,2784,534,7194,6292,7709,3427,6602,9322,7787,4699,165,5968,2356,4912,2522,682,9670,3995,5378,1178,675,3229,8679,5745,5307,8004,2954,4049,4498,3616,6979,4194,4909,3147,4458,8377,4601,7143,4131,5593,4097,2608,8620,802,7601,5574,3082,4522,6494,9095,3317,9413,4120,822,6911,5893,6916,4189,8610,9064,3079,6980,3589,6219,8379,5229,7947,595,8881,3954,6900,1770,8359,1122,8104,1292,2603,3379,466,8669,5043,4337,5500,9151,1463,231,3398,9811,3908,918,7971,1614,5291,5575,8300,5923,4398,7354,2450,5441,2107,3623,6610,1040,1109,9859,9377,7431,6054,6020,2696,3772,6638,8748,402,4039,5791,3263,6661,7335,9198,6171,1799,2244,5972,3661,2477,3110,6502,9570,9520,2955,3065,6100,9194,3897,8847,1863,2364,8041,4195,7693,1624,955,5896,1924,3169,9195,4621,9740,3252,3174,3363,4468,733,515,2624,5991,6042,942,8658,1029,7795,7238,8991,4265,7929,4412,8994,9749,1593,311,6865,2652,6780,5370,7066,1135,7090,9168,9518,8708,7460,6486,544,4614,9625,9117,3813,8776,9934,4197,1628,1681,9914,8731,2976,350,1752,1542,2351,8959,9223,3910,6439,6061,1556,3906,4262,6590,1528,1882,909,8257,5653,3907,7959,9959,6628,239,2645,1390,2498,7061,3485,2515,8189,6774,2676,9669,2310,7147,1205,21,3568,341,4109,2330,9964,1431,6490,3211,9006,2367,7933,8097,6222,7887,2929,8770,6819,9550,2593,4058,2015,1643,3833,5444,1143,5041,4158,6999,1200,9897,1782,7363,4294,8223,681,1997,5475,2347,8210,2982,6563,5260,8254,4632,8498,7982,5379,1489,1735,9412,8711,4811,5864,335,5309,7661,2041,5550,4060,7203,3614,8471,8310,1020,2183,4615,3339,5311,9017,7954,1257,4994,9748,6582,6847,1577,1184,3387,4489,5237,4735,7804,7907,5402,6872,2810,3770,4084,5529,4387,425,5982,6967,3496,1688,2759,1726,9862,3417,4184,8321,4295,115,4714,7358,8250,1518,7627,6372,3555,8396,9372,705,4704,584,3796,1679,6238,2834,7999,229,7506,4696,9985,8612,5091,9526,7156,340,9355,184,8317,9161,279,2138,606,4625,9358,2786,6589,9398,3592,1737,808,937,8001,4220,1715,7803,1686,1878,2268,3430,7814,4141,8908,8587,5900,6008,6395,5633,3767,7960,8101,2317,6751,4565,2226,4577,1118,2634,748,9016,1509,63,8151,1361,3249,2865,8182,7863,191,4804,3320,5384,1462,405,30,4536,1346,9047,3580,175,9911,1571,4500,1493,1058,4915,5573,1723,473,5472,3159,2726,816,2143,1044,3289,3913,469,2727,5289,6014,532,3365,4407,4787,3736,3077,6941,889,4306,9951,7776,9705,6464,2054,3091,8240,8326,7402,9517,6118,9257,8029,1160,3268,1948,1971,1516,2901,9865,4353,9338,2806,763,4477,4208,9778,1078,6928,9674,4321,2878,9023,9085,6371,8722,3411,9796,9633,2489,4261,4922,4183,3418,2811,1055,9469,8822,5216,7308,3919,927,1896,6777,6540,2125,7944,6433,479,8689,5276,5488,4187,6904,1676,6164,8387,4072,1864,4233,90,4002,5027,6510,9630,2235,3602,6147,6344,2873,5495,4053,8276,4845,8823,6833,5525,1045,5451,6521,4762,4803,2532,1537,8741,8409,3804,1964,1076,5842,3477,8874,140,2145,4203,7323,413,7209,5849,2275,8538,1113,3220,9980,4979,5527,8274,4372,3579,6897,6049,9974,8627,3206,3014,2027,1524,6461,1168,9317,4664,7634,5253,9884,845,8163,5248,9614,6703,5138,2920,6018,1453,1644,5829,5118,8294,3201,2390,948,6046,5867,6133,325,4512,9600,9091,9529,4718,4734,4857,6080,1853,4767,7529,1043,8229,2568,9524,8333,840,4697,5552,7768,6829,2420,4649,9484,5150,2911,3725,9893,5397,2732,4112,4124,8487,3928,3468,4709,7704,7331,9781,3717,1739,4062,567,4144,1276,5582,6688,4737,1347,2923,5646,399,4496,2866,3318,2669,5986,3346,6554,6365,66,1810,9773,3198,5848,9659,5838,8648,5625,9839,5081,2599,8564,678,4679,9205,4560,3727,7577,258,145,6813]