ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
            int ar[]=new int[n];
            for(int i=0;i<n;i++)
            {
                ar[i]=sc.nextInt();
            }
            boolean flag=false;
            for(int i=0;i+1<n;i++)
            {
                if(ar[i]<=ar[i+1])
                {
                    ar[i+1]=ar[i+1]-ar[i];
                    ar[i]-=ar[i];
                }
                else
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
            {
                System.out.println("NO");
            }
            else
            {
                System.out.println("YES");
            }
        }
    }
}

Solution 2 (runtime: 655 ms):
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class cf{
    // public static long gcd(long a,long b){
    //     while(a!=0 && b!=0){
    //         if(a<b){
    //             b=b%a;
    //         }else{
    //             a=a%b;
    //         }
    //     }
    //     if(a!=0){
    //         return a;
    //     }else{
    //         return b;
    //     }
    // }  
    // public static long lcm(long a,long b){
    //     return (a/gcd(a,b))*b;
    // }

    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int tsts=sc.nextInt();
        for(int t=0;t<tsts;t++){
            int n=sc.nextInt();
            // int k=sc.nextInt();
            // int a4=sc.nextInt();
            // int a5=sc.nextInt();
            // int r2=sc.nextInt();
            // StringBuilder s=new StringBuilder(sc.next());
            // String a=sc.next();
            // String b=sc.next();
            // String c=sc.next();
            int[] a=new int[n];
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
            }
            // int[] b=new int[n];
            // for(int i=0;i<n;i++){
            //     b[i]=sc.nextInt();
            // }
            // long n=sc.nextLong();
            // long k=sc.nextLong();
            // long r=sc.nextInt();
            // long c=sc.nextInt();
            for(int i=0;i<n-1;i++){
                int temp=Math.min(a[i],a[i+1]);
                a[i]-=temp;
                a[i+1]-=temp;
            }
            int[] b=new int[n];
            for(int i=0;i<n;i++){
                b[i]=a[i];
            }
            Arrays.sort(b);
            boolean flag=true;
            for(int i=0;i<n;i++){
                if(a[i]!=b[i]){
                    flag=false;
                    break;
                }
            }
            if(flag) System.out.println("Yes");
            else System.out.println("No");
        }    
    }
}
Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 280 ms):
import java.io.*;
import java.util.*;

public class TLE {
  public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static FastReader in = new FastReader();

  public static void main(String[] args) throws Exception {
    int testCases = in.nextInt();
    while (testCases-- > 0) {
      solve();
    }
    out.close();
  }

  private static void solve() {
    long sum = 0;
    String s = in.next();
    int two = 0, three = 0;
    for (char c : s.toCharArray()) {
      sum += c - '0';
      if (c == '2') {
        two++;
      } else if (c == '3') {
        three++;
      }
    }
    for (int t = 0; t <= two; t++) {
      for (int th = 0; th <= three; th++) {
        if ((sum + 2 * t + th * 6) % 9 == 0) {
          System.out.println("YES");
          return;
        }
      }
    }
    System.out.println("NO");

  }

  static class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
}


Solution 2 (runtime: 328 ms):
import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    int t=sc.nextInt();
	    while(t-->0){
	        String n=sc.next();
	        int s=0;
	        HashMap<Integer,Integer>hm=new HashMap();
	        for(int i=0;i<n.length();i++){
	            s+=n.charAt(i)-'0';
	            if((n.charAt(i)-'0')==3)
	            hm.put(6,hm.getOrDefault(6,0)+1);
	            else if((n.charAt(i)-'0')==2)
	            hm.put(2,hm.getOrDefault(2,0)+1);

	            
	        }

            int f=0;
	        if(s%9==0)
	        		System.out.println("YES");
	        		else{
	        		    for(int i=0;i<=hm.getOrDefault(2,0);i++){
	        		        if(f==1)
	        		        break;
	        		        
	        		        for(int j=0;j<=hm.getOrDefault(6,0);j++){
	        		            if((s+i*2+j*6)%9==0)
	        		            {
	        		                f=1;
	        		                break;
	        		            }
	        		        }
	        		    }
	        		    System.out.println((f==1)?"YES":"NO");
	                    }



	        
	    }
	}
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 640 ms):
import java.util.*;

public class div2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i = 0 ; i<t ; i++){
            int n = sc.nextInt();

            int[] arr = new int[n];
            for(int j = 0 ; j < n ; j++){
                arr[j] = sc.nextInt();
            }

            int ind = 0;
            boolean flag = true;
            while(ind < n-1){
                if(arr[ind] <= arr[ind+1]){
                    arr[ind+1] -= arr[ind];
                    arr[ind] = 0;
                    ind++;
                }
                else {
                    flag = false;
                    break;
                }
            }
            if(!flag) System.out.println("NO");
            else System.out.println("YES");
        }
    }
}


Solution 2 (runtime: 359 ms):




import java.io.*;

import java.util.*;



public class Main{

	static StreamTokenizer cins=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

	static PrintWriter cout=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

	static int cin()throws Exception {

		cins.nextToken();

		return (int)cins.nval;

	}

	static void solve() throws Exception{

		int n=cin();

		int a[]=new int[n+1];

		for(int i=1;i<=n;i++) {

			a[i]=cin();

		}

		

		for(int i=1;i+1<=n;i++) {

			int now=Math.min(a[i],a[i+1]);

			a[i]-=now;

			a[i+1]-=now;

			if(a[i]>a[i+1]) {

				cout.println("no");

				cout.flush();

				return;

			}

		}

		cout.println("Yes");

		cout.flush();

	}

	public static void main(String[] args)throws Exception{

		int t=cin();

		while(t--!=0)solve();

	}

}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 234 ms):
import java.util.*;

public class DigitalStringMaximization{

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] result = new String[n];
		for(int i = 0; i < n; i++){
			result[i]= maximize(sc.next());
		}
		Arrays.stream(result).forEach(System.out::println);
	}
	
	private static String maximize(String str){
		char[] chars = str.toCharArray();
		int num;
		for(int i = 1; i < chars.length; i++){
			num = chars[i] - '0';
			for(int j = num; j >= 0; j--){
				if( i - j < 0) continue;
				if(chars[ i - j ] < (char)(chars[i] - j)){
					char temp = (char)(chars[i] - j);
					for(int k = i; k > i - j; k--){
						chars[k] = chars[k - 1];
					}
					chars[i - j] = temp;
					break;
				}
			}
		} 
		return new String(chars);
	}
	
	
}

/*
i = 1 51476
i = 2 53176
i = 3 53616 55316
I = 4 55316
i = 5 55351 55431
19


*/


Solution 2 (runtime: 343 ms):
import java.util.*;



public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();



while (t-- > 0) {

char[] sb = sc.next().toCharArray(); // Using char array for efficiency

int n = sb.length;



int index = 1;

while (index < n) {

if (sb[index] != '0') {

int toPlace = index - 1;

int subtract = 1;



while (toPlace >= 0 && (sb[index] - '0') - subtract > (sb[toPlace] - '0')) {

if (toPlace == 0) break;

toPlace--;

subtract++;

}



if ((sb[index] - '0') - subtract <= (sb[toPlace] - '0')) {

toPlace++;

subtract--;

}



if (toPlace != index) {

char res = (char) ((sb[index] - '0' - subtract) + '0');



// Shift elements manually instead of using `add()` and `remove()`

for (int j = index; j > toPlace; j--) {

sb[j] = sb[j - 1];

}

sb[toPlace] = res;

}

}

index++;

}



System.out.println(new String(sb)); // Fast output

}

}

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 375 ms):
import java.util.*;
import java.io.*;

public class Main {
	static InputStream inputStream;
	static OutputStream outputStream;
	static InputReader in;
	static PrintWriter out;
	static int MULTIPLE_TESTACES = 1;
  
	///////////////////////////////////////
	// solution
	///////////////////////////////////////
	static class Solution {
    
    private boolean pos(ArrayList<Long> o){
			int n = o.size();
			ArrayList<Long> a = new ArrayList<Long>();
			for(Long x: o){
				a.add(x);
			}
			for(int i=0; i<n-1;++i){
				long x = a.get(i);
				long y = a.get(i+1);
				long m = Math.min(x, y);
				a.set(i, x-m);
				a.set(i+1, y-m);
			}
			for(int i=0; i<n-1;++i){
				long x = a.get(i);
				long y = a.get(i+1);
				if(x > y)return false;
			}
			return true;
		}
		
		public void solve(int tc) throws IOException{
			int n = in.nextInt();
			ArrayList<Long> a = new ArrayList<Long>();
			for(int i=0; i<n;++i){
				a.add(in.nextLong());
			}
			boolean ans = false;
			ans |= pos(a);
			out.println(ans ? "YES":"NO");
		}	
	}
	
	
	
	
	
	///////////////////////////////////////
	// template
	///////////////////////////////////////
	public static void main(String[] args) throws IOException{
			inputStream = System.in;
			outputStream = System.out;
			in = new InputReader(inputStream);
			out = new PrintWriter(outputStream);
			int tc = 1;
			if(MULTIPLE_TESTACES>0)tc = in.nextInt();
			Solution sol = new Solution();
			for(int i=1; i<=tc;++i){
				 sol.solve(i);
			}
			in.close();
			out.close();
	}
	
	static class InputReader {
		BufferedReader reader;
		StringTokenizer st;
		public InputReader(InputStream in){
			reader = new BufferedReader(new InputStreamReader(in));
			st = null;
		}
		
		public String next() throws IOException{
			while(st == null || !st.hasMoreTokens()){
				st = new StringTokenizer(reader.readLine());
			}
			return st.nextToken();
		}
		
		public Long nextLong() throws IOException{
			return Long.parseLong(next());
		}
		
		public Integer nextInt() throws IOException{
			return Integer.parseInt(next());
		}
		
		public void close() throws IOException{
			reader.close();
		}
	}
}


Solution 2 (runtime: 468 ms):
import java.util.*;

public class Solution {

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

int cases = scn.nextInt();

while (cases > 0) {

int n = scn.nextInt();

int[] arr = new int[n];

for(int i=0;i<n;i++){

arr[i] = scn.nextInt();

}

for(int i=0;i<n-1;i++){

int cur = arr[i];

int next= arr[i+1];

arr[i]-=Math.min(cur,next);

arr[i+1]-=Math.min(cur,next);

}

boolean flag=true;

for(int i=0;i<n-1;i++){

if(arr[i]>arr[i+1]) {

flag=false;

break;

}

}

if(flag) System.out.println("YES");

else System.out.println("NO");

cases--;

}

}

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 639 ms):
import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner s=new Scanner(System.in);
      int t=s.nextInt();
      while(t-->0){
        int n=s.nextInt();
        int arr[]=new int[n];
        for(int i=0;i<n;i++){
          arr[i]=s.nextInt();
        }
        for(int i=1;i<n;i++){
          int a=Math.min(arr[i],arr[i-1]);
          arr[i-1]-=a;
          arr[i]-=a;
        }
        boolean ans=true;
        for(int i=0;i<n-1;i++){
          if(arr[i]!=0){
            ans=false;
            break;
          }
        }
        if(ans){
          System.out.println("YES");
        }else{
          System.out.println("NO");
        }
      }
  }
}

Solution 2 (runtime: 640 ms):
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        outer :
        while(t>0){
            int n = sc.nextInt();

            int[] nums = new int[n];

            for(int i=0;i<n;i++){
                nums[i] = sc.nextInt();
            }

            for(int i = 1 ; i < n ; i++){
                int min = Math.min(nums[i],nums[i-1]);
                nums[i] = nums[i] - min;
                nums[i-1] = nums[i-1] - min;

                if(nums[i-1] > nums[i] ) {
                    System.out.println("NO");
                    t--;
                    continue outer;
                }
            }
            System.out.println("YES");
            t--;
        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
            int ar[]=new int[n];
            for(int i=0;i<n;i++)
            {
                ar[i]=sc.nextInt();
            }
            boolean flag=false;
            for(int i=0;i+1<n;i++)
            {
                if(ar[i]<=ar[i+1])
                {
                    ar[i+1]=ar[i+1]-ar[i];
                    ar[i]-=ar[i];
                }
                else
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
            {
                System.out.println("NO");
            }
            else
            {
                System.out.println("YES");
            }
        }
    }
}

Solution 2 (runtime: 483 ms):
import java.util.*;

public class question {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();

            int[] arr = new int[n];

            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }

            int help = 0 ;

            for(int i = 0 ; i < n-1 ; i++){
                int a = arr[i];
                int b = arr[i+1];

                if(a > b){
                    System.out.println("NO");
                    help++;
                    break;
                }else{

                    arr[i+1] = arr[i+1] - arr[i];
                    arr[i] = 0 ;
                    //System.out.println(arr[i+1]);
                }
            }


            if(help == 0){
                System.out.println("YES");
            }

        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 765 ms):


import java.util.Deque;

import java.util.LinkedList;

import java.util.Scanner;

import javax.print.DocPrintJob;













public class SumTo {

    private static boolean  Solution(int[]a,int n){

        Deque<Integer>dq=new LinkedList<>();

        for(int num:a){

            dq.add(num);

        }

        while(dq.size()>1){

            if(((LinkedList<Integer>) dq).get(0)>((LinkedList<Integer>) dq).get(1)){

                return false;

            }

            else if(((LinkedList<Integer>) dq).get(0)<((LinkedList<Integer>) dq).get(1)){

                int first = dq.removeFirst();

                int second = dq.removeFirst();

                dq.addFirst(second - first);



            }else{

                dq.pollFirst();

                dq.pollFirst();

            }

        }

        return true;

        

    }

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        int t=sc.nextInt();

        while(t-->0){

            int n=sc.nextInt();

            int[]a=new int[n];

            for(int i=0;i<n;i++){

                a[i]=sc.nextInt();

            }

            System.out.println(Solution(a,n)?"Yes":"NO");

        }

    }

}



Solution 2 (runtime: 656 ms):
import java.util.*;

public class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

int tests = scanner.nextInt();

while(tests-- > 0){

int n = scanner.nextInt();

int A[] = new int[n];

boolean flag = false;

for(int i = 0; i < n; i++){

A[i] = scanner.nextInt();

}

for(int i = 0; i < n - 1; i++){

if(A[i] > A[i + 1]){

flag = true;

break;

}else{

int temp = A[i];

A[i] -= temp;

A[i + 1] -= temp;

}

}

if(!flag){

System.out.println("YES");

}else{

System.out.println("NO");

}

}

scanner.close();

}



}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 343 ms):
import java.util.*;



public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();



while (t-- > 0) {

char[] sb = sc.next().toCharArray(); // Using char array for efficiency

int n = sb.length;



int index = 1;

while (index < n) {

if (sb[index] != '0') {

int toPlace = index - 1;

int subtract = 1;



while (toPlace >= 0 && (sb[index] - '0') - subtract > (sb[toPlace] - '0')) {

if (toPlace == 0) break;

toPlace--;

subtract++;

}



if ((sb[index] - '0') - subtract <= (sb[toPlace] - '0')) {

toPlace++;

subtract--;

}



if (toPlace != index) {

char res = (char) ((sb[index] - '0' - subtract) + '0');



// Shift elements manually instead of using `add()` and `remove()`

for (int j = index; j > toPlace; j--) {

sb[j] = sb[j - 1];

}

sb[toPlace] = res;

}

}

index++;

}



System.out.println(new String(sb)); // Fast output

}

}

}



Solution 2 (runtime: 296 ms):


import java.util.*;

import java.io.*;



public class DigitalStringMax {



public static void main(String args[]) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

PrintWriter pw = new PrintWriter(System.out);



int tt = Integer.parseInt(br.readLine());

while(tt-- > 0) {



char ch[] = br.readLine().toCharArray();



int n = ch.length;

for(int i = 0; i < n - 1; i++){

int max = -1;

int ind = -1;

for(int j = i;j < Math.min(n,i+9); j++){



if(max < (ch[j] - (j - i))){

max = (ch[j] - (j - i));

ind = j;

}



}



ch[ind] = (char)(ch[ind]  - (ind - i ));

for(int j = ind; j > i; j--){

char temp = ch[j];



ch[j] = ch[j-1];

ch[j-1] = temp;



}

}



for(int i = 0; i < n; i++){

pw.print(ch[i]);

}

pw.println();



}

pw.flush();

}

}
Problem: 2057C - Trip to the Olympiad
Language: java

Solution 1 (runtime: 249 ms):
import java.util.*;
public class triptotheolympiad {
    public static void main(String[] arg){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int testCase = 0; testCase < t; testCase++){
            long l = sc.nextLong();
            long r = sc.nextLong();

            String lBinary = Long.toBinaryString(l);
            String rBinary = Long.toBinaryString(r);

            int bit = -1;
            if(lBinary.length() < rBinary.length()){
                bit = rBinary.length() - 1;
            }else{
                //System.out.println(lBinary + " " + rBinary);
                for(int i = 0; i < rBinary.length(); i++){
                    if(lBinary.charAt(i) == rBinary.charAt(i)){
                        continue;
                    }else{
                        bit = rBinary.length() - i - 1;
                        break;
                    }
                }
            }

            long val = (long) Math.pow(2, bit);
            long firstNumber = (r / val) * val;

            long secondNumber = firstNumber - 1;
            long thirdNumber = 0;
            if(firstNumber == r){
                thirdNumber = r - 2;
            }else{
                thirdNumber = r;
            }

            System.out.println(firstNumber + " " + secondNumber + " " + thirdNumber);
        }

    }
}


Solution 2 (runtime: 281 ms):
import java.util.*;
public class ProblemC {

    static long mod = (long)(1e9+7);

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0) {
            int l = sc.nextInt();
            int r = sc.nextInt();
       
            int num = 0;
            for(int i =30;i>=0;i--) {
                if(((l&(1<<i))>0 && (r&(1<<i))>0)) {
                    num|=(1<<i);
                } else if(((l&(1<<i))==0 && (r&(1<<i))==0)) {
                    continue;
                } else {
                    num|=(1<<i);
                    break;
                }
            }

            int a = num;
            int b = num-1;
            int c = (l==b) ? r : l;
            System.out.println(a+" "+b+" "+c);
        }
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 640 ms):
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        outer :
        while(t>0){
            int n = sc.nextInt();

            int[] nums = new int[n];

            for(int i=0;i<n;i++){
                nums[i] = sc.nextInt();
            }

            for(int i = 1 ; i < n ; i++){
                int min = Math.min(nums[i],nums[i-1]);
                nums[i] = nums[i] - min;
                nums[i-1] = nums[i-1] - min;

                if(nums[i-1] > nums[i] ) {
                    System.out.println("NO");
                    t--;
                    continue outer;
                }
            }
            System.out.println("YES");
            t--;
        }
    }
}

Solution 2 (runtime: 687 ms):


import java.util.*;



public class AS {



    public static void main(String[] args) {



        Scanner s = new Scanner(System.in);



        int t = s.nextInt();

        s.nextLine();



        for (int i = 0; i < t; i++) {



            int c = s.nextInt();

            s.nextLine();



            int[] arr = new int[c];



            for (int j = 0; j < c; j++) {



                arr[j] = s.nextInt();

            }

            s.nextLine();



            boolean flag = true;

            for (int j = 0; j < c - 1; j++) {



                int temp = Math.min(arr[j], arr[j + 1]);

                arr[j] -= temp;

                arr[j + 1] -= temp;



                if (arr[j] > 0) {

                    flag = false;

                    break;

                }

            }



            if (flag)

                System.out.println("yes");

            else

                System.out.println("no");



        }



        s.close();

    }

}