There is a number k occurring in a sequence which is larger than zero. The number k is a "magic number" if a following condition is met:
- In the sequence, there exists a number e, located n > 0 positions either to the left or to the right from k so that e is equal to n.
As an example there is a sequence: 4 6 7 9 3 6 5 7 2.
The magic number are:
4 n = 7 to the right. As the first 7 is seven positions to the right of 4, 4 is a magic number.
6 n = 3 to the right. As 3 (5) is positioned three (five) steps to the right of 6, 6 is a magic number.
3 n = 4 to the left. Because 4 is four steps to the left of 3, 3 is a magic number.
5 n = 2 to the right. 2 is two steps to the right of 5, so 5 is a magic number.
7 n = 3 to the left. Because 3 (6) is positioned three (six) positions to the left of the second 7, the second 7 is a magic number.
Another sequence : 1 13.
The magic number is:
13 n = 1 to the left.
Now I have created a variable array so that I can put in "whatever" number I wish, however I am having difficulty formulating the rest of the problem into code. There are going be for loops but I don't really know how to phrase the code into them. Also how does one make the program read the code to the left instead of to the right. Any help would be appreciated. Thanks in advance.
4 6 7 9 3 6 5 7 2
The magic numbers:
4 = 7 to the right of 7
6 = 3 to the right of 3
6 = 5 to the right of 5
3 = 4 to the left of 4
5 = 2 to the right of 2
7 = 6 to the left of 6
7 = 3 to the left of 3
// Example program
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
int size=0;
cout<<"Enter the number of elements in the array";
cin>>size;//getting size
vector<int> arr(size);//the array
for(int i=0;i<size;i++)
{
cout<<"Enter the number "<<i+1<<" ";
cin>>arr[i];//just taking the numbers
}
cout<<"The magic numbers: \n";
for(int index=0;index<size;index++)
{
for(int foo=0;foo<size;foo++)
{
if( (index-foo)==arr[foo])
cout<<arr[foo]<<" = "<<(index-foo)<<" to the left\n";
elseif( (foo-index)==arr[foo])
cout<<arr[foo]<<" = "<<(foo-index)<<" to the right\n";
}
}
}
Enter the number of elements in the array: 9
Enter the number 1 4
Enter the number 2 6
Enter the number 3 7
Enter the number 4 9
Enter the number 5 3
Enter the number 6 6
Enter the number 7 5
Enter the number 8 7
Enter the number 9 2
The magic numbers:
4 = 7 to the right
6 = 3 to the right
3 = 4 to the left
5 = 2 to the right
7 = 6 to the left
Enter the number of elements in the array: 2
Enter the number 1 1
Enter the number 2 13
The magic numbers:
13 = 1 to the left
Thank you for the answer, but (not to be rude or anything) the code isn't working for me. I tried input same elements as you did however I get different output than you. I get:
The magic numbers:
7 = 7 to the right
3 = 3 to the right
5 = 5 to the right
4 = 4 to the left
2 = 2 to the right
6 = 6 to the left
3 = 3 to the left
Perhaps some tweaking is in order and I will try to do so however I am not very much used to vectors in c++. Still thank you, kind sir/madam.
// Example program
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
int size=0;
cout<<"Enter the number of elements in the array";
cin>>size;//getting size
vector<int> arr(size);//the array
for(int i=0;i<size;i++)
{
cout<<"Enter the number "<<i+1<<" ";
cin>>arr[i];//just taking the numbers
}
cout<<"The magic numbers: \n";
for(int index=0;index<size;index++)
{
for(int foo=0;foo<size;foo++)
{
if( (index-foo)==arr[foo])
{
cout<<arr[index]<<" = "<<(index-foo)<<" to the left\n";
break;
}
elseif( (foo-index)==arr[foo])
{
cout<<arr[index]<<" = "<<(foo-index)<<" to the right\n";
break;
}
}
}
}