Professional Documents
Culture Documents
21BCS10193_Arjun Kumar_Day_7
21BCS10193_Arjun Kumar_Day_7
21BCS10193_Arjun Kumar_Day_7
COMPUTERSCIENCE& ENGINEERING
Day 7: Domain Camp
Code:
public class Solution {
public int addDigits(int num)
{ if (num == 0) {
return 0;
}
return 1 + (num - 1) % 9;
}
System.out.println(solution.addDigits(38));
System.out.println(solution.addDigits(0));
}
}
Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Q2. Find the Difference
Code:
public class Solution {
public char findTheDifference(String s, String t)
{ int xor = 0;
System.out.println(solution.findTheDifference("abcd", "abcde"));
System.out.println(solution.findTheDifference("", "y"));
}
}
Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 3: Can You Access?
Code:
public class Solution {
public char findTheDifference(String s, String t)
{ int xor = 0;
// Test cases
System.out.println(solution.findTheDifference("abcd", "abcde"));
System.out.println(solution.findTheDifference("", "y"));
}
}
Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 4: ATM Problem
Code:
import java.util.Scanner;
scanner.close();
}
}
Output
Code:
import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
scan.close();
String[] tokens = s.split("[^A-Za-z]+"); List<String>
Output:
Code:
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int romanToInt(String s) {
Map<Character, Integer> romanMap = new HashMap<>();
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
int prevValue = 0;
for (int i = 0; i < s.length(); i++) {
int currentValue = romanMap.get(s.charAt(i));
if (prevValue < currentValue) {
total += currentValue - 2 * prevValue;
} else {
total += currentValue;
}
prevValue = currentValue;
}
return total;
}
Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 7: Pow(x, n)
Code:
public class Solution {
public double myPow(double x, int n) {
if (n == Integer.MIN_VALUE) {
x = x * x;
n = n / 2;
}
if (n < 0) {
x = 1 /
x; n = -
n;
}
double result = 1;
double currentProduct = x;
while (n > 0) {
if (n % 2 == 1) {
result *= currentProduct;
}
currentProduct *= currentProduct;
n /= 2;
}
return result;
}
public static void main(String[] args)
{ Solution solution = new Solution();
System.out.println(solution.myPow(2.00000, 10));
System.out.println(solution.myPow(2.10000, 3));
System.out.println(solution.myPow(2.00000, -2));
}
}
Output:
DEPARTMENT OF
COMPUTERSCIENCE& ENGINEERING
Ques 8. Missing Number
Code:
public class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;