β Lucky number
Lucky number easy Time Limit: 2 sec Memory Limit: 128000 kB
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String str=sc.nextLine();
long sumA=0;
long sumB=0;
for(int i=0;i<n;i++){
sumA +=Integer.parseInt(String.valueOf(str.charAt(i)));
}
for(int i=n;i<2*n;i++){
sumB +=Integer.parseInt(String.valueOf(str.charAt(i)));
}
if(sumA==sumB){
System.out.println("LUCKY");
}else{
System.out.println("UNLUCKY");
}
}
}
//method 02
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String str=sc.next();
long sumA=0;
long sumB=0;
for(int i=0;i<n;i++){
sumA +=str.charAt(i)-48;
}
for(int i=n;i<str.length();i++){
sumB +=str.charAt(i)-48;
}
if(sumA==sumB){
System.out.println("LUCKY");
}else{
System.out.println("UNLUCKY");
}
}
}
//or
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String str=sc.next();
long sumA=0;
long sumB=0;
for(int i=0;i<n;i++){
sumA +=str.charAt(i)-'0';
}
for(int i=n;i<str.length();i++){
sumB +=str.charAt(i)-'0';
}
if(sumA==sumB){
System.out.println("LUCKY");
}else{
System.out.println("UNLUCKY");
}
}
}
```
Last updated