using
System;
using
System.Collections.Generic;
using
System.Linq;
class
GFG {
static
int
Firstocc(List<Tuple<
int
,
int
> > vec,
int
target)
{
int
l = 0, h = vec.Count - 1, index = -1;
while
(l <= h) {
int
mid = (l + h) / 2;
if
(vec[mid].Item1 == target) {
index = mid;
}
if
(vec[mid].Item1 >= target) {
h = mid - 1;
}
else
{
l = mid + 1;
}
}
return
index;
}
static
int
Lastocc(List<Tuple<
int
,
int
> > vec,
int
target)
{
int
l = 0, h = vec.Count - 1, index = -1;
while
(l <= h) {
int
mid = (l + h) / 2;
if
(vec[mid].Item1 == target) {
index = mid;
}
if
(vec[mid].Item1 <= target) {
l = mid + 1;
}
else
{
h = mid - 1;
}
}
return
index;
}
static
bool
subsetSumNonAdjacent(List<
int
> nums,
int
target)
{
int
n = nums.Count;
List<Tuple<
int
,
int
> > vec
=
new
List<Tuple<
int
,
int
> >();
for
(
int
i = 0; i < n; i++) {
vec.Add(Tuple.Create(nums[i], i));
}
vec.Sort();
for
(
int
i = 0; i < n;
i++) {
int
Newtarget = target - vec[i].Item1;
int
Firstindex = Firstocc(vec, Newtarget);
int
Lastindex = Lastocc(vec, Newtarget);
if
(Firstindex >= 0 && Lastindex >= 0) {
int
pos
= vec[i]
.Item2;
for
(
int
j = Firstindex;
j < 4 && j <= Lastindex; j++) {
int
currpos
= vec[j].Item2;
if
(pos != currpos && pos - 1 != currpos
&& pos + 1 != currpos) {
return
true
;
}
}
}
}
return
false
;
}
static
void
Main(
string
[] args)
{
List<
int
> nums =
new
List<
int
>{ 1, 2, 2, 3 };
int
target = 4;
if
(subsetSumNonAdjacent(nums, target)) {
Console.WriteLine(
"true"
);
}
else
{
Console.WriteLine(
"false"
);
}
}
}