I am trying to solve the 'Copy-Paste' programming problem (https://www.codechef.com/problems/RRCOPY ) of codechef
Here is what I have tried
#include <iostream>
using namespace std;
bool isNumberInArray(int array[], int A, int size)
{
bool isFound = false;
for(int i = 0; i < size; i++)
{
if(array[i] == A)
{
isFound = true;
break;
}
}
return isFound;
}
int main()
{
int T, i = 0, A, size = 0, count;
int array[100000];
cin >> T;
while(T--)
{
cin >> size;
count = 0;
i = 0;
while(size--)
{
cin >> A;
if(isNumberInArray(array, A, count) == false)
{
array[i] = A;
count++;
}
i++;
}
cout << count << endl;
}
return 0;
}
Can anyone please tell what I am doing wrong.
Thanks in advance.
There are three mistakes in your program.
First: Why is the variable i outside the if block. You are incrementing the index even if there are duplicates in your array. (You can just use count instead of i).
array[count]=A;
count++;
Second: You shouldn't print the result immediately after evaluating a testcase. You need to store all the results of all testcases in an array, and print them after you solve all of them.
Third: Your algorithm runs on O(n^2) which will definitely result in a TLE.
You need to improve your algorithm to O(n). My solution:https://www.codechef.com/viewsolution/9011078
Next time please be more concise about what you want to ask.
Related
I can't figure out how to check if an integer (x) is found inside a string (num). The if (num[i] == x) doesn't work. Can somebody please help me? Also I'm new to coding/ c++ and barely know the basics, so an easy-to-understand answer would be appreciated! Thank you!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
string num;
int count = 0;
for (int i = 1; i <= n; i++) {
num.append(to_string(i));
}
for (int i = 0; i <= num.size(); i++) {
if (num[i] == x) {
count += 1;
}
}
cout << num;
cout << count;
}
I tried converting it to a string, making a new string, and converting it to a char (not sure how this stuff works), but it keeps giving me the same error or it just doesn't run the way I was expecting. The count doesn't go up, and stays at zero.
Convert x to a string and then use the string::find() method to search for it in num, eg
auto index = num.find(to_string(x));
if (index != string::npos) {
// found
} else {
// not found
}
Hey guys please help me on this I have tried calling my bool function in my main func but it wont even show the first cout of program and the compiler terminates the program here is my code
#include <iostream>
using namespace std;
bool puzzle(int size, int array[], int start)
{
cout <<"how many blocks you want for the puzzle? \n";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "enter your numbers in order for the blocks:\n";
cin >> array[i];
if (array[0] > size) { return false; };
if (array[0] == size) { return true; };
}
}
int main()
{
puzzle;
return 0;
}
Your function has parameters so you need to call them to make it work. In this case (an example):
int size = 5;
int array[5];
int start = 0;
puzzle(size, array, start);
For the life of me I can't get this code to sort correctly. This is a recursion practice, by sorting five numbers that the user inputs, then I display those five numbers from least to greatest. It does most of it correctly, but occasionally it will mess the first or last number up, and switch it with another number in the array. I know the problem is inside the function where is does the swapping, in that second 'if' statement, but I can't figure out how to fix it, I would really appreciate some direction as to how to proceed. Here is my code:
#include <iostream>
#include <array>
using namespace std;
void mySort(int nums[], int first, int size);
int main()
{
int fiveNumbers[5];
int firstNum = 0;
int size = 5;
cout << "Please enter five numbers, pressing enter after each.\n\n";
for (int i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> fiveNumbers[i];
cout << endl;
}
mySort(fiveNumbers, firstNum, size);
for (int i = 0; i < size; i++)
{
cout << fiveNumbers[i] << endl;
}
system("PAUSE");
return 0;
}
void mySort(int nums[], int first, int size)
{
if (size == 0)
{
return;
}
for (int i = 0; i < 5; i++)
{
if (first < nums[i])
{
swap(nums[first], nums[i]);
}
}
first++;
size--;
return mySort(nums, first, size);
}
Changed my function to reflect the value of the array AT point 'first', instead of the variable 'first' itself. So far it has worked every time!
void mySort(int nums[], int first, int size)
{
if (size == 0)
{
return;
}
for (int i = 0; i < 5; i++)
{
if (nums[first] < nums[i])
{
swap(nums[first], nums[i]);
}
}
first++;
size--;
return mySort(nums, first, size);
}
EDIT: Got your code working but forgot the most important part, namely:
You're comparing the index to the array value, use:
if (nums[first] < nums[i])
Instead of:
if (first < nums[i])
Also, you always start swapping from the beginning when you should be starting one past first.
Instead of:
for (int i = 0; i < 5; i++)
You want:
for (int i = first + 1; i < 5; i++)
Recently I am writing a program to generate a fixed number of permutation of alphabets inputted. For example, I inputted 3, 3, ABC, the program will output ABC, ACB, BAC, according to lexicographical order. But the program cannot get through all the test case and i cant find out where is the bug. Please help.
#include <iostream>
using namespace std;
int used[26], cou = 0, k, n, i;
string output;
string sorting(string x, int y)
{
char temp;
int i, j;
for (i = 0; i < y; ++i)
{
for (j = 0; j < y-1; ++j)
{
if (x[j]-'0' > x[j+1]-'0')
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
return x;
}
void out(int x, string y)
{
int i;
if (cou == k)
{
return;
}
if (x == n+1)
{
cout << output << endl;
++cou;
}
else
{
for (i = 0; i < n; ++i)
{
if (used[i] == 0)
{
used[i] = 1;
output[x-1] = y[i];
out(x+1, y);
used[i] = 0;
}
}
}
}
int main()
{
char inpi;
string inp, ha;
cin >> n >> k >> inp;
output.resize(n);
for (i = 0; i < 26; ++i)
{
used[i] = 0;
}
inp = sorting(inp, n);
out(1, inp);
}
I am not sure that I understood the question.
Leaving the algorithm aside, you should know that a standard string is able to tell how long is. Therefore the y parameter in sorting is redundant. Use x.size() to find the size.
Another problem is output[x] = y[i];. You did not set output's size: it is zero. Since you are looking for permutations, I assume its size must equal y's size: output.resize( y.size() );.
One last thing: use meaningful identifiers. y may be good for a compiler; for a human, it may define a bad day.
There doesn't seem to be a need to create a global as you have done
string output;
Create a string to capture the output either within main() and pass it by reference to the functions which need them, in this case fill()
or
create it within fill() and return it by value once its populated
I'm trying some exercise to learn the use of pointers with arrays and functions.
So I tried to code a "strange way" to find out primes within a certain range.
The problem is that the output always add the return value of the function with the algorithm for the primes. if I omit it, it shows is '32767', if I write return *pt, it adds the last number of the range, even if it's not a prime!
Just tried it with number 6: it's not a prime but it pops up!
#include <iostream>
int show_primes(const int * begin, const int * end);
int main()
{
using namespace std;
int i = 0;
int End_Array = 0;
cout << "Write the last number in your range (it always start from number 2)";
cin >> End_Array;
i=End_Array;
int cookies[i];
for(i=-1; i<End_Array; i++)
cookies[i] = i+1;
cout << show_primes(cookies, cookies + End_Array-1);
}
int show_primes (const int * begin, const int * end)
{
using namespace std;
const int * pt;
int z = 0;
for (pt = begin; pt < end; pt++, z=0)
{
for (int n=2; n<=*pt; n++)
if ( *pt%n == 0 )
++z;
if (z==1)
cout << *pt <<endl;
}
return *pt ;
}
Your loop is accessing a value at negative index.
cookies[i] = i+1; //For first iteration, value of i is -1
So for(i=-1; i<End_Array; i++) should be changed to for(i=0; i<End_Array; i++)
Also, you do not need to return from the function as you are printing the values within itself
Although you are using pointers for your learning, a more simpler implementation would be:
#include <iostream>
using namespace std;
void show_primes(int num)
{
bool flag = false;
for (int pt = 2; pt < num; pt++)
{
if ( num%pt == 0 )
{
flag = true;
break;
}
}
if(!flag)
{
cout<<num<<' ';
}
}
int main()
{
int End_Array = 0;
cout << "Write the last number in your range(>2)";
cin >> End_Array;
for(int i=2; i<End_Array; i++)
{
show_primes(i);
}
}
P.S.: Can someone please highlight that is it a bad practice to include std namespace in every functional block as OP has done.(I think it is)
for(i=0; i<End_Array; i++) // Start from zero
cookies[i] = i; //Use i
// Don't use cout
show_primes(cookies, cookies + End_Array-1);