Saturday 12 August 2023

Video Solution for Codeforces Round 892 DIV 2 - Problem A in #JAVA 


Hello fellow programmers,

For those of you who participated in Codeforces Round 892 DIV 2 and were working on Problem A, here is the solution in JAVA


import java.util.*;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); //FOR NUMBER OF TEST CASES

        for (int i = 0; i < t; i++) {
            int n = sc.nextInt(); // length of the array a
            int[] a = new int[n];
            
            // Input the array a
            for (int j = 0; j < n; j++) {
                a[j] = sc.nextInt();
            }
            
            Arrays.sort(a); //SORT THE ARRAY SO THAT IT CAN BE DIVIDED INTO b and c
            boolean flag=false; //TAKING variable flag for output purpose
            for(int k=0 ;  k<n-1 && !flag;k++) // and flag is important 
            {
                if(a[k] != a[k+1])
                {
                    flag=true;
                
             
            System.out.println(k+1 + " " + (n-k-1)); 
            for(int m=0;m<=k;m++)
            {
                System.out.print(a[m]+" ");
            }
            System.out.println();
            for(int p=k+1;p<n;p++)
            {
                System.out.print(a[p]+" ");
            }
                 }
                   
            } //loop is over here
            if(!flag) //otherwise -1 according to the question
                 {
                    System.out.println(-1);
                 }
        }
        sc.close();
    }
}

 
 
 

No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...