Skip to content

Commit

Permalink
n^=(1<<i)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumant3086 committed Jan 21, 2025
1 parent 5a5bc7d commit f3a1dfc
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Collect balls.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Collect balls
// Moderate
// 0/80
// Average time to solve is 15m
// Contributed by
// Asked in companies
// Problem statement
// You have a machine that throws ping-pong balls and has a counter meter installed in it. Each time when the button is pressed to throw the balls, the machine throws the number of balls equal to the integer saved in its counter meter.

// E.g., Let the counter be set to 5; when the throw button is pressed, the machine will throw 5 ping-pong balls and turn itself off. To throw more balls, you need to press that button again with/without resetting the counter.

// You have a box in front of you and want to collect 'N' balls in it. To achieve this, you are going to perform some operations, where each operation can be one of the following types:

// 1. Reset the counter with the number of balls present in the box.
// 2. Press the throw button.
// If initially the counter is set to 0 and the number of balls in the box is 1, find the minimum number of operations needed to collect exactly 'N' balls (first ball inclusive), assuming that all balls thrown by the machine are collected in the box.

// Detailed explanation ( Input/output format, Notes, Images )
// Constraints -
// 1 <= 'T' <= 10^3
// 1 <= 'N' <= 10^6

// Time Limit: 1 sec
// Sample Input 1:
// 2
// 3
// 6
// Sample Output 1:
// 3
// 5
// Explanation for Sample Input 1:
// For case 1:
// Initially, the box contains 1 ball. First operation is resetting the counter from 0 to 1 and for next two consecutive operations, throw 1 ball in each operation. After a total of three operations, the box contains 3 balls.

// For Case 2:
// First operation is resetting the counter from 0 to 1. Second operation is throwing 1 ball. Third operation is resetting the counter to 2 and for next two consecutive operations, throw 2 balls in each operation. After a total of five operations, the box contains 6 balls.
// There exists another order for operations as well, but the number of operations cannot be less than 5.
// Sample Input 2:
// 3
// 1
// 10
// 9
// Sample Output 2:
// 0
// 7
// 6


#include <bits/stdc++.h>
int collectBalls(int N)
{
// Write your code here.
int ans=0;
for(int i=2;i*i<=N;i++){
while(N%i==0){
N/=i;
ans+=i;
}
}
if(N>1){
ans+=N;
}
return ans;
}
47 changes: 47 additions & 0 deletions Toggle K bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Toggle K bits
// Easy
// 0/40
// Average time to solve is 10m
// Asked in companies
// Problem statement
// You are given a 32-bit integer ‘N’. Your task is to toggle the rightmost ‘K’ bits in the given integer and return the new integer.

// For Example :
// If ‘N’ = 12 and ‘K’ = 2:
// The binary representation of 12 is ‘1100’, after toggling rightmost 2 bits, it becomes ‘1111’ i.e. 15. Hence, the answer is 15.
// Detailed explanation ( Input/output format, Notes, Images )
// Constraints:
// 1 <= T <= 10
// 1 <= N <= 10^9
// 1 <= K <= 31

// Time Limit: 1 sec
// Sample Input 1:
// 2
// 21 3
// 40 4
// Sample Output 1:
// 18
// 39
// Explanation For Sample Input 1:
// In example 1, the binary representation of 21 is '10101'. After toggling rightmost 3 bits, it becomes ‘10010’ which is equal to 18.
// In example 2, the binary representation of 40 is ‘101000’. After toggling rightmost 4 bits, it becomes ‘100111’ which is equal to 39.
// Sample Input 2:
// 2
// 20 2
// 85 5
// Sample Output 2:
// 23
// 74
// Explanation For Sample Input 2:
// In example 1, the binary representation of 20 is '10100'. After toggling rightmost 2 bits, it becomes ‘10111’ which is equal to 23.
// In example 2, the binary representation of 85 is ‘1010101’. After toggling rightmost 5 bits, it becomes ‘1001010’ which is equal to 74.

#include <bits/stdc++.h>
int toggleKBits(int n, int k) {
// Write your code here.
for(int i=0;i<k;i++){
n^=(1<<i);
}
return n;
}
57 changes: 57 additions & 0 deletions everse the String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Reverse the String
// Easy
// 0/40
// Average time to solve is 15m
// Contributed by
// Asked in companies
// Problem statement
// You are given a string 'STR'. The string contains [a-z] [A-Z] [0-9] [special characters]. You have to find the reverse of the string.

// For example:

// If the given string is: STR = "abcde". You have to print the string "edcba".
// follow up:
// Try to solve the problem in O(1) space complexity.
// Detailed explanation ( Input/output format, Notes, Images )
// Constraints:
// 1 ≤ T ≤ 10
// 1 ≤ |STR| ≤ 10 ^ 5

// Where |STR| is the length of the string STR.

// Time limit: 1 sec.
// Sample Input 1:
// 3
// abcde
// coding
// hello1
// Sample Output 1:
// edcba
// gnidoc
// 1olleh
// Explanation of the Sample Input 1:
// For the first test case, STR = "abcde". We need to reverse the string, that is the first element becomes the last element and the last element becomes the first element, the second element becomes the second last element and the second last element becomes the second element and so on. So we get, "edcba".
// Sample Input 2:
// 3
// a
// 1det@Z
// $1xYuP
// Sample Output 2
// a
// Z@ted1
// PuYx1$

#include <bits/stdc++.h>
string reverseString(string str)
{
// Write your code here.
int n=str.size();
int left=0;
int right=n-1;
while(left<=right){
swap(str[left],str[right]);
left++;
right--;
}
return str;
}

0 comments on commit f3a1dfc

Please sign in to comment.