message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,177
12
196,354
Tags: greedy, math Correct Solution: ``` # Author : nitish420 -------------------------------------------------------------------- import os import sys from io import BytesIO, IOBase # sys.setrecursionlimit(10**6) from collections import Counter def check(l): prev=l[0] for i in range(1,len(l)): if l[i]==prev: return 0 prev=l[i] return 1 def main(): n=int(input()) a=list(map(int,input().split())) d=Counter(a) k=(n+1)//2 ans=1 for item in d.keys(): if d[item]<=k: pass else: ans=0 break if ans: print("YES") else: print("NO") #---------------------------------------------------------------------------------------- def nouse0(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') def nouse1(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') def nouse2(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') # region fastio 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 nouse3(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') def nouse4(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') def nouse5(): # This is to save my code from plag due to use of FAST IO template in it. a=420 b=420 print(f'i am nitish{(a+b)//2}') # endregion if __name__ == '__main__': main() ```
output
1
98,177
12
196,355
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,178
12
196,356
Tags: greedy, math Correct Solution: ``` n = int(input()) l1 = [int(num) for num in input().split(' ')] l2 = list(set(l1)) l3 = [] for i in l2: l3.append(l1.count(i)) if n == 1: print("YES") else: if len(l2) == 1: print("NO") else: if sum(l3) % 2 == 0: if max(l3) <= sum(l3) // 2: print("YES") else: print("NO") else: if max(l3) <= sum(l3) // 2 + 1: print("YES") else: print("NO") ```
output
1
98,178
12
196,357
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` n = int(input()) arr = list(map(int, input().split())) max_ = max(arr, key = arr.count) print("YES" if(arr.count(max_) <= (n + 1) // 2) else "NO") ```
instruction
0
98,179
12
196,358
Yes
output
1
98,179
12
196,359
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` from collections import Counter n = int(input()) stuff = Counter(input().split()) space = (n-1)//2 print ("YNEOS"[stuff.most_common()[0][1]-1 > space::2]) ```
instruction
0
98,180
12
196,360
Yes
output
1
98,180
12
196,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` import math n = int(input()) numbers = [int(i) for i in input().split()] for item in numbers: if numbers.count(item)>math.ceil(len(numbers)/2): print("NO") break else: print("YES") ```
instruction
0
98,181
12
196,362
Yes
output
1
98,181
12
196,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` n=int(input()) a=list(map(int, input().split())) b={} for i in a: if i in b:b[i]+=1 else:b[i]=1 t=0 for i in b: if b[i]>t:t=b[i] if n-t>=t-1:print('YES') else:print('NO') ```
instruction
0
98,182
12
196,364
Yes
output
1
98,182
12
196,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` n = int(input()) arr = list(map(int, input().split())) track = {} for i in arr: if not (i in track.keys()): track[i] = 0 track[i] += 1 counts = [] for i in track.keys(): counts.append(track[i]) counts.sort() counts.reverse() for i in range(len(counts) - 1): curr = counts[i] sep = sum(counts[i + 1: len(counts)]) if(sep < curr - 1): print("NO") break else: if(len(counts) == 1): print("NO") else: print("YES") ```
instruction
0
98,183
12
196,366
No
output
1
98,183
12
196,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` def yaro(n,arr): if n == 1: return "YES" if n == 2: if arr[0] != arr[1]: return "YES" else: return "NO" if n == 3: if arr[0] != arr[1] or arr[1] != arr[2] or arr[0] == arr[2]: return "YES" else: return "NO" l = [0]*1001 limit = n//2 for i in range(n): l[arr[i]] += 1 for i in l: if i > limit: return "NO" return "YES" n = int(input()) arr = map(int,input().split()) yaro(n,arr) ```
instruction
0
98,184
12
196,368
No
output
1
98,184
12
196,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` n = int(input()) ls = list(map(int,input().split())) ans = "" for i in ls: if ls.count(i)*2 <= n: ans = "YES" else: ans = "NO" print(ans) ```
instruction
0
98,185
12
196,370
No
output
1
98,185
12
196,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000) β€” the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. Submitted Solution: ``` b=int(input()) a={int(s) for s in input().split()} if len(a)>=b/2: print('YES') else: print('NO') ```
instruction
0
98,186
12
196,372
No
output
1
98,186
12
196,373
Provide tags and a correct Python 3 solution for this coding contest problem. We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≀ li ≀ ri ≀ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal β€” as "and". Input The first line contains two integers n, m (1 ≀ n ≀ 105, 1 ≀ m ≀ 105) β€” the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≀ li ≀ ri ≀ n, 0 ≀ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≀ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
instruction
0
98,231
12
196,462
Tags: constructive algorithms, data structures, trees Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from math import sqrt, pi, ceil, log, inf, gcd, floor from itertools import combinations, permutations from bisect import * from fractions import Fraction from heapq import * from random import randint def main(): po=[1] for i in range(30): po.append(po[-1]*2) n,m=map(int,input().split()) q=[] b=[[0 for _ in range(30)] for _ in range(n+2)] for i in range(m): l,r,x=map(int,input().split()) q.append((l,r,x)) j=0 while x: if x&1: b[l][j]+=1 b[r+1][j]-=1 x=x>>1 j+=1 for i in range(1,n+1): for j in range(30): b[i][j]+=b[i-1][j] for i in range(1,n+1): for j in range(30): if b[i][j]>=2: b[i][j]=1 b[i][j]+=b[i-1][j] f=1 for i in q: l,r,x=i z=0 for j in range(30): if b[r][j]-b[l-1][j]==(r-l+1): z+=po[j] if z!=x: f=0 break if f: print("YES") for i in range(1,n+1): z=0 for j in range(30): if b[i][j]-b[i-1][j]==1: z+=po[j] print(z,end=" ") else: print("NO") # region fastio 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") if __name__ == "__main__": main() ```
output
1
98,231
12
196,463
Provide tags and a correct Python 3 solution for this coding contest problem. We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≀ li ≀ ri ≀ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal β€” as "and". Input The first line contains two integers n, m (1 ≀ n ≀ 105, 1 ≀ m ≀ 105) β€” the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≀ li ≀ ri ≀ n, 0 ≀ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≀ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
instruction
0
98,232
12
196,464
Tags: constructive algorithms, data structures, trees Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from math import sqrt, pi, ceil, log, inf, gcd, floor from itertools import combinations, permutations from bisect import * from fractions import Fraction from heapq import * from random import randint def main(): po=[1] for i in range(30): po.append(po[-1]*2) n,m=map(int,input().split()) q=[] b=[[0 for _ in range(30)] for _ in range(n+2)] for i in range(m): l,r,x=map(int,input().split()) q.append((l,r,x)) j=0 while x: if x&1: b[l][j]+=1 b[r+1][j]-=1 x=x>>1 j+=1 for i in range(1,n+1): for j in range(30): b[i][j]+=b[i-1][j] for i in range(1,n+1): for j in range(30): if b[i][j]>=2: b[i][j]=1 b[i][j]+=b[i-1][j] f=1 for i in q: l,r,x=i z=0 for j in range(30): if b[r][j]-b[l-1][j]==(r-l+1): z+=po[j] if z!=x: f=0 break if f: print("YES") a=[] for i in range(1,n+1): z=0 for j in range(30): if b[i][j]-b[i-1][j]==1: z+=po[j] a.append(z) print(*a) else: print("NO") # region fastio 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") if __name__ == "__main__": main() ```
output
1
98,232
12
196,465
Provide tags and a correct Python 3 solution for this coding contest problem. We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≀ li ≀ ri ≀ n) meaning that value <image> should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist. Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal β€” as "and". Input The first line contains two integers n, m (1 ≀ n ≀ 105, 1 ≀ m ≀ 105) β€” the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≀ li ≀ ri ≀ n, 0 ≀ qi < 230) describing the i-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≀ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. Examples Input 3 1 1 3 3 Output YES 3 3 3 Input 3 2 1 3 3 1 3 2 Output NO
instruction
0
98,233
12
196,466
Tags: constructive algorithms, data structures, trees Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): n,m = map(int,input().split()) dp = [[0]*30 for _ in range(n+2)] op = [] for _ in range(m): op.append(tuple(map(int,input().split()))) l,r,q = op[-1] mask,cou = 1,29 while mask <= q: if mask&q: dp[l][cou] += 1 dp[r+1][cou] -= 1 cou -= 1 mask <<= 1 ans = [[0]*30 for _ in range(n)] for i in range(30): a = 0 for j in range(n): a += dp[j+1][i] dp[j+1][i] = dp[j][i] if a: ans[j][i] = 1 dp[j+1][i] += 1 for i in op: l,r,q = i mask = 1 for cou in range(29,-1,-1): if not mask&q and dp[r][cou]-dp[l-1][cou] == r-l+1: print('NO') return mask <<= 1 for i in range(n): ans[i] = int(''.join(map(str,ans[i])),2) print('YES') print(*ans) # Fast IO Region 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") if __name__ == "__main__": main() ```
output
1
98,233
12
196,467
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,295
12
196,590
Tags: brute force, math, number theory Correct Solution: ``` _, n=map(int,input().split()) m={} ans=0 for x in map(int, input().split()): ans+=m.get(x^n,0) m[x]=m.get(x,0)+1 print(ans) ```
output
1
98,295
12
196,591
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,296
12
196,592
Tags: brute force, math, number theory Correct Solution: ``` n, x = map(int, input().split()) l, res = [0] * 131073, 0 for a in map(int, input().split()): res, l[a] = res + l[a ^ x], l[a] + 1 print(res) ```
output
1
98,296
12
196,593
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,297
12
196,594
Tags: brute force, math, number theory Correct Solution: ``` a,b = map(int,input().split()) lista = list(map(int,input().split())) n=0 d = dict() for x in lista: # get(a,b) retorna b se n tiver a chave a n += d.get(x^b,0) d[x] = d.get(x,0)+1 print(n) ```
output
1
98,297
12
196,595
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,298
12
196,596
Tags: brute force, math, number theory Correct Solution: ``` n,x = map(int,input().split()) lis = list(map(int,input().split())) ans=0 has=[0]*(1000000) for i in lis: has[i]+=1 ans=0 if x==0: for i in range(1000000): ans+=((has[i])*(has[i]-1))//2 else: for i in range(n): a=lis[i]^x # print(lis[i],a,ans) # print(has[a],has[lis[i]]) ans+=(has[lis[i]]*has[a]) has[lis[i]]=0 has[a]=0 print(ans) ```
output
1
98,298
12
196,597
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,299
12
196,598
Tags: brute force, math, number theory Correct Solution: ``` n, x = map(int, input().split()) arr = list(map(int, input().split())) b = [0] * int(2e5) for i in arr: b[i] += 1 res = 0 for i in arr: r = i ^ x res += b[r] if x == 0: res -= len(arr) print(res // 2) ```
output
1
98,299
12
196,599
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,300
12
196,600
Tags: brute force, math, number theory Correct Solution: ``` a,b = map(int,input().split()) lst = list(map(int,input().split())) n=0 dct = {} for x in lst: n += dct.get(x^b,0) dct[x] = dct.get(x,0)+1 print(n) ```
output
1
98,300
12
196,601
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,301
12
196,602
Tags: brute force, math, number theory Correct Solution: ``` n, x=[int(i) for i in input().split()] a=[int(i) for i in input().split()] ax=[i^x for i in a] a.sort() ax.sort() pos=0 posx=0 c=1 cx=1 total=0 while pos<n and posx<n: if a[pos]==ax[posx]: if pos<n-1 and a[pos+1]==a[pos]: pos+=1 c+=1 elif posx<n-1 and ax[posx+1]==ax[posx]: posx+=1 cx+=1 else: #print(c, cx, pos, posx) total+=c*cx c=1 cx=1 pos+=1 posx+=1 elif a[pos]>ax[posx]: posx+=1 else: pos+=1 #print(total, a, ax) if x>0: print(total//2) else: print((total-n)//2) ```
output
1
98,301
12
196,603
Provide tags and a correct Python 3 solution for this coding contest problem. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
instruction
0
98,302
12
196,604
Tags: brute force, math, number theory Correct Solution: ``` #http://codeforces.com/problemset/problem/742/B #works but is too slow def sum(x): return int(x*(x+1)/2) from collections import Counter from math import factorial answer = 0 seen = {} ri = input() ri = ri.split() n = int(ri[0]) x = int(ri[1]) ri = input() ri = ri.split() d = Counter(ri) for v1 in list(d.keys()): v2 = str(int(v1) ^ x) if v2 in d: if v1 == v2: answer += sum(d[v1] - 1) del d[v1] else: answer += d[v1] * d[v2] del d[v1] del d[v2] print(answer) ```
output
1
98,302
12
196,605
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` n, x = [int(x) for x in input().split()] l = [int(x) for x in input().split()] la = [0]*int(10e5+5) ans = 0 for i in l: ans += la[i ^ x] la[i] += 1 print(ans) ```
instruction
0
98,303
12
196,606
Yes
output
1
98,303
12
196,607
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` n, x = map(int, input().split()) a, s = [0] * 100001, 0 for i in map(int, input().split()): a[i] += 1 for i in range(100001): if not x: s += a[i] * (a[i] - 1) // 2 a[i] = 0 elif x ^ i < 100001: s += a[i] * a[x ^ i] a[i] = a[x ^ i] = 0 print(s) ```
instruction
0
98,304
12
196,608
Yes
output
1
98,304
12
196,609
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` def solve(): n,x=map(int,input().split()) a=list(map(int,input().split())) d={} g=[] cnt=0 for i in a: if(d.get(i)==None): d[i]=1 else: d[i]+=1 for i in a: r=i^x if(d.get(r)!=None and i!=r): cnt+=d[r] elif(d.get(r)!=None and i==r): cnt+=d[r]-1 print(cnt//2) solve() ```
instruction
0
98,305
12
196,610
Yes
output
1
98,305
12
196,611
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` from sys import stdin, stdout import math n,x=map(int,stdin.readline().split()) arr=list(map(int,stdin.readline().split())) d=[0 for i in range(100005)] count=0 for i in range(len(arr)): if(x^arr[i]<100005): count+=d[x^arr[i]] d[arr[i]]+=1 stdout.write(str(count)+"\n") ```
instruction
0
98,306
12
196,612
Yes
output
1
98,306
12
196,613
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` read = lambda: map(int, input().split()) from collections import Counter as C n, x = read() a = list(read()) c = C() for i in a: c[i ^ x] += 1 cnt = 0 for i in a: cnt += c[i ^ x] print(cnt // 2) ```
instruction
0
98,307
12
196,614
No
output
1
98,307
12
196,615
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` a, b = map(int, input().split()) s = list(map(int, input().split())) from collections import Counter s = Counter(s) res = 0 for i in s: x = b^i if x in s: res += s[x]*s[i] print(res//2) ```
instruction
0
98,308
12
196,616
No
output
1
98,308
12
196,617
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` n,x=map(int,input().split()) k=0 l=list(map(int,input().split())) for i in set(l): k+=l.count(x^i) print(k//2) ```
instruction
0
98,309
12
196,618
No
output
1
98,309
12
196,619
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number x, count the number of pairs of indices i, j (1 ≀ i < j ≀ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation). <image> Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers n and x (1 ≀ n ≀ 105, 0 ≀ x ≀ 105) β€” the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 105) β€” the elements of the array. Output Print a single integer: the answer to the problem. Examples Input 2 3 1 2 Output 1 Input 6 1 5 1 2 3 4 1 Output 2 Note In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1. In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>. Submitted Solution: ``` i = input().split(' ') numbers = input().split(' ') def result(numbers,x): result = 0 i = 0 numberset = list(set(map(int,numbers))) while i < len(numberset): if int(numberset[i]) ^ int(x) in numberset: print(numbers.count(str(int(numberset[i]) ^ int(x))),numbers.count(str(numberset[i]))) result += min(numbers.count(str(int(numberset[i]) ^ int(x))),numbers.count(numberset[i])) i += 1 return result//2 print(result(numbers,i[1])) ```
instruction
0
98,310
12
196,620
No
output
1
98,310
12
196,621
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,726
12
197,452
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` tc = int(input()) for ii in range(tc): n = int(input()) ar = list(map(int,input().split())) if n==1: print(-1) continue x = dict() m = float('inf') c = 0 for i in range(n): if ar[i] in x: dif = i-x[ar[i]] m = min(m,dif) c = 1 x[ar[i]] = i else: x[ar[i]] = i if c==0: print(-1) else: print(m+1) ```
output
1
98,726
12
197,453
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,727
12
197,454
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` import math t=int(input()) for _ in range(t): n=int(input()) a=list(map(int,input().split())) d={} ans=math.inf for i in range(n): if a[i] not in d: d[a[i]]=i else: ans=min(ans,i-d[a[i]]+1) d[a[i]]=i if ans==math.inf: print(-1) else: print(ans) ```
output
1
98,727
12
197,455
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,728
12
197,456
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` t = int(input()) while t != 0: n = int(input()) l = list(map(int, input().strip().split())) dic = {} for index, i in enumerate(l): if i not in dic: dic[i] = list() dic[i].append(index) continue dic[i].append(index) mini = 999999999 flag = False for i in dic: lx = list() lx = dic[i] if len(lx) != 1: flag = True for x in range(0, len(lx)-1): mini = min(mini, lx[x+1]-lx[x]) if flag: print(mini+1) else: print(-1) t -= 1 ```
output
1
98,728
12
197,457
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,729
12
197,458
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` t = int(input()) for i in range(t): n = int(input()) nums = list(map(int, input().split())) i = 0 j = 0 min_len = 100010 sub = set() while i < n and j < n: sub_len = len(sub) sub.add(nums[j]) if len(sub) == sub_len: min_len = min(min_len, sub_len + 1) sub.remove(nums[i]) i += 1 else: j += 1 if min_len == 100010: print(-1) else: print(min_len) ```
output
1
98,729
12
197,459
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,730
12
197,460
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) mark = [-1]*(200001) dist = 200001 for i in range(n): if mark[a[i]] == -1: mark[a[i]] = i else: temp = i - mark[a[i]] if temp >= 1: dist = min(temp, dist) mark[a[i]] = i if dist == 200001: print("-1") continue print(dist+1) ```
output
1
98,730
12
197,461
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,731
12
197,462
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` a = int(input()) for i in range(a): b = int(input()) lst = list(map(int, input().split())) c = b+1 d = [-1]*(b+1) for t in range(b): if d[lst[t]] != -1: c = min(c, t-d[lst[t]]+1) d[lst[t]] = t if c > b: c = -1 print(c) ```
output
1
98,731
12
197,463
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,732
12
197,464
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) li = {} for i in range(n): if a[i] not in li: li[a[i]] = [i] else: li[a[i]].append(i) ans = 10**9 for num in li: tmp = li[num] for j in range(len(tmp) - 1): ans = min(ans, tmp[j+1] - tmp[j] + 1) if ans == 10**9: print(-1) else: print(ans) ```
output
1
98,732
12
197,465
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated.
instruction
0
98,733
12
197,466
Tags: greedy, implementation, sortings, strings, two pointers Correct Solution: ``` T = int(input()) results = [] for i in range(T): n = int(input()) vals = input().split() a = [int(val) for val in vals] if len(a) < 2: results.append(-1) else: d = {} minLen = -1 # key: number, val: lastPos for i in range(n): a_i = a[i] if a_i in d: if minLen == -1: minLen = i - d[a_i] + 1 else: minLen = min(minLen, i - d[a_i] + 1) d[a_i] = i results.append(minLen) for result in results: print(result) ```
output
1
98,733
12
197,467
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` for _ in range(int(input())): n=int(input()) d={} a=list(map(int,input().split())) if n==1: print(-1) else: ans=2*(10**5) p=[None]*(n+1) for i in range(1,n+1): if p[a[i-1]]==None: p[a[i-1]]=i else: ans=min(ans,i-p[a[i-1]]+1) p[a[i-1]]=i if ans == 2*(10**5): print(-1) else: print(ans) ```
instruction
0
98,734
12
197,468
Yes
output
1
98,734
12
197,469
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` import sys input = sys.stdin.readline INF = 10**15 Q = int(input()) Query = [] for _ in range(Q): N = int(input()) A = list(map(int, input().split())) Query.append((N, A)) for N, A in Query: ans = INF dic = {} for i, a in enumerate(A): if a in dic: ans = min(ans, i-dic[a]+1) dic[a] = i else: dic[a] = i if ans == INF: ans = -1 print(ans) ```
instruction
0
98,735
12
197,470
Yes
output
1
98,735
12
197,471
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` import sys from collections import defaultdict t = int(input()) for kkk in range(t): n = int(sys.stdin.readline()) a = list(map(int ,sys.stdin.readline().split())) d = defaultdict(int) m = n+1 for i in range(len(a)): if d[a[i]] == 0: d[a[i]] = i+1 else: m = min(m, i - d[a[i]] + 2) d[a[i]] = i+1 if m == n+1: print(-1) else: print(m) ```
instruction
0
98,736
12
197,472
Yes
output
1
98,736
12
197,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` t=int(input()) for tst in range(t): n=int(input()) l=list(map(int,input().split())) mini=float("inf") d={} if len(l)<2: print(-1) else: for i in range(n): if l[i] not in d: d[l[i]]=i #print(d) else: mini=min(i-d[l[i]]+1,mini) d[l[i]]=i #print(d,"555555") if mini!=float("inf"): print(mini) else: print(-1) ```
instruction
0
98,737
12
197,474
Yes
output
1
98,737
12
197,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` for _ in range(int(input())): n = int(input()) a = list(map(int,input().split())) d = dict() dup = False mx = 1 mx_index = -1 if len(a)>=2: for i in range(len(a)): num = a[i] if num not in d: d[num] = [i] else: d[num].append(i) for i in d.keys(): l = len(d[i]) if l>mx: dup = False mx = l mx_index = i elif l==mx: dup = True if dup==True or mx==1: print(-1) else: m = 100000000 for i in range(mx-1): m = min(d[mx_index][i+1]-d[mx_index][i]+1, m) print(m) else: print(-1) ```
instruction
0
98,738
12
197,476
No
output
1
98,738
12
197,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` t=int(input()) for k in range(t): n=int(input()) count=n arr=list(map(int,input().split())) if len(arr)==1: print(-1) elif len(arr)==2: if arr[0]==arr[1]: print(2) else: print(-1) else: i = 0 j = n-1 while i<n-1: if arr[j] == arr[i]: count = min(count, j + 1 - i) j -= 1 else: j -= 1 if j == i: j = n - 1 i += 1 if count<n: print(count) else: print(-1) ```
instruction
0
98,739
12
197,478
No
output
1
98,739
12
197,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` for x in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] b = [0] * (n + 5) if n == 1: print(-1) continue for x in a: b[x] += 1 mx = b.index(max(b)) b.sort() if b[len(b) - 1] == b[len(b) - 2]: print(-1) else: ans = n + 5 j = -1 for i in range(n): if a[i] == mx: if j != -1: ans = min(ans, i - j + 1) j = i print(ans) ```
instruction
0
98,740
12
197,480
No
output
1
98,740
12
197,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not. Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not. You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 ≀ i ≀ j ≀ n. Input The first line contains single integer T (1 ≀ T ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the corresponding values of the array a. It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β‹… 10^5. Output Print T integers β€” one per test case. For each test case print the only integer β€” the length of the shortest dominated subarray, or -1 if there are no such subarrays. Example Input 4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3 Output -1 6 3 2 Note In the first test case, there are no subarrays of length at least 2, so the answer is -1. In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray. In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray. In the fourth test case, all subarrays of length more than one are dominated. Submitted Solution: ``` t = int(input()) for ti in range(t): l=int(input()) arr=[int(i) for i in input().split(" ")] if l==1: print(-1) continue mx=1 tg=0 masterKey=-1 curlen=9999999999 minlen=9999999999 di={} for j in range(len(arr)): if arr[j] in di: di[arr[j]]+=1 if di[arr[j]]>mx: mx=di[arr[j]] masterKey=arr[j] tg=0 elif di[arr[j]]==mx: tg=1 else: di[arr[j]]=1 if tg: print(-1) continue if mx==1: print(-1) continue cd={} cdm=1 masnum=1 for j in arr: if masterKey==j: curlen+=1 if curlen<minlen and cdm<=masnum: minlen=curlen elif masnum<cdm: masnum+=1 else: curlen=1 cd={} cdm=1 else: if j in cd: cd[j]+=1 if cdm<cd[j]: cdm=cd[j] else: cd[j]=1 curlen+=1 #print(di) if minlen>l: print(-1) continue print(minlen) ```
instruction
0
98,741
12
197,482
No
output
1
98,741
12
197,483
Provide tags and a correct Python 3 solution for this coding contest problem. Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Elements of the array are not guaranteed to be different. Output Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any. Examples Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 Note In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9. [11, 4, 0, 6] is also a valid answer.
instruction
0
98,742
12
197,484
Tags: brute force, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) bitFreq = [0] * 32 for i in a : for j in range(0,32): if (i & (1 << j)) > 0 : bitFreq[j] += 1 currBest = 0 currIn = 0 for i in range(len(a)): currScore = 0 for j in range(0,32): if (a[i] & (1 << j)) > 0 and bitFreq[j] == 1: currScore = (currScore | 1 << j) if currScore > currBest: currBest = currScore currIn = i tup = a[0],a[currIn] a[currIn],a[0] = tup print(*a) ```
output
1
98,742
12
197,485
Provide tags and a correct Python 3 solution for this coding contest problem. Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Elements of the array are not guaranteed to be different. Output Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any. Examples Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 Note In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9. [11, 4, 0, 6] is also a valid answer.
instruction
0
98,743
12
197,486
Tags: brute force, greedy, math Correct Solution: ``` from math import ceil def mlt(): return map(int, input().split()) x = int(input()) s = list(mlt()) cur = 1 << 32 fst = -1 while cur: ct = 0 tm = -1 for n in s: if n & cur: ct += 1 tm = n if ct == 1: fst = tm break cur >>= 1 if (fst != -1): print(fst, end=' ') for n in s: if n != fst: print(n, end=' ') ```
output
1
98,743
12
197,487
Provide tags and a correct Python 3 solution for this coding contest problem. Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Elements of the array are not guaranteed to be different. Output Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any. Examples Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 Note In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9. [11, 4, 0, 6] is also a valid answer.
instruction
0
98,744
12
197,488
Tags: brute force, greedy, math Correct Solution: ``` n=int(input()) pw = [2**(30-i) for i in range(31)] def kek(x): global pw #x=int(x) ans=set() mind=0 for i in range(31): pwi=pw[i] if pwi<=x: x-=pwi ans.add(pwi) return ans ra=list(map(int,input().split())) a=list(map(kek,ra)) t=dict([[pw[i],0] for i in range(31)]) for ii in a: for i in ii: t[i]+=1 q=set() meme=1 for i in range(30,-1,-1): if t[pw[i]]>1: q.add(pw[i]) m,mv=-1,-1 for i in a: idf=sum(i.difference(q)) if idf>mv: m,mv=i,idf m=sum(m) print(m,end=' ') nmust=False ra.pop(ra.index(m)) print(' '.join(map(str,ra))) ```
output
1
98,744
12
197,489
Provide tags and a correct Python 3 solution for this coding contest problem. Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Elements of the array are not guaranteed to be different. Output Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any. Examples Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 Note In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9. [11, 4, 0, 6] is also a valid answer.
instruction
0
98,745
12
197,490
Tags: brute force, greedy, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) ans=[] for i in range(32,-1,-1): flag=-1 badflag=0 for j in range(n): if a[j]&1<<i: if flag==-1: flag=j else: badflag=1 if badflag==1: break if flag!=-1 and badflag==0: ans.append(str(a[flag])) for j in range(n): if j!=flag: ans.append(str(a[j])) break if len(ans)==0: for i in range(n): ans.append(str(a[i])) print(' '.join(ans)) ```
output
1
98,745
12
197,491
Provide tags and a correct Python 3 solution for this coding contest problem. Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9). Elements of the array are not guaranteed to be different. Output Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any. Examples Input 4 4 0 11 6 Output 11 6 4 0 Input 1 13 Output 13 Note In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9. [11, 4, 0, 6] is also a valid answer.
instruction
0
98,746
12
197,492
Tags: brute force, greedy, math Correct Solution: ``` from sys import stdin from bisect import bisect_left as bl from bisect import bisect_right as br def input(): return stdin.readline()[:-1] def intput(): return int(input()) def sinput(): return input().split() def intsput(): return map(int, sinput()) class RangedList: def __init__(self, start, stop, val=0): self.shift = 0 - start self.start = start self.stop = stop self.list = [val] * (stop - start) def __setitem__(self, key, value): self.list[key + self.shift] = value def __getitem__(self, key): return self.list[key + self.shift] def __repr__(self): return str(self.list) def __iter__(self): return iter(self.list) # Code n = intput() originals = list(intsput()) flips = [~x for x in originals] forward = [2**100 - 1] backward = [2 ** 100 - 1] for i in range(len(flips)): forward.append(forward[-1] & flips[i]) for i in range(len(flips) - 1, -1, -1): backward.append(backward[-1] & flips[i]) best = -1 first = None for i in range(len(originals)): trial = forward[i] & originals[i] & backward[len(originals) - 1 - i] if trial > best: best = trial first = originals[i] originals.remove(first) originals = [first] + originals print(*originals) ```
output
1
98,746
12
197,493