C# - O(NM) Using HashSet to check AP with brief explanation
We can find first two elements of arithmetic sequence by considering first and second smallest elements. The diff between them gives common difference between t
We can find first two elements of arithmetic sequence by considering first and second smallest elements. The diff between them gives common difference between terms.\n\nThen we add all elements in HashSet and check if element + diff is already present in HashSet except for the last element which would be largest element in sequence. \n\nWe need to handle the case when diff = 0. In this case, all elements must be same so HashSet Count should be 1.\nIf diff != 0 then there should not be any other duplicate elements, HashSet Count should be length end - start + 1.\n\n```csharp\npublic IList<bool> CheckArithmeticSubarrays(int[] nums, int[] l, int[] r)\n{\n\tbool[] result = new bool[l.Length];\n\n\tfor (int i = 0; i < l.Length; i++)\n\t{\n\t\tint start = l[i];\n\t\tint end = r[i];\n\n\t\tif (end - start + 1 < 2)\n\t\t{\n\t\t\tresult[i] = false;\n\t\t\tcontinue;\n\t\t}\n\n\t\tint firstSmall = int.MaxValue;\n\t\tint secondSmall = int.MaxValue;\n\t\tint largest = int.MinValue;\n\n\t\tfor (int j = start; j <= end; j++)\n\t\t{\n\t\t\tif (nums[j] < firstSmall)\n\t\t\t{\n\t\t\t\tsecondSmall = firstSmall;\n\t\t\t\tfirstSmall = nums[j];\n\t\t\t}\n\n\t\t\telse if (nums[j] < secondSmall)\n\t\t\t{\n\t\t\t\tsecondSmall = nums[j];\n\t\t\t}\n\n\t\t\tlargest = Math.Max(largest, nums[j]);\n\t\t}\n\n\t\tHashSet<int> set = new HashSet<int>();\n\t\tfor (int j = start; j <= end; j++)\n\t\t{\n\t\t\tset.Add(nums[j]);\n\t\t}\n\n\t\t// All elements have equal value\n\t\tif(firstSmall == secondSmall && set.Count != 1)\n\t\t{\n\t\t\tresult[i] = false;\n\t\t\tcontinue;   \n\t\t}\n\n\t\t// check if there are any duplicates\n\t\tif(firstSmall != secondSmall && set.Count != (end - start + 1))\n\t\t{\n\t\t\tresult[i] = false;\n\t\t\tcontinue;                       \n\t\t}\n\n\t\tint diff = secondSmall - firstSmall;\n\t\tbool isValid = true;\n\t\tfor (int j = start; j <= end; j++)\n\t\t{\n\t\t\tif (nums[j] != largest && !set.Contains(nums[j] + diff))\n\t\t\t{\n\t\t\t\tisValid = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tresult[i] = isValid;\n\t}\n\n\treturn result;\n}\n```
[]