✅Lucky number

Lucky number easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement:

A 2n digits number is said to be lucky if sum of n most significant digits is equal to sum of n least significant digits.

Given a number find out if the number is lucky or not? Input First line contains n. Next line contains a number of 2n digits.

Constraints 1 ≤ n ≤ 105 Number contains digits only. Output Print "LUCKY" if the number is lucky otherwise print "UNLUCKY". Example Input: 3 213411

Output: LUCKY

Explanation : sum of 3 most significant digits = 2 + 1 + 3 = 6 sum of 3 least significant digits = 4 + 1 + 1 = 6

Input: 1 69

Output: UNLUCKY

link:https://my.newtonschool.co/playground/code/jqojdk4zs0pf/

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