-
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.
- Loading branch information
1 parent
fe4d2f2
commit ebdf533
Showing
9 changed files
with
262 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package BitManupulation; | ||
|
||
public class AddTwo { | ||
public static void main(String[] args) { | ||
System.out.println(getSum(4,3)); | ||
} | ||
public static int getSum(int a, int b) { | ||
int res =0,idx = 0; | ||
while(a > 0 || b > 0){ | ||
|
||
int carry =0 , sum = 0, x = res; | ||
if((a&1) == 1 && (b&1) == 1){ | ||
carry = 1; | ||
sum = 0; | ||
}else{ | ||
sum = 1; | ||
carry = 0; | ||
} | ||
a = a >> 1; | ||
b = b >> 1; | ||
res = x << idx | sum; | ||
if(carry == 1){ | ||
res = x << idx+1 | carry; | ||
} | ||
idx++; | ||
} | ||
return res; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class MaxProd { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int num1 = sc.nextInt(), num2 = sc.nextInt(); | ||
char[] a = Integer.toString(num1).toCharArray(),b = Integer.toString(num2).toCharArray(); | ||
int n = Math.min(a.length, b.length); | ||
int new1 = 0, new2 = 0; | ||
int base = 1; | ||
for(int i = 0 ; i < n ; i++){ | ||
if(i % 2 != 0){ | ||
new1 = a[i] + new1*base; | ||
new2 = b[i] + new2*base; | ||
}else{ | ||
new1 = b[i] + new1*base; | ||
new2 = a[i] + new2*base; | ||
} | ||
} | ||
System.out.println(new1 + " " + new2); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package Priority_queue; | ||
|
||
import java.util.Arrays; | ||
import java.util.PriorityQueue; | ||
|
||
public class NumberGame { | ||
public static void main(String[] args) { | ||
// System.out.println(printMaxDis(new int[]{4,8,2,8})); | ||
System.out.println(Arrays.toString(numberGame(new int []{5,4,2}))); | ||
} | ||
public static int[] numberGame(int[] nums) { | ||
PriorityQueue<Integer> pq = new PriorityQueue<>(); | ||
for (int num : nums) { | ||
pq.add(num); | ||
} | ||
int [] res = new int[pq.size()]; | ||
int i = 0; | ||
while(!pq.isEmpty()){ | ||
if(i+1 < nums.length){ | ||
res[i+1] = pq.poll(); | ||
}else { | ||
res[i] = pq.poll(); | ||
} | ||
if(!pq.isEmpty()){ | ||
res[i] = pq.poll(); | ||
} | ||
i = i+2; | ||
} | ||
return res; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package Recursion.basic; | ||
|
||
public class PrintN { | ||
|
||
public static void main(String[] args) { | ||
print(1); | ||
} | ||
private static void print(int n){ | ||
System.out.print(n + " "); | ||
if(n == 5) return; | ||
print(n+1); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package Recursion.basic; | ||
|
||
public class PrintNameNTimes { | ||
public static void main(String[] args) { | ||
printName(5); | ||
} | ||
public static void printName(int n){ | ||
if(n <= 0){ | ||
return; | ||
} | ||
System.out.print("FARAZ" + " "); | ||
printName(n-1); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package Recursion.basic; | ||
|
||
public class SumOfOneToN { | ||
public static void main(String[] args) { | ||
SumOfN(10 , 0); | ||
} | ||
|
||
private static void SumOfN(int i, int sum) { | ||
if(i < 1) { | ||
System.out.println(sum); | ||
return; | ||
} | ||
SumOfN(i-1,sum+i); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package SlidingWindow; | ||
|
||
import java.util.*; | ||
public class Xyz{ | ||
public static void main(String[] args) { | ||
// System.out.println(printMaxDis(new int[]{4,8,2,8})); | ||
System.out.println(Arrays.toString(numberGame(new int []{5,4,2}))); | ||
} | ||
public static int[] numberGame(int[] nums) { | ||
PriorityQueue<Integer> pq = new PriorityQueue<>(); | ||
for (int num : nums) { | ||
pq.add(num); | ||
} | ||
int [] res = new int[pq.size()]; | ||
int i = 0; | ||
while(!pq.isEmpty()){ | ||
if(i+1 < nums.length){ | ||
res[i+1] = pq.poll(); | ||
}else { | ||
res[i] = pq.poll(); | ||
} | ||
if(!pq.isEmpty()){ | ||
res[i] = pq.poll(); | ||
} | ||
i = i+2; | ||
} | ||
return res; | ||
} | ||
private static int printMaxDis(int[] nums) { | ||
int maxIndex = 0 , minIndex = nums.length; | ||
for(int i = 0; i < nums.length; i++){ | ||
int count = 0; | ||
for(int j = 1 ; j < nums[i] ; j++){ | ||
if(nums[i] % j == 0){ | ||
count++; | ||
} | ||
} | ||
if(count == 1){ | ||
maxIndex = Math.max(maxIndex,i); | ||
minIndex = Math.min(minIndex,i); | ||
} | ||
} | ||
return maxIndex - minIndex; | ||
} | ||
} |
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,5 +1,63 @@ | ||
import java.util.*; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Scanner; | ||
|
||
public class checks { | ||
public static void main(String[] args) { | ||
// Scanner sc = new Scanner(System.in); | ||
// int cases = sc.nextInt(); | ||
// while(cases > 0){ | ||
// int n = sc.nextInt(), k = sc.nextInt(); | ||
// int [] dur = new int[n]; | ||
// for(int i = 0 ; i < n ; i++){ | ||
// dur[i] = sc.nextInt(); | ||
// } | ||
// AttackShip(dur,k); | ||
// cases--; | ||
// } | ||
System.out.println(timeRequiredToBuy(new int[]{2,3,2},2)); | ||
System.out.println(ForMinString("41312",2)); | ||
} | ||
|
||
private static void AttackShip(int[] dur, int k) { | ||
int shipsFallen = 0; | ||
int p1 = 0 , p2 = dur.length-1; | ||
for(int i = 1; i <= k && p1 <= p2 ; i++){ | ||
if(i%2 != 0){ | ||
dur[p1] = dur[p1]-1; | ||
if(dur[p1] == 0){ | ||
p1++; | ||
shipsFallen++; | ||
} | ||
}else{ | ||
dur[p2] = dur[p2]-1; | ||
if(dur[p2] == 0){ | ||
p2--; | ||
shipsFallen++; | ||
} | ||
} | ||
} | ||
System.out.println(shipsFallen); | ||
} | ||
private static String ForMinString(String num , int k){ | ||
String min = ""; | ||
int mini = Integer.MAX_VALUE; | ||
for(int i = 0; i < num.length() ; i++){ | ||
String curr = num.substring(i,num.length()-1); | ||
} | ||
return min; | ||
} | ||
public static int timeRequiredToBuy(int[] tickets, int k) { | ||
int time = 0; | ||
|
||
for (int i = 0; i < tickets.length; i++) { | ||
if (i <= k) { | ||
time += Math.min(tickets[i], tickets[k]); | ||
} else { | ||
time += Math.min(tickets[i], tickets[k] - 1); | ||
} | ||
} | ||
|
||
return time; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
import java.util.Scanner; | ||
|
||
public class softDrink { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
// int a = sc.nextInt() , b = sc.nextInt() , c = sc.nextInt(); | ||
// int toReachA = Math.abs(b-a) + Math.abs(c-a), toReactB = Math.abs(b-a) + Math.abs(c-b), toReactC = Math.abs(c-a) + Math.abs(c-b); | ||
// System.out.println(Math.min(Math.min(toReachA,toReactB),toReactC)); | ||
// System.out.println(Integer.toBinaryString(5000)); | ||
int cases = sc.nextInt(); | ||
while (cases > 0){ | ||
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); | ||
int ans = PaintRibbons(a,b,c); | ||
|
||
if(ans == 0) System.out.println("NO"); | ||
else System.out.println("YES"); | ||
cases--; | ||
} | ||
} | ||
private static int PaintRibbons(int a,int b , int c){ | ||
int differentPairs = (a/b) * (b-1); | ||
// if(a %2 == 0){ | ||
// differentPairs = (a/b) * (b-1); | ||
// }else { | ||
// differentPairs = (a/b)*(b-1) + 1; | ||
// } | ||
if(differentPairs > c){ | ||
return 1; | ||
}else return 0; | ||
} | ||
} |