Skip to content

Commit

Permalink
Solved issue 238. All Problems organised. Links are fioxed. Readme fi…
Browse files Browse the repository at this point in the history
…le updated.
  • Loading branch information
chirag-ghosh committed Dec 23, 2020
1 parent cfaf75d commit 8fd03d5
Show file tree
Hide file tree
Showing 71 changed files with 621 additions and 621 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// The method would return the unique value in the array
// O(n) runtime. O(1) space. Uses XOR. Keep in mind:
// 1) x ^ x = 0
// 2) x ^ 0 = x
// 3) XOR is commutative and associative
static int lonelyinteger(int[] a) {
// We would XOR all the numbers in the array
// Only the unique number would be left
int val = 0;
for (int num : a) {
val = val ^ num; // ^ is XOR operator
}
return val;
}

/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] a = new int[n];
String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}
int result = lonelyinteger(a);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// The method would return the unique value in the array
// O(n) runtime. O(1) space. Uses XOR. Keep in mind:
// 1) x ^ x = 0
// 2) x ^ 0 = x
// 3) XOR is commutative and associative
static int lonelyinteger(int[] a) {
// We would XOR all the numbers in the array
// Only the unique number would be left
int val = 0;
for (int num : a) {
val = val ^ num; // ^ is XOR operator
}
return val;
}

/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] a = new int[n];
String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}
int result = lonelyinteger(a);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Method to compute the value of XOR sequence
static long xorSequence(long l, long r) {
//The answer is the xor of value obtained by calc function of left and right range
long answer = calc(r) ^ calc(l - 1);
return answer;
}
//Perform xor of 7 with the limit passed and return on the basis of the conditions
private static long calc(long limit) {
switch ((int) (limit & 7)) {
case 0:
return limit;
case 1:
return limit;
case 2:
return 2;
case 3:
return 2;
case 4:
return limit + 2;
case 5:
return limit + 2;
case 6:
return 0;
case 7:
return 0;
default:
throw new RuntimeException();
}
}
/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String[] lr = scanner.nextLine().split(" ");
long l = Long.parseLong(lr[0]);
long r = Long.parseLong(lr[1]);
long result = xorSequence(l, r);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Method to compute the value of XOR sequence
static long xorSequence(long l, long r) {
//The answer is the xor of value obtained by calc function of left and right range
long answer = calc(r) ^ calc(l - 1);
return answer;
}
//Perform xor of 7 with the limit passed and return on the basis of the conditions
private static long calc(long limit) {
switch ((int) (limit & 7)) {
case 0:
return limit;
case 1:
return limit;
case 2:
return 2;
case 3:
return 2;
case 4:
return limit + 2;
case 5:
return limit + 2;
case 6:
return 0;
case 7:
return 0;
default:
throw new RuntimeException();
}
}
/* Driver Code */
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String[] lr = scanner.nextLine().split(" ");
long l = Long.parseLong(lr[0]);
long r = Long.parseLong(lr[1]);
long result = xorSequence(l, r);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import java.util.*;

class Solution {

public static void bonAppetit(int[] cost, int k, int charged) {
int total = (charged << 1);
// Traverse through the cost array and subtract the current value from cost array
for (int i : cost) {
total = total - i;
}
// If total is greater than print half the cost
// else print Bon Appetit
System.out.println( (total >= 0) ? cost[k] / 2 : "Bon Appetit");
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[] cost = new int[n];
for (int i = 0; i < n; i++) {
cost[i] = scan.nextInt();
}
int charged = scan.nextInt();
scan.close();

bonAppetit(cost, k, charged);
}
import java.util.*;

class Solution {

public static void bonAppetit(int[] cost, int k, int charged) {
int total = (charged << 1);
// Traverse through the cost array and subtract the current value from cost array
for (int i : cost) {
total = total - i;
}
// If total is greater than print half the cost
// else print Bon Appetit
System.out.println( (total >= 0) ? cost[k] / 2 : "Bon Appetit");
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[] cost = new int[n];
for (int i = 0; i < n; i++) {
cost[i] = scan.nextInt();
}
int charged = scan.nextInt();
scan.close();

bonAppetit(cost, k, charged);
}
}
File renamed without changes.
Loading

0 comments on commit 8fd03d5

Please sign in to comment.