1656B - Subtract Operation - CodeForces Solution


data structures math two pointers

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase
 
BUFSIZE = 8192
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()
 
	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()
 
	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def inpa(): return list(map(int, input().split()))
def outa(var): print(' '.join(map(str, var)))

def redirect_io():
    import pathlib, sys
    fname = pathlib.Path(__file__).parent/"input.txt"
    sys.stdin = open(fname, 'r')

def solve(n, k, arr):
    arr.sort()
    l, r = 0, 1
    while r < n:
        while (r < n and arr[r] - arr[l] < k):
            r += 1
        if r == n:
            break
        if arr[r] - arr[l] == k:
            return "YES"
        l += 1

    return "NO"

ans = []
for _ in range(int(input())):
    n, k = inpa()
    arr = inpa()
    ans.append(solve(n, k, arr))
print('\n'.join(ans))

C++ Code:

#include<bits/stdc++.h>
#include<string>
#include<vector>
#define mod 1000000007
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int t;
	cin >> t;
	while(t--)
	{
		int n, k, key;
		cin >> n >> k;
        int i = 0, s = 0;
		int a[n];
		for(i=0; i<n; i++)
		{
			cin >> a[i];
		}
		sort(a, a+n);
	    bool flag = false;
		for(i=0; i<n; i++)
		{
			key = a[i]+k;
			flag = binary_search(a+i+1, a+n, key);
			if(flag) break;
		}
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


Comments

Submit
0 Comments
More Questions

Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time