Skip to content

Commit 0e043b2

Browse files
2 parents 9ca9d30 + 36822a0 commit 0e043b2

21 files changed

Lines changed: 853 additions & 1 deletion

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"java.project.sourcePaths": [
3+
"Basic Codes",
4+
"Number Theory"
5+
]
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import java.util.*;
3+
4+
public class maxLength_Of_Subarray {
5+
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
int k = sc.nextInt();
10+
int[] arr = new int[n];
11+
for(int i = 0; i < n; i++){
12+
arr[i] = sc.nextInt();
13+
}
14+
System.out.println(countSubarrays(arr,n,k));
15+
sc.close();
16+
}
17+
18+
public static int countSubarrays(int[] arr, int n, int k) {
19+
20+
HashMap<Integer, Integer> map = new HashMap<>();
21+
int sum = 0, maxLen = 0; // sum = prefix sum which stores sum of every element, maxlen = maximum length of sub array
22+
for(int i = 0; i < n; i++){
23+
sum += arr[i];
24+
if(sum==k){
25+
maxLen = i + 1; // if k itself is present in the array then length will start from 0 to index of k
26+
}
27+
if(!map.containsKey(sum)){ // putting frequencies of every sum
28+
map.put(sum, i);
29+
}
30+
if(map.containsKey(sum-k)){
31+
if(maxLen < (i-map.get(sum-k))){ // updating the maxlen variable from starting point of sub array to end point
32+
maxLen = i - map.get(sum-k);
33+
}
34+
}
35+
}
36+
37+
return maxLen;
38+
39+
}
40+
41+
}

Bit Manipulation/PrintBinary.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Given a number print its Binary Representation using Bit Manipulation
3+
Examples:
4+
5+
Input: 2
6+
Output: 10
7+
8+
Input: 64
9+
Output: 1000000
10+
*/
11+
12+
import java.util.Scanner;
13+
14+
public class PrintBinary {
15+
16+
public static void main(String[] args) {
17+
18+
Scanner sc = new Scanner(System.in);
19+
20+
int num = sc.nextInt();
21+
22+
printBinary(num);
23+
}
24+
25+
public static void printBinary(int num) {
26+
27+
if (num == 0) {
28+
System.out.print(0);
29+
return;
30+
}
31+
32+
//Number of bits needed to represent a number in Binary
33+
int bits = (int) (Math.log(num) / Math.log(2)) + 1;
34+
35+
for (int i = bits - 1; i >= 0; --i) {
36+
//ith bit is set
37+
if ((num & (1 << i)) != 0) {
38+
System.out.print("1");
39+
}
40+
//ith bit is not set
41+
else {
42+
System.out.print("0");
43+
}
44+
}
45+
}
46+
}
47+

CodeChef/JavelinQualification.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
There are N players with IDs from 1 to N, who are participating in the Javelin throw competition which has two rounds. The first is the qualification round, followed by the final round. The qualification round has gotten over, and you are given the longest distance that each of the N players has thrown as A1,A2,…,AN. Now, the selection process for the final round happens in the following two steps:
3+
4+
If the longest throw of a player in the qualification round is greater than or equal to the qualification mark of M cm, they qualify for the final round.
5+
6+
If after step 1, less than X players have qualified for the finals, the remaining spots are filled by players who have thrown the maximum distance among the players who have not qualified yet.
7+
8+
You are given the best throws of the N players in the qualification round A1,A2,…,AN and the integers M and X. Print the list of the players who will qualify for the finals in increasing order of their IDs.
9+
10+
Sample Input 1
11+
3
12+
3 8000 2
13+
5000 5001 5002
14+
3 5000 2
15+
7999 7998 8000
16+
4 6000 3
17+
5999 5998 6000 6001
18+
19+
Sample Output 1
20+
2 2 3
21+
3 1 2 3
22+
3 1 3 4
23+
*/
24+
25+
import java.util.*;
26+
27+
public class JavelinQualification
28+
{
29+
public static void main (String[] args) throws java.lang.Exception
30+
{
31+
// your code goes here
32+
Scanner sc = new Scanner(System.in);
33+
int t = sc.nextInt();
34+
while(t-->0){
35+
int n = sc.nextInt();
36+
int m = sc.nextInt();
37+
int x = sc.nextInt();
38+
int[] arr = new int[n]; // creating array which stores throws of each player
39+
int[] player = new int[n];
40+
int count = 0;
41+
HashMap<Integer,Integer> map = new HashMap<>();
42+
int index = 0;
43+
for(int i = 0; i < n; i++){
44+
arr[i] = sc.nextInt();
45+
map.put(arr[i],i+1);
46+
if(arr[i]>=m){ // if the throw is qualified
47+
player[index] = i+1; // storing selected players in 'player' array
48+
arr[i] = 0;
49+
count++; // storing count of qualified players
50+
x--;
51+
index++;
52+
}
53+
}
54+
while(x-->0){ // getting remaining x player who are left
55+
int max1 = map.getOrDefault(max(arr, n),0); // creating variable to get index of maximum of the remaining players
56+
if(arr[max1-1]>0){ // checking if the player is not selected
57+
player[index] = max1;
58+
arr[max1-1] = 0;
59+
count++;
60+
index++;
61+
}
62+
}
63+
Arrays.sort(player); // sorting player in increasing order
64+
System.out.print(count+" ");
65+
for(int i = 0; i < n; i++){
66+
if(player[i]!=0){ // if the player is qualified
67+
System.out.print(player[i]+" ");
68+
}
69+
}
70+
System.out.println();
71+
}
72+
sc.close();
73+
}
74+
static int max(int[] arr, int n){ // funtion to get maximum throw
75+
int max = Integer.MIN_VALUE;
76+
for(int i = 0; i < n; i++){
77+
if(max<arr[i]){
78+
max=arr[i];
79+
}
80+
}
81+
return max;
82+
}
83+
}

CodeChef/MagicalDoors.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Question;
2+
3+
Chef wants to cross a hallway of N doors. These N doors are represented as a string. Each door is initially either open or close, represented by 1 or 0 respectively. Chef is required to go through all the doors one by one in the order that they appear, starting from the leftmost door and moving only rightwards at each step.
4+
5+
To make the journey easier, Chef has a magic wand, using which Chef can flip the status of all the doors at once. Determine the minimum number of times Chef has to use this wand to cross the hallway of doors.
6+
7+
For example, the doors are 10011. Chef will start from the left and enter the first door. After passing through that door, the magic wand is waved. This flips the string to 01100. Now Chef passes through the next two doors in one go. Again, just before reaching the 4th door, the magic wand is waved. Now that the string is in its original state, Chef passes through the last two doors as well. The minimum number of times the magic wand needed to be used here was 2.
8+
Input Format
9+
First line will contain T, number of testcases. Then the testcases follow.
10+
Each testcase contains of a single string S, representing the doors as given in the problem.
11+
Output Format
12+
For each test case, print a single line containing one integer denoting the minimum number of times the magic wand needs to be used.
13+
14+
Sample Input
15+
3
16+
111
17+
010
18+
10011
19+
20+
Sample Output
21+
0
22+
3
23+
2
24+
25+
*/
26+
27+
//code
28+
/* package codechef; // don't place package name! */
29+
import java.util.*;
30+
import java.lang.*;
31+
import java.io.*;
32+
/* Name of the class has to be "Main" only if the class is public. */
33+
class MagicalDoors
34+
{ //Main Function
35+
public static void main (String[] args) throws java.lang.Exception
36+
{
37+
Scanner in = new Scanner(System.in); //Scanner for taking input
38+
int t = in.nextInt(); //Variable t for no of test case
39+
while(t>0) //loop for every test case
40+
{
41+
42+
StringBuilder str = new StringBuilder(in.next()); //Taking input int String builder str
43+
long res = 0; //Variabe of long type for storing result
44+
if(str.charAt(0) == '0') //checking the first character of the string (Whether it is 0 or 1)
45+
res++; //if 0 then we are adding 1 to result
46+
for(int i = 1; i<str.length(); i++) //for loop starting from 2nd character
47+
{
48+
if(str.charAt(i) != str.charAt(i-1)) //Checking that is the previous charcter same or not
49+
res++; //if not same then adding 1 to result
50+
}
51+
System.out.println(res); //printing the result
52+
t--; //decrementing the value of t
53+
}
54+
}
55+
}

CodeChef/vaccination_queue.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** There are N people in the vaccination queue, Chef is standing on the Pth position from the front of the queue. It takes X minutes to vaccinate a child and Y minutes to vaccinate an elderly person. Assume Chef is an elderly person.
2+
3+
You are given a binary array A1,A2,…,AN of length N, where Ai=1 denotes there is an elderly person standing on the ith position of the queue, Ai=0 denotes there is a child standing on the ith position of the queue. You are also given the three integers P,X,Y. Find the number of minutes after which Chef's vaccination will be completed.
4+
5+
Input Format
6+
First line will contain T, number of testcases. Then the testcases follow.
7+
The first line of each test case contains four space-separated integers N,P,X,Y.
8+
The second line of each test case contains N space-separated integer A1,A2,…,AN.
9+
Output Format
10+
For each testcase, output in a single line the number of minutes after which Chef's vaccination will be completed.**/
11+
12+
import java.util.*;
13+
public class vaccination_queue
14+
{
15+
public static void main(String args[]) //main function
16+
{
17+
Scanner in = new Scanner (System.in);
18+
int t = in.nextInt(); // taking variable t for no. of test cases
19+
while(t>0)
20+
{
21+
int n = in.nextInt(); //n = No.of people
22+
int p = in.nextInt(); //p = Position of chef from front of the queue
23+
int x = in.nextInt(); //x = Minutes taken to vaccinate a child
24+
int y = in.nextInt(); //y = Minutes taken to vaccinate an elder
25+
int countc =0; //Counter that counts no of child present ahead of chef.
26+
int counta =0; //Counter that counts no of elder present ahead of chef.
27+
int arr[] = new int[n]; //Array to store the position of people statnding in line.
28+
//Loop For storing position of people where 1 stands for elder person and 0 stands for child.
29+
for(int i =0;i<n;i++)
30+
{
31+
arr[i] = in.nextInt();
32+
}
33+
//Loop to count all the children and elder person ahead of chef including the chef
34+
for(int j =0;j<p;j++)
35+
{
36+
if(arr[j]==0) //checking if the person is child then incrementing the counter
37+
{
38+
countc++;
39+
}
40+
if(arr[j]==1) //checking if the person is elder then incrementing the counter
41+
{
42+
counta++;
43+
}
44+
45+
}
46+
int tmin = (countc*x)+(counta*y); //Calculating the total time taken by multiplying with the x and y given.
47+
System.out.println(tmin); //printing the result
48+
49+
50+
51+
t--; //decrementing the value after running a testcase.
52+
}
53+
}
54+
}

GeeksForGeeks/diagonal.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* Given a 2D matrix, print all elements of the given matrix in diagonal order.
2+
For example, consider the following 5 X 4 input matrix.
3+
Example:
4+
5+
1 2 3 4
6+
5 6 7 8
7+
9 10 11 12
8+
13 14 15 16
9+
17 18 19 20
10+
11+
Diagonal printing of the above matrix is
12+
13+
1
14+
5 2
15+
9 6 3
16+
13 10 7 4
17+
17 14 11 8
18+
18 15 12
19+
19 16
20+
20 */
21+
22+
import java.io.*;
23+
class diagonal
24+
{
25+
int [][]a;
26+
int p,q;
27+
diagonal() // A constructor
28+
{
29+
p=0;
30+
q=0;
31+
}
32+
void input ()throws IOException // A method to input the array order and elements.
33+
{ InputStreamReader inp=new InputStreamReader(System.in);
34+
BufferedReader br=new BufferedReader(inp);
35+
System.out.println("Enter the matrix order of the matrix");
36+
p=Integer.parseInt(br.readLine());
37+
q=Integer.parseInt(br.readLine());
38+
a=new int[p][q]; // creating a 2D array.
39+
System.out.println("now enter the matrix elements");
40+
for(int i=0;i<p;i++)
41+
{
42+
for(int j=0;j<q;j++)
43+
{ a[i][j]=Integer.parseInt(br.readLine()); }
44+
}
45+
System.out.println("the matrix elements are");
46+
for(int i=0;i<p;i++)
47+
{
48+
for(int j=0;j<q;j++)
49+
{ System.out.print(a[i][j]+"\t");}
50+
System.out.println("\n");
51+
}
52+
}
53+
void logic() // A method to print the diagonal elements..
54+
{
55+
/* The logic() method prints the elements on the diagonal one by one using their index positions
56+
Firstly starting with the number in the top most left corner. Then the number below it and the
57+
number diagonaly oposite to it is printed, thus moving diagonaly from left to right.
58+
*/
59+
60+
System.out.println("The diagonal elements of the matrix are");
61+
for(int i=0;i<p;i++) //This part of the for loop prints diagonal elements..
62+
{ int j=i; //until the principal diagonal(including elements on principal diagonal..
63+
for(int x=0;x<=i && x<q;x++,j--)
64+
{
65+
System.out.print(a[j][x]+"\t");
66+
}
67+
System.out.println();
68+
}
69+
for( int i=1;i<q;i++) //This part is for the elements that are below the principal diagonal...
70+
{
71+
int j=p-1;
72+
for(int x=i;x<q;x++,j--) //Doing a dry run of the code with some smaple inputs
73+
{ // is suggested for understanding the logic() method
74+
System.out.print(a[j][x]+"\t");
75+
}
76+
System.out.println();
77+
}
78+
}
79+
public static void main(String[] args)throws IOException
80+
{
81+
diagonal obj= new diagonal();
82+
obj.input();
83+
obj.logic();
84+
}
85+
}

0 commit comments

Comments
 (0)