C++ User defined sequence integers without using arrays [closed] - c++

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 6 years ago.
Improve this question
I'm trying to figure this one out on my own and I can't.
The professor is asking for this.
User will define the length of the sequence.
User will then enter numbers from any range being both neg and pos.
The program:
Find the two largest numbers and display in descending order.
Find the two smallest numbers and display in ascending order.
Can't use sort or arrays.
I have an outline but, my head just wants to use arrays..... I don't see this simple without an array. Confused. I'm a beginner. I just want help. Advice? Tutor?

Start with a solution to an easier problem, and grow it into a solution to the actual problem that you are solving:
Write a program that finds and prints the largest number entered by the user. This is easy to do with a single variable keeping track of "high watermark"
Modify your program to keep track of the smallest number as well. You can do it by adding another variable, and keeping track of the "low watermark".
The challenge in both tasks above is the initial value of the high/low watermark. This is a common source of errors; there are multiple Q&As on SO explaining the fix.
Now for the fun part:
Modify your program to keep track of the second-largest number by "demoting" the number that was previously considered largest to second-largest each time that you find a number that is larger than the largest one, and by replacing the second-largest when you find a value above it that does not exceed the largest value.
This would require you to write a couple of if statements.
Finally, modify your program to keep track of the second-smallest number by applying the "mirror image" of the algorithm above.

Algorithm ("Find the two largest numbers in a sequence without storing the sequence")
(I decided to undelete this answer. It might be helpful to OP to get away from the array frame of mind)
Input: A sequence of values S.
Output: Values Max and SecondMax.
The consumed, current value of S is E
1 : Set Max to E
2 : Set SecondMax to E (Second value in S)
3 : If Max < SecondMax -> Swap Max and SecondMax.
4 : while ( e:= E exists)
4.1 : If ( e > Max ) SecondMax := Max, Max := e
4.1.1 : Else If ( e > SecondMax ) SecondMax := e
This is trivially extended to the minimums.

Related

How Gcd works in this code? [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 5 years ago.
Improve this question
I tried solving a question on hackerearth and i was not able to solve it so i saw editorial.They gave only code without explanation.Can u expain logic behind why gcd used here?
Question:
Scooby and all of his friends have gathered for a party. There are N friends present. Scooby is really happy to see all of his friends in one place and is excited to greet them.
All N friends are seated in a circle, and are numbered from 0 to N-1. Scooby is initially sitting beside the Ath friend. After greeting one friend, he goes clockwise to the Bth next friend, sits next to him and greets him. He repeats this till he returns to the Ath friend.
In his excitement, it is possible that Scooby misses out on greeting some friends. Your job is to find the number of friends (including A) that Scooby will have greeted before reaching back to A.
Solution given:
int main()
{
int T;
cin>>T;
while(T--)
{
long long N,A,B;
cin>>A>>B>>N;
long long g=gcd(B,N);
cout<<N/g<<endl;
}
return 0;
}
To explain the solution of the above problem I will first show that the answer is - LCM(B,N)/B and then show you how this is equal to N/GCD(B,N).
First Part-
Now assume that when it again reaches A after following the above mentioned steps he would have greeted f friends.(Note no two friends greeted through the above mentioned procedure can be same). Moreover, assume that when he reached A he would have made r rounds of the circle.
Now we can say that -
f * B = r * N = C.
Let this be equal to some constant C. Clearly C is some multiple of B and N moreover, it is the Lowest Common Multiple(LCM) of B and N(as we want to give answer as soon as it reaches for the first time).
So f = LCM(B,N)/B. Note f is the number of friends he greeted so it is the required answer.
Second Part-
For two positive integers a and b with their GCD and LCM g and l respectively, we have the following relation - a*b = g*l.
From the above relation we can say that -
LCM(B,N)*GCD(B,N) = B*N
=> LCM(B,N)/B = N/GCD(B,N)
So finally we have our answer = LCM(B,N)/B = N/GCD(B,N).

How does this code works in 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 6 years ago.
Improve this question
I participated in Codeforces Round #396 (Div. 2) yesterday.
The (A) problem seemed pretty advanced in my opinion when I read it.
I tried solving it all the competition time yesterday and some time today.
I came up with a 200 lines long(half-working) solution. And then I gave up.
I looked what other people wrote there and I saw max 20 lines long code that seems magic to me.
The problem asks you to output the length of the longest uncommon subsequence of letters from two strings.
You can read the full problem here:
http://codeforces.com/contest/766/problem/A
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main(){
cin>>a>>b;
printf("%d",a==b?-1:max(a.size(),b.size()));
return 0;
}
This is all the code used to solve the problem, and I really want to know how that one line of code
printf("%d",a==b?-1:max(a.size(),b.size()));
can solve this "advanced" task?
If the two strings are equal, there is no "uncommon subsequence". If they are not equal, neither one is a subsequence of the other, but each one is a subsequence of itself, so each one is an "uncommon subsequence". The longer of the two is the longest "uncommon subsequence", and its length is the correct answer. If the two are not equal but have the same length, then each one is an "uncommon subsequence" and the length of the longest is just the length of either one.
Don't get tangled up in what you (or any other reasonable person) think a "subsequence" is. The problem defines "uncommon subsequence", and all you have to do is apply its definition. This "problem" is about word play, not coding.
It does not seem to solve the task at hand. It just outputs -1 when the strings are equal and the length of the longer string when they are not different. However, the problem asks for the longest uncommon sequence. It would fail with the inputs abc and abcd to give the answer 1 would rather give 4.
What the line does is the following:
It checks whether a == b, whether the strings are equal.
If they are equal, the condition A is true in the ternary operator A ? B : C, so that expression will evaluate to B which is just -1 here.
If they are not equal, it will evaluate to C which is the maximum of the two string lengths, so it will be length of the longer string.
The value of the ternary expression is then printed, albeit without a newline, which is a bit bad.
So the code is so short because it only solves the problem for a few cases but not the general case.
printf ("%d", a == b ? -1 : max (a.size (), b.size ()));
is equivalent to
if (a == b) {
printf ("%d", -1);
}
else {
if (a.size () > b.size ()) {
printf ("%d", a.size ());
}
else {
printf ("%d", b.size());
}
}
BTW That's only a small step in solving the problem...
[CORRECTION]
IT DOES SOLVE THE WHOLE PROBLEM (See answer of Pete Becker)

C++ : How to arrange 4 values in an ascending order? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I know it may be a beginners question but I need help.
I need to compare between 4 values added by the user and arrange them in am ascending order by using a function that takes 2 inputs and return the smaller one. I know it can be done by arrays but I must not do it. I already have the function but I don't know how to use to do the trick without having a very long code. Thanks
This seems to me to be an obvious "homework question," so let me answer it cryptically in order to maybe push you in the right direction.
First, the hint: divide and conquer.
Second hint: the "Towers of Hanoi" problem.
You have a function that can compare two values. Okay, then: "four elements" can be viewed as "two groups of two values each." Given that either of the two input to your comparison-function can be the result obtained by a nested call to the same function . . . you can, indeed, solve this problem, in one line of code, without using arrays.
I'm trying here to "teach you to fish," so I'm not handing you the fish on a platter.
If you know c++ then you can use sort function. But for this you have to include algorithm as:
#include <algorithm>
and sort function will be used as:
sort(array, array+N);
where array is the array name and N is the size of array.After this operation you will get a sorted array in ascending order and return first element.Now the function will look like as:
int smallest(int *array) {
int size = sizeof(array) / sizeof(array[0]);
sort(array, array+size);
return (array[0]);
}
And now call this function from main()

what's number anagram which are palindromes in string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the number of anagrams which are palindromes in a string?
Example : string = "aaabbbb";
Possible anagram's which are palindromes "abbabba" , "bbaaabb" and "bababab".
The problem here is the time, i have string of size 10^9.
here's my final code can anybody tell me what's the wrong with it ?
Every letter in your input string has to appear in an even amount, execpt one letter can appear in an odd amount. This letter has a fixed position in the palindron. It has to be exactly in the middle. Lets say the amounts of the letter a,b,c,... are #a, #b, #c, ...
You only care about half of those letters, because in an palindron, the second half depands of the first half. So we only use half of the letters:
I used the floor function, so I calculate the letter, which appears in an odd amount, correct.
So how many permutations are in the first half? This is a case of distinct permutation, so we get
possibilities.
For your example:
string = "aaabbbb";
We get: #a=3, #b=4. Therefore
We get 3 palindroms, these are "abbabba" , "bbaaabb" and "bababab", like you posted.
So, if you have a very large string:
Count the amounts of each letter
Check, if there is only 1 letter that appears in an odd amount. It there are more, you can't create palindroms.
Use the formular to calculate the number of different palindroms.
Since each side of the anagram must be a mirror image of the other, the number of anagrams we care about is basically just the number of anagrams we can form on one side, so:
group the characters in the string so identical characters are together (e.g., by sorting).
Check for an odd number of more than one character (of so, # anagrams = 0).
Take half the characters of each group of identical (truncating in the case of odd number).
Compute the number of unique permutations of those characters.

A particularly interesting program [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
we did this problem today in math class to kill time after a quiz, and i was wondering if i could make a program along these lines
so the idea is this:
take the number 123,456,789
the first digit from the left is divisible by 1, good, continue
the first 2 digits from the left are divisible by 2, good, continue
the first 3 digits from the left are divisible by 3, good, continue
the first 4 digits from the left are NOT divisible by 4, you get a remainder, bad, restart with different numbers, or go back until one of the numbers can be replaced (the even spaces are pretty interchangeable)
the tricky part here is that you cant use the same integer twice
the only place we can be sure about is the fifth place, so we can tell that the number will look something like: _ _ _, _ 5 _, _ _ _
i want the program to print the number(s) which is(are) perfectly divisible, all the way to the ones place. im pretty sure that only one number fits this criteria, but id like to know if this is true, which is why im trying to make this program.
what i need help with is how i should go about checking if each number divided by its place has no remainder, and how to go back if it doesnt fit.
pretty new to coding..anything helps
Programming is just describing a series of steps to a computer
Edit - sorry, rereading the question it would be easier to store the value as a long integer and then increase it in a loop.
In which case you will have to understand how to get the first 3 digits by dividing by 10^6 (or whatever) and then taking only the integer part.
You migth also want the modulus operator %
You could setup a loop, that uses the modulus operator (%) to get the remainder of a number. I would setup an array with a legnth of 10, and modulus the number by 10 and put the result into the array (because you will read the numbers in reverse you need to place them into the array in reverse), until you have each digit.
I would then setup an int (named maybe value), and put the first value in the array, into this int. I would also designate another int (named maybe place) and set it equal to 1. Define a bool (named maybe flag).
I would then enter a loop so that you have the equation if(value % place == 0) flag = 0.
Then have the loop break if flag == 1, and goto a label before the loop, where you have an algorithm that generates a new number (maybe somthing as simple as adding one, or what ever you want). Also inside the loop if place == 10 and your last value in the number is 0, you need to print the number, and then jump to your label from earlier, before the loop, to pick a new number. Also don't forget that if flag == 0, and place != 10 (the number hasn't failed yet, but isn't at the end yet) , that you need to multiply the value by 10 , then add the next number in the array to be able to test the next place. And you also need to increase place by 1 each time.
You said you're new, so I'll include a few definations:
'=' sets a variable equal to somthing, '==' is used to test equality
'!=' means "not equal to". It's also used to test variables against other variables or numbers.
'%' or modulus divides two numbers but rather returns the remainder (ex. 10 % 3 = 1, because 10 / 3 = 9 remainder 1)