Skip to content

Commit 2abc997

Browse files
2 parents 75a94fc + ce7f23c commit 2abc997

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

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+
}

LeetCode/DuplicateNumber.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//leetcode question - https://leetcode.com/problems/find-the-duplicate-number/
2+
3+
public class DuplicateNumber {
4+
public static void main(String[] args) {
5+
int[] arr = {1,3,4,2,2}; //leetcode sample case1
6+
int ans =findDuplicate(arr);
7+
System.out.println(ans);
8+
9+
}
10+
11+
static int findDuplicate(int[] nums) { //cyclic sort
12+
int i =0;
13+
while(i < nums.length){
14+
int correct = nums[i] -1;
15+
if(nums[i] != nums[correct]){
16+
swap(nums,i,correct);
17+
}else{
18+
i++;
19+
}
20+
}
21+
22+
return nums[nums.length -1];
23+
24+
}
25+
26+
static void swap(int[] arr,int first,int second){ //function to swap 2 numbers in array
27+
int temp = arr[first];
28+
arr[first] = arr[second];
29+
arr[second] = temp;
30+
31+
}
32+
}

0 commit comments

Comments
 (0)