problem from contest

Zaikia and sticks Problem Code: CKOJ20A   

Zaikia has N sticks of distinct positive lengths A1,A2,,AN. For no good reason at all, he wants to know if there is a triplet of sticks which when connected end-to-end will form a non-trivial triangle. Here non-trivial refers to a triangle with positive area.

Help Zaikia know if such a triplet exists or not. If such a triplet exists, help him find the lexicographically largest applicable triplet.


Input

  • The first line contains an integer N.
  • The second line contains N space-seperated integers A1,A2,,AN.

Output

  • In the first line print YES if a triplet exists or NO if it doesn't.
  • If such a triplet exists, then in the second line print the lexicographically largest applicable triplet.

Constraints

  • 3N2×105
  • 1Ai109 for each valid i

Sample Input 1

5
4 2 10 3 5

Sample Output 1

YES
5 4 3

Explanation 1

There are three unordered triplets of sticks which can be used to create a triangle:

  • 4,2,3
  • 4,2,5
  • 4,3,5

Arranging them in lexicographically largest fashion

  • 4,3,2
  • 5,4,2
  • 5,4,3

Here 5,4,3 is the lexicographically largest so it is the triplet which dristiron wants


Sample Input 2

5
1 2 4 8 16

Sample Output 2

NO

Explanation 2

There are no triplets of sticks here that can be used to create a triangle.

1

#include <bits/stdc++.h>

using namespace std;

const int N = 200010;

int n, a[N];

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  sort(a + 1, a + n + 1);
  for (int i = n; i >= 3; --i) {
    if (a[i - 1] + a[i - 2] > a[i]) {
      puts("YES");
      printf("%d %d %d\n", a[i], a[i - 1], a[i - 2]);
      return 0;
    }
  }
  puts("NO");
  return 0;
}

2.


n = int(input())
a = list(map(int,input().split()))
a.sort()
i=n-3
ans = []
tmp=0
while(i>=0):
    if a[i]+a[i+1]>a[i+2]:
        print("YES")
        print(a[i+2],a[i+1],a[i])
        tmp=1
        break
    i-=1
if tmp==0:
    print("NO")

# sabse pahle arr ko sort kr diya aur last se check krna shuru kiya ,
kyu ki arr sort hai jo sabse pahle condtion satisfied krega wahi ans hoga
kuchh nhi milega to 'NO' print kr diya
(sare triplet nikalne ki jarurat nhi hai )






               Even-tual Reduction Problem Code: EVENTUAL 

Read problems statements in Hindi, Mandarin Chinese, Russian, Vietnamese, and Bengali as well.

You are given a string

with length . You may perform the following operation any number of times: choose a non-empty substring of (possibly the whole string ) such that each character occurs an even number of times in this substring and erase this substring from . (The parts of

before and after the erased substring are concatenated and the next operation is performed on this shorter string.)

For example, from the string "acabbad", we can erase the highlighted substring "abba", since each character occurs an even number of times in this substring. After this operation, the remaining string is "acd".

Is it possible to erase the whole string using one or more operations?

Note: A string

is a substring of a string if can be obtained from

by deleting several (possibly none or all) characters from the beginning and several (possibly none or all) characters from the end.

Input

  • The first line of the input contains a single integer
denoting the number of test cases. The description of
  • test cases follows.
  • The first line of each test case contains a single integer
  • .
  • The second line contains a single string
  • with length
    • .

    Output

    For each test case, print a single line containing the string "YES" if it is possible to erase the whole string or "NO" otherwise (without quotes).

    Constraints

    • contains only lowercase English letters

    Example Input

    4
    6
    cabbac
    7
    acabbad
    18
    fbedfcbdaebaaceeba
    21
    yourcrushlovesyouback
    

    Example Output

    YES
    NO
    YES
    NO
    

    Explanation

    Example case 1: We can perform two operations: erase the substring "abba", which leaves us with the string "cc", and then erase "cc".

    1.

    for i in range(int(input())):
        n=int(input())
        s=input()
        dict_s={}
        for item in s:
            if (item in dict_s):
                dict_s[item]+=1 
            else:
                dict_s[item]=1
        flag=1
        for key in dict_s:
            if(dict_s[key]%2!=0):
                print('NO')
                flag=0
                break
        if(flag):
            print('YES')
            


    2.

    # cook your dish here
    x=int(input())
    while(x):
        x-=1
        n=int(input())
        s=input()
        s1=set(s) 
        #if n%2!=0:
            #flag="NO"
            #break
        for i in s1:
            if s.count(i)%2!=0:
                flag="NO"
                break
            else:
                flag="YES"
        print(flag)




    Comments

    Popular posts from this blog

    bookmarks

    numpy(Python for data science)

    Rural Development