content
stringlengths
219
31.2k
complexity
stringclasses
5 values
file_name
stringlengths
6
9
complexity_ranked
float64
0.1
0.9
import java.util.*; public class java{ public static void main(String[]arg) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); String s=sc.next(); boolean f=true; boolean f2=true; boolean f3=true; boolean f4=true; int v=0; for(int i=0;i<s.length()-1;i++) { if(s.charAt(i)==s.charAt(i+1)&&(s.charAt(i)!='?'||s.charAt(i+1)!='?')) { f=false; break; }else { f=true; } } for(int i=0;i<s.length();i++) { if(s.charAt(i)=='?') { if(i==0||i==s.length()-1) { f2=true; v++; }else if(s.charAt(i)==s.charAt(i+1)) { f2=true; v++; } else if(s.charAt(i-1)==s.charAt(i+1)&&i!=0&&i!=s.length()-1) { f2=true; v++; } }else { if(v>0) f2=true; else f2=false; } } if(f&&f2) { System.out.println("YES"); }else { System.out.println("NO"); } } }
n
848.java
0.5
// A O(n) and O(n) extra space Java program to find // longest common subarray of two binary arrays with // same sum class Test { static int arr1[] = new int []{ 0 , 1 , 0 , 1 , 1 , 1 , 1 }; static int arr2[] = new int []{ 1 , 1 , 1 , 1 , 1 , 0 , 1 }; // Returns length of the longest common sum in arr1[] // and arr2[]. Both are of same size n. static int longestCommonSum( int n) { // Initialize result int maxLen = 0 ; // Initialize prefix sums of two arrays int preSum1 = 0 , preSum2 = 0 ; // Create an array to store staring and ending // indexes of all possible diff values. diff[i] // would store starting and ending points for // difference "i-n" int diff[] = new int [ 2 *n+ 1 ]; // Initialize all starting and ending values as -1. for ( int i = 0 ; i < diff.length; i++) { diff[i] = - 1 ; } // Traverse both arrays for ( int i= 0 ; i<n; i++) { // Update prefix sums preSum1 += arr1[i]; preSum2 += arr2[i]; // Comput current diff and index to be used // in diff array. Note that diff can be negative // and can have minimum value as -1. int curr_diff = preSum1 - preSum2; int diffIndex = n + curr_diff; // If current diff is 0, then there are same number // of 1's so far in both arrays, i.e., (i+1) is // maximum length. if (curr_diff == 0 ) maxLen = i+ 1 ; // If current diff is seen first time, then update // starting index of diff. else if ( diff[diffIndex] == - 1 ) diff[diffIndex] = i; // Current diff is already seen else { // Find length of this same sum common span int len = i - diff[diffIndex]; // Update max len if needed if (len > maxLen) maxLen = len; } } return maxLen; } // Driver method to test the above function public static void main(String[] args) { System.out.print( "Length of the longest common span with same sum is " ); System.out.println(longestCommonSum(arr1.length)); } }
n
85.java
0.5
import java.util.*; import java.io.*; public class Waw{ public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = sc.nextLong(); long[] p = new long[n]; p[n-1] = a[n-1]; for(int i=n-2;i>=0;i--){ if(a[i]<p[i+1]) p[i] = p[i+1]-1; else p[i] = a[i]; } long max = p[0]; long res = p[0] - a[0]; for(int i=1;i<n;i++){ if(max < p[i]) max = p[i]; res += max - a[i]; } System.out.println(res); } }
n
851.java
0.5
import java.util.Scanner; public class Codeforces { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, f[], c=0; n = in.nextInt(); f = new int[n]; while (--n>0){ f[in.nextInt()-1] ++; f[in.nextInt()-1]++; in.nextLine(); } for(int i=0; i<f.length; i++) if (f[i] == 1) c++; System.out.println(c); } }
n
854.java
0.5
/* * Created on 17.05.2019 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author Wolfgang Weck */ public class C01Easy { public static void main(String[] args) { try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in))) { final String[] line = r.readLine().split(" "); final int N = Integer.parseInt(line[0]), P = Integer.parseInt(line[1]); final String[] numS = r.readLine().split(" "); if (numS.length != N) throw new IllegalArgumentException(); final int[] n = new int[N]; int sum1 = 0, sum2 = 0; for (int i = 0; i < N; i++) { n[i] = Integer.parseInt(numS[i]) % P; sum2 += n[i]; if (sum2 >= P) sum2 -= P; } int max = sum2; for (int i = 0; i < N; i++) { sum1 += n[i]; if (sum1 >= P) sum1 -= P; sum2 -= n[i]; if (sum2 < 0) sum2 += P; final int s = sum1 + sum2; if (s > max) max = s; } System.out.println(max); } catch (IOException e) { e.printStackTrace(); } } }
n
856.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Code { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); HashMap<Double,Integer>h = new HashMap<>(); double [] temp = new double[n]; int m = 0; for(int i=0;i<n;i++) { String l = br.readLine(); int[] x = new int[4]; int k=0; boolean t = false; for(int j=0;j<l.length();j++) { if(l.charAt(j)=='(' || l.charAt(j)=='+' || l.charAt(j)==')' || l.charAt(j)=='/') x[k++] = j; } double a = Integer.parseInt(l.substring(x[0]+1,x[1])); double b = Integer.parseInt(l.substring(x[1]+1, x[2])); double c = Integer.parseInt(l.substring(x[3]+1)); temp[m++] = (a+b)/c; //System.out.print((a+b)/c + " "); if(h.containsKey((a+b)/c)) h.put((a+b)/c, h.get((a+b)/c)+1); else h.put((a+b)/c, 1); } //System.out.println(h); for(int i=0;i<n;i++) { System.out.print(h.get(temp[i]) + " "); } } }
n
857.java
0.5
import java.io.*; import java.util.*; public class Main { private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); if (n < 6) { out.println(-1); } else { int m = (n - 2); for (int i = 2; i <= m; i++) { out.println("1 " + i); } out.println(m + " " + (m + 1)); out.println(m + " " + (m + 2)); } for (int i = 2; i <= n; i++) { out.println("1 " + i); } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
n
860.java
0.5
import java.util.*; import java.io.*; import java.math.*; public class loser { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class card{ long a; int i; public card(long a,int i) { this.a=a; this.i=i; } } static class sort implements Comparator<pair> { public int compare(pair o1,pair o2) { if(o1.a!=o2.a) return (int)(o1.a-o2.a); else return (int)(o1.b-o2.b); } } static void shuffle(long a[]) { List<Long> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } /*static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); }*/ /*static boolean valid(int i,int j) { if(i<4 && i>=0 && j<4 && j>=0) return true; else return false; }*/ static class pair{ int a,b; public pair(int a,int b) { this.a=a; this.b=b; } } public static void main(String[] args) { InputReader sc=new InputReader(System.in); char c[]=sc.next().toCharArray(); int l=c.length; int a[]=new int[3]; for(int i=0;i<l;i++) { a[c[i]-'a']++; if(i>0 && c[i]<c[i-1]) { System.out.println("NO"); System.exit(0); } } if(a[0]>0 && a[1]>0 && (a[2]==a[1] || a[2]==a[0])) System.out.println("YES"); else System.out.println("NO"); } }
n
861.java
0.5
import java.io.*; import java.util.*; public class LectureSleep { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int n = r.nextInt(); // duration of lecture int k = r.nextInt(); // number of minutes keep mishka awake int[] theorems = new int[n+1]; for(int i = 1; i <= n; i++){ theorems[i] = r.nextInt(); } int[] mishka = new int[n+1]; for(int i = 1; i <= n; i++){ mishka[i] = r.nextInt(); } int[] sums = new int[n+1]; for(int i = 1; i <= n; i++){ if(mishka[i] == 0){ sums[i] = sums[i-1] + theorems[i]; } else{ sums[i] = sums[i-1]; } } int max = 0; for(int i = 1; i <= n-k+1; i++){ int sum = sums[i+k-1] - sums[i-1]; max = Math.max(max, sum); } int totalSum = 0; for(int i = 1; i <= n; i++){ if(mishka[i] == 1){ totalSum += theorems[i]; } } pw.println(totalSum + max); pw.close(); } }
n
864.java
0.5
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Equator { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static StringTokenizer st; public static void main(String[] args) throws IOException { int n = nextInt(); int[] a = intArray(n); long s = 0; for (int x : a) s += x; long m = 0; for (int i = 0; i < n; i++) { m += a[i]; if (m*2 >= s) { System.out.println(i+1); return; } } } public static String nextLine() throws IOException { return in.readLine(); } public static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static int[] intArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] intArray(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } public static long[] longArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
n
867.java
0.5
import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int ans = 0; int t= sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ int nn = sc.nextInt(); ans+=a; if(b<c){ ans += (t-nn) * (c - b); } } System.out.println(ans); } }
n
869.java
0.5
import java.io.*; import java.util.*; import java.util.stream.Collectors; import java.math.*; public class C { public static void main(String[] args) throws IOException { /**/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); /*/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/c.in")))); /**/ int n = sc.nextInt(); int[] counts = new int[60]; ArrayList<ArrayDeque<Long>> nums = new ArrayList<>(); for (int i = 0; i < 60; i++) { nums.add(new ArrayDeque<>()); } for (int i = 0; i < n; i++) { long num = sc.nextLong(); for (int j = 1; j <= 60; ++j) { if (num < (1L<<j)) { nums.get(j-1).add(num); ++counts[j-1]; break; } } } long curr = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { for (int j = 0; j <= 60; ++j) { if (j==60) { System.out.println("No"); return; } if (counts[j]==0||(curr&(1L<<j))!=0) continue; long num = nums.get(j).removeFirst(); --counts[j]; curr ^= num; if (i>0) ans.append(" "); ans.append(num); break; } } System.out.println("Yes"); System.out.println(ans); } }
n
874.java
0.5
import java.util.*; public class Main{ private static final int MAX_SIZE = 100005; public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if(((m + 1) / 60 < a) || ((m + 1) / 60 == a && (m + 1) % 60 <= b)) { out(0, 0); System.exit(0); } for(int i = 2; i <= n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int bb = b + 2 * m + 2; int aa = a + bb / 60; bb %= 60; if((aa < x) || (aa == x && bb <= y)) { b = b + m + 1; a = a + b / 60; b %= 60; out(a, b); System.exit(0); } a = x; b = y; } b = b + m + 1; a = a + b / 60; b = b % 60; out(a, b); } private static void out(int a, int b) { cout(a); cout(" "); cout(b); } private static void cout(Object a) { System.out.print(a); } }
n
875.java
0.5
import java.io.*; import java.util.*; public class Main { public void solve() { int n = ni(); int a = ni(); int b = ni(); long ans = 0; HashMap<Long, Long> m = new HashMap<>(); HashMap<String, Long> s = new HashMap<>(); for (int i = 0; i < n; i++) { ni(); long vx = ni(); long vy = ni(); long v = (long) a * vx - vy; String k = vx + "|" + vy; long cs = s.getOrDefault(k, 0L); long c = m.getOrDefault(v, 0L); ans += c - cs; m.put(v, c + 1); s.put(k, cs + 1); } write (ans * 2 + "\n"); } public static void main(String[] args) { Main m = new Main(); m.solve(); try { m.out.close(); } catch (IOException e) {} } BufferedReader in; BufferedWriter out; StringTokenizer tokenizer; public Main() { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); } public String n() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(in.readLine()); } catch (IOException e) {} } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(n()); } public long nl() { return Long.parseLong(n()); } public void write(String s) { try { out.write(s); } catch (IOException e) {} } }
n
880.java
0.5
import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class Practice { public static void main(String []args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); //System.out.println(s); char c[]=s.toCharArray(); ArrayList a =new ArrayList(); for(int i=0;i<c.length;i++) { //System.out.println(c[i]); a.add(c[i]); } int x=Collections.frequency(a,'0' ); int y=Collections.frequency(a,'1'); //System.out.println(x+ " "+y ); if(y==0 || y==1) { System.out.println(s); } else { if(y>=2) { String s1="1"; for(int i=0;i<x;i++) { s1=s1+"0"; } System.out.println(s1); } } } }
n
881.java
0.5
import java.util.*; import java.io.*; public class TwoGram { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); HashMap <String, Integer> hm = new HashMap<>(); for (int i = 0; i < n - 1; i++) { String curr = s.substring(i, i + 2); if (hm.containsKey(curr)) { hm.put(curr, hm.get(curr) + 1); } else { hm.put(curr, 1); } } String ans = ""; int currMax = 0; for (String twoGram : hm.keySet()) { if (hm.get(twoGram) > currMax) { ans = twoGram; currMax = hm.get(twoGram); } } System.out.println(ans); sc.close(); } }
n
883.java
0.5
import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(bf.readLine()); int[]f=new int[1001]; int[]a=new int[n]; StringTokenizer tk=new StringTokenizer(bf.readLine()); for (int i = 0; i < n; i++) { int element=Integer.parseInt(tk.nextToken()); a[i]=element; f[element]++; } PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); ArrayList<Integer> h=new ArrayList<>(); int counter=0; for (int i = 0; i < n; i++) { if(f[a[i]]==1){counter++; h.add(a[i]);} else{f[a[i]]-=1;} } pw.write(counter+"\n"); for (int i = 0; i < h.size(); i++) { pw.write(h.get(i)+" "); } pw.flush(); } }
n
886.java
0.5
import java.io.*; import java.util.Scanner; public class abc{ public static int check(StringBuilder s) { int countRemove=0; if(!s.toString().contains("xxx")) return countRemove; else{ for(int i=1;i<s.length()-1;i++) { if(s.charAt(i-1)=='x' && s.charAt(i)=='x' && s.charAt(i+1)=='x') { countRemove++; } } return countRemove; } } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //sc= new Scanner(System.in); String s = sc.next(); StringBuilder sb = new StringBuilder(""); sb.append(s); System.out.println(check(sb)); } }
n
887.java
0.5
class MergeArrays { /* Function to move m elements at the end of array mPlusN[] */ void moveToEnd( int mPlusN[], int size) { int i, j = size - 1 ; for (i = size - 1 ; i >= 0 ; i--) { if (mPlusN[i] != - 1 ) { mPlusN[j] = mPlusN[i]; j--; } } } /* Merges array N[] of size n into array mPlusN[] of size m+n*/ void merge( int mPlusN[], int N[], int m, int n) { int i = n; /* Current index of i/p part of mPlusN[]*/ int j = 0 ; /* Current index of N[]*/ int k = 0 ; /* Current index of output mPlusN[]*/ while (k < (m + n)) { /* Take an element from mPlusN[] if a) value of the picked element is smaller and we have not reached end of it b) We have reached end of N[] */ if ((i < (m + n) && mPlusN[i] <= N[j]) || (j == n)) { mPlusN[k] = mPlusN[i]; k++; i++; } else // Otherwise take element from N[] { mPlusN[k] = N[j]; k++; j++; } } } /* Utility that prints out an array on a line */ void printArray( int arr[], int size) { int i; for (i = 0 ; i < size; i++) System.out.print(arr[i] + " " ); System.out.println( "" ); } public static void main(String[] args) { MergeArrays mergearray = new MergeArrays(); /* Initialize arrays */ int mPlusN[] = { 2 , 8 , - 1 , - 1 , - 1 , 13 , - 1 , 15 , 20 }; int N[] = { 5 , 7 , 9 , 25 }; int n = N.length; int m = mPlusN.length - n; /*Move the m elements at the end of mPlusN*/ mergearray.moveToEnd(mPlusN, m + n); /*Merge N[] into mPlusN[] */ mergearray.merge(mPlusN, N, m, n); /* Print the resultant mPlusN */ mergearray.printArray(mPlusN, m + n); } } // This code has been contributed by Mayank Jaiswal
n
89.java
0.5
import java.util.Scanner; public class TreasureHunt { public static String Solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String kuro = sc.nextLine(), shiro = sc.nextLine(), katie = sc.nextLine(); sc.close(); String[] output = {"Kuro", "Shiro", "Katie", "Draw"}; if(n >= kuro.length()) return output[3]; int[] maxArr = new int[3]; int[][] freq = new int[3][58]; for(int i = 0; i < kuro.length(); i++) { maxArr[0] = ++freq[0][kuro.charAt(i) - 65] > maxArr[0]? freq[0][kuro.charAt(i) - 65] : maxArr[0]; maxArr[1] = ++freq[1][shiro.charAt(i) - 65] > maxArr[1]? freq[1][shiro.charAt(i) - 65] : maxArr[1]; maxArr[2] = ++freq[2][katie.charAt(i) - 65] > maxArr[2]? freq[2][katie.charAt(i) - 65] : maxArr[2]; } int winner = 0, max = 0; for(int i = 0; i < 3; i++) { if(kuro.length() - maxArr[i] >= n) maxArr[i] += n; else maxArr[i] = n == 1? kuro.length() - 1: kuro.length(); if(max < maxArr[i]) { winner = i; max = maxArr[i]; } else if(max == maxArr[i]) winner = 3; } return output[winner]; } public static void main(String[] args) { System.out.println(Solve()); } }
n
890.java
0.5
import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class Practice { public static void main(String []args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); int n=0; int m=0; //System.out.println(5%0); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='-') { n++; } else { m++; } } if(m==0) { System.out.println("YES"); } else { if(n%m==0) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
n
892.java
0.5
import java.math.BigInteger; import java.util.Scanner; public class Main { static Scanner sc = new Scanner (System.in); public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); char str[][] = new char[5][n]; for(int i = 0;i < 4;i ++){ for(int j = 0;j < n;j ++) str[i][j] = '.'; } if(k % 2 == 0){ k /= 2; for(int i = 1;i <= 2;i++){ for(int j = 1;j <= k;j++) str[i][j] = '#'; } } else{ str[1][n / 2] = '#'; if(k != 1){ int tmp = n / 2; if(k <= n - 2){ for(int i = 1;i<= (k - 1) / 2;i++){ str[1][i] = '#'; str[1][n - 1 - i] = '#'; } } else{ for(int i = 1;i <= n - 2;i++) str[1][i] = '#'; k -= n - 2; for(int i = 1;i <= k/2;i++){ str[2][i] = '#'; str[2][n - 1 - i]='#'; } } } } System.out.println("YES"); for(int i = 0;i < 4;i ++){ System.out.println(str[i]); } } }
n
893.java
0.5
import java.util.*; public class A { public static int palin(String str) { int flag=0; int l=str.length(); for(int i=0;i<l/2;i++) { if(str.charAt(i)!=str.charAt(l-i-1)) { flag=1; break; } } if(flag==1) return 0; else return 1; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); String str=sc.next(); HashSet<Character> hs=new HashSet<>(); for(int i=0;i<str.length();i++) { hs.add(str.charAt(i)); } if(hs.size()==1) System.out.println(0); else if(palin(str)==0) System.out.println(str.length()); else System.out.println(str.length()-1); } }
n
896.java
0.5
import java.io.*; import java.util.*; public class Main { private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); List<List<Integer>> g = new ArrayList<>(n + 1); for (int i = 0; i < n + 1; i++) { g.add(new LinkedList<>()); } int degree1 = 0, degree2 = 0, root = 0; for (int i = 0; i < n - 1; i++) { int a = in.nextInt(); int b = in.nextInt(); g.get(a).add(b); g.get(b).add(a); if (g.get(a).size() > degree1) { if (a == root) { degree1 = g.get(a).size(); } else { degree2 = degree1; degree1 = g.get(a).size(); root = a; } } else if (g.get(a).size() > degree2) { degree2 = g.get(a).size(); } if (g.get(b).size() > degree1) { if (b == root) { degree1 = g.get(b).size(); } else { degree2 = degree1; degree1 = g.get(b).size(); root = b; } } else if (g.get(b).size() > degree2) { degree2 = g.get(b).size(); } } if (degree2 > 2) { out.print("No"); } else { out.println("Yes"); List<Integer> leaves = new LinkedList<>(); for (int i = 1; i <= n; i++) { if (i != root) { if (g.get(i).size() == 1) { leaves.add(i); } } } out.println(leaves.size()); for (int i : leaves) { out.println(root + " " + i); } } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
n
898.java
0.5
// Java program to Split the array and // add the first part to the end class Geeks { /* Function to reverse arr[] from index start to end*/ static void rvereseArray( int arr[], int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } // Function to print an array static void printArray( int arr[], int size) { for ( int i = 0 ; i < size; i++) System.out.print(arr[i] + " " ); } /* Function to left rotate arr[] of size n by k */ static void splitArr( int arr[], int k, int n) { rvereseArray(arr, 0 , n - 1 ); rvereseArray(arr, 0 , n - k - 1 ); rvereseArray(arr, n - k, n - 1 ); } /* Driver program to test above functions */ public static void main(String args[]) { int arr[] = { 12 , 10 , 5 , 6 , 52 , 36 }; int n = arr.length; int k = 2 ; // Function calling splitArr(arr, k, n); printArray(arr, n); } } // This code is contributed by ankita_saini.
n
9.java
0.5
// Efficient Java program to sort an // array of numbers in range from 1 // to n. import java.io.*; import java.util.*; public class GFG { // function for sort array static void sortit( int []arr, int n) { for ( int i = 0 ; i < n; i++) { arr[i]=i+ 1 ; } } // Driver code public static void main(String args[]) { int []arr = { 10 , 7 , 9 , 2 , 8 , 3 , 5 , 4 , 6 , 1 }; int n = arr.length; // for sort an array sortit(arr, n); // for print all the // element in sorted way for ( int i = 0 ; i < n; i++) System.out.print(arr[i] + " " ); } } // This code is contributed by Manish Shaw // (manishshaw1)
n
90.java
0.5
import java.util.*; public class mohamedy23 { public static void main (String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt();String s=sc.next();int i=s.length()-1; if(n==1) { if(s.charAt(0)=='1') { System.out.print("YES");return; }else { System.out.print("NO");return; } }else if(n==2) { if(s.contains("00")||s.contains("11")) { System.out.print("NO");return; }else { System.out.print("Yes");return; } } else if(s.contains("000")||s.contains("11")) { System.out.print("NO");return; } else if(s.charAt(0)=='0'&&s.charAt(1)=='0'&&s.charAt(2)=='1') { System.out.print("NO");return; } else if(s.charAt(i)=='0'&&s.charAt(i-1)=='0') { System.out.print("NO"); return; } System.out.print("YES"); } }
n
900.java
0.5
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author El-Bishoy */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); D2C982_cut_them_all solver = new D2C982_cut_them_all(); solver.solve(1, in, out); out.close(); } static class D2C982_cut_them_all { int n; ArrayList<Integer>[] adj; int[] sizes = new int[n]; boolean[] visited = new boolean[n]; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); if ((n & 1) == 1) { out.println(-1); return; } sizes = new int[n]; visited = new boolean[n]; adj = new ArrayList[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; adj[u].add(v); adj[v].add(u); } int root = 0; for (int i = 1; i < n; i++) { if (adj[i].size() > adj[root].size()) { root = i; } } dfs(root); int cnt = 0; for (int i = 0; i < n; i++) { if ((sizes[i] & 1) == 0) cnt++; } out.println(cnt - 1); } int dfs(int u) { visited[u] = true; int cnt = 1; for (int w : adj[u]) if (!visited[w]) cnt += dfs(w); sizes[u] = cnt; return cnt; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
n
902.java
0.5
import java.util.Arrays; // Java program to test whether an array // can be sorted by swapping adjacent // elements using boolean array class GFG { // Return true if array can be // sorted otherwise false static boolean sortedAfterSwap( int A[], boolean B[], int n) { int i, j; // Check bool array B and sorts // elements for continuos sequence of 1 for (i = 0 ; i < n - 1 ; i++) { if (B[i]) { j = i; while (B[j]) { j++; } // Sort array A from i to j Arrays.sort(A, i, 1 + j); i = j; } } // Check if array is sorted or not for (i = 0 ; i < n; i++) { if (A[i] != i + 1 ) { return false ; } } return true ; } // Driver program to test sortedAfterSwap() public static void main(String[] args) { int A[] = { 1 , 2 , 5 , 3 , 4 , 6 }; boolean B[] = { false , true , true , true , false }; int n = A.length; if (sortedAfterSwap(A, B, n)) { System.out.println( "A can be sorted" ); } else { System.out.println( "A can not be sorted" ); } } }
n
91.java
0.5
import java.io.*; import java.util.*; public class Main { static StringBuilder data = new StringBuilder(); final static FastReader in = new FastReader(); public static void main(String[] args) { int n = in.nextInt(), k = in.nextInt(), t; int[] a = new int[101]; int answ = 0; for (long i = 0; i < n; i++) { t = in.nextInt(); a[t]++; if (a[t] < 2) { if (answ < k) { data.append(i + 1).append(" "); answ++; } } } if (answ == k) { System.out.println("YES"); System.out.println(data); } else { System.out.println("NO"); } } static void fileOut(String s) { File out = new File("output.txt"); try { FileWriter fw = new FileWriter(out); fw.write(s); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String path) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
n
913.java
0.5
import java.io.*; import java.util.Arrays; public class Main { public static void main(String[] args) { IO io = new IO(); String s=io.nextLine(); if (s.length()<3)io.println("No"); else { int[]b=new int[200]; for (int i=2;i<s.length();i++){ b['.']=b['A']=b['B']=b['C']=0; b[s.charAt(i-2)]=1; b[s.charAt(i-1)]=1; b[s.charAt(i)]=1; if (b['A']+b['B']+b['C']==3){io.println("Yes");return;} } io.println("No"); } } static class IO { BufferedInputStream din; final int BUFFER_SIZE = 1 << 16; byte[] buffer; int byteRead, bufferPoint; StringBuilder builder; PrintWriter pw; public IO() { din = new BufferedInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPoint = byteRead = 0; builder = new StringBuilder(); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( System.out )), true); } int read() { if (bufferPoint >= byteRead) { try { byteRead = din.read(buffer, bufferPoint = 0, BUFFER_SIZE); } catch (IOException e) { e.printStackTrace(); } if (byteRead == -1) buffer[0] = -1; } return buffer[bufferPoint++]; } int peek() { if (byteRead == -1) return -1; if (bufferPoint >= byteRead) { try { byteRead = din.read(buffer, bufferPoint = 0, BUFFER_SIZE); } catch (IOException e) { return -1; } if (byteRead <= 0) return -1; } return buffer[bufferPoint]; } boolean hasNext() { int c = peek(); while (c != -1 && c <= ' ') { read(); c = peek(); } return c != -1; } char nextChar() { int c = read(); while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1) { c = read(); } return (char) c; } double nextDouble() { double ret = 0, div = 1; int c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } String nextLine() { byte[] strBuf = new byte[64]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt == 0) { continue; } else { break; } } if (strBuf.length == cnt) { strBuf = Arrays.copyOf(strBuf, strBuf.length * 2); } strBuf[cnt++] = (byte) c; } return new String(strBuf, 0, cnt); } String next() { byte[] strBuf = new byte[64]; int cnt = 0, c; while ((c = read()) != -1) { if (Character.isWhitespace(c)) { if (cnt == 0) { continue; } else { break; } } if (strBuf.length == cnt) { strBuf = Arrays.copyOf(strBuf, strBuf.length * 2); } strBuf[cnt++] = (byte) c; } return new String(strBuf, 0, cnt); } int nextInt() { int ans = 0; int c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ans = ans * 10 + c - '0'; } while ('0' <= (c = read()) && c <= '9'); bufferPoint--; return neg ? -ans : ans; } long nextLong() { long ans = 0; int c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ans = ans * 10 + c - '0'; } while ('0' <= (c = read()) && c <= '9'); bufferPoint--; return neg ? -ans : ans; } void println(Object o) { pw.println(o); } void print(Object o) { pw.print(o); } void printf(String format, Object... objects) { pw.printf(format, objects); } void close() { pw.close(); } void done(Object o) { print(o); close(); } } }
n
916.java
0.5
import javax.print.DocFlavor; import javax.swing.plaf.basic.BasicInternalFrameTitlePane; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.net.CookieHandler; import java.nio.Buffer; import java.nio.charset.IllegalCharsetNameException; import java.sql.BatchUpdateException; import java.util.*; import java.util.stream.Stream; import java.util.Vector; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import static java.lang.Math.*; import java.util.*; import java.nio.file.StandardOpenOption; import java.util.Comparator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Iterator; import java.util.PriorityQueue; public class icpc { public static void main(String[] args)throws IOException { // Reader in = new Reader(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s1[] = in.readLine().split(" "); int n = Integer.parseInt(s1[0]); int p = Integer.parseInt(s1[1]); String s = in.readLine(); StringBuilder stringBuilder = new StringBuilder(s); boolean flag = false; for(int i=0;i<n;i++) { if(i + p < n) { if(s.charAt(i) != '.' && s.charAt(i + p) != '.' && s.charAt(i) != s.charAt(i + p)) { flag = true; break; } else if(s.charAt(i) == '.' && s.charAt(i + p) != '.') { int x = s.charAt(i + p) - '0'; char ch = (char)((x + 1) % 2 + 48); stringBuilder.setCharAt(i, ch); flag = true; break; } else if(s.charAt(i) != '.' && s.charAt(i + p) == '.') { int x = s.charAt(i) - '0'; char ch = (char)((x + 1) % 2 + 48); stringBuilder.setCharAt(i + p, ch); flag = true; break; } else if(s.charAt(i) == '.' && s.charAt(i + p) == '.') { stringBuilder.setCharAt(i, '1'); stringBuilder.setCharAt(i + p, '0'); flag = true; break; } } } if(flag) { for(int i=0;i<stringBuilder.length();i++) { if(stringBuilder.charAt(i) == '.') { stringBuilder.setCharAt(i, '0'); } } System.out.println(stringBuilder); } else System.out.println("No"); } } class StringAlgorithms { public int[] calculateZ(char input[]) { int Z[] = new int[input.length]; int left = 0; int right = 0; for(int k = 1; k < input.length; k++) { if(k > right) { left = right = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } else { //we are operating inside box int k1 = k - left; //if value does not stretches till right bound then just copy it. if(Z[k1] < right - k + 1) { Z[k] = Z[k1]; } else { //otherwise try to see if there are more matches. left = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } } } return Z; } public ArrayList<Integer> matchPattern(char text[], char pattern[]) { char newString[] = new char[text.length + pattern.length + 1]; int i = 0; for(char ch : pattern) { newString[i] = ch; i++; } newString[i] = '$'; i++; for(char ch : text) { newString[i] = ch; i++; } ArrayList<Integer> result = new ArrayList<>(); int Z[] = calculateZ(newString); for(i = 0; i < Z.length ; i++) { if(Z[i] == pattern.length) { result.add(i - pattern.length - 1); } } return result; } } class BasicFunctions { public long min(long[] A) { long min = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min = Math.min(min, A[i]); } return min; } public long max(long[] A) { long max = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { max = Math.max(max, A[i]); } return max; } } class Name implements Comparable<Name> { int x; int y; public Name(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Name ob) { if(this.x < ob.x) return -1; else if(this.x > ob.x) return 1; return 0; } } class Matrix { long a; long b; long c; long d; public Matrix(long a, long b, long c, long d) { this.a = a; this.b = b; this.c = c; this.d = d; } } class Game implements Comparable<Game> { long x; long y; public Game(long x, long y) { this.x = x; this.y = y; } @Override public int compareTo(Game ob) { if(this.x < ob.x) return -1; else if(this.x > ob.x) return 1; else { if(this.y < ob.y) return -1; else if(this.y > ob.y) return 1; else return 0; } } } class MergeSortInt { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } } class MergeSortLong { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } } class Node { String a; String b; Node(String s1,String s2) { this.a = s1; this.b = s2; } @Override public boolean equals(Object ob) { if(ob == null) return false; if(!(ob instanceof Node)) return false; if(ob == this) return true; Node obj = (Node)ob; if(this.a.equals(obj.a) && this.b.equals(obj.b)) return true; return false; } @Override public int hashCode() { return (int)this.a.length(); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class FenwickTree { public void update(long[] fenwickTree,long delta,int index) { index += 1; while(index < fenwickTree.length) { fenwickTree[index] += delta; index = index + (index & (-index)); } } public long prefixSum(long[] fenwickTree,int index) { long sum = 0L; index += 1; while(index > 0) { sum += fenwickTree[index]; index -= (index & (-index)); } return sum; } } class SegmentTree { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int[] input) { int np2 = nextPowerOfTwo(input.length); int[] segmentTree = new int[np2 * 2 - 1]; for(int i=0;i<segmentTree.length;i++) segmentTree[i] = Integer.MIN_VALUE; constructSegmentTree(segmentTree,input,0,input.length-1,0); return segmentTree; } private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/ 2; constructSegmentTree(segmentTree,input,low,mid,2*pos + 1); constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2); segmentTree[pos] = Math.max(segmentTree[2*pos + 1],segmentTree[2*pos + 2]); } public int rangeMinimumQuery(int []segmentTree,int qlow,int qhigh,int len) { return rangeMinimumQuery(segmentTree,0,len-1,qlow,qhigh,0); } private int rangeMinimumQuery(int segmentTree[],int low,int high,int qlow,int qhigh,int pos) { if(qlow <= low && qhigh >= high){ return segmentTree[pos]; } if(qlow > high || qhigh < low){ return Integer.MIN_VALUE; } int mid = (low+high)/2; return Math.max(rangeMinimumQuery(segmentTree, low, mid, qlow, qhigh, 2 * pos + 1), rangeMinimumQuery(segmentTree, mid + 1, high, qlow, qhigh, 2 * pos + 2)); } }
n
917.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * * @author Haya */ public class CommentaryBoxes { public static void main(String[] args) { FastReader in = new FastReader(); long n = in.nextLong(); long m = in.nextLong(); long a = in.nextLong(); long b = in.nextLong(); long total = 0; long val =(n%m); if (n%m != 0){ long x = (val)*b; long y = (m-val)*a; total = Math.min(x, y); } System.out.println(Math.abs(total)); } public 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 string = ""; try { string = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return string; } } }
n
918.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static java.lang.Math.*; public class Main { private FastScanner scanner = new FastScanner(); public static void main(String[] args) { new Main().solve(); } private void solve() { int n = scanner.nextInt(); Map<Integer, Integer> cnt = new HashMap<>(); for (int i = 0; i < n; i++) { String s = scanner.nextLine(); LinkedList<Character> st = new LinkedList<>(); for (char c : s.toCharArray()) { if (c == ')' && !st.isEmpty() && st.getLast() == '(') { st.pollLast(); continue; } st.addLast(c); } int t = st.size(); Set<Character> set = new HashSet<>(st); if (set.size() > 1) { continue; } if (set.isEmpty()) { cnt.put(0, cnt.getOrDefault(0, 0) + 1); continue; } if (st.getLast() == '(') { cnt.put(t, cnt.getOrDefault(t, 0) + 1); } else { cnt.put(-t, cnt.getOrDefault(-t, 0) + 1); } } long ans = 0; for (int next : cnt.keySet()) { if (next == 0) { ans += (long) cnt.get(next) * (cnt.get(next) - 1) + cnt.get(next); } else if (next > 0) { int t = next * -1; if (cnt.containsKey(t)) { ans += (long) cnt.get(next) * cnt.get(t); } } } System.out.print(ans); } class FastScanner { BufferedReader reader; StringTokenizer tokenizer; FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } Integer[] nextA(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } return a; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } }
n
921.java
0.5
import java.util.HashSet; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); HashSet<Integer> set = new HashSet<>(); for(int i = 0; i<n; i++){ int a = sc.nextInt(); if(a!=0){ set.add(a); } } System.out.println(set.size()); } }
n
926.java
0.5
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Fingerprints { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static StringTokenizer st; public static void main(String[] args) throws IOException { int n = nextInt(); int m = nextInt(); int[] a = intArray(n); Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < m; i++) set.add(nextInt()); String s = ""; for (int i = 0; i < n; i++) if (set.contains(a[i])) s += " " + a[i]; System.out.println(s.length() == 0 ? s : s.substring(1)); } public static String nextLine() throws IOException { return in.readLine(); } public static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static int[] intArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] intArray(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } public static long[] longArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
n
929.java
0.5
import java.io.*; import java.util.*; public class Main { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int best = 1; int bestTime = Integer.MAX_VALUE; for(int i=0;i<n;i++) { int time; int a = sc.nextInt(); time = (a%n==0 || a%n<=i) ? a/n : (a+n)/n; if(time < bestTime) { best = i + 1; bestTime = time; } } pw.println(best); pw.close(); } }
n
935.java
0.5
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); } System.out.println(solve(a)); sc.close(); } static String solve(int[] a) { if (a.length == 1 || (a.length == 2 && a[0] == a[1])) { return "-1"; } int sum = Arrays.stream(a).sum(); for (int i = 0;; i++) { if (a[i] * 2 != sum) { return String.format("1\n%d", i + 1); } } } }
n
938.java
0.5
import java.io.*; import java.util.*; public class Main { static StringBuilder data = new StringBuilder(); final static FastReader in = new FastReader(); public static void main(String[] args) { int n = in.nextInt(), m = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int h=0,t=n-1,answ=0; while (h<n&&t>=0){ if(a[h]<=m){ answ++; h++; }else if(a[t]<=m){ t--; answ++; }else{ break; } } System.out.println(answ); } static void fileOut(String s) { File out = new File("output.txt"); try { FileWriter fw = new FileWriter(out); fw.write(s); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String path) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); } catch (FileNotFoundException e) { e.printStackTrace(); } } 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()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
n
939.java
0.5
import java.util.*; public class Solution{ public static void main(String sp[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String st = sc.next(); char arr[] = st.toCharArray(); boolean b=false; for(char j='a';j<='z';j++){ for(int i=0;i<arr.length;i++){ if(arr[i]==j){ arr[i]='*'; k--; } if(k==0){ b=true; prin(arr); return; } }} } public static void prin(char arr[]){ StringBuilder sb = new StringBuilder(); for(int i=0;i<arr.length;i++){ if(arr[i]!='*') sb.append(arr[i]); } if(sb.length()!=0) System.out.println(sb.toString()); } }
n
940.java
0.5
import java.io.*; import java.util.*; public class D999 { public static void main(String args[])throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int req=n/m; int arr[]=new int[n+1]; int size[]=new int[m]; List<Integer> list[]=new ArrayList[m]; for(int i=0;i<m;i++) { list[i]=new ArrayList<>(); } for(int i=1;i<=n;i++) { arr[i]=sc.nextInt(); size[arr[i]%m]++; list[arr[i]%m].add(i); } long tot=0;int x=0,y=0; List<Integer> idx=new ArrayList<>(); for(int i=0;i < 2*m;i++) { //System.out.println(i+" "+size[i%m]); if(size[i%m]>req) { for(int j=0;j<size[i%m]-req;j++) { idx.add(list[i%m].get(j)); y++; } size[i%m]=req; //System.out.println(i+" "+x+" "+y); } else if(size[i%m]<req) { //System.out.println(idx+" "+i); while(x!=y && size[i%m]<req) { int num=arr[idx.get(x)]; int gg=i-num%m; tot+=gg; arr[idx.get(x)]+=gg; x++; size[i%m]++; } } } System.out.println(tot); for(int i=1;i<=n;i++) { System.out.print(arr[i]+" "); } } }
n
941.java
0.5
import java.util.*; import java.io.*; import java.math.*; public class loser { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class card{ String s; int l; public card(String s,int i) { this.s=s; this.l=i; } } static class sort implements Comparator<card> { public int compare(card o1,card o2) { if(o1.l!=o2.l) return (o1.l-o2.l); else return o1.s.compareTo(o2.s); } } static void shuffle(long a[]) { List<Long> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } /*static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static boolean valid(int i,int j,int r,int c) { if(i<r && i>=0 && j<c && j>=0) return true; else return false; }*/ static class Pair { int a;int b; public Pair(int a,int b) { this.a =a; this.b =b; } } public static void main(String[] args) { InputReader sc=new InputReader(System.in); int n=sc.nextInt(); HashMap<String ,Integer> m=new HashMap<>(); for(int i=0;i<n;i++) { String t=sc.next(); if(m.containsKey(t)) m.put(t,m.get(t)+1); else m.put(t,1); } int ans=0; for(int i=0;i<n;i++) { String t=sc.next(); if(m.containsKey(t) && m.get(t)>0) { m.put(t,m.get(t)-1); ans++; } } System.out.println(n-ans); } }
n
942.java
0.5
import java.util.*; import java.io.*; public class A { public static void main(String ar[]) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s1[]=br.readLine().split(" "); String s2[]=br.readLine().split(" "); int n=Integer.parseInt(s1[0]); int m=Integer.parseInt(s1[1]); int a[]=new int[n]; int b[]=new int[n]; int c[]=new int[n]; int d[]=new int[n]; HashSet<Integer> hs=new HashSet<Integer>(); hs.add(0); hs.add(m); int max=0; for(int i=0;i<n;i++) { a[i]=Integer.parseInt(s2[i]); if(i%2==0) b[i]=1; hs.add(a[i]); } c[0]=a[0]; for(int i=1;i<n;i++) { if(b[i]==0) c[i]=c[i-1]; else c[i]=c[i-1]+a[i]-a[i-1]; } if(b[n-1]==0) d[n-1]=m-a[n-1]; for(int i=n-2;i>=0;i--) { if(b[i]==1) d[i]=d[i+1]; else d[i]=d[i+1]+a[i+1]-a[i]; } max=c[n-1]; if(b[n-1]==0) max+=m-a[n-1]; //System.out.println(max); for(int i=n-1;i>=0;i--) { int u=a[i]-1; int v=a[i]+1; if(!hs.contains(u)) { if(b[i]==0) { int r=1+m-a[i]-d[i]+c[i-1]; max=Math.max(max,r); } else { int l=0; if(i>0) l=a[i-1]; int r=c[i]-1+m-a[i]-d[i]; max=Math.max(max,r); } } if(!hs.contains(v)) { if(b[i]==0) { if(i==n-1) { int r=c[i]+1; max=Math.max(max,r); } else { int r=c[i]+1+m-a[i+1]-d[i+1]; max=Math.max(max,r); } } else { if(i==n-1) { int r=c[i]+m-a[i]-1; max=Math.max(max,r); } else { int r=c[i]+m-a[i+1]-d[i+1]+a[i+1]-1-a[i]; max=Math.max(max,r); } } } } System.out.println(max); } }
n
944.java
0.5
import java.io.PrintWriter; import java.util.Scanner; public class pr902A { static Scanner in = new Scanner(System.in); public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); out.println(solve(n, m)); out.flush(); out.close(); } private static String solve(int n, int m) { int[] grid = new int[m+1]; for (int i = 0; i < n; i++) { int start = in.nextInt(); int end = in.nextInt(); grid[start]++; grid[end]--; } int sum = 0; for(int i = 0; i < m; i++){ sum += grid[i]; if(sum == 0) return "NO"; } return "YES"; } }
n
946.java
0.5
import java.util.*; public class ErrorCorrectSystem { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String a = scan.next(); String b = scan.next(); int[][] mismatch = new int[26][26]; for(int i = 0; i < 26; i++) Arrays.fill(mismatch[i], -1); int[][] pair = new int[2][26]; for(int i = 0; i < 2; i++) Arrays.fill(pair[i], -1); int hd = 0; for(int i = 0; i < n; i++) { if(a.charAt(i) != b.charAt(i)) { hd++; mismatch[a.charAt(i)-'a'][b.charAt(i)-'a'] = i; pair[0][a.charAt(i)-'a'] = i; pair[1][b.charAt(i)-'a'] = i; } } for(int i = 0; i < 26; i++) { for(int j = i+1; j < 26; j++) { if(mismatch[i][j] > -1 && mismatch[j][i] > -1) { System.out.println(hd-2); System.out.println((mismatch[i][j]+1)+" "+(mismatch[j][i]+1)); return; } } } for(int i = 0; i < n; i++) { if(a.charAt(i) != b.charAt(i)) { //try a gets b's letter if(pair[0][b.charAt(i)-'a'] > -1) { System.out.println(hd-1); System.out.println((i+1)+" "+(pair[0][b.charAt(i)-'a']+1)); return; } } } System.out.println(hd); System.out.println("-1 -1"); } }
n
947.java
0.5
import java.io.*; import java.util.*; import java.lang.*; import static java.lang.Math.*; // _ h _ r _ t r _ // _ t _ t _ s t _ public class TaskA implements Runnable { long m = (int)1e9+7; PrintWriter w; InputReader c; final int MAXN = (int)1e6 + 100; public void run() { c = new InputReader(System.in); w = new PrintWriter(System.out); int n = c.nextInt(), hamming_distance = 0; char[] s = c.next().toCharArray(), t = c.next().toCharArray(); HashMap<Character, HashSet<Character>> replace = new HashMap<>(); HashMap<Character, Integer> map = new HashMap<>(); for(int i=0;i<n;++i) if(s[i] != t[i]) { HashSet<Character> temp; if(replace.containsKey(s[i])){ temp = replace.get(s[i]); temp.add(t[i]); } else { temp = new HashSet<>(); temp.add(t[i]); } map.put(s[i],i); replace.put(s[i], temp); hamming_distance++; } int l = -1, r = -1; boolean global_check = false; for(int i=0;i<n;i++) if(s[i] != t[i]) { if(replace.containsKey(t[i])) { HashSet<Character> indices = replace.get(t[i]); int ind = map.get(t[i]); l = i + 1; r = ind + 1; if (indices.contains(s[i])) { hamming_distance -= 2; global_check = true; break; } } if(global_check) break; } if(!global_check && l!=-1) hamming_distance--; else if(global_check){ for(int i=0;i<n;i++) { if(t[i] == s[l-1] && s[i] == t[l-1]){ r = i + 1; break; } } } w.println(hamming_distance); w.println(l+" "+r); w.close(); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void sortbyColumn(int arr[][], int col){ Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public void printArray(int[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } public int[] scanArrayI(int n){ int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = c.nextInt(); return a; } public long[] scanArrayL(int n){ long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = c.nextLong(); return a; } public void printArray(long[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new TaskA(),"TaskA",1<<26).start(); } }
n
948.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.InputMismatchException; public class Solution1 implements Runnable { static final long MAX = 1000000007L; static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Solution1(),"Solution1",1<<26).start(); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } // method to return LCM of two numbers long lcm(long a, long b) { return (a*b)/gcd(a, b); } int root(int a){ while(arr[a] != a){ arr[a] = arr[arr[a]]; a = arr[a]; } return a; } void union(int a,int b){ int xroot = root(a); int yroot = root(b); if(arr[xroot] < arr[yroot]){ arr[xroot] = yroot; }else{ arr[yroot] = xroot; } } boolean find(int a,int b){ int roota = root(a); int rootb = root(b); if(roota == rootb){ return true; }else{ return false; } } int[] arr; final int level = 20; public void run() { InputReader sc= new InputReader(System.in); PrintWriter w= new PrintWriter(System.out); int n = sc.nextInt(); char[] ch = new char[n]; char[] ch2 = new char[n]; ch = sc.next().toCharArray(); ch2 = sc.next().toCharArray(); HashSet<Integer> hset[] = new HashSet[26]; for(int i = 0;i < 26;i++){ hset[i] =new HashSet(); } int count = 0; for(int i = 0;i < ch.length;i++){ if(ch[i] != ch2[i]){ hset[ch[i]-97].add(ch2[i]-97); count++; } } boolean flag = false; int swap1 = -1; int swap2 = -1; int rem = -1; for(int i = 0;i < ch.length;i++){ if(ch[i] != ch2[i]){ if(hset[ch2[i]-97].size() != 0){ swap1 = i; flag = true; if(hset[ch2[i]-97].contains(ch[i]-97)){ rem = i; count-=2; flag = false; break; } } } } if(flag){ count--; w.println(count); for(int i = 0;i < n;i++){ if(i != swap1 && ch[i] == ch2[swap1] && ch[i] != ch2[i]){ w.println((swap1+1) + " " + (i+1)); w.close(); System.exit(0); } } }else{ if(rem == -1){ w.println(count); w.println("-1 -1"); }else{ w.println(count); for(int i = 0;i < n;i++){ if(i != rem && ch[i] == ch2[rem] && ch[rem] == ch2[i] && ch[i] != ch2[i]){ w.println((rem+1) + " " + (i+1)); w.close(); System.exit(0); } } } } w.close(); } boolean fun(long[] prefix,long mid,long temp,long[] arr){ if(temp >= prefix[(int)mid]){ return true; } return false; } static class Pair implements Comparable<Pair>{ int x; int y; Pair(){} Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair p){ return Long.compare(this.x,p.x); } } }
n
951.java
0.5
import java.util.*; import java.io.*; import java.math.*; import java.util.HashMap; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String args[]) { Reader sc=new Reader(); PrintWriter out=new PrintWriter(System.out); int n=sc.i(); String s1=sc.s(); String s2=sc.s(); int pos1=-1; int pos2=-1; int arr[][][]=new int[100][100][2]; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) { if(arr[s2.charAt(i)-97][s1.charAt(i)-97][0]==1) { pos2=i; pos1=arr[s2.charAt(i)-97][s1.charAt(i)-97][1]; break; } arr[s1.charAt(i)-97][s2.charAt(i)-97][0]=1; arr[s1.charAt(i)-97][s2.charAt(i)-97][1]=i; } } int ham=0; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) ham++; } if(pos1!=-1&&pos2!=-1) { System.out.println(ham-2); System.out.println(pos1+1+" "+(pos2+1)); System.exit(0); } int arr1[][]=new int[100][2]; int arr2[][]=new int[100][2]; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) { if(arr1[s1.charAt(i)-97][0]==1) { pos2=i; pos1=arr1[s1.charAt(i)-97][1]; break; } if(arr2[s2.charAt(i)-97][0]==1) { pos2=i; pos1=arr2[s2.charAt(i)-97][1]; break; } arr1[s2.charAt(i)-97][0]=1; arr1[s2.charAt(i)-97][1]=i; arr2[s1.charAt(i)-97][0]=1; arr2[s1.charAt(i)-97][1]=i; } } if(pos1!=-1&&pos2!=-1) { System.out.println(ham-1); System.out.println(pos1+1+" "+(pos2+1)); System.exit(0); } System.out.println(ham); System.out.println(pos1+" "+pos2); } }
n
952.java
0.5
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.ArrayList; import java.util.Scanner; /** * * @author Ahmed */ public class Watermelon { static class Passengers { public int floor ; public int time; public Passengers( int floor , int time){ this.floor =floor; this.time =time; } } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner in = new Scanner(System.in); int x = in.nextInt() , y = in.nextInt(); ArrayList<Passengers> list = new ArrayList<>(); for(int i = 1 ; i <= x ; ++i){ list.add(new Passengers(in.nextInt(), in.nextInt())); } int sum = 0 ; for(int i = list.size() - 1 ; i >= 0 ; --i) { int s = y - list.get(i).floor; sum = sum + s ; if(sum < list.get(i).time) { sum = sum + ( list.get(i).time - sum); } y = list.get(i).floor; } if( list.get(list.size() - 1).floor != 0){ sum = sum + (list.get(0).floor); } System.out.println(sum); } }
n
963.java
0.5
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = sc.nextInt(); int[] f = new int[n]; int[] t = new int[n]; for (int i = 0; i < n; i++) { f[i] = sc.nextInt(); t[i] = sc.nextInt(); } System.out.println(solve(f, t, s)); sc.close(); } static int solve(int[] f, int[] t, int s) { int[] maxTimes = new int[s + 1]; for (int i = 0; i < f.length; i++) { maxTimes[f[i]] = Math.max(maxTimes[f[i]], t[i]); } int time = 0; for (int i = s; i > 0; i--) { time = Math.max(time, maxTimes[i]); time++; } return time; } }
n
965.java
0.5
import java.util.Scanner; /** * * @author User */ public class Code { static int [] reverse(int a[]) { int[] b = new int[a.length]; int j = 0 ; for (int i = a.length-1; i >= 0; i--) { b[i] = a[j] ; j++; } return b ; } public static void main(String[] args) { int pas ; int top ; Scanner in = new Scanner(System.in) ; pas= in.nextInt(); top=in.nextInt() ; int a [] = new int[pas] ; int b [] = new int[pas] ; for (int i = 0; i < pas; i++) { a[i] = in.nextInt() ; b[i] = in.nextInt() ; } a = reverse(a) ; b = reverse(b) ; int ftime =0 ; int t; int po = top ; for (int i = 0; i < pas; i++) { ftime+=(po-a[i]) ; t = Math.max(b[i]-ftime, 0) ; ftime+=t ; po = a[i] ; } if(po!=0) ftime+=po ; System.out.println(ftime); } }
n
968.java
0.5
import java.util.Scanner; public class HammingDistancesSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(), b = sc.nextLine(); long sum = 0; int frequency[][] = new int[200010][2]; for (int i = 1; i <= b.length(); i++) { for (int j = 0; j < 2; j++) frequency[i][j] = frequency[i - 1][j]; frequency[i][Character.getNumericValue((b.charAt(i - 1)))]++; } for (int i = 0; i < a.length(); i++) { int c = Character.getNumericValue(a.charAt(i)); for (int j = 0; j < 2; j++) { int flippingTerm = Math.abs(c - j); int endOfWindowValue = frequency[b.length() - a.length() + i + 1][j]; int startOfWindowOffset = frequency[i][j]; sum += flippingTerm * (endOfWindowValue - startOfWindowOffset); } } System.out.println(sum); sc.close(); } }
n
969.java
0.5
// Java program to find union of // two sorted arrays class FindUnion { /* Function prints union of arr1[] and arr2[] m is the number of elements in arr1[] n is the number of elements in arr2[] */ static int printUnion( int arr1[], int arr2[], int m, int n) { int i = 0 , j = 0 ; while (i < m && j < n) { if (arr1[i] < arr2[j]) System.out.print(arr1[i++]+ " " ); else if (arr2[j] < arr1[i]) System.out.print(arr2[j++]+ " " ); else { System.out.print(arr2[j++]+ " " ); i++; } } /* Print remaining elements of the larger array */ while (i < m) System.out.print(arr1[i++]+ " " ); while (j < n) System.out.print(arr2[j++]+ " " ); return 0 ; } public static void main(String args[]) { int arr1[] = { 1 , 2 , 4 , 5 , 6 }; int arr2[] = { 2 , 3 , 5 , 7 }; int m = arr1.length; int n = arr2.length; printUnion(arr1, arr2, m, n); } }
n
97.java
0.5
import java.io.*; import java.lang.*; import java.util.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Math.*; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class Main { Main() throws IOException { String a = nextLine(); String b = nextLine(); long ans = 0; int s = 0; for (int i = 0; i < b.length() - a.length(); ++i) { s += b.charAt(i) == '1' ? 1 : 0; } for (int i = 0; i < a.length(); ++i) { s += b.charAt(i + b.length() - a.length()) == '1' ? 1 : 0; ans += a.charAt(i) == '1' ? b.length() - a.length() + 1 - s : s; s -= b.charAt(i) == '1' ? 1 : 0; } out.println(ans); } ////////////////////////////// PrintWriter out = new PrintWriter(System.out, false); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer stok = null; String nextLine() throws IOException { while (stok == null || !stok.hasMoreTokens()) { stok = new StringTokenizer(in.readLine()); } return stok.nextToken(); } public static void main(String args[]) throws IOException { if (args.length > 0) { setIn(new FileInputStream(args[0] + ".inp")); setOut(new PrintStream(args[0] + ".out")); } Main solver = new Main(); solver.out.flush(); // could be replace with a method, but nah, this is just competitive programming :p } }
n
970.java
0.5
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] x = new int [200010][10]; String a = sc.nextLine(); String b = sc.nextLine(); int n = a.length(); int m = b.length(); for (int i = 1; i <= m; i++) { for (int j = 0; j < 2; j++) { x[i][j] = x[i - 1][j]; } ++x[i][b.charAt(i - 1) - '0']; } long res = 0; for (int i = 0, c; i < n; i++) { c = a.charAt(i) - '0'; for (int j = 0; j < 2; j++) { res += Math.abs(c - j) * (x[m - n + i + 1][j] - x[i][j]); } } System.out.println(res); } }
n
971.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static java.lang.Math.*; public class Main2 { private FastScanner scanner = new FastScanner(); public static void main(String[] args) { new Main2().solve(); } private void solve() { String a = scanner.nextLine(), b = scanner.nextLine(); int n = b.length(), m = a.length(); int p[] = new int[n]; p[0] = b.charAt(0) - '0'; for (int i = 1; i < n; i++) { p[i] = p[i - 1] + (b.charAt(i) - '0'); } long ans = 0; for (int i = 0; i < m; i++) { int cur = a.charAt(i) - '0'; int cnt = p[n - m + i] - (i > 0 ? p[i - 1] : 0); if (cur == 0) { ans += cnt; } else { ans += n - m + 1 - cnt; } } System.out.println(ans); } class Pair { int c, f; } class FastScanner { BufferedReader reader; StringTokenizer tokenizer; FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } Integer[] nextA(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } return a; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } }
n
972.java
0.5
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { static int check(int temp) { int count1 = 0; while (temp>0) { if(temp % 2 != 0) count1++; temp/= 2; } return count1; } public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); String a=sc.next(); String b=sc.next(); int m=a.length(); int n=b.length(); int[] zero=new int[n]; int[] one=new int[n]; for(int i=0;i<n;i++) { if(i==0) { if(b.charAt(i)=='0') zero[i]++; else one[i]++; } else { zero[i]=zero[i-1]; one[i]=one[i-1]; if(b.charAt(i)=='0') zero[i]++; else one[i]++; } } long res=0; for(int i=0;i<m;i++) { int x=n-m+i; if(a.charAt(i)=='0') res+=one[x]; else res+=zero[x]; if(i>0) { if(a.charAt(i)=='0') res-=one[i-1]; else res-=zero[i-1]; } } System.out.println(res); } }
n
973.java
0.5
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class TaskB implements Runnable { boolean prime[] = new boolean[(int)1e6+10]; InputReader c; PrintWriter w; public void run() { c = new InputReader(System.in); w = new PrintWriter(System.out); char a[] = c.next().toCharArray(), b[] = c.next().toCharArray(); int n = a.length, m = b.length; int[][] prefix = new int[m][2]; for(int i=0;i<m;i++){ if(i!=0) { prefix[i][0] = prefix[i-1][0]; prefix[i][1] = prefix[i-1][1]; } prefix[i][b[i] - '0']++; //w.println(prefix[i][0]+" "+prefix[i][1]); } long res = 0; for(int i=0;i<n;i++){ int temp = a[i] - '0'; res += prefix[m - n + i][temp^1]; if(i!=0) res -= prefix[i-1][temp^1]; } w.println(res); w.close(); } void sieveOfEratosthenes(int n) { for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } class pair implements Comparable<pair>{ char ch; int ind; @Override public String toString() { return "pair{" + "ch=" + ch + ", ind=" + ind + '}'; } public pair(char ch, int ind) { this.ch = ch; this.ind = ind; } public int compareTo(pair car) { if(this.ch==car.ch) return this.ind - car.ind; return this.ch - car.ch; } } public static void sortbyColumn(int arr[][], int col){ Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } static long gcd(long a, long b){ if (b == 0) return a; return gcd(b, a % b); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public void printArray(int[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } public int[] scanArrayI(int n){ int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = c.nextInt(); return a; } public long[] scanArrayL(int n){ long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = c.nextLong(); return a; } public void printArray(long[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new TaskB(),"TaskB",1<<26).start(); } }
n
974.java
0.5
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class BOOL { static char [][]ch; static int n,m; private static FastReader in =new FastReader(); public static void main(String[] args) { int n=in.nextInt(); int a[]=new int[1000002]; int dp[]=new int[1000002],ans=0; for(int i=0;i<n;i++){a[in.nextInt()]=in.nextInt();} dp[0]=a[0]==0?0:1; for(int i=1;i<1000002;i++){ if(a[i]==0){dp[i]=dp[i-1];} else{ if(a[i]>=i){dp[i]=1;} else{ dp[i]=dp[i-a[i]-1]+1; }} if(dp[i]>=ans)ans=dp[i]; } System.out.println(n-ans); }} 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; } }
n
976.java
0.5
import java.util.Scanner; public class codef8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int beacon[] = new int[1000001]; for (int i = 0; i < num; i++) { beacon[sc.nextInt()] = sc.nextInt(); } int dp[] = new int[1000001]; int max = 1; if (beacon[0] > 0) { dp[0] = 1; } for (int i = 1; i <= 1000000; i++) { if (beacon[i] == 0) { dp[i] = dp[i-1]; } else { int b = beacon[i]; if (i <= b) { dp[i] = 1; } else { dp[i] = dp[i-b-1] + 1; } } max = Math.max(max, dp[i]); } System.out.println(num-max); sc.close(); } }
n
977.java
0.5
import java.util.Scanner; public class codef8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int beacon[] = new int[1000001]; int pos[] = new int[num]; for (int i = 0; i < num; i++) { int position = sc.nextInt(); beacon[position] = sc.nextInt(); pos[i] = position; } int dp[] = new int[1000001]; int max = 1; if (beacon[0] != 0) dp[0] = 1; for (int i = 1; i <= 1000000; i++) { if (beacon[i] == 0) { dp[i] = dp[i-1]; } else { int j = i - beacon[i] - 1; if (j < 0) { dp[i] = 1; } else { dp[i] = dp[j] + 1; } } max = Math.max(max, dp[i]); } System.out.println(num-max); } }
n
978.java
0.5
import java.util.Scanner; public class codef8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int beacon[] = new int[1000001]; int pos[] = new int[num]; for (int i = 0; i < num; i++) { int position = sc.nextInt(); beacon[position] = sc.nextInt(); pos[i] = position; } int dp[] = new int[1000001]; int max = 0; if (beacon[0] != 0) dp[0] = 1; for (int i = 1; i <= 1000000; i++) { if (beacon[i] == 0) { dp[i] = dp[i-1]; } else { int j = i - beacon[i] - 1; if (j < 0) { dp[i] = 1; } else { dp[i] = dp[j] + 1; } } max = Math.max(max, dp[i]); } System.out.println(num-max); } }
n
979.java
0.5
// Java program to find union of two // sorted arrays (Handling Duplicates) class FindUnion { static void UnionArray( int arr1[], int arr2[]) { // Taking max element present in either array int m = arr1[arr1.length - 1 ]; int n = arr2[arr2.length - 1 ]; int ans = 0 ; if (m > n) { ans = m; } else ans = n; // Finding elements from 1st array // (non duplicates only). Using // another array for storing union // elements of both arrays // Assuming max element present // in array is not more than 10^7 int newtable[] = new int [ans + 1 ]; // First element is always // present in final answer System.out.print(arr1[ 0 ] + " " ); // Incrementing the First element's count // in it's corresponding index in newtable ++newtable[arr1[ 0 ]]; // Starting traversing the first // array from 1st index till last for ( int i = 1 ; i < arr1.length; i++) { // Checking whether current element // is not equal to it's previous element if (arr1[i] != arr1[i - 1 ]) { System.out.print(arr1[i] + " " ); ++newtable[arr1[i]]; } } // Finding only non common // elements from 2nd array for ( int j = 0 ; j < arr2.length; j++) { // By checking whether it's already // present in newtable or not if (newtable[arr2[j]] == 0 ) { System.out.print(arr2[j] + " " ); ++newtable[arr2[j]]; } } } // Driver Code public static void main(String args[]) { int arr1[] = { 1 , 2 , 2 , 2 , 3 }; int arr2[] = { 2 , 3 , 4 , 5 }; UnionArray(arr1, arr2); } }
n
98.java
0.5
import java.util.Scanner; public class ChainReaction { public static void main(String [] args) { Scanner kb = new Scanner(System.in); int num = kb.nextInt(); int[] beacons = new int[1000002]; for (int i=0; i<num; i++) { beacons[kb.nextInt()] = kb.nextInt(); } int [] dp = new int[1000002]; int max = 0; if (beacons[0] != 0) dp[0] = 1; for (int i=1; i<dp.length; i++) { if (beacons[i] == 0) { dp[i] = dp[i-1]; } else { int index = i-1-beacons[i]; if (index<0) dp[i] = 1; else dp[i] = 1 + dp[index]; } max = Math.max(max, dp[i]); //if (i<11) //System.out.println(i +" is "+dp[i]); } System.out.println(num-max); } }
n
981.java
0.5
import java.util.*; import java.io.*; import java.math.*; public class round569d2a { public static void main(String args[]) { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); int sum = 1; int tracker = 4; while (n > 1) { sum += tracker; tracker += 4; n--; } System.out.println(sum); } // ====================================================================================== // =============================== Reference Code ======================================= // ====================================================================================== static int greatestDivisor(int n) { int limit = (int) Math.sqrt(n); int max = 1; for (int i = 2; i <= limit; i++) { if (n % i == 0) { max = Integer.max(max, i); max = Integer.max(max, n / i); } } return max; } // Method to return all primes smaller than or equal to // n using Sieve of Eratosthenes static boolean[] sieveOfEratosthenes(int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // Binary search for number greater than or equal to target // returns -1 if number not found private static int bin_gteq(int[] a, int key) { int low = 0; int high = a.length; int max_limit = high; while (low < high) { int mid = low + (high - low) / 2; if (a[mid] < key) { low = mid + 1; } else high = mid; } return high == max_limit ? -1 : high; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class Tuple<X, Y> { public final X x; public final Y y; public Tuple(X x, Y y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } } static class Tuple3<X, Y, Z> { public final X x; public final Y y; public final Z z; public Tuple3(X x, Y y, Z z) { this.x = x; this.y = y; this.z = z; } public String toString() { return "(" + x + "," + y + "," + z + ")"; } } static Tuple3<Integer, Integer, Integer> gcdExtended(int a, int b, int x, int y) { // Base Case if (a == 0) { x = 0; y = 1; return new Tuple3(0, 1, b); } int x1 = 1, y1 = 1; // To store results of recursive call Tuple3<Integer, Integer, Integer> tuple = gcdExtended(b % a, a, x1, y1); int gcd = tuple.z; x1 = tuple.x; y1 = tuple.y; // Update x and y using results of recursive // call x = y1 - (b / a) * x1; y = x1; return new Tuple3(x, y, gcd); } // Returns modulo inverse of a // with respect to m using extended // Euclid Algorithm. Refer below post for details: // https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/ static int inv(int a, int m) { int m0 = m, t, q; int x0 = 0, x1 = 1; if (m == 1) return 0; // Apply extended Euclid Algorithm while (a > 1) { // q is quotient q = a / m; t = m; // m is remainder now, process // same as euclid's algo m = a % m; a = t; t = x0; x0 = x1 - q * x0; x1 = t; } // Make x1 positive if (x1 < 0) x1 += m0; return x1; } // k is size of num[] and rem[]. // Returns the smallest number // x such that: // x % num[0] = rem[0], // x % num[1] = rem[1], // .................. // x % num[k-2] = rem[k-1] // Assumption: Numbers in num[] are pairwise // coprime (gcd for every pair is 1) static int findMinX(int num[], int rem[], int k) { // Compute product of all numbers int prod = 1; for (int i = 0; i < k; i++) prod *= num[i]; // Initialize result int result = 0; // Apply above formula for (int i = 0; i < k; i++) { int pp = prod / num[i]; result += rem[i] * inv(pp, num[i]) * pp; } return result % prod; } /** * Source: Matt Fontaine */ static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int chars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (chars == -1) throw new InputMismatchException(); if (curChar >= chars) { curChar = 0; try { chars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (chars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
n
994.java
0.5
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static long oo = 1000000000000L; static int[][] memo; public static void main(String[] args) throws IOException { int[] cnt = new int[101]; cnt[1] = 1; for(int i = 2; i <= 100; ++i) { cnt[i] = cnt[i-1] + 4 * (i - 1); } int n = in.nextInt(); System.out.println( cnt[n] ); out.close(); } static int maxHit(ArrayList<Integer> a, int p, int i) { if(i == a.size()) return 0; if(memo[p][i] != -1) return memo[p][i]; int ret = maxHit(a, p, i + 1); if(p == -1 || a.get(p) < a.get(i)) { ret = Math.max(ret, maxHit(a, i, i + 1) ); } return memo[p][i] = ret; } static long lcm(long a, long b) { return a * b / gcd(a, b); } static boolean nextPermutation(int[] a) { for(int i = a.length - 2; i >= 0; --i) { if(a[i] < a[i+1]) { for(int j = a.length - 1; ; --j) { if(a[i] < a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; for(i++, j = a.length - 1; i < j; ++i, --j) { t = a[i]; a[i] = a[j]; a[j] = t; } return true; } } } } return false; } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static void shuffle(long[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); long t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int lower_bound(long[] a, int n, long k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
n
997.java
0.5
import java.io.*; import java.lang.*; import java.util.*; public class alex { public static void main(String[] args)throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt();int sum=1; for(int i=1;i<=n;i++) { sum=sum+(4*(i-1)); } System.out.println(sum); } }
n
998.java
0.5
import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int num=1; int add; for(int i=1;i<n;i++){ add=4*i; num+=add; } System.out.println(num); } }
n
999.java
0.5
// Java program to find the only repeating // element in an array where elements are // from 1 to n-1. class GFG { static int findRepeating( int arr[], int n) { // res is going to store value of // 1 ^ 2 ^ 3 .. ^ (n-1) ^ arr[0] ^ // arr[1] ^ .... arr[n-1] int res = 0 ; for ( int i = 0 ; i < n - 1 ; i++) res = res ^ (i + 1 ) ^ arr[i]; res = res ^ arr[n - 1 ]; return res; } // Driver code public static void main(String[] args) { int arr[] = { 9 , 8 , 2 , 6 , 1 , 8 , 5 , 3 , 4 , 7 }; int n = arr.length; System.out.println(findRepeating(arr, n)); } } // This code is contributed by // Smitha Dinesh Semwal.
n
114.java
0.5
import java.util.HashMap; /* Program for finding out majority element in an array */ class MajorityElement { private static void findMajority( int [] arr) { HashMap<Integer,Integer> map = new HashMap<Integer, Integer>(); for ( int i = 0 ; i < arr.length; i++) { if (map.containsKey(arr[i])) { int count = map.get(arr[i]) + 1 ; if (count > arr.length / 2 ) { System.out.println( "Majority found :- " + arr[i]); return ; } else map.put(arr[i], count); } else map.put(arr[i], 1 ); } System.out.println( " No Majority element" ); } /* Driver program to test the above functions */ public static void main(String[] args) { int a[] = new int []{ 2 , 2 , 2 , 2 , 5 , 5 , 2 , 3 , 3 }; findMajority(a); } } // This code is contributed by karan malhotra
n
130.java
0.5
// Java code to find maximum triplet sum import java.io.*; import java.util.*; class GFG { // This function assumes that there // are at least three elements in arr[]. static int maxTripletSum( int arr[], int n) { // Initialize Maximum, second maximum and third // maximum element int maxA = - 100000000 , maxB = - 100000000 ; int maxC = - 100000000 ; for ( int i = 0 ; i < n; i++) { // Update Maximum, second maximum // and third maximum element if (arr[i] > maxA) { maxC = maxB; maxB = maxA; maxA = arr[i]; } // Update second maximum and third maximum // element else if (arr[i] > maxB) { maxC = maxB; maxB = arr[i]; } // Update third maximum element else if (arr[i] > maxC) maxC = arr[i]; } return (maxA + maxB + maxC); } // Driven code public static void main(String args[]) { int arr[] = { 1 , 0 , 8 , 6 , 4 , 2 }; int n = arr.length; System.out.println(maxTripletSum(arr, n)); } } // This code is contributed by Nikita Tiwari.
n
137.java
0.5
/* Java program to check if linked list is palindrome recursively */ class LinkedList { Node head; // head of list Node left; /* Linked list Node*/ class Node { char data; Node next; Node( char d) { data = d; next = null ; } } // Initial parameters to this function are &head and head boolean isPalindromeUtil(Node right) { left = head; /* stop recursion when right becomes NULL */ if (right == null ) return true ; /* If sub-list is not palindrome then no need to check for current left and right, return false */ boolean isp = isPalindromeUtil(right.next); if (isp == false ) return false ; /* Check values at current left and right */ boolean isp1 = (right.data == (left).data); /* Move left to next node */ left = left.next; return isp1; } // A wrapper over isPalindromeUtil() boolean isPalindrome(Node head) { boolean result = isPalindromeUtil(head); return result; } /* Push a node to linked list. Note that this function changes the head */ public void push( char new_data) { /* Allocate the Node & Put in the data */ Node new_node = new Node(new_data); /* link the old list off the new one */ new_node.next = head; /* Move the head to point to new Node */ head = new_node; } // A utility function to print a given linked list void printList(Node ptr) { while (ptr != null ) { System.out.print(ptr.data + "->" ); ptr = ptr.next; } System.out.println( "NULL" ); } /* Driver program to test the above functions */ public static void main(String[] args) { /* Start with the empty list */ LinkedList llist = new LinkedList(); char str[] = { 'a' , 'b' , 'a' , 'c' , 'a' , 'b' , 'a' }; String string = new String(str); for ( int i = 0 ; i < 7 ; i++) { llist.push(str[i]); llist.printList(llist.head); if (llist.isPalindrome(llist.head) != false ) { System.out.println( "Is Palindrome" ); System.out.println( "" ); } else { System.out.println( "Not Palindrome" ); System.out.println( "" ); } } } } // This code has been contributed by Mayank Jaiswal(mayank_24)
n
160.java
0.5
// Java program to find maximum sum // of all rotation of i*arr[i] using pivot. import java.util.*; import java.lang.*; import java.io.*; class GFG { // function definition static int maxSum( int arr[], int n) { int sum = 0 ; int i; int pivot = findPivot(arr, n); // difference in pivot and index of // last element of array int diff = n - 1 - pivot; for (i = 0 ; i < n; i++) { sum= sum + ((i + diff) % n) * arr[i]; } return sum; } // function to find pivot static int findPivot( int arr[], int n) { int i; for (i = 0 ; i < n; i++) { if (arr[i] > arr[(i + 1 ) % n]) return i; } return 0 ; } // Driver code public static void main(String args[]) { // rotated input array int arr[] = { 8 , 3 , 1 , 2 }; int n = arr.length; int max = maxSum(arr,n); System.out.println(max); } }
n
20.java
0.5
// Most efficient Java program to count all // substrings with same first and last characters. public class GFG { // assuming lower case only static final int MAX_CHAR = 26 ; static int countSubstringWithEqualEnds(String s) { int result = 0 ; int n = s.length(); // Calculating frequency of each character // in the string. int [] count = new int [MAX_CHAR]; for ( int i = 0 ; i < n; i++) count[s.charAt(i)- 'a' ]++; // Computing result using counts for ( int i = 0 ; i < MAX_CHAR; i++) result += (count[i] * (count[i] + 1 ) / 2 ); return result; } // Driver function public static void main(String args[]) { String s = "abcab" ; System.out.println(countSubstringWithEqualEnds(s)); } } // This code is contributed by Sumit Ghosh
n
259.java
0.5
// Java program for nth Catalan Number class GFG { // Returns value of Binomial Coefficient C(n, k) static long binomialCoeff( int n, int k) { long res = 1 ; // Since C(n, k) = C(n, n-k) if (k > n - k) { k = n - k; } // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for ( int i = 0 ; i < k; ++i) { res *= (n - i); res /= (i + 1 ); } return res; } // A Binomial coefficient based function to find nth catalan // number in O(n) time static long catalan( int n) { // Calculate value of 2nCn long c = binomialCoeff( 2 * n, n); // return 2nCn/(n+1) return c / (n + 1 ); } // Driver program to test above function public static void main(String[] args) { for ( int i = 0 ; i < 10 ; i++) { System.out.print(catalan(i) + " " ); } } }
n
291.java
0.5
// A O(n) time and O(1) extra // space solution to calculate // the Permutation Coefficient import java.io.*; class GFG { static int PermutationCoeff( int n, int k) { int Fn = 1 , Fk = 1 ; // Compute n! and (n-k)! for ( int i = 1 ; i <= n; i++) { Fn *= i; if (i == n - k) Fk = Fn; } int coeff = Fn / Fk; return coeff; } // Driver Code public static void main(String args[]) { int n = 10 , k = 2 ; System.out.println( "Value of P( " + n + "," + k + ") is " + PermutationCoeff(n, k) ); } } // This code is contributed by Nikita Tiwari.
n
297.java
0.5
class GFG { // Returns count of ways n people // can remain single or paired up. static int countFriendsPairings( int n) { int a = 1 , b = 2 , c = 0 ; if (n <= 2 ) { return n; } for ( int i = 3 ; i <= n; i++) { c = b + (i - 1 ) * a; a = b; b = c; } return c; } // Driver code public static void main(String[] args) { int n = 4 ; System.out.println(countFriendsPairings(n)); } } // This code is contributed by Ravi Kasha.
n
301.java
0.5
// Java program to find remaining // chocolates after k iterations class GFG { // A O(n) C++ program to count // even length binary sequences // such that the sum of first // and second half bits is same // Returns the count of // even length sequences static int countSeq( int n) { int nCr = 1 , res = 1 ; // Calculate SUM ((nCr)^2) for ( int r = 1 ; r <= n ; r++) { // Compute nCr using nC(r-1) // nCr/nC(r-1) = (n+1-r)/r; nCr = (nCr * (n + 1 - r)) / r; res += nCr * nCr; } return res; } // Driver code public static void main(String args[]) { int n = 2 ; System.out.print( "Count of sequences is " ); System.out.println(countSeq(n)); } } // This code is contributed // by Shivi_Aggarwal
n
312.java
0.5
// A simple JAVA program to rearrange // contents of arr[] such that arr[j] // becomes j if arr[i] is j class GFG { // A simple method to rearrange // 'arr[0..n-1]' so that 'arr[j]' // becomes 'i' if 'arr[i]' is 'j' static void rearrange( int arr[], int n) { for ( int i = 0 ; i < n; i++) { // retrieving old value and // storing with the new one arr[arr[i] % n] += i * n; } for ( int i = 0 ; i < n; i++) { // retrieving new value arr[i] /= n; } } // A utility function to print // contents of arr[0..n-1] static void printArray( int arr[], int n) { for ( int i = 0 ; i < n; i++) { System.out.print(arr[i] + " " ); } System.out.println(); } // Drive code public static void main(String[] args) { int arr[] = { 2 , 0 , 1 , 4 , 5 , 3 }; int n = arr.length; System.out.println( "Given array is : " ); printArray(arr, n); rearrange(arr, n); System.out.println( "Modified array is :" ); printArray(arr, n); } } // This code has been contributed by 29AjayKumar
n
34.java
0.5
// Java program to find all elements // in array which have atleast // two greater elements itself. import java.util.*; import java.io.*; class GFG { static void findElements( int arr[], int n) { int first = Integer.MIN_VALUE; int second = Integer.MAX_VALUE; for ( int i = 0 ; i < n; i++) { // If current element is smaller // than first then update both // first and second if (arr[i] > first) { second = first; first = arr[i]; } /* If arr[i] is in between first and second then update second */ else if (arr[i] > second) second = arr[i]; } for ( int i = 0 ; i < n; i++) if (arr[i] < second) System.out.print(arr[i] + " " ) ; } // Driver code public static void main(String args[]) { int arr[] = { 2 , - 6 , 3 , 5 , 1 }; int n = arr.length; findElements(arr, n); } } // This code is contributed by Sahil_Bansall
n
49.java
0.5
// Java O(n) solution for // finding smallest subarray // with sum greater than x import java.io.*; class GFG { // Returns length of smallest // subarray with sum greater // than x. If there is no // subarray with given sum, // then returns n+1 static int smallestSubWithSum( int arr[], int n, int x) { // Initialize current // sum and minimum length int curr_sum = 0 , min_len = n + 1 ; // Initialize starting // and ending indexes int start = 0 , end = 0 ; while (end < n) { // Keep adding array // elements while current // sum is smaller than x while (curr_sum <= x && end < n) { // Ignore subarrays with // negative sum if x is // positive. if (curr_sum <= 0 && x > 0 ) { start = end; curr_sum = 0 ; } curr_sum += arr[end++]; } // If current sum becomes // greater than x. while (curr_sum > x && start < n) { // Update minimum // length if needed if (end - start < min_len) min_len = end - start; // remove starting elements curr_sum -= arr[start++]; } } return min_len; } // Driver Code public static void main (String[] args) { int arr1[] = {- 8 , 1 , 4 , 2 , - 6 }; int x = 6 ; int n1 = arr1.length; int res1 = smallestSubWithSum(arr1, n1, x); if (res1 == n1 + 1 ) System.out.println( "Not possible" ); else System.out.println (res1); } } // This code is contributed by ajit
n
77.java
0.5
// Java program to find intersection of // two sorted arrays class FindIntersection { /* Function prints Intersection of arr1[] and arr2[] m is the number of elements in arr1[] n is the number of elements in arr2[] */ static void printIntersection( int arr1[], int arr2[], int m, int n) { int i = 0 , j = 0 ; while (i < m && j < n) { if (arr1[i] < arr2[j]) i++; else if (arr2[j] < arr1[i]) j++; else { System.out.print(arr2[j++]+ " " ); i++; } } } public static void main(String args[]) { int arr1[] = { 1 , 2 , 4 , 5 , 6 }; int arr2[] = { 2 , 3 , 5 , 7 }; int m = arr1.length; int n = arr2.length; printIntersection(arr1, arr2, m, n); } }
n
99.java
0.5
// Java program to rotate an array by // d elements class RotateArray { /*Function to left rotate arr[] of size n by d*/ void leftRotate( int arr[], int d, int n) { for ( int i = 0 ; i < d; i++) leftRotatebyOne(arr, n); } void leftRotatebyOne( int arr[], int n) { int i, temp; temp = arr[ 0 ]; for (i = 0 ; i < n - 1 ; i++) arr[i] = arr[i + 1 ]; arr[i] = temp; } /* utility function to print an array */ void printArray( int arr[], int n) { for ( int i = 0 ; i < n; i++) System.out.print(arr[i] + " " ); } // Driver program to test above functions public static void main(String[] args) { RotateArray rotate = new RotateArray(); int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; rotate.leftRotate(arr, 2 , 7 ); rotate.printArray(arr, 7 ); } } // This code has been contributed by Mayank Jaiswal
n_square
1.java
0.9
import java.io.*; import java.math.BigInteger; import java.util.InputMismatchException; import java.util.PriorityQueue; import java.util.StringTokenizer; public class D { static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } 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; } BigInteger nextBigInteger() { try { return new BigInteger(nextLine()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } } public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); FastWriter fw = new FastWriter(); int n = fr.nextInt(); int m = fr.nextInt(); for (int r = 0; r < n / 2; r++) { for (int c = 0; c < m; c++) { fw.println((r + 1) + " " + (c + 1)); fw.println((n - r) + " " + (m - c)); } } if (n % 2 != 0) { int r = n / 2; for (int c = 0; c < m / 2; c++) { fw.println((r + 1) + " " + (c + 1)); fw.println((r + 1) + " " + (m - c)); } if (m % 2 != 0) fw.println((r + 1) + " " + (m / 2 + 1)); } fw.close(); } }
n_square
1014.java
0.9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; public class D { int[][] fast(int n, int m){ int[][] ans = new int[2][n * m]; int c = 0; for (int left = 1, right = m; left < right; left++, right--) { for (int l = 1, r = n; l <= n && r >= 1; l++, r--) { ans[0][c] = l; ans[1][c++] = left; ans[0][c] = r; ans[1][c++] = right; } } if (m % 2 == 1) { int x = m/2 + 1; for(int l = 1, r = n;l < r;l++, r--){ ans[0][c] = l; ans[1][c++] = x; ans[0][c] = r; ans[1][c++] = x; if(n % 2 == 1 && l + 2 == r){ ans[0][c] = l+1; ans[1][c++] = x; } } } if(n == 1 && m % 2 == 1){ ans[0][c] = 1; ans[1][c] = m/2 + 1; } return ans; } void stress(){ for(int i = 3;i<=5;i++){ for(int j = 2;j<=5;j++){ int[][] ans = new int[2][]; try{ ans = fast(i, j); }catch(Exception e){ out.println("ошибка"); out.print(i + " " + j); return; } boolean[][] check = new boolean[i][j]; for(int c = 0;c<ans[0].length;c++){ int x = ans[0][c] - 1; int y = ans[1][c] - 1; check[x][y] = true; } for(int c = 0;c<i;c++){ for(int q = 0;q<j;q++){ if(!check[c][q]){ out.println(i + " " + j); out.println("точки"); for(int w = 0;w<ans[0].length;w++){ out.println(ans[0][w] + " " + ans[1][w]); } return; } } } HashSet<String> set = new HashSet<>(); for(int c = 1;c<ans[0].length;c++){ int x = ans[0][c] - ans[0][c- 1]; int y = ans[1][c] - ans[1][c - 1]; set.add(x + " " + y); } if(set.size() < i * j - 1){ out.println(i + " " + j); out.println("вектора"); for(int w = 0;w<ans[0].length;w++){ out.println(ans[0][w] + " " + ans[1][w]); } return; } } } } void normal(){ int n =readInt(); int m = readInt(); int[][] ans = fast(n, m); for(int i = 0;i<ans[0].length;i++){ out.println(ans[0][i] + " " + ans[1][i]); } } boolean stress = false; void solve(){ if(stress) stress(); else normal(); } public static void main(String[] args) { new D().run(); } void run(){ init(); solve(); out.close(); } BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init(){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } String readLine(){ try{ return in.readLine(); }catch(Exception ex){ throw new RuntimeException(ex); } } String readString(){ while(!tok.hasMoreTokens()){ String nextLine = readLine(); if(nextLine == null) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(); } int readInt(){ return Integer.parseInt(readString()); } long readLong(){ return Long.parseLong(readString()); } double readDouble(){ return Double.parseDouble(readString()); } }
n_square
1015.java
0.9
import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.util.*; public class E1180D { public static void main(String args[]) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int m = sc.nextInt(); // Move from both ends, Time limit exceeded on test 6 for (int i= 1; i<= m/2; i++) { // String s = ""; int i2 = m -i + 1; // the other end of i // i is left row, i2 is right row for (int j = 1; j <= n ; j++) { int j2 = n - j + 1; // start with (i,j), then go thru all the cell with (,i) and (,i2) pw.println(j + " " + i); pw.println(j2+ " " + i2); // s += j + " " + i + "\n" + j2+ " " + i2 + "\n"; } // out.print(s); } // if n is odd, there is one line in the middle if (m % 2 == 1) { int i2 = m /2 + 1; // this is the middle column for (int j = 1; j <= n/2 ; j++) { int j2 = n - j + 1; // start with (i,j), then go thru all the cell with (,i) and (,i2) pw.println(j + " " + i2); pw.println(j2+ " " + i2); } if (n %2 == 1) { int j = n /2 + 1; pw.println(j + " " + i2); } } pw.flush(); pw.close(); } }
n_square
1016.java
0.9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { private void solve() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(), m = nextInt(); boolean[][] used = new boolean[n + 1][m + 1]; for (int j = 1; j <= (m + 1) / 2; j++) { int x1 = 1, x2 = n; for (int i = 1; i <= n; i++) { if (x1 <= n && !used[x1][j]) { out.println(x1 + " " + j); used[x1++][j] = true; } if (x2 > 0 && !used[x2][m - j + 1]) { out.println(x2 + " " + (m - j + 1)); used[x2--][m - j + 1] = true; } } } out.close(); } public static void main(String[] args) { new D().solve(); } private BufferedReader br; private StringTokenizer st; private PrintWriter out; private String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } }
n_square
1017.java
0.9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { private void solve() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(), m = nextInt(), u = 1, d = n; while (u < d) { for (int i = 1; i <= m; i++) { out.println(u + " " + i); out.println(d + " " + (m - i + 1)); } u++; d--; } if (u == d) { int l = 1, r = m; while (l < r) { out.println(u + " " + l++); out.println(d + " " + r--); } if (l == r) out.println(u + " " + l); } out.close(); } public static void main(String[] args) { new D().solve(); } private BufferedReader br; private StringTokenizer st; private PrintWriter out; private String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } }
n_square
1018.java
0.9
// Java program to count number of triangles that can be // formed from given array import java.io.*; import java.util.*; class CountTriangles { // Function to count all possible triangles with arr[] // elements static int findNumberOfTriangles( int arr[]) { int n = arr.length; // Sort the array elements in non-decreasing order Arrays.sort(arr); // Initialize count of triangles int count = 0 ; // Fix the first element. We need to run till n-3 as // the other two elements are selected from arr[i+1...n-1] for ( int i = 0 ; i < n- 2 ; ++i) { // Initialize index of the rightmost third element int k = i + 2 ; // Fix the second element for ( int j = i+ 1 ; j < n; ++j) { /* Find the rightmost element which is smaller than the sum of two fixed elements The important thing to note here is, we use the previous value of k. If value of arr[i] + arr[j-1] was greater than arr[k], then arr[i] + arr[j] must be greater than k, because the array is sorted. */ while (k < n && arr[i] + arr[j] > arr[k]) ++k; /* Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr[i] and arr[j]. All elements between arr[j+1] to arr[k-1] can form a triangle with arr[i] and arr[j]. One is subtracted from k because k is incremented one extra in above while loop. k will always be greater than j. If j becomes equal to k, then above loop will increment k, because arr[k] + arr[i] is always/ greater than arr[k] */ if (k>j) count += k - j - 1 ; } } return count; } public static void main (String[] args) { int arr[] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 }; System.out.println( "Total number of triangles is " + findNumberOfTriangles(arr)); } } /*This code is contributed by Devesh Agrawal*/
n_square
102.java
0.9
import java.util.*; public class TestClass { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int arr[] = new int[n+1]; for(int i =0;i<n;i++) arr[i+1]= in.nextInt(); long sum[] = new long [n+1]; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+arr[i]; long dp[] = new long[n+1]; for(int i =1;i<=n;i++) { for(int j=i;j>i-m&&j>=1;j--) { long val = sum[i]-sum[j-1]+dp[j-1]-k; dp[i]= Math.max(dp[i],val); } } long max =0; for(int i =1;i<=n;i++) max=Math.max(max,dp[i]); System.out.println(max); } }
n_square
1035.java
0.9
import java.util.*; import java.math.*; public class Solution{ private long [] sums; private void solve(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int [] arr = new int[n]; this.sums = new long[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); sums[i] = arr[i] + (i == 0 ? 0 : sums[i - 1]); } long ans = 0; for(int i = 1; i <= n && i <= m; i++){ ans = Math.max(ans, sum(0, i - 1) - k); } long [] dp = new long[n]; for(int i = 0; i < n; i++){ if(i + 1 >= m){ long cur = sum(i - m + 1, i) - k; if(i - m >= 0){ cur += dp[i - m]; } dp[i] = Math.max(dp[i], cur); } for(int j = 0; j <= m && i + j < n; j++){ ans = Math.max(ans, dp[i] + sum(i + 1, i + j) - k * (j > 0 ? 1 : 0)); } } System.out.println(ans); } private long sum(int l, int r){ if(l <= 0){ return sums[r]; }else{ return sums[r] - sums[l - 1]; } } public static void main(String [] args){ new Solution().solve(); } }
n_square
1036.java
0.9
//package ContestEd69; import java.io.*; import java.util.StringTokenizer; public class mainD { public static PrintWriter out = new PrintWriter(System.out); public static FastScanner enter = new FastScanner(System.in); public static long[] arr; public static void main(String[] args) throws IOException { int n=enter.nextInt(); int m=enter.nextInt(); long k=enter.nextLong(); arr=new long[n+1]; for (int i = 1; i <n+1 ; i++) { arr[i]=enter.nextLong(); } long[] summ=new long[n+1]; for (int i = 1; i <n+1 ; i++) { summ[i]+=arr[i]+summ[i-1]; } long[] best=new long[n+1]; for (int i = 1; i <n+1 ; i++) { best[i]=Math.max(0, ((i-m>=0) ? best[i-m]+summ[i]-summ[i-m]-k:0)); } long ans=best[1]; for (int i = 1; i <n+1 ; i++) { ans=Math.max(ans,best[i]); for (int j = 1; j <m ; j++) { ans=Math.max(ans, ((i-j>=0) ? best[i-j] -k +summ[i]-summ[i-j]:0)); } } System.out.println(ans); } static class FastScanner { BufferedReader br; StringTokenizer stok; FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } } }
n_square
1037.java
0.9
import java.util.*; import javax.lang.model.util.ElementScanner6; import java.io.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader inp = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(inp, out); out.close(); } static class Solver { class pair implements Comparable<pair>{ int i; long dist; public pair(int i,long dist) { this.i=i; this.dist=dist; } public int compareTo(pair p) { return Long.compare(this.dist,p.dist); } } class Node implements Comparable < Node > { int i; int cnt; Node(int i, int cnt) { this.i = i; this.cnt = cnt; } public int compareTo(Node n) { if (this.cnt == n.cnt) { return Integer.compare(this.i, n.i); } return Integer.compare(this.cnt, n.cnt); } } public boolean done(int[] sp, int[] par) { int root; root = findSet(sp[0], par); for (int i = 1; i < sp.length; i++) { if (root != findSet(sp[i], par)) return false; } return true; } public int findSet(int i, int[] par) { int x = i; boolean flag = false; while (par[i] >= 0) { flag = true; i = par[i]; } if (flag) par[x] = i; return i; } public void unionSet(int i, int j, int[] par) { int x = findSet(i, par); int y = findSet(j, par); if (x < y) { par[y] = x; } else { par[x] = y; } } public long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } public boolean isPrime(int n) { for(int i=2;i<n;i++) { if(n%i==0) { return false; } } return true; } public void minPrimeFactor(int n, int[] s) { boolean prime[] = new boolean[n + 1]; Arrays.fill(prime, true); s[1] = 1; s[2] = 2; for (int i = 4; i <= n; i += 2) { prime[i] = false; s[i] = 2; } for (int i = 3; i <= n; i += 2) { if (prime[i]) { s[i] = i; for (int j = 2 * i; j <= n; j += i) { prime[j] = false; s[j] = i; } } } } public void findAllPrime(int n, ArrayList < Node > al, int s[]) { int curr = s[n]; int cnt = 1; while (n > 1) { n /= s[n]; if (curr == s[n]) { cnt++; continue; } Node n1 = new Node(curr, cnt); al.add(n1); curr = s[n]; cnt = 1; } } public int binarySearch(int n, int k) { int left = 1; int right = 100000000 + 5; int ans = 0; while (left <= right) { int mid = (left + right) / 2; if (n / mid >= k) { left = mid + 1; ans = mid; } else { right = mid - 1; } } return ans; } public boolean checkPallindrom(String s) { char ch[] = s.toCharArray(); for (int i = 0; i < s.length() / 2; i++) { if (ch[i] != ch[s.length() - 1 - i]) return false; } return true; } public void remove(ArrayList < Integer > [] al, int x) { for (int i = 0; i < al.length; i++) { for (int j = 0; j < al[i].size(); j++) { if (al[i].get(j) == x) al[i].remove(j); } } } public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public void printDivisors(long n, ArrayList < Long > al) { // Note that this loop runs till square root for (long i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { // If divisors are equal, print only one if (n / i == i) { al.add(i); } else // Otherwise print both { al.add(i); al.add(n / i); } } } } public static long constructSegment(long seg[], long arr[], int low, int high, int pos) { if (low == high) { seg[pos] = arr[low]; return seg[pos]; } int mid = (low + high) / 2; long t1 = constructSegment(seg, arr, low, mid, (2 * pos) + 1); long t2 = constructSegment(seg, arr, mid + 1, high, (2 * pos) + 2); seg[pos] = t1 + t2; return seg[pos]; } public static long querySegment(long seg[], int low, int high, int qlow, int qhigh, int pos) { if (qlow <= low && qhigh >= high) { return seg[pos]; } else if (qlow > high || qhigh < low) { return 0; } else { long ans = 0; int mid = (low + high) / 2; ans += querySegment(seg, low, mid, qlow, qhigh, (2 * pos) + 1); ans += querySegment(seg, mid + 1, high, qlow, qhigh, (2 * pos) + 2); return ans; } } public static int lcs(char[] X, char[] Y, int m, int n) { if (m == 0 || n == 0) return 0; if (X[m - 1] == Y[n - 1]) return 1 + lcs(X, Y, m - 1, n - 1); else return Integer.max(lcs(X, Y, m, n - 1), lcs(X, Y, m - 1, n)); } public static long recursion(long start, long end, long cnt[], int a, int b) { long min = 0; long count = 0; int ans1 = -1; int ans2 = -1; int l = 0; int r = cnt.length - 1; while (l <= r) { int mid = (l + r) / 2; if (cnt[mid] >= start) { ans1 = mid; r = mid - 1; } else { l = mid + 1; } } l = 0; r = cnt.length - 1; while (l <= r) { int mid = (l + r) / 2; if (cnt[mid] <= end) { ans2 = mid; l = mid + 1; } else { r = mid - 1; } } if (ans1 == -1 || ans2 == -1 || ans2 < ans1) { // System.out.println("min1 "+min); min = a; return a; } else { min = b * (end - start + 1) * (ans2 - ans1 + 1); } if (start == end) { // System.out.println("min "+min); return min; } long mid = (end + start) / 2; min = Long.min(min, recursion(start, mid, cnt, a, b) + recursion(mid + 1, end, cnt, a, b)); // System.out.println("min "+min); return min; } public int dfs_util(ArrayList < Integer > [] al, boolean vis[], int x, int[] s, int lvl[]) { vis[x] = true; int cnt = 1; for (int i = 0; i < al[x].size(); i++) { if (!vis[al[x].get(i)]) { lvl[al[x].get(i)] = lvl[x] + 1; cnt += dfs_util(al, vis, al[x].get(i), s, lvl); } } s[x] = cnt; return s[x]; } public void dfs(ArrayList[] al, int[] s, int[] lvl) { boolean vis[] = new boolean[al.length]; for (int i = 0; i < al.length; i++) { if (!vis[i]) { lvl[i] = 1; dfs_util(al, vis, i, s, lvl); } } } public int[] computeLps(String s) { int ans[] =new int[s.length()]; char ch[] = s.toCharArray(); int n = s.length(); int i=1; int len=0; ans[0]=0; while(i<n) { if(ch[i]==ch[len]) { len++; ans[i]=len; i++; } else { if(len!=0) { len=ans[len-1]; } else { ans[i]=len; i++; } } } return ans; } private void solve(InputReader inp, PrintWriter out1) { int n = inp.nextInt(); int m = inp.nextInt(); long k = inp.nextLong(); long arr[] = new long[n]; for(int i=0;i<n;i++) { arr[i] = inp.nextLong(); } long ans=0; for(int i=0;i<m;i++) { long sum=0; for(int j=i;j<n;j++) { if(j%m==i) { if(sum<0) { sum=0; } sum-=k; } sum+=arr[j]; ans=Math.max(ans,sum); } } System.out.println(ans); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } } class ele implements Comparable < ele > { int value; int i; boolean flag; public ele(int value, int i) { this.value = value; this.i = i; this.flag = false; } public int compareTo(ele e) { return Integer.compare(this.value, e.value); } }
n_square
1038.java
0.9
// A simple Java program to //count pairs with difference k import java.util.*; import java.io.*; class GFG { static int countPairsWithDiffK( int arr[], int n, int k) { int count = 0 ; // Pick all elements one by one for ( int i = 0 ; i < n; i++) { // See if there is a pair // of this picked element for ( int j = i + 1 ; j < n; j++) if (arr[i] - arr[j] == k || arr[j] - arr[i] == k) count++; } return count; } // Driver code public static void main(String args[]) { int arr[] = { 1 , 5 , 3 , 4 , 2 }; int n = arr.length; int k = 3 ; System.out.println( "Count of pairs with given diff is " + countPairsWithDiffK(arr, n, k)); } } // This code is contributed // by Sahil_Bansall
n_square
104.java
0.9
// Java program program to merge two // sorted arrays with O(1) extra space. import java.util.Arrays; class Test { static int arr1[] = new int []{ 1 , 5 , 9 , 10 , 15 , 20 }; static int arr2[] = new int []{ 2 , 3 , 8 , 13 }; static void merge( int m, int n) { // Iterate through all elements of ar2[] starting from // the last element for ( int i=n- 1 ; i>= 0 ; i--) { /* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater element is not found */ int j, last = arr1[m- 1 ]; for (j=m- 2 ; j >= 0 && arr1[j] > arr2[i]; j--) arr1[j+ 1 ] = arr1[j]; // If there was a greater element if (j != m- 2 || last > arr2[i]) { arr1[j+ 1 ] = arr2[i]; arr2[i] = last; } } } // Driver method to test the above function public static void main(String[] args) { merge(arr1.length,arr2.length); System.out.print( "After Merging nFirst Array: " ); System.out.println(Arrays.toString(arr1)); System.out.print( "Second Array: " ); System.out.println(Arrays.toString(arr2)); } }
n_square
107.java
0.9
// Java Program to find max subarray // sum excluding some elements import java.io.*; class GFG { // Function to check the element // present in array B static boolean isPresent( int B[], int m, int x) { for ( int i = 0 ; i < m; i++) if (B[i] == x) return true ; return false ; } // Utility function for findMaxSubarraySum() // with the following parameters // A => Array A, // B => Array B, // n => Number of elements in Array A, // m => Number of elements in Array B static int findMaxSubarraySumUtil( int A[], int B[], int n, int m) { // set max_so_far to INT_MIN int max_so_far = - 2147483648 , curr_max = 0 ; for ( int i = 0 ; i < n; i++) { // if the element is present in B, // set current max to 0 and move to // the next element if (isPresent(B, m, A[i])) { curr_max = 0 ; continue ; } // Proceed as in Kadane's Algorithm curr_max = Math.max(A[i], curr_max + A[i]); max_so_far = Math.max(max_so_far, curr_max); } return max_so_far; } // Wrapper for findMaxSubarraySumUtil() static void findMaxSubarraySum( int A[], int B[], int n, int m) { int maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m); // This case will occour when all // elements of A are are present // in B, thus no subarray can be formed if (maxSubarraySum == - 2147483648 ) { System.out.println( "Maximum Subarray Sum" + " " + "can't be found" ); } else { System.out.println( "The Maximum Subarray Sum = " + maxSubarraySum); } } // Driver code public static void main(String[] args) { int A[] = { 3 , 4 , 5 , - 4 , 6 }; int B[] = { 1 , 8 , 5 }; int n = A.length; int m = B.length; // Calling Function findMaxSubarraySum(A, B, n, m); } } // This code is contributed by Ajit.
n_square
115.java
0.9
// java program to find maximum // equilibrium sum. import java.io.*; class GFG { // Function to find maximum // equilibrium sum. static int findMaxSum( int []arr, int n) { int res = Integer.MIN_VALUE; for ( int i = 0 ; i < n; i++) { int prefix_sum = arr[i]; for ( int j = 0 ; j < i; j++) prefix_sum += arr[j]; int suffix_sum = arr[i]; for ( int j = n - 1 ; j > i; j--) suffix_sum += arr[j]; if (prefix_sum == suffix_sum) res = Math.max(res, prefix_sum); } return res; } // Driver Code public static void main (String[] args) { int arr[] = {- 2 , 5 , 3 , 1 , 2 , 6 , - 4 , 2 }; int n = arr.length; System.out.println(findMaxSum(arr, n)); } } // This code is contributed by anuj_67.
n_square
117.java
0.9
// Java program to find equilibrium // index of an array class EquilibriumIndex { int equilibrium( int arr[], int n) { int i, j; int leftsum, rightsum; /* Check for indexes one by one until an equilibrium index is found */ for (i = 0 ; i < n; ++i) { /* get left sum */ leftsum = 0 ; for (j = 0 ; j < i; j++) leftsum += arr[j]; /* get right sum */ rightsum = 0 ; for (j = i + 1 ; j < n; j++) rightsum += arr[j]; /* if leftsum and rightsum are same, then we are done */ if (leftsum == rightsum) return i; } /* return -1 if no equilibrium index is found */ return - 1 ; } // Driver code public static void main(String[] args) { EquilibriumIndex equi = new EquilibriumIndex(); int arr[] = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 }; int arr_size = arr.length; System.out.println(equi.equilibrium(arr, arr_size)); } } // This code has been contributed by Mayank Jaiswal
n_square
120.java
0.9
class LeadersInArray { /*Java Function to print leaders in an array */ void printLeaders( int arr[], int size) { for ( int i = 0 ; i < size; i++) { int j; for (j = i + 1 ; j < size; j++) { if (arr[i] <= arr[j]) break ; } if (j == size) // the loop didn't break System.out.print(arr[i] + " " ); } } /* Driver program to test above functions */ public static void main(String[] args) { LeadersInArray lead = new LeadersInArray(); int arr[] = new int []{ 16 , 17 , 4 , 3 , 5 , 2 }; int n = arr.length; lead.printLeaders(arr, n); } }
n_square
122.java
0.9
// Java program to find Majority // element in an array import java.io.*; class GFG { // Function to find Majority element // in an array static void findMajority( int arr[], int n) { int maxCount = 0 ; int index = - 1 ; // sentinels for ( int i = 0 ; i < n; i++) { int count = 0 ; for ( int j = 0 ; j < n; j++) { if (arr[i] == arr[j]) count++; } // update maxCount if count of // current element is greater if (count > maxCount) { maxCount = count; index = i; } } // if maxCount is greater than n/2 // return the corresponding element if (maxCount > n/ 2 ) System.out.println (arr[index]); else System.out.println ( "No Majority Element" ); } // Driver code public static void main (String[] args) { int arr[] = { 1 , 1 , 2 , 1 , 3 , 5 , 1 }; int n = arr.length; // Function calling findMajority(arr, n); } //This code is contributed by ajit. }
n_square
128.java
0.9