Question: Nvidia (Software) , Online Assessment (IIT Delhi & IIT Kharagpur) | Count of 8-Bit Integers | Array of Numbers | October 2022.
3
Entering edit mode

ADD COMMENTlink 14 months ago PoGo 2.4k
2
Entering edit mode

q1:   count no. of set bits saay cnt in each number if it has cnt==4  return yes else no

q2:  Two sum  problem  simple two pointer approach

ADD COMMENTlink 14 months ago Systumm • 200
0
Entering edit mode

 Question 1: 

bool check(int num){
    int count = 0;
    while(num) {
        if (num & 1) {
            count++;
        }
        num = num>>1;
    }

    return (count==4);

}

int countans(vector<int> arr){
    int n = arr.size();
    int ans = 0;
    for(int i=0; i<n; i++){
        if(check(arr[i])){
            ans++;
        }
    }
    return ans;
}

Question: 2

​
vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> mp;
        for(int i = 0; i < nums.size(); i++){
            if(mp.find(target - nums[i]) == mp.end()){
                mp[nums[i]] = i;
            }
            else{
                return{nums[i],target - nums[i]};
            }
        }
        
    }

​

 

ADD COMMENTlink 13 months ago Mayur Randive • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts