34A - Reconnaissance 2 - CodeForces Solution


implementation *800

Please click on ads to support us..

Python Code:


N = int(input())
heights = input().split(" ")

for h in range(0, len(heights)): 
    heights[h] = int(heights[h])

diffs = []

for h in range(0, len(heights)-1):
    diff = heights[h] - heights[h+1]
    if diff < 0: diff *= -1
    diffs.append(diff)
    
extra_diff = heights[0] - heights[-1]
if extra_diff < 0: extra_diff *= -1
diffs.append(extra_diff)

minimal_position = diffs.index(min(diffs))
if minimal_position+1 == len(diffs):
    minimal_pair = [minimal_position + 1, 1]
else:
    minimal_pair = [minimal_position + 1, minimal_position + 2]

print(minimal_pair[0], minimal_pair[1])

C++ Code:

// base template for CP

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define float double
#define sz 100005
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define debug cout << "here" << endl;
#define f(i,s,n) for (int i = s; i < n; ++i)
#define endl "\n"
#define br "\n"
#define printMap(mp) for(auto x : mp){cout<<x.first<<" : "<<x.second<<br;}
#define printv(v) for(auto x:v){cout<<x<<" ";}
#define printvv(v) for(auto x:v){{for(auto it:x) cout<<it<<" ";}cout<<br;}
#define readVector(v) f(i,0,v.size()) cin >> v[i]; 
#define w(t) while(t--)
const int inf = 4.5e18;
#define spc " "
/*
*max_element(arr, arr + n); 
int x = min_element(v.begin(),v.end()) - v.begin(); index of min element
string binary = std::bitset<64>(n).to_string(); decimal to binary
stoi(a, 0, 2);-->a is string of 0 and 1 -->gives decimal value
number of bits in number = > (int)log2(num)+1
vector <int> v[n]; // 2D vector of size n => vector<vector<int>> v(n);
bool flag = binary_search(v.begin(),v.end(), search_value);
int index = upper_bound(v.begin(), v.end(), search_value) - v.begin();
int index = lower_bound(v.begin(), v.end(), search_value) - v.begin();
gcd of two numbers = __gcd(a,b)

string line input
cin>>t; cin.ignore(); w(t--){string s; getline(cin,s);}

//string to lower case
transform(s.begin(), s.end(), s.begin(), ::tolower);

// global arr max size
    int arr[1e7];
    bool arr[1e8];
// inside finction arr max size
    int arr[1e6];
    bool arr[1e7];
*/

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;cin>>n;
    vi v(n); readVector(v);

      if(n==2){
        cout<<"1 2";
        return 0;
    }

    int idx=0;
    int diff = INT_MAX;
    f(i,1,n){
        int curr_diff = abs(v[i-1] - v[i]);
        if(curr_diff < diff){
            diff = curr_diff;
            idx=i;
        }
        if(diff==0)break;
    }

    if(abs(v[n-1] - v[0]) < diff){
        cout<<1<<spc<<n;
    }
    else cout<<idx<<spc<<idx+1;

    return 0;
} 




Comments

Submit
0 Comments
More Questions

437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
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