Duplicate Local Variable error in a For loop - for-loop

I'm trying to make a program that finds the sum of all the integers between two numbers, inclusive. I'll paste what I have so far, but I'm getting an error (i is a duplicate local variable?). Thanks
public int sum(int num1, int num2){
if (num1 == num2){
return num1;
}
if (num1 > num2){
for (int i = (num2 + 1), i <= num1, i++){
num2 += i;
}
return num2;
}
if (num2 > num1){
for(int i = num1 + 1, i <= num2, i++){
num1 += i;
}
return num1;
}
}

Use semi-colons in your for loops, not commas:
public int sum(int num1, int num2){
if (num1 == num2){
return num1;
}
if (num1 > num2){
for (int i = (num2 + 1); i <= num1; i++){
num2 += i;
}
return num2;
}
if (num2 > num1){
for(int i = num1 + 1; i <= num2; i++){
num1 += i;
}
return num1;
}
}

Related

CSES Dynamic Range Minimum Queries

https://cses.fi/problemset/task/1649
I'm solving this problem using Segment Trees and the solution I've written is
#include <bits/stdc++.h>
#define MAX 1000000001
using namespace std;
int n;
vector<int> tree;
int sum(int a, int b)
{
a += n;
b += n;
int s = INT_MAX;
while(a <= b) {
if (a % 2 == 1) s = min(s, tree[a++]);
if (b % 2 == 0) s = min(s, tree[b--]);
a>>=1;
b>>=1;
}
return s;
}
void update(int k, int change)
{
k += n;
tree[k] = change;
for(int i = k>>1; i >= 1; i>>=1) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
return;
}
int main()
{
int q;
cin >> n >> q;
n = pow(2, ceil(log2(n)));
tree.resize(2*n, INT_MAX);
for(int i = 0; i < n; i++) {
cin >> tree[i+n];
}
for(int i = n-1; i >= 1; i--) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
int type, a, b;
for(int i = 0; i < q; i++) {
cin >> type >> a >> b;
if (type == 1) {
update(a-1, b);
} else {
cout << sum(a-1, b-1) << endl;
}
}
return 0;
}
It works with first test case, but not with the second one. I've looked at other solutions online and they all look similar to mine. Please, help me spot the mistake.

I have problem swap variables function in Dart

I'm gonna crazy everything is looking good where is the problem?
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
swap(num1,num2);
print("Last : $num1, $num2");
}
void swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
MY OUTPUT:
First: 10,11
Last: 10,11
You should try removing the swapping from the swap function and place it directly inside the main function. When calling functions, references to the variables' values are passed and not the variable itself.
#Ketan Ramteke is right. Or you can return a list and use that value like this:
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
List arr = swap(num1,num2);
print("Last : ${arr[0]}, ${arr[1]}");
}
List swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
return [num1, num2];
}
// Dart 2.6.
void main(){
int num1 = 10;
int num2 = 11;
print("First : $num1, $num2");
// get returned swapped values
List result = swap(num1,num2);
// assign them to original variables
num1 = result[0];
num2 = result[1];
//print the mutated num1 and num2
print("Last : $num1, $num2");
}
List swap(int num1,int num2){
int temp;
temp = num1;
num1 = num2;
num2 = temp;
// print("Last : $num1, $num2");
return [num1, num2];
}

CSES food division problem solving approach

Has anyone tried to solve this particular problem? https://cses.fi/problemset/task/1189
Can someone help with an approach?
This works but doesn't optimize the steps
Tried recursion, but since the array 'a' needs to be updated, not sure how recursion can work in this case.
private int findMinSteps(int[] a, int[] b, int n) {
int cnt = 0;
do {
for(int i=1; i <= n; i++) {
if(a[i] == b[i]) continue;
if(a[i] > b[i]) { //we can give
if(a[left(i)] < b[left(i)]) { //give to left
a[i] -= 1;
a[left(i)] += 1;
cnt ++;
} else /*if(a[right(i)] < b[right(i)])*/ { //give to right
a[i] -= 1;
a[right(i)] += 1;
cnt ++;
}
} else { //we have to receive
if(a[left(i)] > b[left(i)]) { //receive from left`
a[i] += 1;
a[left(i)] -= 1;
cnt ++;
} else { //receive from right
a[i] += 1;
a[right(i)] -= 1;
cnt ++;
}
} //if-else
} //for
} while(!Arrays.equals(a, b));
return cnt;
} //findMinSteps
Here's a recursive solution that doesn't work either
private int findMinSteps(int[] a, int[] b, int n) {
/*if only one kid or zero kids are there, there is no mis-match
*because the total amount of food is correct
*/
//System.out.println(Arrays.toString(a));
//System.out.println(Arrays.toString(b));
if(n < 1)
return 0;
if(a[n] == b[n])
return findMinSteps(a, b, n-1);
if(a[n] > b[n]) {
//At each step a child can give 1 unit of food to neighbor
int l = left(n);
int r = right(n);
a[n] -= 1;
if(a[l] < b[l]) {
a[l] += 1;
} else if(a[r] < b[r]) {
a[r] += 1;
}
return 1+findMinSteps(a, b, n-1);
} else
return findMinSteps(a, b, n-1);
}

Finding longest sequence of '1's in a binary array by replacing any one '0' with '1'

I have an array which is constituted of only 0s and 1s. Task is to find index of a 0, replacing which with a 1 results in the longest possible sequence of ones for the given array.
Solution has to work within O(n) time and O(1) space.
Eg:
Array - 011101101001
Answer - 4 ( that produces 011111101001)
My Approach gives me a result better than O(n2) but times out on long string inputs.
int findIndex(int[] a){
int maxlength = 0; int maxIndex= -1;
int n=a.length;
int i=0;
while(true){
if( a[i] == 0 ){
int leftLenght=0;
int j=i-1;
//finding count of 1s to left of this zero
while(j>=0){
if(a[j]!=1){
break;
}
leftLenght++;
j--;
}
int rightLenght=0;
j=i+1;
// finding count of 1s to right of this zero
while(j<n){
if(a[j]!=1){
break;
}
rightLenght++;
j++;
}
if(maxlength < leftLenght+rightLenght + 1){
maxlength = leftLenght+rightLenght + 1;
maxIndex = i;
}
}
if(i == n-1){
break;
}
i++;
}
return maxIndex;
}
The approach is simple, you just need to maintain two numbers while iterating through the array, the current count of the continuous block of one, and the last continuous block of one, which separated by zero.
Note: this solution assumes that there will be at least one zero in the array, otherwise, it will return -1
int cal(int[]data){
int last = 0;
int cur = 0;
int max = 0;
int start = -1;
int index = -1;
for(int i = 0; i < data.length; i++){
if(data[i] == 0){
if(max < 1 + last + cur){
max = 1 + last + cur;
if(start != -1){
index = start;
}else{
index = i;
}
}
last = cur;
start = i;
cur = 0;
}else{
cur++;
}
}
if(cur != 0 && start != -1){
if(max < 1 + last + cur){
return start;
}
}
return index;
}
O(n) time, O(1) space
Live demo: https://ideone.com/1hjS25
I believe the problem can we solved by just maintaining a variable which stores the last trails of 1's that we saw before reaching a '0'.
int last_trail = 0;
int cur_trail = 0;
int last_seen = -1;
int ans = 0, maxVal = 0;
for(int i = 0; i < a.size(); i++) {
if(a[i] == '0') {
if(cur_trail + last_trail + 1 > maxVal) {
maxVal = cur_trail + last_trail + 1;
ans = last_seen;
}
last_trail = cur_trail;
cur_trail = 0;
last_seen = i;
} else {
cur_trail++;
}
}
if(cur_trail + last_trail + 1 > maxVal && last_seen > -1) {
maxVal = cur_trail + last_trail + 1;
ans = last_seen;
}
This can be solved by a technique that is known as two pointers. Most two-pointers use O(1) space and O(n) time.
Code : https://www.ideone.com/N8bznU
#include <iostream>
#include <string>
using namespace std;
int findOptimal(string &s) {
s += '0'; // add a sentinel 0
int best_zero = -1;
int prev_zero = -1;
int zeros_in_interval = 0;
int start = 0;
int best_answer = -1;
for(int i = 0; i < (int)s.length(); ++i) {
if(s[i] == '1') continue;
else if(s[i] == '0' and zeros_in_interval == 0) {
zeros_in_interval++;
prev_zero = i;
}
else if(s[i] == '0' and zeros_in_interval == 1) {
int curr_answer = i - start; // [start, i) only contains one 0
cout << "tried this : [" << s.substr(start, i - start) << "]\n";
if(curr_answer > best_answer) {
best_answer = curr_answer;
best_zero = prev_zero;
}
start = prev_zero + 1;
prev_zero = i;
}
}
cout << "Answer = " << best_zero << endl;
return best_zero;
}
int main() {
string input = "011101101001";
findOptimal(input);
return 0;
}
This is an implementation in C++. The output looks like this:
tried this : [0111]
tried this : [111011]
tried this : [1101]
tried this : [10]
tried this : [01]
Answer = 4

Codechef PERMUT2 solution

Following website has the problem.
http://www.codechef.com/problems/PERMUT2
I have been trying to code solution for PERMUT2. My below solution is failing on some test cases. Kindly help me uncover flaws in the below code.
#include <stdio.h>
int a[100000];
int main()
{
int i, j, n, ret;
while(1)
{
scanf("%d", &n);
if(n == 0)
break;
ret = 0;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++)
if(a[i] != i + 1)
ret++;
if(ret % 2 == 0)
printf("ambiguous\n");
else
printf("not ambiguous\n");
}
return 0;
}
You are not checking the right property. if(a[i] != i + 1) ret++; is not the right checking.
You want to check a[a[i] - 1] == i + 1 for all elements on the array:
bool ambiguous = true;
for(i = 0; i < n; i++) {
if (a[a[i] - 1] != i + 1) {
ambiguous = false;
break;
}
}
if(ambiguous)
printf("ambiguous\n");
else
printf("not ambiguous\n");

Resources