Maximum difference between two elements in an array using single loop [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
How to find the maximum difference between two numbers in an array using single loop and single iteration?
Ex: Consider an array A[20]={10,3,6,8,9,4,3} How to find the maximum difference between two numbers in the array using single loop and single iteration?

To solve this problem in a single loop consider how the numbers A and B that produce maximal difference A-B influence the difference:
The difference will be greater when you pick larger A, and
The difference will be greater when you pick smaller B
Once you make this observation, it becomes clear that you are looking for the largest A and the smallest B in order to achieve the maximum difference. This can be done in a single loop in O(n) time and O(1) space.

Related

Counting the number of 1's in an integer C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What is the best way to count the number of 1's in an 32-bit integer x in C without using for or while loops, and without using constants greater than 0xFF?
What I thought of is shifting x 24 to the right and count how many 1's in the shifted integer and store that in a variable count. And then, shifting x 16 to the right and increment count by the number of 1's in the shifted integer, and so on.
So, any ideas of a better solution?
You can tabulate the number of 1's in all d bits numbers. This takes a table of 2^d entries, each not exceeding the value d (<255).
Now you can cut your number in slices of d bits and lookup the counts for all slices.
A good compromise between space/number of operations is probably with d=4 (8 slices, table size=16).

check whether a number is bleak or supported in efficient way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Given a number how to recognize if it is bleak or supported by some number in efficient manner?
Given an array of numbers, how to check efficiently whether each number is supported with in the
array or bleak if not supported with in the array?
Brute force : Find binary equivalent, count number of 1's and search for it in the array.
About Bleak and supported numbers:
For each number, count the number of ones in its own binary representation, and add this count to itself to obtain the value of the number it supports. That is, if j is the number of ones in the binary representation of m, then m supports m+j.
Example:number eight (1000 in binary) supports nine, whereas nine supports eleven.
However, in this way not all the numbers get supported; some are left without support, and these numbers are called bleak. For example since one supports two, two supports three and three supports five, there is no number less than four, which would support four, so four is bleak.
If n is not bleak, it must be supported by a number in the range n-ceil(log2(n)) to n-1. This gives a very small range you have to check. For the array, first sorting the array then using the same principle should give you an efficient solution.
Denote a as the given array and count(x) as the number of 1 bits in x.
Question 2:
Iterate through the array and save the the number a[i] + count(a[i]) into a binary search tree. Time: O(n log n)
Iterate through the array and output "Supported" if a[i] is in the binary search tree and "Bleak" otherwise. Time: O(n log n)
Note: a[i] is the current element at the iteration.

Printing range of number upward or backward according to users first input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to print a range of integers (all the positive integers that to be found between the user's 2 input integers) and I need to do it by the order the user typed it.
for example:
for the input: 4,9 output would be: 4 5 6 7 8 9
and for the input: 9,4 the output would be: 9 8 7 6 5 4
I'm not allowed to use any array/strings/functions, just basic C commands.
Anybody have any ideas?
Since this is most likely a learning exercise, here are some points to think about:
You need a loop.
The loop starts at the first number the user enters, and ends upon reaching the second number
The step of the loop depends on the order of the numbers entered by the user
If the first number is smaller than the second one, the step of the loop is one
If the first number is larger than the second one, the step of the loop is negative one
Therefore, the structure of your program is going to be as follows:
Prompt the user for the two numbers
Read numbers one and two
Compute the step using an if
Print out the range using a loop (for, while, or do/while, it's up to you).
This should be enough to complete your assignment. Good luck!

Remove element from list of integers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
How can I delete an integer from a list initialized with int list[9999]?
I know how to remove a specified integer from that list by specify a key of list, but I will need to shift other elements to the left. What is the alternative?, shifting all elements is a highly cost CPU operation, should I use a Linked List and delete from memory that entity from the list, the other elements to be untouched?
Thanks!
If you want constant time insertion/deletion, a linked list is pretty much required - but iterating to the desired element is still going to be linear time. However, there may be a better way to optimize your program. Are you performing this operation very frequently? Can you maybe perform this operation less frequently by changing the structure of your program? A CPU can shift 39K (worst-case scenario with 10000 elements) of data pretty darn quickly. Are you sure this is your bottleneck?

Are there any algorithms for scrambling a word? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a word scrambler and am wondering if there are any algorithms I should use or if I should just build it from scratch. Any pointers would be helpful!
The standard algorithm for finding a random permutation of a sequence of elements (or, in your case, letters in a word) is the Fisher-Yates shuffle, which in linear time produces a truly random permutation of a sequence of elements. The algorithm is well-established and many standard libraries provide implementations of it (for example, the C++ std::random_shuffle algorithm is typically implemented using this algorithm), so you may be able to find a prewritten implementation. If not, the algorithm is extremely easy to implement, and here's some pseudocode for it:
for each index i = 0 to n - 1, inclusive:
choose a random index j in the range i to n - 1, inclusive.
swap A[i] and A[j]
Be careful when implementing this that when picking a random index, you do not pick an index between 0 and n-1 inclusive; this produces a nonuniform distribution of letters (you can read more about that in this earlier question).
Hope this helps!
Go with the Knuth Shuffle (AKA the Fisher–Yates Shuffle). It has the desirable feature of ensuring that every permutation of the set is equally likely. Here's a link to an implementation in C (along with implementations in other languages) that works on arbitrarily sized objects.

Resources