forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved issue 238. All Problems organised. Links are fioxed. Readme fi…
…le updated.
- Loading branch information
1 parent
cfaf75d
commit 8fd03d5
Showing
71 changed files
with
621 additions
and
621 deletions.
There are no files selected for viewing
File renamed without changes.
90 changes: 45 additions & 45 deletions
90
Algorithms/Lonely Integer/solution.java → ...Manipulation/Lonely Integer/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
114 changes: 57 additions & 57 deletions
114
...t Manipulation/Xor sequence/solution.java → ...t Manipulation/Xor-sequence/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 28 additions & 28 deletions
56
Algorithms/Bill Division/solution.java → ...mplementation/Bill Division/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
Oops, something went wrong.