1725G - Garage - CodeForces Solution


binary search math

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
def valid(i,j,n,m):
        if i<n and i>=0 and j>=0 and j< m :return True         return  False
def sumn(i,n):
    return (n-i)*(i+n)/2
def sqfun(a,b,c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a
def getprime(num):     
        if  all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):return  True
           


def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]



def solve():
        n = inp()
    if n==1:return 3
    return 4+(4*n-3)//3


if __name__ == "__main__":
        print(solve())

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define M 1
//tc fori forj
//1 sec -10^8
//max len of arr -10^7
//max len of vector 2147483648 < 10^10
//for loops not more that 10^5
//QUALITY>QUANTITY
// NO WRONG SUBMISSIONS ONLY CORRECT ANSWERS
//-------------------------------------------------------------------------------------------------------------------------------
//
//FIRST YOU SHOULD GET THE LOGIC CORRECTLY , READ THE QUESTION CORRECTLY
//
//-------------------------------------------------------------------------------------------------------------------------------
//===============================================vignesh========================================================================

void solve()
{
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    if (n==1)
    {
        cout<<3;
    }
    else if (n==2)
    {
        cout<<5;
    }
    else if (n==3)
    {
        cout<<7;
    }
    else
    {
        ll mo=n%3;
        n=(n-1)/3;
        n=(n+1)*4;
        if (mo==1)
        {
            n+=0;
        }
        if (mo==2)
        {
            n+=1;
        }
        else if (mo==0)
        {
            n+=3;
        }
        cout<<n;
    }
  

}


Comments

Submit
0 Comments
More Questions

150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle