Statistics

Problem Statement for "GuessingNextElement"

Problem Statement

An integer arithmetic progression is a sequence defined by two positive integers, p and q, where p is the first element in the sequence, and all other elements are obtained by adding q to the previous element. For example, if p = 1 and q = 2, the sequence would be: 1, 3, 5, 7, ...

An integer geometric progression is a sequence defined by two positive integers, p and q, where p is the first element in the sequence, and all other elements are obtained by multiplying the previous element by q. For example, if p = 3 and q = 2, the sequence would be: 3, 6, 12, ...

You are given a int[] A, which contains either an integer arithmetic or geometric progression. Determine which one it is and return the next element in the sequence. It is guaranteed that A will uniquely represent either an arithmetic or geometric progression and that result will fit in a 32-bit signed integer.

Definition

Class:
GuessingNextElement
Method:
guess
Parameters:
int[]
Returns:
int
Method signature:
int guess(int[] A)
(be sure your method is public)

Constraints

  • A will contain between 3 and 50 elements, inclusive.
  • Each element of A will be between 1 and 10^6, inclusive.
  • A will be sorted in ascending order.
  • A will uniquely represent either an arithmetic or geometric progression.

Examples

  1. {364,843,1322,1801}

    Returns: 2280

    This sequence represents an arithmetic progression where p = 364 and q = 479. The next element is 1801 + 479 = 2280.

  2. {315,785,1255,1725,2195,2665}

    Returns: 3135

  3. {394,1172,1950,2728,3506,4284,5062,5840}

    Returns: 6618

  4. {944,1073,1202,1331,1460,1589,1718,1847,1976,2105}

    Returns: 2234

  5. {162,549,936,1323,1710,2097,2484,2871,3258,3645,4032,4419}

    Returns: 4806

  6. {165,983,1801,2619,3437,4255,5073,5891,6709,7527,8345,9163,9981,10799}

    Returns: 11617

  7. {467,1309,2151,2993,3835,4677,5519,6361,7203,8045,8887,9729,10571,11413,12255,13097}

    Returns: 13939

  8. {403,415,427,439,451,463,475,487,499,511,523,535,547,559,571,583,595,607}

    Returns: 619

  9. {866,1459,2052,2645,3238,3831,4424,5017,5610,6203,6796,7389,7982,8575,9168,9761,10354,10947,11540,12133}

    Returns: 12726

  10. {480,976,1472,1968,2464,2960,3456,3952,4448,4944,5440,5936,6432,6928,7424,7920,8416,8912,9408,9904,10400,10896}

    Returns: 11392

  11. {716,769,822,875,928,981,1034,1087,1140,1193,1246,1299,1352,1405,1458,1511,1564,1617,1670,1723,1776,1829,1882,1935}

    Returns: 1988

  12. {103,1016,1929,2842,3755,4668,5581,6494,7407,8320,9233,10146,11059,11972,12885,13798,14711,15624,16537,17450,18363,19276,20189,21102,22015,22928}

    Returns: 23841

  13. {470,1260,2050,2840,3630,4420,5210,6000,6790,7580,8370,9160,9950,10740,11530,12320,13110,13900,14690,15480,16270,17060,17850,18640,19430,20220,21010,21800}

    Returns: 22590

  14. {239,377,515,653,791,929,1067,1205,1343,1481,1619,1757,1895,2033,2171,2309,2447,2585,2723,2861,2999,3137,3275,3413,3551,3689,3827,3965,4103,4241}

    Returns: 4379

  15. {346,1055,1764,2473,3182,3891,4600,5309,6018,6727,7436,8145,8854,9563,10272,10981,11690,12399,13108,13817,14526,15235,15944,16653,17362,18071,18780,19489,20198,20907,21616,22325}

    Returns: 23034

  16. {881,1479,2077,2675,3273,3871,4469,5067,5665,6263,6861,7459,8057,8655,9253,9851,10449,11047,11645,12243,12841,13439,14037,14635,15233,15831,16429,17027,17625,18223,18821,19419,20017,20615}

    Returns: 21213

  17. {140,994,1848,2702,3556,4410,5264,6118,6972,7826,8680,9534,10388,11242,12096,12950,13804,14658,15512,16366,17220,18074,18928,19782,20636,21490,22344,23198,24052,24906,25760,26614,27468,28322,29176,30030}

    Returns: 30884

  18. {820,956,1092,1228,1364,1500,1636,1772,1908,2044,2180,2316,2452,2588,2724,2860,2996,3132,3268,3404,3540,3676,3812,3948,4084,4220,4356,4492,4628,4764,4900,5036,5172,5308,5444,5580,5716,5852}

    Returns: 5988

  19. {838,900,962,1024,1086,1148,1210,1272,1334,1396,1458,1520,1582,1644,1706,1768,1830,1892,1954,2016,2078,2140,2202,2264,2326,2388,2450,2512,2574,2636,2698,2760,2822,2884,2946,3008,3070,3132,3194,3256}

    Returns: 3318

  20. {179,608,1037,1466,1895,2324,2753,3182,3611,4040,4469,4898,5327,5756,6185,6614,7043,7472,7901,8330,8759,9188,9617,10046,10475,10904,11333,11762,12191,12620,13049,13478,13907,14336,14765,15194,15623,16052,16481,16910,17339,17768}

    Returns: 18197

  21. {890,1352,1814,2276,2738,3200,3662,4124,4586,5048,5510,5972,6434,6896,7358,7820,8282,8744,9206,9668,10130,10592,11054,11516,11978,12440,12902,13364,13826,14288,14750,15212,15674,16136,16598,17060,17522,17984,18446,18908,19370,19832,20294,20756}

    Returns: 21218

  22. {530,721,912,1103,1294,1485,1676,1867,2058,2249,2440,2631,2822,3013,3204,3395,3586,3777,3968,4159,4350,4541,4732,4923,5114,5305,5496,5687,5878,6069,6260,6451,6642,6833,7024,7215,7406,7597,7788,7979,8170,8361,8552,8743,8934,9125}

    Returns: 9316

  23. {166,391,616,841,1066,1291,1516,1741,1966,2191,2416,2641,2866,3091,3316,3541,3766,3991,4216,4441,4666,4891,5116,5341,5566,5791,6016,6241,6466,6691,6916,7141,7366,7591,7816,8041,8266,8491,8716,8941,9166,9391,9616,9841,10066,10291,10516,10741}

    Returns: 10966

  24. {973,1161,1349,1537,1725,1913,2101,2289,2477,2665,2853,3041,3229,3417,3605,3793,3981,4169,4357,4545,4733,4921,5109,5297,5485,5673,5861,6049,6237,6425,6613,6801,6989,7177,7365,7553,7741,7929,8117,8305,8493,8681,8869,9057,9245,9433,9621,9809,9997,10185}

    Returns: 10373

  25. {30,60,120,240,480,960,1920,3840,7680,15360,30720,61440,122880,245760,491520}

    Returns: 983040

  26. {33,231,1617,11319,79233}

    Returns: 554631

    This

  27. {9,18,36,72,144,288,576,1152,2304,4608,9216,18432,36864,73728,147456,294912}

    Returns: 589824

  28. {13,117,1053,9477,85293}

    Returns: 767637

    This sequence represents a geometric progression where p = 13 and q = 9. The next element is 85293 * 9 = 76737.

  29. {17,34,68,136,272,544,1088,2176,4352,8704,17408,34816,69632,139264,278528}

    Returns: 557056

  30. {11,22,44,88,176,352,704,1408,2816,5632,11264,22528,45056,90112,180224,360448}

    Returns: 720896

  31. {4,16,64,256,1024,4096,16384,65536}

    Returns: 262144

  32. {33,132,528,2112,8448,33792,135168}

    Returns: 540672

  33. {27,54,108,216,432,864,1728,3456,6912,13824,27648,55296,110592,221184,442368}

    Returns: 884736

  34. {3,15,75,375,1875,9375,46875}

    Returns: 234375

  35. {15,30,60,120,240,480,960,1920,3840,7680,15360,30720,61440,122880,245760,491520}

    Returns: 983040

  36. {27,54,108,216,432,864,1728,3456,6912,13824,27648,55296,110592,221184,442368}

    Returns: 884736

  37. {21,42,84,168,336,672,1344,2688,5376,10752,21504,43008,86016,172032,344064}

    Returns: 688128

  38. {22,220,2200,22000}

    Returns: 220000

  39. {27,54,108,216,432,864,1728,3456,6912,13824,27648,55296,110592,221184,442368}

    Returns: 884736

  40. {10,20,40,80,160,320,640,1280,2560,5120,10240,20480,40960,81920,163840,327680}

    Returns: 655360

  41. {12,24,48,96,192,384,768,1536,3072,6144,12288,24576,49152,98304,196608,393216}

    Returns: 786432

  42. {35,245,1715,12005,84035}

    Returns: 588245

  43. {5,10,20,40,80,160,320,640,1280,2560,5120,10240,20480,40960,81920,163840,327680}

    Returns: 655360

  44. {28,56,112,224,448,896,1792,3584,7168,14336,28672,57344,114688,229376,458752}

    Returns: 917504

  45. {1,1000,1000000}

    Returns: 1000000000

  46. {250000, 500000, 1000000}

    Returns: 2000000

  47. {250000, 500000, 1000000 }

    Returns: 2000000

  48. {2, 3, 4 }

    Returns: 5

  49. {3, 4, 5 }

    Returns: 6

  50. {4, 6, 8 }

    Returns: 10

  51. {2, 4, 6 }

    Returns: 8

  52. {1, 2, 4 }

    Returns: 8

  53. {10000, 100000, 1000000 }

    Returns: 10000000

  54. {1, 5, 9, 13 }

    Returns: 17

  55. {100000, 200000, 400000 }

    Returns: 800000

  56. {2, 3, 4, 5 }

    Returns: 6


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: