You have to perform the following operation N/2 times:
1. Select nums, nums; such that (i+j) from the list
2. Take their bitwise OR, add the value of the least significant set bit to the score
3. Delete those two numbers
If the selected numbers are 5=(101)2 and 7=(111)2 then the bitwise OR of the two numbers is 7=(111)2. So, the least significant bit of 7 1.e. 1= (001), will be added to the current score.
Notes
• H-based indexing is followed
• A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each
pair of corresponding bits. The result in each position is O if both bits are 0, while otherwise, the result is 1.
Strings in 02 denote the binary representation.
Task
- 100.0
Print the maximum possible score.
Example
100.0
Assumptions
•
N-4
• nums = [1,2.1.4)
Approach
. For the given case pairing (1, 1), (2, 4) that is (nums/1), nums[3]), (nums[2],,nums[4]) with 1-based indexing results in a maximum score of
3. pairing like (4,1),(1, 2) that is (nums[4], nums/1). (nums(3), nums[2]) results in 2.
Hence, the answer is 3.
please answer in cpp else dont message wrong things
Answers
Answer:
- You have to perform the following operation N/2 times:
- 1. Select nums, nums; such that (i+j) from the list
- 2. Take their bitwise OR, add the value of the least significant set bit to the score
- 3. Delete those two numbers
- If the selected numbers are 5=(101)2 and 7=(111)2 then the bitwise OR of the two numbers is 7=(111)2. So, the least significant bit of 7 1.e. 1= (001), will be added to the current score.
- Notes
- • H-based indexing is followed
- • A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each
- pair of corresponding bits. The result in each position is O if both bits are 0, while otherwise, the result is 1.
- Strings in 02 denote the binary representation.
- Task
- - 100.0
- Print the maximum possible score.
- Example
- 100.0
- Assumptions
- •
- N-4
- • nums = [1,2.1.4)
- Approach
- . For the given case pairing (1, 1), (2, 4) that is (nums/1), nums[3]), (nums[2],,nums[4]) with 1-based indexing results in a maximum score of
- 3. pairing like (4,1),(1, 2) that is (nums[4], nums/1). (nums(3), nums[2]) results in 2.
- Hence, the answer is 3.
- please answer in cpp else dont message wrong things
Answer:
A pair (i, j) is called good if nums[i] == nums[j] and i < j
Explanation:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Assume that each input would have exactly one solution, and you may not use the same element twice.
Return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Approach :
Now two approaches to solving this question :
1. Brute Force Method
In this approach I will iterate the array using two loops to find if the sum is equal to the target.
for(int i=0;i<nums.size()-1;i++)
{
for(int j = i+1;j<nums.size();j++)
{
if(nums[i]+nums[j]==target)
...
}
}
Time Complexity - O(n*n)
Space Complexity - O(1)
2. Using Hashmap
In this approach I will use hashmap and a vector to return the index of the elements if the target is found by addition of two elements.
Time Complexity - O(n) because I will tarverse the array only once and Hasmap has a time compelexity of O(1) for insertion.
Space Complexity - O(1)
Code is as follows :
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>ans;
unordered_map<int,int>m;
for(int i=0;i<nums.size();i++)
{
int val = target-nums[i];
if(m.find(val)!=m.end()) // in case the second element is found
{
ans.push_back(m.find(val)->second);
ans.push_back(i);
break;
}
m.insert(pair<int,int>(nums[i],i)); // in case the above criteria is not satisfied I will keep inserting the element in the hashmap
}
return ans;
}
};