I have to find the maximum sum within a one-dimensional array (1 <= N <= 500000) of integers (|a| <= 4000).
If there are multiple subarrays with the same maximum sum, I have to print the shortest one. If there are multiple shortest subarrays, I have to print the leftmost one.
The code passes most of its tests, except one and I don't know where I did wrong.
The code:
#include <bits/stdc++.h>
using namespace std;
vector<int> adj;
int kStart = 0, kEnd = 0;
void getOutput()
{
ofstream output("max.out");
output << kStart + 1 << " " << kEnd + 1;
output.close();
}
void kadane()
{
int max_global = INT_MIN, max_current = 0, kS = 0;
for(int i = 0; i < adj.size(); i++)
{
max_current += adj[i];
if(max_current > max_global || (max_current == max_global && i - kS < kEnd - kStart))
{
max_global = max_current;
kStart = kS;
kEnd = i;
}
if(max_current < 0)
{
max_current = 0;
kS = i + 1;
}
}
}
void getData()
{
ifstream input("max.in");
int n; input >> n;
for(int i = 0; i < n; i++)
{
int num; input >> num;
adj.push_back(num);
}
input.close();
}
int main()
{
getData();
kadane();
getOutput();
system("pause");
}
Related
I wrote this code for obtaining the prime factors of a number taken as an input from the user.
#include<bits/stdc++.h>
using namespace std;
void prime_Factors(int);
bool isPrime(int);
int main()
{
int num;
cout << "Enter the number to find it's prime factors: ";
cin >> num;
prime_Factors(num);
}
void prime_Factors(int n1)
{
for(int i = 2; i<n1; i++)
{
if(isPrime(i))
{
int x = i;
while(n1%x==0)
{
cout << i << " ";
x *= i;
}
}
}
}
bool isPrime(int n0)
{
if(n0==1)
return false;
for(int i = 0; i*i <= n0; i++)
{
if(n0%i==0)
return false;
}
return true;
}
The prime_Factors() function call in main() function is not printing the prime factors. Pls help!!
The ranges of the loops are wrong.
Firstly, the loop for(int i = 2; i<n1; i++) will fail to find prime factors of prime numbers (the numbers theirself). It should be for(int i = 2; i<=n1; i++).
Secondly, the loop for(int i = 0; i*i <= n0; i++) will result in division-by-zero. It should be for(int i = 2; i*i <= n0; i++).
Thinking about using the Sieve of Eratosthenes made me try it out:
#include <iostream>
#include <cstdint>
#include <vector>
void prime_factors(uint32_t n) {
while(n % 2 == 0) {
std::cout << "2 ";
n /= 2;
}
std::vector<bool> sieve(n / 2, true);
for (uint32_t i = 3; i * i <= n; i += 2) {
if (sieve.at(i / 2 - 1)) {
uint32_t j = i * i;
for (; j < n; j += 2 * i) {
sieve.at(j / 2 - 1) = false;
}
if (j == n) {
do {
std::cout << i << " ";
n /= i;
} while (!sieve.at(n / 2 - 1));
}
}
}
if (n > 1) std::cout << n;
std::cout << "\n";
}
int main() {
prime_factors(123456789);
}
https://godbolt.org/z/8doWbYrs6
can anyone explain why first fact() function code doesn't give me correct output but second one does?
what's wrong with the commented factorial function code???
#include <iostream>
using namespace std;
// int fact(int n){
// for (int i = n-1; i > 0; i--){
// n = n * i;
// }
// return n;
// }
int fact(int n){
int temp=1;
for (int i = 2; i <=n; i++){
temp= temp * i;
}
return temp;
}
int nCr(int n, int r){
int temp=(fact(n) / (fact(n - r) * fact(r)));
return temp;
}
int main(){
int n;
cout << "enter no: ";
cin >> n;
for (int i = 0; i < n;i++){
for (int k = 1; k < (n - i);k++)
cout << " ";
for (int j = 0; j <= i; j++)
{
cout << nCr(i, j) << " ";
}
cout << endl;
}
return 0;
}
The second fact function correctly returns 1 when n = 0, but the first one wrongly returns 0 when n = 0.
Adding check for this case will make the first function work well.
int fact(int n){
if (n == 0) return 1; // check for n = 0 case
for (int i = n-1; i > 0; i--){
n = n * i;
}
return n;
}
I'm doing some C++ array homework. The goals is to convert decimal to binary (include negative numbers). Here's my code, it gets the job done, but I would like to see if anything can be improved, or any better algorithm (using binary shift maybe?).
#include <iostream>
using namespace std;
// doi tu thap phan sang nhi phan
void decToBinary(int n, int nhiphan[])
{
for (int i=0; i < 16; i++)
{
// to binary
nhiphan[i] = n % 2;
n = n / 2;
}
// inverse array
for (int i = 0, j = 15; i < j; i++, j--)
{
int temp = nhiphan[i];
nhiphan[i] = nhiphan[j];
nhiphan[j] = temp;
}
}
void reverse(int& a)
{
if (a == 0)
a++;
else a--;
}
void outArr(const int a[], int size) {
for (int i = 0; i < size; ++i)
cout << a[i];
}
int main()
{
int nhiphan[16];
int n;
do {
cout << "Nhap so (-255 <= n <= 255) chuyen doi sang nhi phan (16 bit): ";
cin >> n;
} while (n > 255 || n < -255);
if (n < 0) {//check negative
n *= -1;
decToBinary(n, nhiphan);
for (int i = 0; i < 16; i++)// 1's complement
reverse(nhiphan[i]);
// +1
if (nhiphan[15] == 0)//2's complement
nhiphan[15] = 1;
else
{
nhiphan[15] = 0;
int i = 15;
do {
reverse(nhiphan[i-1]);
i--;
} while (nhiphan[i-1] == 0);
}
}
else decToBinary(n, nhiphan);
outArr(nhiphan, 16);
return 0;
}
I'm stuck in a loop in here. I have a matrix filled with zeroes and I want to add k edges to a graph. I have no errors showed in Visual Studio. While it goes to this line of code:
int z = 0;
while (z!=k);
{
int a = std::rand() % n;
int b = std::rand() % n;
if ((v[a][b] != 1) && (a != b))
{
v[a][b] = 1;
v[b][a] = 1;
z++;
}
}
program gets stuck in infinite loop;
Here is full code:
std::vector<std::vector<int>> random_gnk(int n, int k)
{
srand(time(NULL));
int temp = 0;
for (int i = 0; i < n; i++)
{
temp = temp + i;
}
std::vector<std::vector<int>> v;
if (k > temp || k<0)
{
std::cout << "Blad. Podano zla liczbe krawedzi." << std::endl;
return v;
}
for (int i = 0; i < n; i++)
{
std::vector<int>row;
for (int j = 0; j < n; j++)
{
row.push_back(0);
}
v.push_back(row);
}
int z = 0;
while (z!=k);
{
int a = std::rand() % n;
int b = std::rand() % n;
if ((v[a][b] != 1) && (a != b))
{
v[a][b] = 1;
v[b][a] = 1;
z++;
}
}
return v;
}
void GNK()
{
int n, k;
std::cout << "Podaj wielkosc n grafu: " << std::endl;
std::cin >> n;
std::cout << "Podaj liczbe k krawedzi grafu: " << std::endl;
std::cin >> k;
print_matrix(random_gnk(n, k));
return;
}
Put your code through clang-format:
int z = 0;
while (z != k)
;
{
This should be clear: remove the semicolon!
There's an online formatter here. If you like it, get it set up with your IDE or code editor. It will solve problems like this and make your code more beautiful.
I have an assignment for school where I need to create a lottery program. It is supposed to allow the user to input six numbers and then generate six random numbers for comparison. I got the inputs working, but I have encountered a problem where the random number generator (located in the while loop) is stuck in an infinite loop, and I have absolutely no idea what is causing it since I have never had an infinite loop in any previous programs. If someone could please look through the code and possibly establish what is wrong, I would greatly appreciate it.
#include<iostream>
#include<time.h>
using namespace std;
void randomizeSeed();
int randomRange(int min, int max);
int getInteger();
int main()
{
randomizeSeed();
const int minNumber = 1;
const int maxNumber = 49;
const int Size = 6;
int luckyNumbers[6] = {};
int randomNumber = randomRange(minNumber, maxNumber);
int winningNumbers[6] = {};
cout << "Enter six numbers between 1 and 49...\n";
{
for (int i = 0; i < Size; i++)
{
luckyNumbers[i] = getInteger();
}
for (int i = 0; i < Size; i++)
{
for (int i = 0; i < Size - 1; i++)
{
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
}
}
cout << "Lucky Numbers: ";
for (int i = 0; i < Size; i++)
{
cout << luckyNumbers[i] << " ";
}
cout << "\n";
cout << "Press any button to see the Winning Numbers.\n";
system("pause");
bool exist = true;
while (exist == true)
{
int count = 0;
cout << "Winning Numbers: ";
for (int j = 0; j < 6; j++)
{
winningNumbers[j] = randomRange(1, 49);
cout << winningNumbers[j] << " ";
system("pause");
}
}
}
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
int getInteger()
{
int value = 0;
while (!(cin >> value) || (value >= 50) || (value <= 0))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
for (int i = 0; i < Size; i++)
for (int i = 0; i < Size - 1; i++)
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
You have two loops and they both use i. You probably mean to use the second loop with another variable name, for example:
for (int i = 0; i < Size; i++)
{
for (int k = 0; k < Size - 1; k++)
{
if (luckyNumbers[i] > luckyNumbers[k + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[k + 1];
luckyNumbers[k + 1] = temp;
}
}
}
If you set your compiler warning level to 4 then compiler should warn you about these errors. Try to resolve all compiler warnings.