Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
90 views

Codeforces Java Solutions

codeforces java solutions

Uploaded by

Ujjwal Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Codeforces Java Solutions

codeforces java solutions

Uploaded by

Ujjwal Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Solutions for Codeforces Problems

Problem A: Young Physicist

Java Code:

-------------------

import java.util.Scanner;

public class YoungPhysicist {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int sumX = 0, sumY = 0, sumZ = 0;

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

int x = sc.nextInt();

int y = sc.nextInt();

int z = sc.nextInt();

sumX += x;

sumY += y;

sumZ += z;

if (sumX == 0 && sumY == 0 && sumZ == 0) {

System.out.println("YES");

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

-------------------

Problem B: Nearly Lucky Number

Java Code:

-------------------

import java.util.Scanner;

public class NearlyLuckyNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

long n = sc.nextLong();

int count = 0;

while (n > 0) {

long digit = n % 10;

if (digit == 4 || digit == 7) {

count++;

n /= 10;

}
boolean isLucky = true;

while (count > 0) {

int digit = count % 10;

if (digit != 4 && digit != 7) {

isLucky = false;

break;

count /= 10;

if (isLucky) {

System.out.println("YES");

} else {

System.out.println("NO");

-------------------

Problem C: Panoramix's Prediction

Java Code:

-------------------

import java.util.Scanner;

public class PanoramixPrediction {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int m = sc.nextInt();

if (isNextPrime(n, m)) {

System.out.println("YES");

} else {

System.out.println("NO");

public static boolean isPrime(int num) {

if (num < 2) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

public static boolean isNextPrime(int n, int m) {

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

if (isPrime(i)) return false;

return isPrime(m);

}
}

-------------------

Problem D: I_love_%username%

Java Code:

-------------------

import java.util.Scanner;

public class ILoveUsername {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int firstScore = sc.nextInt();

int minScore = firstScore, maxScore = firstScore;

int amazingCount = 0;

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

int score = sc.nextInt();

if (score > maxScore) {

amazingCount++;

maxScore = score;

} else if (score < minScore) {

amazingCount++;

minScore = score;

}
System.out.println(amazingCount);

-------------------

Problem E: Team

Java Code:

-------------------

import java.util.Scanner;

public class Team {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int solveCount = 0;

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

int petya = sc.nextInt();

int vasya = sc.nextInt();

int tonya = sc.nextInt();

if (petya + vasya + tonya >= 2) {

solveCount++;

}
System.out.println(solveCount);

-------------------

Problem F: Even Odds

Java Code:

-------------------

import java.util.Scanner;

public class EvenOdds {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

long n = sc.nextLong();

long k = sc.nextLong();

long oddCount = (n + 1) / 2;

if (k <= oddCount) {

System.out.println(2 * k - 1); // k-th odd number

} else {

System.out.println(2 * (k - oddCount)); // k-th even number

-------------------
Problem G: Jeff and Digits

Java Code:

-------------------

import java.util.Scanner;

public class JeffAndDigits {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int count5 = 0, count0 = 0;

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

int digit = sc.nextInt();

if (digit == 5) {

count5++;

} else if (digit == 0) {

count0++;

if (count0 == 0) {

System.out.println("-1"); // No zeroes, can't form a number divisible by 10

} else if (count5 < 9) {

System.out.println("0"); // Not enough '5's to make the sum divisible by 9

} else {
// Print as many '5's as possible that are divisible by 9, followed by '0's

for (int i = 0; i < count5 / 9 * 9; i++) {

System.out.print("5");

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

System.out.print("0");

System.out.println();

-------------------

Problem H: Five Layers of Patterns

Java Code:

-------------------

import java.util.Scanner;

public class FivePatterns {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int m = sc.nextInt();

// Pattern 1: Grid of Stars

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


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

System.out.print("*");

System.out.println();

System.out.println();

// Pattern 2: Alphabet Grid

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

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

System.out.print((char)('A' + i));

System.out.println();

System.out.println();

// Pattern 3: Alphabet Sequence

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

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

System.out.print((char)('A' + j));

System.out.println();

System.out.println();

// Pattern 4: Number Grid


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

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

System.out.print(i + 1);

System.out.println();

System.out.println();

// Pattern 5: Sequential Numbers

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

for (int j = 1; j <= m; j++) {

System.out.print(j);

System.out.println();

-------------------

You might also like