C++ Question
C++ Question
C++ Question
Array Problem
1. Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
Example 1:
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is
[2,3,5,4,1,7].
2. here is a programming language with only four operations and one variable X:
● ++X and X++ increments the value of the variable X by 1.
● --X and X-- decrements the value of the variable X by 1.
Given an array of strings operations containing a list of operations, return the final value of
X after performing all the operations.
Example 1:
Output: 1
Initially, X = 0.
X++: X is incremented by 1, X = -1 + 1 = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
3. Given an array nums. We define a running sum of an array as runningSum[i] =
sum(nums[0]…nums[i]).
Example 1:
Output: [1,3,6,10]
4. You are given an m x n integer grid accounts where accounts[i][j] is the amount of
money the ithcustomer has in the jthbank. Return the wealth that the richest
customer has. A customer's wealth is the amount of money they have in all their
bank accounts. The richest customer is the customer that has the maximum wealth.
Example 1:
Output: 6
Explanation:
Both customers are considered the richest with a wealth of 6 each, so return 6.
5. A sentence is a list of words that are separated by a single space with no leading or
trailing spaces.
You are given an array of strings sentences, where each sentences[i] represents a
single sentence.
Example 1:
Input: sentences = ["alice and bob love leetcode", "i think so too", "this is great
thanks very much"]
Output: 6
Explanation:
- The first sentence, "alice and bob love leetcode", has 5 words in total.
- The third sentence, "this is great thanks very much", has 6 words in total.
Thus, the maximum number of words in a single sentence comes from the third
sentence, which has 6 words.
6. There are n kids with candies. You are given an integer array candies, where each
candies[i] represents the number of candies the ith kid has, and an integer
extraCandies, denoting the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving the ith
kid all the extraCandies, they will have the greatest number of candies among all the
kids, or false otherwise.
Note that multiple kids can have the greatest number of candies.
Example 1:
Output: [true,true,true,false,true]
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
Return the absolute difference between the element sum and digit sum of nums.
Note that the absolute difference between two integers x and y is defined as |x - y|
Example 1:
Output: 9
Explanation:
The absolute difference between the element sum and digit sum is |25 - 16| = 9.
8. Given the array nums, for each nums[i] find out how many numbers in the array
are smaller than it. That is, for each nums[i] you have to count the number of valid
j's such that j != i and nums[j] < nums[i].
Example 1:
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
9. You are given an array items, where each items[i] = [typei, colori, namei] describes
the type, color, and name of the ith item. You are also given a rule represented by
two strings, ruleKey and ruleValue.
The ith item is said to match the rule if one of the following is true:
Example 1:
Input: items =
[["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iph
one"]], ruleKey = "color", ruleValue = "silver"
Output: 1
Explanation: There is only one item matching the given rule, which is
["computer","silver","lenovo"].
Structure problem
1. Write a structure to store the roll no., name, age (between 11 to 14) and
address of students (more than 10). Store the information of the
students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having
even roll no.
3 - Write another function to display the details of the student whose roll
no is given (i.e. roll no. entered by the user).