You are on page 1of 14

ROLEX SIR 🔪

JAVA ONLINE SUBMISSION SUMS OF WEEK – 5&6

PAGE - 83

https://leetcode.com/problems/convert-a-number-to-
hexadecimal/

class Solution {
public String toHex(int num) {
if (num == 0)
return "0";
int[] bytes = new int[8];
for (int i = 0; i < 8; i++) {
int temp = (num >> 4 * i) & 0xf;
bytes[7 - i] = temp;
}
String hex = "";
boolean flag = false;
ROLEX SIR 🔪

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


int curByte = bytes[i];
if (!flag && curByte == 0)
continue;
hex += hexChar(curByte);
flag = true;
}
return hex;
}

public char hexChar(int num) {


if (num < 10)
return (char) (num + '0');
else
return (char) (num - 10 + 'a');
}
}

PAGE - 85
ROLEX SIR 🔪

note :-- earase the temporary code and paste it

https://www.hackerrank.com/challenges/java-int-to-
string/problem

String s = Integer.toString(n);

page - 87

https://leetcode.com/problems/nim-game/

class Solution {
public boolean canWinNim(int n) {
return (n % 4 != 0);
}
ROLEX SIR 🔪

PAGE - 91

https://leetcode.com/problems/number-of-unequal-
triplets-in-array/

class Solution {
public int unequalTriplets(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int v : nums) {
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
}
int ans = 0, a = 0;
int n = nums.length;
for (int b : cnt.values()) {
int c = n - a - b;
ans += a * b * c;
a += b;
}
ROLEX SIR 🔪

return ans;
}
}

PAGE - 94

https://leetcode.com/problems/sort-the-people/

class Solution {
public String[] sortPeople(String[] names, int[] heights)
{
int n = heights.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {heights[i], i};
}
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
String[] ans = new String[n];
for (int i = 0; i < n; ++i) {
ROLEX SIR 🔪

ans[i] = names[arr[i][1]];
}
return ans;
}
}

PAGE - 99

https://leetcode.com/problems/most-frequent-even-
element/description/

class Solution {
public int mostFrequentEven(int[] A) {
HashMap<Integer,Integer> mp= new HashMap<>();
int val=1000000,freq=0;
for(var i:A){
//if even element
if(i%2==0){
//increase frequency in map
int curr= mp.getOrDefault(i,0)+1;
mp.put(i,curr);
ROLEX SIR 🔪

//Update smallest with greatest frequency


if(curr>freq || curr==freq && i<val){
val=i;
freq=curr;
}
}
}
return freq==0? -1 : val;
}
}

WEEK - 6

PAGE - 104

https://www.hackerrank.com/challenges/java-
inheritance-2/problem
ROLEX SIR 🔪

class Arithmetic{
public int add(int a, int b){
int sum = a + b;
return sum;
}
}

class Adder extends Arithmetic{


public int callAdd(int a, int b){
return add(a, b);
}
}

PAGE - 106

https://www.hackerrank.com/challenges/java-
inheritance-1/problem
ROLEX SIR 🔪

class Bird extends Animal {


void fly() {
System.out.println("I am flying");
}

void sing() {
System.out.println("I am singing");
}
}

PAGE - 107

https://www.hackerrank.com/challenges/java-
singleton/problem?isFullScreen=true

import java.io.*;
import java.util.*;
ROLEX SIR 🔪

import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.reflect.*;

class Singleton {
private static Singleton instance;
public static String str;
private Singleton() {}

static Singleton getSingleInstance() {


if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

PAGE - 113
ROLEX SIR 🔪

https://www.hackerrank.com/challenges/java-
arraylist/problem

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args)


{

Scanner scan = new Scanner(System.in);


ArrayList[] list= new ArrayList[20002];
int n;
n=scan.nextInt();
for(int i=1;i<=n;i++)
{

list[i]=new ArrayList();
ROLEX SIR 🔪

int x=scan.nextInt();

for(int j=1;j<=x;j++)
{
int val=scan.nextInt();
list[i].add(val);

}
}
int q=scan.nextInt();

for(int i=1;i<=q;i++)
{
int x,y;
x=scan.nextInt();
y=scan.nextInt();
try
{
System.out.println(list[x].get(y-1));
}catch(Exception e)
{
System.out.println("ERROR!");
ROLEX SIR 🔪

}
}
}
}

PAGE - 115

https://www.hackerrank.com/challenges/java-
interface/problem?isFullScreen=true

//Write your code here


class MyCalculator implements AdvancedArithmetic {
public int divisor_sum(int n) {
if (n <= 1) { return n; }

int res = n + 1;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
ROLEX SIR 🔪

res += i;
}
}

return res;
}
}

You might also like