1588A - Two Arrays - CodeForces Solution


graph matchings greedy math sortings *900

Please click on ads to support us..

Python Code:

import sys
from collections import Counter
 
count = 0
lst_input = []
for line in sys.stdin:
    if count == 0:
        count = 1
        continue
    lst_input.append(line)

lst = []
for i in range(0, len(lst_input), 3):
    lst.append(lst_input[i:i+3])

def two_array(a, b):
    a = Counter(a)
    b = Counter(b)

    for k in sorted(b.keys()):
        while b[k]:
            if a[k-1] > 0:
                a[k-1] -= 1
                b[k] -= 1
            elif a[k] > 0:
                a[k] -= 1
                b[k] -=1
            else:
                return False
    return True


for i in lst:
    a = i[1]
    b = i[2]

    a = list(map(int, a.split()))
    b = list(map(int, b.split()))

    a = Counter(a)
    b = Counter(b)

    res = two_array(a, b)

    if res:
        print("YES")
    else:
        print("NO")

C++ Code:

#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
int a[105],b[105];
int main() {
    int T = 1, kase = 0;
    cin >> T;
    while (T--) {
        int n,fl=1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        sort(a+1,a+n+1),sort(b+1,b+n+1);
        for(int i=1;i<=n;i++) if(b[i]-a[i]!=1&&b[i]-a[i]!=0) fl=0;
        puts(fl?"YES":"NO");
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers